Skip to content

Commit d2cf88b

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

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-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+
- "*.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: yarn test:scripts

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

0 commit comments

Comments
 (0)