asdf

Форк
0
/
version_commands.bats 
494 строки · 16.1 Кб
1
#!/usr/bin/env bats
2
# shellcheck disable=SC2012,SC2030,SC2031,SC2164
3

4
load test_helpers
5

6
setup() {
7
  setup_asdf_dir
8
  install_dummy_plugin
9
  install_dummy_version "1.0.0"
10
  install_dummy_version "1.1.0"
11
  install_dummy_version "2.0.0"
12

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"
18

19
  PROJECT_DIR="$HOME/project"
20
  mkdir -p "$PROJECT_DIR"
21

22
  CHILD_DIR="$PROJECT_DIR/child-dir"
23
  mkdir -p "$CHILD_DIR"
24

25
  cd "$PROJECT_DIR"
26

27
  # asdf lib needed to run asdf.sh
28
  cp -rf "$BATS_TEST_DIRNAME"/../{bin,lib} "$ASDF_DIR/"
29
}
30

31
teardown() {
32
  clean_asdf_dir
33
}
34

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"
38
  [ "$status" -eq 1 ]
39
  [ "$output" = "Usage: asdf local <name> <version>" ]
40
}
41

42
@test "local should emit an error when plugin does not exist" {
43
  run asdf local "inexistent" "1.0.0"
44
  [ "$status" -eq 1 ]
45
  [ "$output" = "No such plugin: inexistent" ]
46
}
47

48
@test "local should emit an error when plugin version does not exist" {
49
  run asdf local "dummy" "0.0.1"
50
  [ "$status" -eq 1 ]
51
  [ "$output" = "version 0.0.1 is not installed for dummy" ]
52
}
53

54
@test "local should create a local .tool-versions file if it doesn't exist" {
55
  run asdf local "dummy" "1.1.0"
56
  [ "$status" -eq 0 ]
57
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.1.0" ]
58
}
59

60
@test "[local - dummy_plugin] with latest should use the latest installed version" {
61
  run asdf local "dummy" "latest"
62
  [ "$status" -eq 0 ]
63
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 2.0.0" ]
64
}
65

66
@test "[local - dummy_plugin] with latest:version should use the latest valid installed version" {
67
  run asdf local "dummy" "latest:1.0"
68
  [ "$status" -eq 0 ]
69
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.0.0" ]
70
}
71

72
@test "[local - dummy_plugin] with latest:version should return an error for invalid versions" {
73
  run asdf local "dummy" "latest:99"
74
  [ "$status" -eq 1 ]
75
  [ "$output" = "No compatible versions available (dummy 99)" ]
76
}
77

78
@test "[local - dummy_legacy_plugin] with latest should use the latest installed version" {
79
  run asdf local "legacy-dummy" "latest"
80
  [ "$status" -eq 0 ]
81
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = "legacy-dummy 5.1.0" ]
82
}
83

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"
86
  [ "$status" -eq 0 ]
87
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = "legacy-dummy 1.0.0" ]
88
}
89

90
@test "[local - dummy_legacy_plugin] with latest:version should return an error for invalid versions" {
91
  run asdf local "legacy-dummy" "latest:99"
92
  [ "$status" -eq 1 ]
93
  [ "$output" = "No compatible versions available (legacy-dummy 99)" ]
94
}
95

96
@test "local should allow multiple versions" {
97
  run asdf local "dummy" "1.1.0" "1.0.0"
98
  [ "$status" -eq 0 ]
99
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.1.0 1.0.0" ]
100
}
101

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"
105
  cd "$WHITESPACE_DIR"
106

107
  run asdf local "dummy" "1.1.0"
108

109
  tool_version_contents=$(cat "$WHITESPACE_DIR/.tool-versions")
110
  [ "$status" -eq 0 ]
111
  [ "$tool_version_contents" = "dummy 1.1.0" ]
112
}
113

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"
116

117
  run asdf local "dummy" "1.1.0"
118
  [ "$status" -eq 0 ]
119
  [ "$(ls "$PROJECT_DIR/.tool-versions"* | wc -l)" -eq 1 ]
120
}
121

122
@test "local should overwrite the existing version if it's set" {
123
  echo 'dummy 1.0.0' >>"$PROJECT_DIR/.tool-versions"
124

125
  run asdf local "dummy" "1.1.0"
126
  [ "$status" -eq 0 ]
127
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.1.0" ]
128
}
129

130
@test "local should append trailing newline before appending new version when missing" {
131
  echo -n 'foobar 1.0.0' >>"$PROJECT_DIR/.tool-versions"
132

133
  run asdf local "dummy" "1.1.0"
134
  [ "$status" -eq 0 ]
135
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = $'foobar 1.0.0\ndummy 1.1.0' ]
136
}
137

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"
140

141
  run asdf local "dummy" "1.1.0"
142
  [ "$status" -eq 0 ]
143
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = $'foobar 1.0.0\ndummy 1.1.0' ]
144
}
145

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" ]
149
  [ "$status" -eq 1 ]
150
}
151

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"
155
  [ "$status" -eq 0 ]
156
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy path:$PROJECT_DIR/local" ]
157
}
158

159
@test "local -p/--parent should set should emit an error when called with incorrect arity" {
160
  run asdf local -p "dummy"
161
  [ "$status" -eq 1 ]
162
  [ "$output" = "Usage: asdf local <name> <version>" ]
163
}
164

165
@test "local -p/--parent should emit an error when plugin does not exist" {
166
  run asdf local -p "inexistent" "1.0.0"
167
  [ "$status" -eq 1 ]
168
  [ "$output" = "No such plugin: inexistent" ]
169
}
170

171
@test "local -p/--parent should emit an error when plugin version does not exist" {
172
  run asdf local -p "dummy" "0.0.1"
173
  [ "$status" -eq 1 ]
174
  [ "$output" = "version 0.0.1 is not installed for dummy" ]
175
}
176

177
@test "local -p/--parent should allow multiple versions" {
178
  cd "$CHILD_DIR"
179
  touch "$PROJECT_DIR/.tool-versions"
180
  run asdf local -p "dummy" "1.1.0" "1.0.0"
181
  [ "$status" -eq 0 ]
182
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.1.0 1.0.0" ]
183
}
184

185
@test "local -p/--parent should overwrite the existing version if it's set" {
186
  cd "$CHILD_DIR"
187
  echo 'dummy 1.0.0' >>"$PROJECT_DIR/.tool-versions"
188
  run asdf local -p "dummy" "1.1.0"
189
  [ "$status" -eq 0 ]
190
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.1.0" ]
191
}
192

193
@test "local -p/--parent should set the version if it's unset" {
194
  cd "$CHILD_DIR"
195
  touch "$PROJECT_DIR/.tool-versions"
196
  run asdf local -p "dummy" "1.1.0"
197
  [ "$status" -eq 0 ]
198
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.1.0" ]
199
}
200

201
@test "global should create a global .tool-versions file if it doesn't exist" {
202
  run asdf global "dummy" "1.1.0"
203
  [ "$status" -eq 0 ]
204
  [ "$(cat "$HOME/.tool-versions")" = "dummy 1.1.0" ]
205
}
206

207
@test "[global - dummy_plugin] with latest should use the latest installed version" {
208
  run asdf global "dummy" "latest"
209
  [ "$status" -eq 0 ]
210
  [ "$(cat "$HOME/.tool-versions")" = "dummy 2.0.0" ]
211
}
212

213
@test "[global - dummy_plugin] with latest:version should use the latest valid installed version" {
214
  run asdf global "dummy" "latest:1.0"
215
  [ "$status" -eq 0 ]
216
  [ "$(cat "$HOME/.tool-versions")" = "dummy 1.0.0" ]
217
}
218

219
@test "[global - dummy_plugin] with latest:version should return an error for invalid versions" {
220
  run asdf global "dummy" "latest:99"
221
  [ "$status" -eq 1 ]
222
  [ "$output" = "No compatible versions available (dummy 99)" ]
223
}
224

225
@test "[global - dummy_legacy_plugin] with latest should use the latest installed version" {
226
  run asdf global "legacy-dummy" "latest"
227
  [ "$status" -eq 0 ]
228
  [ "$(cat "$HOME/.tool-versions")" = "legacy-dummy 5.1.0" ]
229
}
230

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"
233
  [ "$status" -eq 0 ]
234
  [ "$(cat "$HOME/.tool-versions")" = "legacy-dummy 1.0.0" ]
235
}
236

237
@test "[global - dummy_legacy_plugin] with latest:version should return an error for invalid versions" {
238
  run asdf global "legacy-dummy" "latest:99"
239
  [ "$status" -eq 1 ]
240
  [ "$output" = "No compatible versions available (legacy-dummy 99)" ]
241
}
242

243
@test "global should accept multiple versions" {
244
  run asdf global "dummy" "1.1.0" "1.0.0"
245
  [ "$status" -eq 0 ]
246
  [ "$(cat "$HOME/.tool-versions")" = "dummy 1.1.0 1.0.0" ]
247
}
248

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"
252
  [ "$status" -eq 0 ]
253
  [ "$(cat "$HOME/.tool-versions")" = "dummy 1.1.0" ]
254
}
255

256
@test "global should append trailing newline before appending new version when missing" {
257
  echo -n 'foobar 1.0.0' >>"$HOME/.tool-versions"
258

259
  run asdf global "dummy" "1.1.0"
260
  [ "$status" -eq 0 ]
261
  [ "$(cat "$HOME/.tool-versions")" = $'foobar 1.0.0\ndummy 1.1.0' ]
262
}
263

264
@test "global should not append trailing newline before appending new version when one present" {
265
  echo 'foobar 1.0.0' >>"$HOME/.tool-versions"
266

267
  run asdf global "dummy" "1.1.0"
268
  [ "$status" -eq 0 ]
269
  [ "$(cat "$HOME/.tool-versions")" = $'foobar 1.0.0\ndummy 1.1.0' ]
270
}
271

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" ]
275
  [ "$status" -eq 1 ]
276
}
277

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"
281
  [ "$status" -eq 0 ]
282
  [ "$(cat "$HOME/.tool-versions")" = "dummy path:$PROJECT_DIR/local" ]
283
}
284

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"
288
  [ "$status" -eq 0 ]
289
  [ "$(cat "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME")" = "dummy 1.1.0" ]
290
  [ -z "$(cat .tool-versions)" ]
291
  unset ASDF_DEFAULT_TOOL_VERSIONS_FILENAME
292
}
293

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"
298
  [ "$status" -eq 0 ]
299
  [ "$(cat "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME")" = "dummy 1.1.0" ]
300
  [ -z "$(cat .tool-versions)" ]
301
  unset ASDF_DEFAULT_TOOL_VERSIONS_FILENAME
302
}
303

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"
307
  [ "$status" -eq 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
311
}
312

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"
317
  [ "$status" -eq 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
321
}
322

323
@test "local should preserve symlinks when setting versions" {
324
  mkdir other-dir
325
  touch other-dir/.tool-versions
326
  ln -s other-dir/.tool-versions .tool-versions
327
  run asdf local "dummy" "1.1.0"
328
  [ "$status" -eq 0 ]
329
  [ -L .tool-versions ]
330
  [ "$(cat other-dir/.tool-versions)" = "dummy 1.1.0" ]
331
}
332

333
@test "local should preserve symlinks when updating versions" {
334
  mkdir other-dir
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"
339
  [ "$status" -eq 0 ]
340
  [ -L .tool-versions ]
341
  [ "$(cat other-dir/.tool-versions)" = "dummy 1.1.0" ]
342
}
343

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"
348

349
  run asdf global "dummy" "1.1.0"
350
  [ "$status" -eq 0 ]
351
  [ -L "$HOME/.tool-versions" ]
352
  [ "$(cat "$HOME/other-dir/.tool-versions")" = "dummy 1.1.0" ]
353
}
354

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"
359

360
  run asdf global "dummy" "1.1.0"
361
  run asdf global "dummy" "1.1.0"
362
  [ "$status" -eq 0 ]
363
  [ -L "$HOME/.tool-versions" ]
364
  [ "$(cat "$HOME/other-dir/.tool-versions")" = "dummy 1.1.0" ]
365
}
366

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
372
}
373

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
381
}
382

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"
387

388
  run asdf shell "nonexistent" "1.0.0"
389
  [ "$status" -eq 1 ]
390
  [ "$output" = "$expected" ]
391
}
392

393
@test "shell should emit an error when wrapper function is not loaded" {
394
  run asdf shell "dummy" "1.1.0"
395
  [ "$status" -eq 1 ]
396
  [ "$output" = "Shell integration is not enabled. Please ensure you source asdf in your shell setup." ]
397
}
398

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
402
false"
403

404
  run asdf export-shell-version sh "nonexistent" "1.0.0"
405
  [ "$status" -eq 1 ]
406
  [ "$output" = "$expected" ]
407
}
408

409
@test "export-shell-version should emit an error when version does not exist" {
410
  expected="version nonexistent is not installed for dummy
411
false"
412

413
  run asdf export-shell-version sh "dummy" "nonexistent"
414
  [ "$status" -eq 1 ]
415
  [ "$output" = "$expected" ]
416
}
417

418
@test "export-shell-version should export version if it exists" {
419
  run asdf export-shell-version sh "dummy" "1.1.0"
420
  [ "$status" -eq 0 ]
421
  [ "$output" = "export ASDF_DUMMY_VERSION=\"1.1.0\"" ]
422
}
423

424
@test "export-shell-version should use set when shell is fish" {
425
  run asdf export-shell-version fish "dummy" "1.1.0"
426
  [ "$status" -eq 0 ]
427
  [ "$output" = "set -gx ASDF_DUMMY_VERSION \"1.1.0\"" ]
428
}
429

430
@test "export-shell-version should use set-env when shell is elvish" {
431
  run asdf export-shell-version elvish "dummy" "1.1.0"
432
  [ "$status" -eq 0 ]
433
  [ "$output" = $'set-env\nASDF_DUMMY_VERSION\n1.1.0' ]
434
}
435

436
@test "export-shell-version should unset when --unset flag is passed" {
437
  run asdf export-shell-version sh "dummy" "--unset"
438
  [ "$status" -eq 0 ]
439
  [ "$output" = "unset ASDF_DUMMY_VERSION" ]
440
}
441

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"
444
  [ "$status" -eq 0 ]
445
  [ "$output" = "set -e ASDF_DUMMY_VERSION" ]
446
}
447

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"
450
  [ "$status" -eq 0 ]
451
  [ "$output" = $'unset-env\nASDF_DUMMY_VERSION' ]
452
}
453

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
459
}
460

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
466
}
467

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"
471
  [ "$status" -eq 0 ]
472
  [ "$(cat "$HOME/.tool-versions")" = "dummy 1.0.0 2.0.0" ]
473
}
474

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"
478
  [ "$status" -eq 0 ]
479
  [ "$(cat "$HOME/.tool-versions")" = "legacy-dummy 1.0.0 5.1.0" ]
480
}
481

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"
485
  [ "$status" -eq 0 ]
486
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = "dummy 1.0.0 2.0.0" ]
487
}
488

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"
492
  [ "$status" -eq 0 ]
493
  [ "$(cat "$PROJECT_DIR/.tool-versions")" = "legacy-dummy 1.0.0 5.1.0" ]
494
}
495

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.