|
| 1 | +#! /usr/bin/env bats |
| 2 | + |
| 3 | +load ../environment |
| 4 | + |
| 5 | +setup() { |
| 6 | + test_filter |
| 7 | + @go.create_test_go_script \ |
| 8 | + 'collect_dirs_impl() {' \ |
| 9 | + ' [[ -d "$1" ]] && dirs_searched+=("$1")' \ |
| 10 | + ' [[ "$((--COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS))" -eq "0" ]]' \ |
| 11 | + '}' \ |
| 12 | + 'collect_dirs() {' \ |
| 13 | + ' local dirs_searched=()' \ |
| 14 | + ' @go.search_plugins collect_dirs_impl' \ |
| 15 | + ' local result="$?"' \ |
| 16 | + ' printf "%s\n" "${dirs_searched[@]}"' \ |
| 17 | + ' return "$result"' \ |
| 18 | + '}' \ |
| 19 | + 'if [[ -z "$COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS" ]]; then' \ |
| 20 | + ' COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS=1' \ |
| 21 | + 'fi' \ |
| 22 | + '@go "$@"' |
| 23 | +} |
| 24 | + |
| 25 | +teardown() { |
| 26 | + @go.remove_test_go_rootdir |
| 27 | +} |
| 28 | + |
| 29 | +@test "$SUITE: _GO_PLUGINS_DIR doesn't exist" { |
| 30 | + @go.create_test_command_script 'top-level' 'collect_dirs' |
| 31 | + run "$TEST_GO_SCRIPT" 'top-level' |
| 32 | + assert_success '' |
| 33 | +} |
| 34 | + |
| 35 | +@test "$SUITE: _GO_PLUGINS_DIR exists" { |
| 36 | + mkdir -p "$TEST_GO_PLUGINS_DIR" |
| 37 | + @go.create_test_command_script 'top-level' 'collect_dirs' |
| 38 | + run "$TEST_GO_SCRIPT" 'top-level' |
| 39 | + assert_success "$TEST_GO_PLUGINS_DIR" |
| 40 | +} |
| 41 | + |
| 42 | +@test "$SUITE: _GO_PLUGINS_DIR exists, search returns failure" { |
| 43 | + mkdir -p "$TEST_GO_PLUGINS_DIR" |
| 44 | + @go.create_test_command_script 'top' 'collect_dirs' |
| 45 | + COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS='0' run "$TEST_GO_SCRIPT" 'top' |
| 46 | + assert_failure "$TEST_GO_PLUGINS_DIR" |
| 47 | +} |
| 48 | + |
| 49 | +@test "$SUITE: plugin without plugins dir finds _GO_PLUGINS_DIR" { |
| 50 | + @go.create_test_command_script 'plugins/foo/bin/foo' 'collect_dirs' |
| 51 | + COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS='2' run "$TEST_GO_SCRIPT" 'foo' |
| 52 | + assert_success "$TEST_GO_PLUGINS_DIR" |
| 53 | +} |
| 54 | + |
| 55 | +@test "$SUITE: plugin with plugins dir finds both plugins dirs" { |
| 56 | + mkdir -p "$TEST_GO_PLUGINS_DIR/foo/bin/plugins" |
| 57 | + @go.create_test_command_script 'plugins/foo/bin/foo' 'collect_dirs' |
| 58 | + COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS='2' run "$TEST_GO_SCRIPT" 'foo' |
| 59 | + assert_success "$TEST_GO_PLUGINS_DIR/foo/bin/plugins" \ |
| 60 | + "$TEST_GO_PLUGINS_DIR" |
| 61 | +} |
| 62 | + |
| 63 | +@test "$SUITE: plugin with plugins dir, return success after first dir" { |
| 64 | + mkdir -p "$TEST_GO_PLUGINS_DIR/foo/bin/plugins" |
| 65 | + @go.create_test_command_script 'plugins/foo/bin/foo' 'collect_dirs' |
| 66 | + COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS='1' run "$TEST_GO_SCRIPT" 'foo' |
| 67 | + assert_success "$TEST_GO_PLUGINS_DIR/foo/bin/plugins" |
| 68 | +} |
| 69 | + |
| 70 | +@test "$SUITE: plugin finds both plugins dirs, returns failure" { |
| 71 | + mkdir -p "$TEST_GO_PLUGINS_DIR/foo/bin/plugins" |
| 72 | + @go.create_test_command_script 'plugins/foo/bin/foo' 'collect_dirs' |
| 73 | + COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS='3' run "$TEST_GO_SCRIPT" 'foo' |
| 74 | + assert_failure "$TEST_GO_PLUGINS_DIR/foo/bin/plugins" \ |
| 75 | + "$TEST_GO_PLUGINS_DIR" |
| 76 | +} |
| 77 | + |
| 78 | +@test "$SUITE: nested_plugin without plugins dir finds parent dirs" { |
| 79 | + mkdir -p "$TEST_GO_PLUGINS_DIR/foo/bin/plugins/bar/bin" |
| 80 | + @go.create_test_command_script 'plugins/foo/bin/foo' '@go bar' |
| 81 | + @go.create_test_command_script 'plugins/foo/bin/plugins/bar/bin/bar' \ |
| 82 | + 'collect_dirs' |
| 83 | + COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS='3' run "$TEST_GO_SCRIPT" 'foo' |
| 84 | + assert_success "$TEST_GO_PLUGINS_DIR/foo/bin/plugins" \ |
| 85 | + "$TEST_GO_PLUGINS_DIR" |
| 86 | +} |
| 87 | + |
| 88 | +@test "$SUITE: nested_plugin with plugins dir finds all plugin dirs" { |
| 89 | + mkdir -p "$TEST_GO_PLUGINS_DIR/foo/bin/plugins/bar/bin/plugins" |
| 90 | + @go.create_test_command_script 'plugins/foo/bin/foo' '@go bar' |
| 91 | + @go.create_test_command_script 'plugins/foo/bin/plugins/bar/bin/bar' \ |
| 92 | + 'collect_dirs' |
| 93 | + COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS='3' run "$TEST_GO_SCRIPT" 'foo' |
| 94 | + assert_success "$TEST_GO_PLUGINS_DIR/foo/bin/plugins/bar/bin/plugins" \ |
| 95 | + "$TEST_GO_PLUGINS_DIR/foo/bin/plugins" \ |
| 96 | + "$TEST_GO_PLUGINS_DIR" |
| 97 | +} |
| 98 | + |
| 99 | +@test "$SUITE: nested_plugin stops after parent plugin dir" { |
| 100 | + @go.create_test_command_script 'plugins/foo/bin/foo' '@go bar' |
| 101 | + @go.create_test_command_script 'plugins/foo/bin/plugins/bar/bin/bar' \ |
| 102 | + 'collect_dirs' |
| 103 | + COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS='2' run "$TEST_GO_SCRIPT" 'foo' |
| 104 | + # Note it doesn't have its own plugin dir this time. |
| 105 | + assert_success "$TEST_GO_PLUGINS_DIR/foo/bin/plugins" |
| 106 | +} |
| 107 | + |
| 108 | +@test "$SUITE: nested_plugin with plugins dir finds all dirs, returns failure" { |
| 109 | + mkdir -p "$TEST_GO_PLUGINS_DIR/foo/bin/plugins/bar/bin/plugins" |
| 110 | + @go.create_test_command_script 'plugins/foo/bin/foo' '@go bar' |
| 111 | + @go.create_test_command_script 'plugins/foo/bin/plugins/bar/bin/bar' \ |
| 112 | + 'collect_dirs' |
| 113 | + COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS='4' run "$TEST_GO_SCRIPT" 'foo' |
| 114 | + assert_failure "$TEST_GO_PLUGINS_DIR/foo/bin/plugins/bar/bin/plugins" \ |
| 115 | + "$TEST_GO_PLUGINS_DIR/foo/bin/plugins" \ |
| 116 | + "$TEST_GO_PLUGINS_DIR" |
| 117 | +} |
| 118 | + |
| 119 | +@test "$SUITE: /plugins/ in _GO_ROOTDIR, _GO_SCRIPTS_DIR (pathological)" { |
| 120 | + local test_rootdir="$TEST_GO_ROOTDIR/plugins/plugins" |
| 121 | + local test_go_script="$test_rootdir/go" |
| 122 | + local test_scripts_dir="$test_rootdir/plugins" |
| 123 | + local test_plugins_dir="$test_scripts_dir/plugins" |
| 124 | + mkdir -p "$test_plugins_dir/foo/bin/plugins/bar/bin/plugins" |
| 125 | + |
| 126 | + local line |
| 127 | + while IFS= read -r line; do |
| 128 | + line="${line%$'\r'}" |
| 129 | + if [[ "$line" =~ go-core\.bash ]]; then |
| 130 | + line=". '$_GO_CORE_DIR/go-core.bash' 'plugins'" |
| 131 | + fi |
| 132 | + printf '%s\n' "$line" >> "$test_go_script" |
| 133 | + done < "$TEST_GO_SCRIPT" |
| 134 | + chmod 700 "$test_go_script" |
| 135 | + |
| 136 | + local foo_path="${test_plugins_dir#$BATS_TEST_ROOTDIR/}/foo/bin/foo" |
| 137 | + local bar_path="${foo_path%/*}/plugins/bar/bin/bar" |
| 138 | + |
| 139 | + # We can't use `@go.create_test_command_script` since we can't change the |
| 140 | + # readonly `TEST_GO_*` variables. |
| 141 | + create_bats_test_script "$foo_path" '@go bar' |
| 142 | + create_bats_test_script "$bar_path" 'collect_dirs' |
| 143 | + |
| 144 | + test_printf 'test_go_script: %s\n' "$test_go_script" >&2 |
| 145 | + test_printf 'test_plugins_dir: %s\n' "$test_plugins_dir" >&2 |
| 146 | + test_printf 'foo_path: %s\n' "${foo_path}" >&2 |
| 147 | + test_printf 'bar_path: %s\n' "${bar_path}" >&2 |
| 148 | + COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS='10' run "$test_go_script" 'foo' |
| 149 | + assert_failure "$test_plugins_dir/foo/bin/plugins/bar/bin/plugins" \ |
| 150 | + "$test_plugins_dir/foo/bin/plugins" \ |
| 151 | + "$test_plugins_dir" |
| 152 | +} |
0 commit comments