返回 CodeWhale
check-ohos-deps.test.sh
根目录 / scripts / release / check-ohos-deps.test.sh
1 #!/usr/bin/env bash
2 set -euo pipefail
3
4 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
5 checker="${repo_root}/scripts/release/check-ohos-deps.sh"
6 tmp_base="${TMPDIR:-/tmp}"
7 tmp_base="${tmp_base%/}"
8 tmp_base="${tmp_base:-/}"
9 fixture="$(mktemp -d "${tmp_base}/check-ohos-deps-test.XXXXXX")"
10 unwritable_dir="${fixture}/unwritable-tmp"
11 cleanup() {
12 if [[ -d "${unwritable_dir}" ]]; then
13 chmod 700 "${unwritable_dir}" || true
14 fi
15 rm -rf "${fixture}"
16 }
17 trap cleanup EXIT
18
19 mkdir -p "${fixture}/bin" "${fixture}/grep-fail-bin" "${fixture}/tmp" "${unwritable_dir}"
20 chmod 500 "${unwritable_dir}"
21
22 cat >"${fixture}/bin/cargo" <<'EOF'
23 #!/usr/bin/env bash
24 set -euo pipefail
25 mode="${FAKE_CARGO_MODE:-ok}"
26 pkg=""
27 while [[ "$#" -gt 0 ]]; do
28 case "$1" in
29 --package)
30 pkg="${2:-}"
31 shift 2
32 ;;
33 *)
34 shift
35 ;;
36 esac
37 done
38
39 case "${mode}" in
40 ok)
41 if [[ "${pkg}" == "codewhale-tui" ]]; then
42 printf 'codewhale-tui v0.10.3\ntokio v1.40.0\n'
43 else
44 printf 'codewhale-workflow-js v0.10.3\nrquickjs feature "bindgen"\nrquickjs-sys feature "bindgen"\n'
45 fi
46 ;;
47 disallowed)
48 if [[ "${pkg}" == "codewhale-tui" ]]; then
49 printf 'codewhale-tui v0.10.3\nportable-pty v0.8.1\n'
50 else
51 printf 'codewhale-workflow-js v0.10.3\nrquickjs feature "bindgen"\nrquickjs-sys feature "bindgen"\n'
52 fi
53 ;;
54 empty)
55 exit 0
56 ;;
57 missing_root)
58 printf 'tokio v1.40.0\n'
59 ;;
60 missing_bindgen)
61 if [[ "${pkg}" == "codewhale-tui" ]]; then
62 printf 'codewhale-tui v0.10.3\ntokio v1.40.0\n'
63 else
64 printf 'codewhale-workflow-js v0.10.3\nrquickjs feature "bindgen"\n'
65 fi
66 ;;
67 *)
68 echo "unknown FAKE_CARGO_MODE: ${mode}" >&2
69 exit 2
70 ;;
71 esac
72 EOF
73 chmod +x "${fixture}/bin/cargo"
74
75 real_grep="$(command -v grep)"
76 cat >"${fixture}/grep-fail-bin/grep" <<EOF
77 #!/usr/bin/env bash
78 for arg in "\$@"; do
79 if [[ "\${arg}" == *'portable-pty'* ]]; then
80 echo "simulated grep internal failure" >&2
81 exit 2
82 fi
83 done
84 exec "${real_grep}" "\$@"
85 EOF
86 chmod +x "${fixture}/grep-fail-bin/grep"
87
88 run_case() {
89 local expected="$1"
90 local label="$2"
91 shift 2
92 local out_file="${fixture}/${label}.out"
93 local rc=0
94 "$@" >"${out_file}" 2>&1 || rc=$?
95 if [[ "${expected}" == "pass" ]]; then
96 if ((rc != 0)); then
97 echo "FAIL [${label}]: expected exit 0, got ${rc}" >&2
98 cat "${out_file}" >&2
99 exit 1
100 fi
101 else
102 if ((rc == 0)); then
103 echo "FAIL [${label}]: expected non-zero exit, got 0" >&2
104 cat "${out_file}" >&2
105 exit 1
106 fi
107 fi
108 }
109
110 run_case pass clean \
111 env PATH="${fixture}/bin:${PATH}" TMPDIR="${fixture}/tmp" CODEWHALE_OHOS_DEP_RETRIES=1 \
112 FAKE_CARGO_MODE=ok "${checker}"
113 grep -Fq 'OHOS dependency graph OK for codewhale-tui' "${fixture}/clean.out"
114 grep -Fq 'OHOS rquickjs-sys bindgen feature edge OK for codewhale-workflow-js' "${fixture}/clean.out"
115
116 run_case fail disallowed \
117 env PATH="${fixture}/bin:${PATH}" TMPDIR="${fixture}/tmp" CODEWHALE_OHOS_DEP_RETRIES=1 \
118 FAKE_CARGO_MODE=disallowed "${checker}"
119 grep -Fq 'portable-pty v0.8.1' "${fixture}/disallowed.out"
120
121 run_case fail empty \
122 env PATH="${fixture}/bin:${PATH}" TMPDIR="${fixture}/tmp" CODEWHALE_OHOS_DEP_RETRIES=1 \
123 FAKE_CARGO_MODE=empty "${checker}"
124 grep -Fq 'returned empty output' "${fixture}/empty.out"
125
126 run_case fail missing_root \
127 env PATH="${fixture}/bin:${PATH}" TMPDIR="${fixture}/tmp" CODEWHALE_OHOS_DEP_RETRIES=1 \
128 FAKE_CARGO_MODE=missing_root "${checker}"
129 grep -Fq 'did not contain root package codewhale-tui' "${fixture}/missing_root.out"
130
131 run_case fail unwritable_tmpdir \
132 env PATH="${fixture}/bin:${PATH}" TMPDIR="${unwritable_dir}" CODEWHALE_OHOS_DEP_RETRIES=1 \
133 FAKE_CARGO_MODE=ok "${checker}"
134 grep -Fq 'Failed to create temporary file' "${fixture}/unwritable_tmpdir.out"
135
136 run_case fail grep_internal_error \
137 env PATH="${fixture}/grep-fail-bin:${fixture}/bin:${PATH}" TMPDIR="${fixture}/tmp" CODEWHALE_OHOS_DEP_RETRIES=1 \
138 FAKE_CARGO_MODE=ok "${checker}"
139 grep -Fq 'Failed to inspect OHOS dependency graph' "${fixture}/grep_internal_error.out"
140
141 run_case fail missing_bindgen \
142 env PATH="${fixture}/bin:${PATH}" TMPDIR="${fixture}/tmp" CODEWHALE_OHOS_DEP_RETRIES=1 \
143 FAKE_CARGO_MODE=missing_bindgen "${checker}"
144 grep -Fq 'lost the rquickjs bindgen feature edges' "${fixture}/missing_bindgen.out"
145
146 echo "check-ohos-deps tests passed (7/7)"
147
147 lines BASH