Skip to content

Commit b58b8fe

Browse files
committed
Add installer unit tests
1 parent 36b65ed commit b58b8fe

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

.github/workflows/scripts.yml

Lines changed: 27 additions & 0 deletions
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+
centos:
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: Install code-server
27+
run: ./ci/dev/test-scripts.sh

ci/dev/test-scripts.sh

Lines changed: 9 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ There are several differences, however. Here is what is needed:
5454
- `apt-get install -y build-essential` - used by VS Code
5555
- `rsync` and `unzip`
5656
- used for code-server releases
57+
- `bats`
58+
- used to run script unit tests
5759

5860
## Development Workflow
5961

package.json

Lines changed: 1 addition & 0 deletions
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

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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.
7+
export VERSION="9999.99.9"
8+
9+
function should-use-deb() {
10+
DISTRO=$1 ARCH=$2 OS=linux run "$SCRIPT" --dry-run
11+
[ "$status" -eq 0 ]
12+
[ "${lines[1]}" = "Installing v$VERSION of the $2 deb package from GitHub." ]
13+
}
14+
15+
function should-use-rpm() {
16+
DISTRO=$1 ARCH=$2 OS=linux run "$SCRIPT" --dry-run
17+
[ "$status" -eq 0 ]
18+
[ "${lines[1]}" = "Installing v$VERSION of the $2 rpm package from GitHub." ]
19+
}
20+
21+
function should-fallback-npm() {
22+
YARN_PATH=true DISTRO=$1 ARCH=$2 OS=linux run "$SCRIPT" --dry-run
23+
echo "status = $status"
24+
echo "output = $output"
25+
[ "$status" -eq 0 ]
26+
[ "${lines[1]}" = "No standalone releases for $2." ]
27+
[ "${lines[2]}" = "Falling back to installation from npm." ]
28+
[ "${lines[3]}" = "Installing latest from npm." ]
29+
}
30+
31+
function should-use-npm() {
32+
YARN_PATH=true DISTRO=$1 ARCH=$2 OS=linux run "$SCRIPT" --dry-run
33+
[ "$status" -eq 0 ]
34+
[ "${lines[1]}" = "Installing latest from npm." ]
35+
}
36+
37+
function should-use-aur() {
38+
DISTRO=$1 ARCH=$2 OS=linux run "$SCRIPT" --dry-run
39+
[ "$status" -eq 0 ]
40+
[ "${lines[1]}" = "Installing latest from the AUR." ]
41+
}
42+
43+
function should-fallback-npm-brew() {
44+
YARN_PATH=true BREW_PATH= OS=macos ARCH=$1 run "$SCRIPT" --dry-run
45+
[ "$status" -eq 0 ]
46+
[ "${lines[1]}" = "Homebrew not installed." ]
47+
[ "${lines[2]}" = "Falling back to standalone installation." ]
48+
[ "${lines[3]}" = "No standalone releases for $1." ]
49+
[ "${lines[4]}" = "Falling back to installation from npm." ]
50+
[ "${lines[5]}" = "Installing latest from npm." ]
51+
}
52+
53+
function should-use-brew() {
54+
BREW_PATH=true OS=macos ARCH=$1 run "$SCRIPT" --dry-run
55+
[ "$status" -eq 0 ]
56+
[ "${lines[1]}" = "Installing latest from Homebrew." ]
57+
}
58+
59+
@test "$SCRIPT_NAME: usage with --help" {
60+
run "$SCRIPT" --help
61+
[ "$status" -eq 0 ]
62+
[ "${lines[0]}" = "Installs code-server." ]
63+
}
64+
65+
# These use the deb but fall back to npm for unsupported arch.
66+
@test "$SCRIPT_NAME: debian arm64" {
67+
should-use-deb "debian" "arm64"
68+
}
69+
@test "$SCRIPT_NAME: debian amd64" {
70+
should-use-deb "debian" "amd64"
71+
}
72+
@test "$SCRIPT_NAME: debian i386" {
73+
should-fallback-npm "debian" "i386"
74+
}
75+
76+
# These use the rpm but fall back to npm for unsupported arch.
77+
@test "$SCRIPT_NAME: fedora arm64" {
78+
should-use-rpm "fedora" "arm64"
79+
}
80+
@test "$SCRIPT_NAME: fedora amd64" {
81+
should-use-rpm "fedora" "amd64"
82+
}
83+
@test "$SCRIPT_NAME: fedora i386" {
84+
should-fallback-npm "fedora" "i386"
85+
}
86+
87+
# These always use npm regardless of the arch.
88+
@test "$SCRIPT_NAME: alpine arm64" {
89+
should-use-npm "alpine" "arm64"
90+
}
91+
@test "$SCRIPT_NAME: alpine amd64" {
92+
should-use-npm "alpine" "amd64"
93+
}
94+
@test "$SCRIPT_NAME: alpine i386" {
95+
should-use-npm "alpine" "i386"
96+
}
97+
98+
@test "$SCRIPT_NAME: freebsd arm64" {
99+
should-use-npm "freebsd" "arm64"
100+
}
101+
@test "$SCRIPT_NAME: freebsd amd64" {
102+
should-use-npm "freebsd" "amd64"
103+
}
104+
@test "$SCRIPT_NAME: freebsd i386" {
105+
should-use-npm "freebsd" "i386"
106+
}
107+
108+
# Arch Linux uses AUR but falls back to npm for unsuppported arch.
109+
@test "$SCRIPT_NAME: arch arm64" {
110+
should-use-aur "arch" "arm64"
111+
}
112+
@test "$SCRIPT_NAME: arch amd64" {
113+
should-use-aur "arch" "amd64"
114+
}
115+
@test "$SCRIPT_NAME: arch i386" {
116+
should-fallback-npm "arch" "i386"
117+
}
118+
119+
# macOS use homebrew but falls back to standalone when brew is unavailable and
120+
# to npm for unsupported arch.
121+
@test "$SCRIPT_NAME: macos arm64 (no brew)" {
122+
should-fallback-npm-brew "arm64"
123+
}
124+
@test "$SCRIPT_NAME: macos amd64 (no brew)" {
125+
BREW_PATH= OS=macos ARCH=amd64 run "$SCRIPT" --dry-run
126+
[ "$status" -eq 0 ]
127+
[ "${lines[1]}" = "Homebrew not installed." ]
128+
[ "${lines[2]}" = "Falling back to standalone installation." ]
129+
[ "${lines[3]}" = "Installing v$VERSION of the amd64 release from GitHub." ]
130+
}
131+
@test "$SCRIPT_NAME: macos i386 (no brew)" {
132+
should-fallback-npm-brew "i386"
133+
}
134+
135+
@test "$SCRIPT_NAME: macos arm64 (brew)" {
136+
should-use-brew "arm64"
137+
}
138+
@test "$SCRIPT_NAME: macos amd64 (brew)" {
139+
should-use-brew "amd64"
140+
}
141+
@test "$SCRIPT_NAME: macos i386 (brew)" {
142+
should-use-brew "i386"
143+
}

0 commit comments

Comments
 (0)