Skip to content

Commit 2c0cf74

Browse files
authored
test: add e2e tests for cli (#234)
1 parent 6d9dafc commit 2c0cf74

File tree

6 files changed

+145
-1
lines changed

6 files changed

+145
-1
lines changed

MODULE.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ git_override(
2121
remote = "https://github.com/bazel-contrib/toolchains_llvm",
2222
)
2323

24+
bazel_lib = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "toolchains")
25+
bazel_lib.bats()
26+
2427
multitool = use_extension("@rules_multitool//multitool:extension.bzl", "multitool")
2528
multitool.hub(lockfile = "//tools:tools.lock.json")
2629
use_repo(multitool, "multitool")
2730

2831
node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node", dev_dependency = True)
2932
node.toolchain(node_version = "20.18.0")
33+
use_repo(node, "nodejs", "nodejs_toolchains")
3034

3135
pnpm = use_extension("@aspect_rules_js//npm:extensions.bzl", "pnpm")
3236
pnpm.pnpm(

e2e/cli/BUILD.bazel

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load("@aspect_bazel_lib//lib:bats.bzl", "bats_test")
2+
3+
bats_test(
4+
name = "test",
5+
size = "small",
6+
srcs = [
7+
"e2e.bats",
8+
],
9+
data = [
10+
"//e2e/fixtures",
11+
"//e2e/fixtures:versioned",
12+
"//e2e/fixtures:zip",
13+
"//src/application/cli",
14+
"//src/application/cli:bin",
15+
"@nodejs_toolchains//:resolved_toolchain",
16+
],
17+
env = {
18+
"CLI_BIN": "$(rootpath //src/application/cli:bin)",
19+
"NODE_BIN": "$(rootpath @nodejs_toolchains//:resolved_toolchain)",
20+
},
21+
)

e2e/cli/e2e.bats

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
bats_load_library "bats-assert"
2+
bats_load_library "bats-file"
3+
bats_load_library "bats-support"
4+
5+
setup_file() {
6+
export CLI_BIN="$(dirname "${CLI_BIN}")/../main.js"
7+
}
8+
9+
setup() {
10+
export REGISTRY_PATH="${TEST_TMPDIR}/bazel-central-registry"
11+
mkdir -p "${REGISTRY_PATH}/modules"
12+
}
13+
14+
teardown() {
15+
rm -rf "${TEST_TMPDIR}/*"
16+
}
17+
18+
swap_source_url() {
19+
local SRC=$1
20+
local URL=$2
21+
22+
cat "${SRC}" | jq ".url = \"${URL}\"" > "${TEST_TMPDIR}/tmp"
23+
mv "${TEST_TMPDIR}/tmp" "${SRC}"
24+
}
25+
26+
@test 'no_args_shows_help' {
27+
run "${NODE_BIN}" "${CLI_BIN}"
28+
29+
assert_output --partial 'Not enough non-option arguments: got 0, need at least 1'
30+
assert_output --partial 'publish-to-bcr <cmd> [args]'
31+
32+
assert_failure
33+
}
34+
35+
@test '--help' {
36+
run "${NODE_BIN}" "${CLI_BIN}" --help
37+
38+
assert_output --partial 'publish-to-bcr <cmd> [args]'
39+
assert_output --partial 'publish-to-bcr create-entry Create a new module version entry for the BCR'
40+
assert_output --partial '--version Show version number'
41+
42+
assert_success
43+
}
44+
45+
@test 'create entry with tar archive' {
46+
FIXTURE="e2e/fixtures/versioned"
47+
cp -R "${FIXTURE}" "${TEST_TMPDIR}/"
48+
FIXTURE="${TEST_TMPDIR}/$(basename "${FIXTURE}")"
49+
TEMPLATES_DIR="${FIXTURE}/.bcr"
50+
RELEASE_ARCHIVE="e2e/fixtures/versioned-versioned-1.0.0.tar"
51+
52+
swap_source_url "${TEMPLATES_DIR}/source.template.json" "file://$(realpath "${RELEASE_ARCHIVE}")"
53+
54+
run "${NODE_BIN}" "${CLI_BIN}" create-entry --local-registry "${REGISTRY_PATH}" --templates-dir "${TEMPLATES_DIR}" --module-version 1.0.0 --github-repository owner/versioned --tag v1.0.0
55+
56+
assert_success
57+
58+
ENTRY_PATH="${REGISTRY_PATH}/modules/versioned"
59+
60+
assert_file_exists "${ENTRY_PATH}/metadata.json"
61+
assert_file_exists "${ENTRY_PATH}/1.0.0/MODULE.bazel"
62+
assert_file_exists "${ENTRY_PATH}/1.0.0/source.json"
63+
assert_file_exists "${ENTRY_PATH}/1.0.0/presubmit.yml"
64+
}
65+
66+
@test 'create entry with zip archive' {
67+
FIXTURE="e2e/fixtures/zip"
68+
cp -R "${FIXTURE}" "${TEST_TMPDIR}/"
69+
FIXTURE="${TEST_TMPDIR}/$(basename "${FIXTURE}")"
70+
TEMPLATES_DIR="${FIXTURE}/.bcr"
71+
RELEASE_ARCHIVE="e2e/fixtures/zip-zip-1.0.0.zip"
72+
73+
swap_source_url "${TEMPLATES_DIR}/source.template.json" "file://$(realpath "${RELEASE_ARCHIVE}")"
74+
75+
run "${NODE_BIN}" "${CLI_BIN}" create-entry --local-registry "${REGISTRY_PATH}" --templates-dir "${TEMPLATES_DIR}" --module-version 1.0.0 --github-repository owner/zip --tag v1.0.0
76+
77+
assert_success
78+
79+
ENTRY_PATH="${REGISTRY_PATH}/modules/zip"
80+
81+
assert_file_exists "${ENTRY_PATH}/metadata.json"
82+
assert_file_exists "${ENTRY_PATH}/1.0.0/MODULE.bazel"
83+
assert_file_exists "${ENTRY_PATH}/1.0.0/source.json"
84+
assert_file_exists "${ENTRY_PATH}/1.0.0/presubmit.yml"
85+
}
86+
87+
@test 'missing OWNER/REPO vars' {
88+
FIXTURE="e2e/fixtures/versioned"
89+
cp -R "${FIXTURE}" "${TEST_TMPDIR}/"
90+
FIXTURE="${TEST_TMPDIR}/$(basename "${FIXTURE}")"
91+
TEMPLATES_DIR="${FIXTURE}/.bcr"
92+
RELEASE_ARCHIVE="e2e/fixtures/versioned-versioned-1.0.0.tar"
93+
94+
swap_source_url "${TEMPLATES_DIR}/source.template.json" "file://$(realpath "${RELEASE_ARCHIVE}")"
95+
96+
run "${NODE_BIN}" "${CLI_BIN}" create-entry --local-registry "${REGISTRY_PATH}" --templates-dir "${TEMPLATES_DIR}" --module-version 1.0.0 --tag v1.0.0
97+
98+
assert_failure
99+
100+
assert_output --partial 'Did you forget to pass --github-repository to substitute the OWNER and REPO variables?'
101+
}

src/application/cli/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ts_project(
1010
"main.ts",
1111
"yargs.ts",
1212
],
13+
visibility = ["//e2e/cli:__pkg__"],
1314
deps = [
1415
"//:node_modules/@nestjs/common",
1516
"//:node_modules/@nestjs/core",
@@ -26,6 +27,7 @@ js_binary(
2627
name = "bin",
2728
data = [":cli"],
2829
entry_point = "main.js",
30+
visibility = ["//e2e/cli:__pkg__"],
2931
)
3032

3133
ts_project(

src/application/cli/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ async function main() {
1616
await parser.parse();
1717
} catch (err) {
1818
console.error(`${err.message}\n ${await parser.getHelp()}`);
19+
process.exit(1);
1920
}
2021

2122
await app.close();

src/domain/release-archive.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ export class MissingModuleFileError extends UserFacingError {
4545
}
4646

4747
export class ReleaseArchive {
48-
public static readonly SUPPORTED_EXTENSIONS = ['.zip', '.tar.gz', '.tar.xz'];
48+
public static readonly SUPPORTED_EXTENSIONS = [
49+
'.zip',
50+
'.tar',
51+
'.tar.gz',
52+
'.tar.xz',
53+
];
4954
public static async fetch(
5055
url: string,
5156
stripPrefix: string
@@ -92,6 +97,9 @@ export class ReleaseArchive {
9297
}
9398

9499
private isSupportedTarball(): boolean {
100+
if (this._diskPath.endsWith('.tar')) {
101+
return true;
102+
}
95103
if (this._diskPath.endsWith('.tar.gz')) {
96104
return true;
97105
}
@@ -176,6 +184,13 @@ async function download(url: string, dest: string): Promise<void> {
176184
url = `http://${host}:${port}${parsed.path}`;
177185
}
178186

187+
// Support downloading from another location on disk.
188+
// Useful for swapping in a local path for e2e tests.
189+
if (url.startsWith('file://')) {
190+
fs.copyFileSync(url.substring('file://'.length), dest);
191+
return;
192+
}
193+
179194
const writer = fs.createWriteStream(dest, { flags: 'w' });
180195

181196
// Retry the request in case the artifact is still being uploaded.

0 commit comments

Comments
 (0)