From d70df24051a46831a81247144168e009d292cc61 Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Mon, 3 Aug 2015 18:47:13 +0200 Subject: [PATCH] Support plugin definition in sourced files --- CHANGELOG.md | 1 + scripts/helpers/plugin_functions.sh | 35 +++++++--- tests/test_plugin_installation.sh | 103 +++++++++++++++++++++++++++- 3 files changed, 130 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34c5966..93cfd12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ assert expectations - refactor code that sets 'TMUX_PLUGIN_MANAGER_PATH' global env var - stop using global variable for 'tpm path' +- support defining plugins via `set -g @plugin` in sourced files as well ### v2.0.0, 2015-07-07 - enable overriding default key bindings diff --git a/scripts/helpers/plugin_functions.sh b/scripts/helpers/plugin_functions.sh index 4c34412..e1da249 100644 --- a/scripts/helpers/plugin_functions.sh +++ b/scripts/helpers/plugin_functions.sh @@ -1,21 +1,40 @@ # using @tpm_plugins is now deprecated in favor of using @plugin syntax tpm_plugins_variable_name="@tpm_plugins" +# manually expanding tilde char or `$HOME` variable. +_manual_expansion() { + local path="$1" + local expanded_tilde="${path/#\~/$HOME}" + echo "${expanded_tilde/#\$HOME/$HOME}" +} + _tpm_path() { local string_path="$(tmux start-server\; show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=)/" - # manually expanding tilde or `$HOME` variable. - string_path="${string_path/#\~/$HOME}" - echo "${string_path/#\$HOME/$HOME}" + _manual_expansion "$string_path" } _CACHED_TPM_PATH="$(_tpm_path)" -tpm_path() { - echo "$_CACHED_TPM_PATH" -} - _tmux_conf_contents() { cat /etc/tmux.conf ~/.tmux.conf 2>/dev/null + if [ "$1" == "full" ]; then # also output content from sourced files + local file + for file in $(_sourced_files); do + cat $(_manual_expansion "$file") 2>/dev/null + done + fi +} + +# return files sourced from tmux config files +_sourced_files() { + _tmux_conf_contents | + awk '/^ *source(-file)? +/ { gsub(/'\''/,""); gsub(/'\"'/,""); print $2 }' +} + +# PUBLIC FUNCTIONS BELOW + +tpm_path() { + echo "$_CACHED_TPM_PATH" } tpm_plugins_list_helper() { @@ -23,7 +42,7 @@ tpm_plugins_list_helper() { echo "$(tmux start-server\; show-option -gqv "$tpm_plugins_variable_name")" # read set -g @plugin "tmux-plugins/tmux-example-plugin" entries - _tmux_conf_contents | + _tmux_conf_contents "full" | awk '/^ *set +-g +@plugin/ { gsub(/'\''/,""); gsub(/'\"'/,""); print $4 }' } diff --git a/tests/test_plugin_installation.sh b/tests/test_plugin_installation.sh index c161746..7e3404d 100755 --- a/tests/test_plugin_installation.sh +++ b/tests/test_plugin_installation.sh @@ -5,6 +5,8 @@ PLUGINS_DIR="$HOME/.tmux/plugins" TPM_DIR="$PWD" CUSTOM_PLUGINS_DIR="$HOME/foo/plugins" +ADDITIONAL_CONFIG_FILE_1="$HOME/.tmux/additional_config_file_1" +ADDITIONAL_CONFIG_FILE_2="$HOME/.tmux/additional_config_file_2" source "$CURRENT_DIR/helpers/helpers.sh" source "$CURRENT_DIR/helpers/tpm.sh" @@ -63,7 +65,6 @@ test_multiple_plugins_installation_via_tmux_key_binding() { run-shell "$TPM_DIR/tpm" HERE - # opens tmux and test it with `expect` "$CURRENT_DIR/expect_successful_multiple_plugins_download" || fail_helper "[key-binding] multiple plugins installation fails" @@ -76,6 +77,51 @@ test_multiple_plugins_installation_via_tmux_key_binding() { teardown_helper } +test_plugins_installation_from_sourced_file_via_tmux_key_binding() { + set_tmux_conf_helper <<- HERE + source '$ADDITIONAL_CONFIG_FILE_1' + set -g @plugin 'tmux-plugins/tmux-example-plugin' + run-shell "$TPM_DIR/tpm" + HERE + + mkdir ~/.tmux + echo "set -g @plugin 'tmux-plugins/tmux-copycat'" > "$ADDITIONAL_CONFIG_FILE_1" + + "$CURRENT_DIR/expect_successful_multiple_plugins_download" || + fail_helper "[key-binding][sourced file] plugins installation fails" + + check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" || + fail_helper "[key-binding][sourced file] plugin download fails (tmux-example-plugin)" + + check_dir_exists_helper "$PLUGINS_DIR/tmux-copycat/" || + fail_helper "[key-binding][sourced file] plugin download fails (tmux-copycat)" + + teardown_helper +} + +test_plugins_installation_from_multiple_sourced_files_via_tmux_key_binding() { + set_tmux_conf_helper <<- HERE + \ \ source '$ADDITIONAL_CONFIG_FILE_1' + source-file '$ADDITIONAL_CONFIG_FILE_2' + run-shell "$TPM_DIR/tpm" + HERE + + mkdir ~/.tmux + echo "set -g @plugin 'tmux-plugins/tmux-example-plugin'" > "$ADDITIONAL_CONFIG_FILE_1" + echo " set -g @plugin 'tmux-plugins/tmux-copycat'" > "$ADDITIONAL_CONFIG_FILE_2" + + "$CURRENT_DIR/expect_successful_multiple_plugins_download" || + fail_helper "[key-binding][multiple sourced files] plugins installation fails" + + check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" || + fail_helper "[key-binding][multiple sourced files] plugin download fails (tmux-example-plugin)" + + check_dir_exists_helper "$PLUGINS_DIR/tmux-copycat/" || + fail_helper "[key-binding][multiple sourced files] plugin download fails (tmux-copycat)" + + teardown_helper +} + # SCRIPT TESTS test_plugin_installation_via_script() { @@ -152,4 +198,59 @@ test_multiple_plugins_installation_via_script() { teardown_helper } +test_plugins_installation_from_sourced_file_via_script() { + set_tmux_conf_helper <<- HERE + source '$ADDITIONAL_CONFIG_FILE_1' + set -g @plugin 'tmux-plugins/tmux-example-plugin' + run-shell "$TPM_DIR/tpm" + HERE + + mkdir ~/.tmux + echo "set -g @plugin 'tmux-plugins/tmux-copycat'" > "$ADDITIONAL_CONFIG_FILE_1" + + script_run_helper "$TPM_DIR/bin/install_plugins" '"tmux-copycat" download success' || + fail_helper "[script][sourced file] plugins installation fails" + + check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" || + fail_helper "[script][sourced file] plugin download fails (tmux-example-plugin)" + + check_dir_exists_helper "$PLUGINS_DIR/tmux-copycat/" || + fail_helper "[script][sourced file] plugin download fails (tmux-copycat)" + + script_run_helper "$TPM_DIR/bin/install_plugins" 'Already installed "tmux-copycat"' || + fail_helper "[script][sourced file] plugins already installed message fail" + + teardown_helper +} + +test_plugins_installation_from_multiple_sourced_files_via_script() { + set_tmux_conf_helper <<- HERE + \ \ source '$ADDITIONAL_CONFIG_FILE_1' + source-file '$ADDITIONAL_CONFIG_FILE_2' + set -g @plugin 'tmux-plugins/tmux-example-plugin' + run-shell "$TPM_DIR/tpm" + HERE + + mkdir ~/.tmux + echo " set -g @plugin 'tmux-plugins/tmux-copycat'" > "$ADDITIONAL_CONFIG_FILE_1" + echo "set -g @plugin 'tmux-plugins/tmux-sensible'" > "$ADDITIONAL_CONFIG_FILE_2" + + script_run_helper "$TPM_DIR/bin/install_plugins" '"tmux-sensible" download success' || + fail_helper "[script][multiple sourced files] plugins installation fails" + + check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" || + fail_helper "[script][multiple sourced files] plugin download fails (tmux-example-plugin)" + + check_dir_exists_helper "$PLUGINS_DIR/tmux-copycat/" || + fail_helper "[script][multiple sourced files] plugin download fails (tmux-copycat)" + + check_dir_exists_helper "$PLUGINS_DIR/tmux-sensible/" || + fail_helper "[script][multiple sourced files] plugin download fails (tmux-sensible)" + + script_run_helper "$TPM_DIR/bin/install_plugins" 'Already installed "tmux-sensible"' || + fail_helper "[script][multiple sourced files] plugins already installed message fail" + + teardown_helper +} + run_tests