File: install.bats

package info (click to toggle)
awscli 2.31.35-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156,692 kB
  • sloc: python: 213,816; xml: 14,082; makefile: 189; sh: 178; javascript: 8
file content (149 lines) | stat: -rw-r--r-- 4,055 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env bats

setup() {
  EXE_BUNDLE_DIR="$BATS_TMPDIR/aws"
  EXE_DIST_DIR="$EXE_BUNDLE_DIR/dist"
  AWS_EXE_VERSION="2.0.0dev0"
  new_exe_bundle "$AWS_EXE_VERSION"
  INSTALL_DIR="$BATS_TMPDIR/aws-cli"
  BIN_DIR="$BATS_TMPDIR/bin"
}

teardown() {
  clear_preexisting_bundle
  rm -rf "$INSTALL_DIR"
  rm -rf "$BIN_DIR"
}

new_exe_bundle() {
  aws_exe_version="$1"
  clear_preexisting_bundle
  mkdir -p "$EXE_BUNDLE_DIR"
  mkdir -p "$EXE_DIST_DIR"
  cp "${BATS_TEST_DIRNAME}/../assets/install" "$EXE_BUNDLE_DIR"
  create_exes "$aws_exe_version"
}

clear_preexisting_bundle() {
  [ -d "$EXE_BUNDLE_DIR" ] || rm -rf "$EXE_BUNDLE_DIR"
}

create_exes() {
  aws_exe_version="$1"
  aws_exe="$EXE_DIST_DIR/aws"
  aws_completer_exe="$EXE_DIST_DIR/aws_completer"

  aws_exe_contents="echo \"$(aws_version_output "$aws_exe_version")\""
  echo "$aws_exe_contents" > "$aws_exe"
  chmod +x "$aws_exe"

  touch "$aws_completer_exe"
  chmod +x "$aws_completer_exe"
}

aws_version_output() {
  echo "aws-cli/$1 Python/3.9.11 Darwin/17.7.0 botocore/1.12.48"
}

run_install() {
  run "$EXE_BUNDLE_DIR/install" "$@"
}

assert_expected_installation() {
  expected_installed_version="$1"
  expected_current_dir="$INSTALL_DIR/v2/current"
  expected_install_dir="$INSTALL_DIR/v2/$expected_installed_version"

  # Assert that the installation of the installed version is correct
  [ -d "$expected_install_dir" ]
  assert_expected_symlink "$expected_install_dir/bin/aws" "../dist/aws"
  assert_expected_symlink "$expected_install_dir/bin/aws_completer" "../dist/aws_completer"

  # Assert that current points to the expected installed version
  readlink "$expected_current_dir"
  assert_expected_symlink "$expected_current_dir" "$expected_install_dir"

  # Assert the bin symlinks are correct
  assert_expected_symlink "$BIN_DIR/aws" "$expected_current_dir/bin/aws"
  assert_expected_symlink "$BIN_DIR/aws_completer" "$expected_current_dir/bin/aws_completer"

  # Assert the executable works with the expected output
  [ "$("$BIN_DIR/aws" --version)" = "$(aws_version_output "$expected_installed_version")" ]
}

assert_expected_symlink() {
  expected_symlink="$1"
  expected_target="$2"
  [ -L "$expected_symlink" ]
  [ "$(readlink "$expected_symlink")" = "$expected_target" ]
}

@test "-h prints help" {
  run_install -h
  [[ "$output" = *"USAGE"* ]]
}

@test "--help prints help" {
  run_install -h
  [[ "$output" = *"USAGE"* ]]
}

@test "with unexpected arguments" {
  run_install --unexpected
  [ "$status" -eq 1 ]
  [[ "$output" = *"unexpected argument"* ]]
}

@test "install" {
  run_install --install-dir "$INSTALL_DIR" --bin-dir "$BIN_DIR"
  [ "$status" -eq 0 ]
  [ "$output" = "You can now run: $BIN_DIR/aws --version" ]
  assert_expected_installation "$AWS_EXE_VERSION"
}

@test "using shorthand parameters" {
  run_install -i "$INSTALL_DIR" -b "$BIN_DIR"
  [ "$status" -eq 0 ]
  [ "$output" = "You can now run: $BIN_DIR/aws --version" ]
  assert_expected_installation "$AWS_EXE_VERSION"
}

@test "fails when detects preexisting installation" {
  run_install -i "$INSTALL_DIR" -b "$BIN_DIR"
  [ "$status" -eq 0 ]

  new_exe_bundle "new-version"
  run_install -i "$INSTALL_DIR" -b "$BIN_DIR"
  [ "$status" -eq 1 ]
  [[ "$output" = *"Found preexisting"* ]]
}

@test "--updates updates to new version" {
  run_install -i "$INSTALL_DIR" -b "$BIN_DIR"
  [ "$status" -eq 0 ]

  new_exe_bundle "new-version"
  run_install -i "$INSTALL_DIR" -b "$BIN_DIR" --update
  [ "$status" -eq 0 ]
  assert_expected_installation "new-version"
}

@test "-u updates to new version" {
  run_install -i "$INSTALL_DIR" -b "$BIN_DIR"
  [ "$status" -eq 0 ]

  new_exe_bundle "new-version"
  run_install -i "$INSTALL_DIR" -b "$BIN_DIR" -u
  [ "$status" -eq 0 ]
  assert_expected_installation "new-version"
}

@test "--update skips for same version" {
  run_install -i "$INSTALL_DIR" -b "$BIN_DIR"
  [ "$status" -eq 0 ]

  run_install -i "$INSTALL_DIR" -b "$BIN_DIR" -u
  [ "$status" -eq 0 ]
  [[ "$output" = *"Found same AWS CLI version"* ]]
  assert_expected_installation "$AWS_EXE_VERSION"
}