2
# shellcheck disable=SC2012,SC2030,SC2031,SC2164
9
install_dummy_version "1.0.0"
10
install_dummy_version "1.1.0"
11
install_dummy_version "2.0.0"
13
install_dummy_legacy_plugin
14
install_dummy_legacy_version "1.0.0"
15
install_dummy_legacy_version "1.1.0"
16
install_dummy_legacy_version "2.0.0"
17
install_dummy_legacy_version "5.1.0"
19
PROJECT_DIR="$HOME/project"
20
mkdir -p "$PROJECT_DIR"
22
CHILD_DIR="$PROJECT_DIR/child-dir"
27
# asdf lib needed to run asdf.sh
28
cp -rf "$BATS_TEST_DIRNAME"/../{bin,lib} "$ASDF_DIR/"
35
# Warn users who invoke the old style command without arguments.
36
@test "local should emit an error when called with incorrect arity" {
37
run asdf local "dummy"
39
[ "$output" = "Usage: asdf local <name> <version>" ]
42
@test "local should emit an error when plugin does not exist" {
43
run asdf local "inexistent" "1.0.0"
45
[ "$output" = "No such plugin: inexistent" ]
48
@test "local should emit an error when plugin version does not exist" {
49
run asdf local "dummy" "0.0.1"
51
[ "$output" = "version 0.0.1 is not installed for dummy" ]
54
@test "local should create a local .tool-versions file if it doesn't exist" {
55
run asdf local "dummy" "1.1.0"
57
[ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.1.0" ]
60
@test "[local - dummy_plugin] with latest should use the latest installed version" {
61
run asdf local "dummy" "latest"
63
[ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 2.0.0" ]
66
@test "[local - dummy_plugin] with latest:version should use the latest valid installed version" {
67
run asdf local "dummy" "latest:1.0"
69
[ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.0.0" ]
72
@test "[local - dummy_plugin] with latest:version should return an error for invalid versions" {
73
run asdf local "dummy" "latest:99"
75
[ "$output" = "No compatible versions available (dummy 99)" ]
78
@test "[local - dummy_legacy_plugin] with latest should use the latest installed version" {
79
run asdf local "legacy-dummy" "latest"
81
[ "$(cat "$PROJECT_DIR/.tool-versions")" = "legacy-dummy 5.1.0" ]
84
@test "[local - dummy_legacy_plugin] with latest:version should use the latest valid installed version" {
85
run asdf local "legacy-dummy" "latest:1.0"
87
[ "$(cat "$PROJECT_DIR/.tool-versions")" = "legacy-dummy 1.0.0" ]
90
@test "[local - dummy_legacy_plugin] with latest:version should return an error for invalid versions" {
91
run asdf local "legacy-dummy" "latest:99"
93
[ "$output" = "No compatible versions available (legacy-dummy 99)" ]
96
@test "local should allow multiple versions" {
97
run asdf local "dummy" "1.1.0" "1.0.0"
99
[ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.1.0 1.0.0" ]
102
@test "local should create a local .tool-versions file if it doesn't exist when the directory name contains whitespace" {
103
WHITESPACE_DIR="$PROJECT_DIR/whitespace\ dir"
104
mkdir -p "$WHITESPACE_DIR"
107
run asdf local "dummy" "1.1.0"
109
tool_version_contents=$(cat "$WHITESPACE_DIR/.tool-versions")
111
[ "$tool_version_contents" = "dummy 1.1.0" ]
114
@test "local should not create a duplicate .tool-versions file if such file exists" {
115
echo 'dummy 1.0.0' >>"$PROJECT_DIR/.tool-versions"
117
run asdf local "dummy" "1.1.0"
119
[ "$(ls "$PROJECT_DIR/.tool-versions"* | wc -l)" -eq 1 ]
122
@test "local should overwrite the existing version if it's set" {
123
echo 'dummy 1.0.0' >>"$PROJECT_DIR/.tool-versions"
125
run asdf local "dummy" "1.1.0"
127
[ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.1.0" ]
130
@test "local should append trailing newline before appending new version when missing" {
131
echo -n 'foobar 1.0.0' >>"$PROJECT_DIR/.tool-versions"
133
run asdf local "dummy" "1.1.0"
135
[ "$(cat "$PROJECT_DIR/.tool-versions")" = $'foobar 1.0.0\ndummy 1.1.0' ]
138
@test "local should not append trailing newline before appending new version when one present" {
139
echo 'foobar 1.0.0' >>"$PROJECT_DIR/.tool-versions"
141
run asdf local "dummy" "1.1.0"
143
[ "$(cat "$PROJECT_DIR/.tool-versions")" = $'foobar 1.0.0\ndummy 1.1.0' ]
146
@test "local should fail to set a path:dir if dir does not exists " {
147
run asdf local "dummy" "path:$PROJECT_DIR/local"
148
[ "$output" = "version path:$PROJECT_DIR/local is not installed for dummy" ]
152
@test "local should set a path:dir if dir exists " {
153
mkdir -p "$PROJECT_DIR/local"
154
run asdf local "dummy" "path:$PROJECT_DIR/local"
156
[ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy path:$PROJECT_DIR/local" ]
159
@test "local -p/--parent should set should emit an error when called with incorrect arity" {
160
run asdf local -p "dummy"
162
[ "$output" = "Usage: asdf local <name> <version>" ]
165
@test "local -p/--parent should emit an error when plugin does not exist" {
166
run asdf local -p "inexistent" "1.0.0"
168
[ "$output" = "No such plugin: inexistent" ]
171
@test "local -p/--parent should emit an error when plugin version does not exist" {
172
run asdf local -p "dummy" "0.0.1"
174
[ "$output" = "version 0.0.1 is not installed for dummy" ]
177
@test "local -p/--parent should allow multiple versions" {
179
touch "$PROJECT_DIR/.tool-versions"
180
run asdf local -p "dummy" "1.1.0" "1.0.0"
182
[ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.1.0 1.0.0" ]
185
@test "local -p/--parent should overwrite the existing version if it's set" {
187
echo 'dummy 1.0.0' >>"$PROJECT_DIR/.tool-versions"
188
run asdf local -p "dummy" "1.1.0"
190
[ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.1.0" ]
193
@test "local -p/--parent should set the version if it's unset" {
195
touch "$PROJECT_DIR/.tool-versions"
196
run asdf local -p "dummy" "1.1.0"
198
[ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.1.0" ]
201
@test "global should create a global .tool-versions file if it doesn't exist" {
202
run asdf global "dummy" "1.1.0"
204
[ "$(cat "$HOME/.tool-versions")" = "dummy 1.1.0" ]
207
@test "[global - dummy_plugin] with latest should use the latest installed version" {
208
run asdf global "dummy" "latest"
210
[ "$(cat "$HOME/.tool-versions")" = "dummy 2.0.0" ]
213
@test "[global - dummy_plugin] with latest:version should use the latest valid installed version" {
214
run asdf global "dummy" "latest:1.0"
216
[ "$(cat "$HOME/.tool-versions")" = "dummy 1.0.0" ]
219
@test "[global - dummy_plugin] with latest:version should return an error for invalid versions" {
220
run asdf global "dummy" "latest:99"
222
[ "$output" = "No compatible versions available (dummy 99)" ]
225
@test "[global - dummy_legacy_plugin] with latest should use the latest installed version" {
226
run asdf global "legacy-dummy" "latest"
228
[ "$(cat "$HOME/.tool-versions")" = "legacy-dummy 5.1.0" ]
231
@test "[global - dummy_legacy_plugin] with latest:version should use the latest valid installed version" {
232
run asdf global "legacy-dummy" "latest:1.0"
234
[ "$(cat "$HOME/.tool-versions")" = "legacy-dummy 1.0.0" ]
237
@test "[global - dummy_legacy_plugin] with latest:version should return an error for invalid versions" {
238
run asdf global "legacy-dummy" "latest:99"
240
[ "$output" = "No compatible versions available (legacy-dummy 99)" ]
243
@test "global should accept multiple versions" {
244
run asdf global "dummy" "1.1.0" "1.0.0"
246
[ "$(cat "$HOME/.tool-versions")" = "dummy 1.1.0 1.0.0" ]
249
@test "global should overwrite the existing version if it's set" {
250
echo 'dummy 1.0.0' >>"$HOME/.tool-versions"
251
run asdf global "dummy" "1.1.0"
253
[ "$(cat "$HOME/.tool-versions")" = "dummy 1.1.0" ]
256
@test "global should append trailing newline before appending new version when missing" {
257
echo -n 'foobar 1.0.0' >>"$HOME/.tool-versions"
259
run asdf global "dummy" "1.1.0"
261
[ "$(cat "$HOME/.tool-versions")" = $'foobar 1.0.0\ndummy 1.1.0' ]
264
@test "global should not append trailing newline before appending new version when one present" {
265
echo 'foobar 1.0.0' >>"$HOME/.tool-versions"
267
run asdf global "dummy" "1.1.0"
269
[ "$(cat "$HOME/.tool-versions")" = $'foobar 1.0.0\ndummy 1.1.0' ]
272
@test "global should fail to set a path:dir if dir does not exists " {
273
run asdf global "dummy" "path:$PROJECT_DIR/local"
274
[ "$output" = "version path:$PROJECT_DIR/local is not installed for dummy" ]
278
@test "global should set a path:dir if dir exists " {
279
mkdir -p "$PROJECT_DIR/local"
280
run asdf global "dummy" "path:$PROJECT_DIR/local"
282
[ "$(cat "$HOME/.tool-versions")" = "dummy path:$PROJECT_DIR/local" ]
285
@test "local should write to ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" {
286
export ASDF_DEFAULT_TOOL_VERSIONS_FILENAME="local-tool-versions"
287
run asdf local "dummy" "1.1.0"
289
[ "$(cat "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME")" = "dummy 1.1.0" ]
290
[ -z "$(cat .tool-versions)" ]
291
unset ASDF_DEFAULT_TOOL_VERSIONS_FILENAME
294
@test "local should overwrite contents of ASDF_DEFAULT_TOOL_VERSIONS_FILENAME if set" {
295
export ASDF_DEFAULT_TOOL_VERSIONS_FILENAME="local-tool-versions"
296
echo 'dummy 1.0.0' >>"$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME"
297
run asdf local "dummy" "1.1.0"
299
[ "$(cat "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME")" = "dummy 1.1.0" ]
300
[ -z "$(cat .tool-versions)" ]
301
unset ASDF_DEFAULT_TOOL_VERSIONS_FILENAME
304
@test "global should write to ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" {
305
export ASDF_DEFAULT_TOOL_VERSIONS_FILENAME="global-tool-versions"
306
run asdf global "dummy" "1.1.0"
308
[ "$(cat "$HOME/$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME")" = "dummy 1.1.0" ]
309
[ -z "$(cat "$HOME/.tool-versions")" ]
310
unset ASDF_DEFAULT_TOOL_VERSIONS_FILENAME
313
@test "global should overwrite contents of ASDF_DEFAULT_TOOL_VERSIONS_FILENAME if set" {
314
export ASDF_DEFAULT_TOOL_VERSIONS_FILENAME="global-tool-versions"
315
echo 'dummy 1.0.0' >>"$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME"
316
run asdf global "dummy" "1.1.0"
318
[ "$(cat "$HOME/$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME")" = "dummy 1.1.0" ]
319
[ -z "$(cat "$HOME/.tool-versions")" ]
320
unset ASDF_DEFAULT_TOOL_VERSIONS_FILENAME
323
@test "local should preserve symlinks when setting versions" {
325
touch other-dir/.tool-versions
326
ln -s other-dir/.tool-versions .tool-versions
327
run asdf local "dummy" "1.1.0"
329
[ -L .tool-versions ]
330
[ "$(cat other-dir/.tool-versions)" = "dummy 1.1.0" ]
333
@test "local should preserve symlinks when updating versions" {
335
touch other-dir/.tool-versions
336
ln -s other-dir/.tool-versions .tool-versions
337
run asdf local "dummy" "1.1.0"
338
run asdf local "dummy" "1.1.0"
340
[ -L .tool-versions ]
341
[ "$(cat other-dir/.tool-versions)" = "dummy 1.1.0" ]
344
@test "global should preserve symlinks when setting versions" {
345
mkdir "$HOME/other-dir"
346
touch "$HOME/other-dir/.tool-versions"
347
ln -s other-dir/.tool-versions "$HOME/.tool-versions"
349
run asdf global "dummy" "1.1.0"
351
[ -L "$HOME/.tool-versions" ]
352
[ "$(cat "$HOME/other-dir/.tool-versions")" = "dummy 1.1.0" ]
355
@test "global should preserve symlinks when updating versions" {
356
mkdir "$HOME/other-dir"
357
touch "$HOME/other-dir/.tool-versions"
358
ln -s other-dir/.tool-versions "$HOME/.tool-versions"
360
run asdf global "dummy" "1.1.0"
361
run asdf global "dummy" "1.1.0"
363
[ -L "$HOME/.tool-versions" ]
364
[ "$(cat "$HOME/other-dir/.tool-versions")" = "dummy 1.1.0" ]
367
@test "shell wrapper function should export ENV var" {
368
. "$(dirname "$BATS_TEST_DIRNAME")/asdf.sh"
369
asdf shell "dummy" "1.1.0"
370
[ "$ASDF_DUMMY_VERSION" = "1.1.0" ]
371
unset ASDF_DUMMY_VERSION
374
@test "shell wrapper function with --unset should unset ENV var" {
375
. "$(dirname "$BATS_TEST_DIRNAME")/asdf.sh"
376
asdf shell "dummy" "1.1.0"
377
[ "$ASDF_DUMMY_VERSION" = "1.1.0" ]
378
asdf shell "dummy" --unset
379
[ -z "$ASDF_DUMMY_VERSION" ]
380
unset ASDF_DUMMY_VERSION
383
@test "shell wrapper function should return an error for missing plugins" {
384
. "$(dirname "$BATS_TEST_DIRNAME")/asdf.sh"
385
expected="No such plugin: nonexistent
386
version 1.0.0 is not installed for nonexistent"
388
run asdf shell "nonexistent" "1.0.0"
390
[ "$output" = "$expected" ]
393
@test "shell should emit an error when wrapper function is not loaded" {
394
run asdf shell "dummy" "1.1.0"
396
[ "$output" = "Shell integration is not enabled. Please ensure you source asdf in your shell setup." ]
399
@test "export-shell-version should emit an error when plugin does not exist" {
400
expected="No such plugin: nonexistent
401
version 1.0.0 is not installed for nonexistent
404
run asdf export-shell-version sh "nonexistent" "1.0.0"
406
[ "$output" = "$expected" ]
409
@test "export-shell-version should emit an error when version does not exist" {
410
expected="version nonexistent is not installed for dummy
413
run asdf export-shell-version sh "dummy" "nonexistent"
415
[ "$output" = "$expected" ]
418
@test "export-shell-version should export version if it exists" {
419
run asdf export-shell-version sh "dummy" "1.1.0"
421
[ "$output" = "export ASDF_DUMMY_VERSION=\"1.1.0\"" ]
424
@test "export-shell-version should use set when shell is fish" {
425
run asdf export-shell-version fish "dummy" "1.1.0"
427
[ "$output" = "set -gx ASDF_DUMMY_VERSION \"1.1.0\"" ]
430
@test "export-shell-version should use set-env when shell is elvish" {
431
run asdf export-shell-version elvish "dummy" "1.1.0"
433
[ "$output" = $'set-env\nASDF_DUMMY_VERSION\n1.1.0' ]
436
@test "export-shell-version should unset when --unset flag is passed" {
437
run asdf export-shell-version sh "dummy" "--unset"
439
[ "$output" = "unset ASDF_DUMMY_VERSION" ]
442
@test "export-shell-version should use set -e when --unset flag is passed and shell is fish" {
443
run asdf export-shell-version fish "dummy" "--unset"
445
[ "$output" = "set -e ASDF_DUMMY_VERSION" ]
448
@test "export-shell-version should use unset-env when --unset flag is passed and shell is elvish" {
449
run asdf export-shell-version elvish "dummy" "--unset"
451
[ "$output" = $'unset-env\nASDF_DUMMY_VERSION' ]
454
@test "[shell - dummy_plugin] wrapper function should support latest" {
455
. "$(dirname "$BATS_TEST_DIRNAME")/asdf.sh"
456
asdf shell "dummy" "latest"
457
[ "$ASDF_DUMMY_VERSION" = "2.0.0" ]
458
unset ASDF_DUMMY_VERSION
461
@test "[shell - dummy_legacy_plugin] wrapper function should support latest" {
462
. "$(dirname "$BATS_TEST_DIRNAME")/asdf.sh"
463
asdf shell "legacy-dummy" "latest"
464
[ "$ASDF_LEGACY_DUMMY_VERSION" = "5.1.0" ]
465
unset ASDF_LEGACY_DUMMY_VERSION
468
@test "[global - dummy_plugin] should support latest" {
469
echo 'dummy 1.0.0' >>"$HOME/.tool-versions"
470
run asdf global "dummy" "1.0.0" "latest"
472
[ "$(cat "$HOME/.tool-versions")" = "dummy 1.0.0 2.0.0" ]
475
@test "[global - dummy_legacy_plugin] should support latest" {
476
echo 'legacy-dummy 1.0.0' >>"$HOME/.tool-versions"
477
run asdf global "legacy-dummy" "1.0.0" "latest"
479
[ "$(cat "$HOME/.tool-versions")" = "legacy-dummy 1.0.0 5.1.0" ]
482
@test "[local - dummy_plugin] should support latest" {
483
echo 'dummy 1.0.0' >>"$PROJECT_DIR/.tool-versions"
484
run asdf local "dummy" "1.0.0" "latest"
486
[ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.0.0 2.0.0" ]
489
@test "[local - dummy_legacy_plugin] should support latest" {
490
echo 'legacy-dummy 1.0.0' >>"$PROJECT_DIR/.tool-versions"
491
run asdf local "legacy-dummy" "1.0.0" "latest"
493
[ "$(cat "$PROJECT_DIR/.tool-versions")" = "legacy-dummy 1.0.0 5.1.0" ]