Skip to content

Commit 24f6834

Browse files
committed
Add installer unit tests
1 parent c31e72f commit 24f6834

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed

.github/workflows/scripts.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Script unit tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- "installer.sh"
9+
pull_request:
10+
branches:
11+
- main
12+
13+
jobs:
14+
test:
15+
name: Run script unit tests
16+
runs-on: ubuntu-latest
17+
# This runs on Alpine to make sure we're testing with actual sh.
18+
container: "alpine:3.14"
19+
steps:
20+
- name: Checkout repo
21+
uses: actions/checkout@v2
22+
23+
- name: Install bats
24+
run: apk add bats
25+
26+
- name: Run script unit tests
27+
run: ./ci/dev/test-scripts.sh

ci/dev/test-scripts.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
main() {
5+
cd "$(dirname "$0")/../.."
6+
bats ./test/scripts
7+
}
8+
9+
main "$@"

docs/CONTRIBUTING.md

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Here is what is needed:
4444
- Get this by running `apt-get install -y build-essential`
4545
- `rsync` and `unzip`
4646
- Used for code-server releases
47+
- `bats`
48+
- Used to run script unit tests
4749

4850
## Creating pull requests
4951

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"test:e2e": "./ci/dev/test-e2e.sh",
2121
"test:standalone-release": "./ci/build/test-standalone-release.sh",
2222
"test:unit": "./ci/dev/test-unit.sh",
23+
"test:scripts": "./ci/dev/test-scripts.sh",
2324
"package": "./ci/build/build-packages.sh",
2425
"postinstall": "./ci/dev/postinstall.sh",
2526
"update:vscode": "./ci/dev/update-vscode.sh",

test/scripts/install.bats

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#!/usr/bin/env bats
2+
3+
SCRIPT_NAME="install.sh"
4+
SCRIPT="$BATS_TEST_DIRNAME/../../$SCRIPT_NAME"
5+
6+
# Override version so it doesn't have to curl and to avoid caching in case the
7+
# user already has the latest version installed.
8+
export VERSION="9999.99.9"
9+
10+
function should-use-deb() {
11+
DISTRO=$1 ARCH=$2 OS=linux run "$SCRIPT" --dry-run
12+
[ "$status" -eq 0 ]
13+
[ "${lines[1]}" = "Installing v$VERSION of the $2 deb package from GitHub." ]
14+
[ "${lines[-5]}" = "deb package has been installed." ]
15+
}
16+
17+
function should-use-rpm() {
18+
DISTRO=$1 ARCH=$2 OS=linux run "$SCRIPT" --dry-run
19+
[ "$status" -eq 0 ]
20+
[ "${lines[1]}" = "Installing v$VERSION of the $2 rpm package from GitHub." ]
21+
[ "${lines[-5]}" = "rpm package has been installed." ]
22+
}
23+
24+
function should-fallback-npm() {
25+
YARN_PATH=true DISTRO=$1 ARCH=$2 OS=linux run "$SCRIPT" --dry-run
26+
[ "$status" -eq 0 ]
27+
[ "${lines[1]}" = "No standalone releases for $2." ]
28+
[ "${lines[2]}" = "Falling back to installation from npm." ]
29+
[ "${lines[3]}" = "Installing latest from npm." ]
30+
[ "${lines[-5]}" = "npm package has been installed." ]
31+
}
32+
33+
function should-use-npm() {
34+
YARN_PATH=true DISTRO=$1 ARCH=$2 OS=linux run "$SCRIPT" --dry-run
35+
[ "$status" -eq 0 ]
36+
[ "${lines[1]}" = "Installing latest from npm." ]
37+
[ "${lines[-5]}" = "npm package has been installed." ]
38+
}
39+
40+
function should-use-aur() {
41+
DISTRO=$1 ARCH=$2 OS=linux run "$SCRIPT" --dry-run
42+
[ "$status" -eq 0 ]
43+
[ "${lines[1]}" = "Installing latest from the AUR." ]
44+
[ "${lines[-5]}" = "AUR package has been installed." ]
45+
}
46+
47+
function should-fallback-npm-brew() {
48+
YARN_PATH=true BREW_PATH= OS=macos ARCH=$1 run "$SCRIPT" --dry-run
49+
[ "$status" -eq 0 ]
50+
[ "${lines[1]}" = "Homebrew not installed." ]
51+
[ "${lines[2]}" = "Falling back to standalone installation." ]
52+
[ "${lines[3]}" = "No standalone releases for $1." ]
53+
[ "${lines[4]}" = "Falling back to installation from npm." ]
54+
[ "${lines[5]}" = "Installing latest from npm." ]
55+
[ "${lines[-5]}" = "npm package has been installed." ]
56+
}
57+
58+
function should-use-brew() {
59+
BREW_PATH=true OS=macos ARCH=$1 run "$SCRIPT" --dry-run
60+
[ "$status" -eq 0 ]
61+
[ "${lines[1]}" = "Installing latest from Homebrew." ]
62+
[ "${lines[-3]}" = "Brew release has been installed." ]
63+
}
64+
65+
function should-use-standalone() {
66+
DISTRO=$1 ARCH=$2 OS=$3 run "$SCRIPT" --method standalone --dry-run
67+
[ "$status" -eq 0 ]
68+
[ "${lines[1]}" = "Installing v$VERSION of the $2 release from GitHub." ]
69+
[[ "${lines[-5]}" = "Standalone release has been installed"* ]]
70+
}
71+
72+
@test "$SCRIPT_NAME: usage with --help" {
73+
run "$SCRIPT" --help
74+
[ "$status" -eq 0 ]
75+
[ "${lines[0]}" = "Installs code-server." ]
76+
[[ "${lines[-1]}" = "More installation docs are at"* ]]
77+
}
78+
79+
# These use the deb but fall back to npm for unsupported architectures.
80+
@test "$SCRIPT_NAME: debian arm64" {
81+
should-use-deb "debian" "arm64"
82+
}
83+
@test "$SCRIPT_NAME: debian amd64" {
84+
should-use-deb "debian" "amd64"
85+
}
86+
@test "$SCRIPT_NAME: debian i386" {
87+
should-fallback-npm "debian" "i386"
88+
}
89+
90+
# These use the rpm but fall back to npm for unsupported architectures.
91+
@test "$SCRIPT_NAME: fedora arm64" {
92+
should-use-rpm "fedora" "arm64"
93+
}
94+
@test "$SCRIPT_NAME: fedora amd64" {
95+
should-use-rpm "fedora" "amd64"
96+
}
97+
@test "$SCRIPT_NAME: fedora i386" {
98+
should-fallback-npm "fedora" "i386"
99+
}
100+
101+
# These always use npm regardless of the architecture.
102+
@test "$SCRIPT_NAME: alpine arm64" {
103+
should-use-npm "alpine" "arm64"
104+
}
105+
@test "$SCRIPT_NAME: alpine amd64" {
106+
should-use-npm "alpine" "amd64"
107+
}
108+
@test "$SCRIPT_NAME: alpine i386" {
109+
should-use-npm "alpine" "i386"
110+
}
111+
112+
@test "$SCRIPT_NAME: freebsd arm64" {
113+
should-use-npm "freebsd" "arm64"
114+
}
115+
@test "$SCRIPT_NAME: freebsd amd64" {
116+
should-use-npm "freebsd" "amd64"
117+
}
118+
@test "$SCRIPT_NAME: freebsd i386" {
119+
should-use-npm "freebsd" "i386"
120+
}
121+
122+
# Arch Linux uses AUR but falls back to npm for unsuppported architectures.
123+
@test "$SCRIPT_NAME: arch arm64" {
124+
should-use-aur "arch" "arm64"
125+
}
126+
@test "$SCRIPT_NAME: arch amd64" {
127+
should-use-aur "arch" "amd64"
128+
}
129+
@test "$SCRIPT_NAME: arch i386" {
130+
should-fallback-npm "arch" "i386"
131+
}
132+
133+
# macOS use homebrew but falls back to standalone when brew is unavailable then
134+
# to npm for unsupported architectures.
135+
@test "$SCRIPT_NAME: macos arm64 (no brew)" {
136+
should-fallback-npm-brew "arm64"
137+
}
138+
@test "$SCRIPT_NAME: macos amd64 (no brew)" {
139+
BREW_PATH= OS=macos ARCH=amd64 run "$SCRIPT" --dry-run
140+
[ "$status" -eq 0 ]
141+
[ "${lines[1]}" = "Homebrew not installed." ]
142+
[ "${lines[2]}" = "Falling back to standalone installation." ]
143+
[ "${lines[3]}" = "Installing v$VERSION of the amd64 release from GitHub." ]
144+
[[ "${lines[-5]}" = "Standalone release has been installed"* ]]
145+
}
146+
@test "$SCRIPT_NAME: macos i386 (no brew)" {
147+
should-fallback-npm-brew "i386"
148+
}
149+
150+
@test "$SCRIPT_NAME: macos arm64 (brew)" {
151+
should-use-brew "arm64"
152+
}
153+
@test "$SCRIPT_NAME: macos amd64 (brew)" {
154+
should-use-brew "amd64"
155+
}
156+
@test "$SCRIPT_NAME: macos i386 (brew)" {
157+
should-use-brew "i386"
158+
}
159+
160+
# Force standalone.
161+
@test "$SCRIPT_NAME: debian amd64 --method standalone" {
162+
should-use-standalone "debian" "amd64" "linux"
163+
}

0 commit comments

Comments
 (0)