这是indexloc提供的服务,不要输入任何密码
Skip to content

Commit 4834bb0

Browse files
Added: Add termux-core-tests
1 parent 8e8ba6b commit 4834bb0

File tree

5 files changed

+1195
-1
lines changed

5 files changed

+1195
-1
lines changed

Makefile

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,19 @@ all: | pre-build
238238
"$(TERMUX_CORE__TERMUX_REPLACE_TERMUX_CORE_SRC_SCRIPTS_FILE)" $(BIN_BUILD_OUTPUT_DIR)/*
239239

240240

241+
@printf "\ntermux-core-package: %s\n" "Building tests/*"
242+
@mkdir -p $(TESTS_BUILD_OUTPUT_DIR)
243+
find tests -maxdepth 1 -type f -name "*.in" -print0 | xargs -0 -n1 sh -c \
244+
'output_file="$(TESTS_BUILD_OUTPUT_DIR)/$$(printf "%s" "$$0" | sed -e "s|^tests/||" -e "s/\.in$$//")" && mkdir -p "$$(dirname "$$output_file")" && sed $(TERMUX__CONSTANTS__SED_ARGS) "$$0" > "$$output_file"'
245+
find $(TESTS_BUILD_OUTPUT_DIR) -maxdepth 1 -type f -exec chmod 700 "{}" \;
246+
247+
@printf "\ntermux-core-package: %s\n" "Building tests/scripts/*"
248+
@mkdir -p $(TESTS_BUILD_OUTPUT_DIR)/scripts
249+
find tests/scripts -type f -name "*.in" -print0 | xargs -0 -n1 sh -c \
250+
'output_file="$(TESTS_BUILD_OUTPUT_DIR)/scripts/$$(printf "%s" "$$0" | sed -e "s|^tests/scripts/||" -e "s/\.in$$//")" && mkdir -p "$$(dirname "$$output_file")" && sed $(TERMUX__CONSTANTS__SED_ARGS) "$$0" > "$$output_file"'
251+
find $(TESTS_BUILD_OUTPUT_DIR)/scripts -type f -exec chmod 700 "{}" \;
252+
253+
241254
@printf "\ntermux-core-package: %s\n" "Building packaging/debian/*"
242255
@mkdir -p $(DEBIAN_PACKAGING_BUILD_OUTPUT_DIR)
243256
find packaging/debian -mindepth 1 -maxdepth 1 -type f -name "*.in" -exec sh -c \
@@ -265,10 +278,17 @@ clean:
265278
install:
266279
@printf "termux-core-package: %s\n" "Installing termux-core-package in $(TERMUX_CORE_PKG__INSTALL_PREFIX)"
267280
install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/bin
281+
install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec
268282

269283

270284
find $(BIN_BUILD_OUTPUT_DIR) -maxdepth 1 \( -type f -o -type l \) -exec cp -a "{}" $(TERMUX_CORE_PKG__INSTALL_PREFIX)/bin/ \;
271285

286+
287+
288+
rm -rf $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec/installed-tests/termux-core
289+
install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec/installed-tests
290+
cp -a $(TESTS_BUILD_OUTPUT_DIR) $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec/installed-tests/termux-core
291+
272292
@printf "\ntermux-core-package: %s\n\n" "Install termux-core-package successful"
273293

274294
uninstall:
@@ -277,6 +297,10 @@ uninstall:
277297
find scripts \( -type f -o -type l \) -exec sh -c \
278298
'rm -f "$(TERMUX_CORE_PKG__INSTALL_PREFIX)/bin/$$(basename "$$1" | sed "s/\.in$$//")"' sh "{}" \;
279299

300+
301+
302+
rm -rf $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec/installed-tests/termux-core
303+
280304
@printf "\ntermux-core-package: %s\n\n" "Uninstall termux-core-package successful"
281305

282306

@@ -286,4 +310,33 @@ packaging-debian-build: all
286310

287311

288312

289-
.PHONY: all pre-build clean install uninstall packaging-debian-build
313+
test: all
314+
$(MAKE) TERMUX_CORE_PKG__INSTALL_PREFIX=$(PREFIX_BUILD_INSTALL_DIR) install
315+
316+
@printf "\ntermux-core-package: %s\n" "Executing termux-core-package tests"
317+
bash $(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-core/termux-core-tests \
318+
--tests-path="$(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-core" \
319+
--only-termux-core-tests \
320+
-vvv all
321+
322+
test-unit: all
323+
$(MAKE) TERMUX_CORE_PKG__INSTALL_PREFIX=$(PREFIX_BUILD_INSTALL_DIR) install
324+
325+
@printf "\ntermux-core-package: %s\n" "Executing termux-core-package unit tests"
326+
bash $(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-core/termux-core-tests \
327+
--tests-path="$(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-core" \
328+
--only-termux-core-tests \
329+
-vvv unit
330+
331+
test-runtime: all
332+
$(MAKE) TERMUX_CORE_PKG__INSTALL_PREFIX=$(PREFIX_BUILD_INSTALL_DIR) install
333+
334+
@printf "\ntermux-core-package: %s\n" "Executing termux-core-package runtime tests"
335+
bash $(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-core/termux-core-tests \
336+
--tests-path="$(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-core" \
337+
--only-termux-core-tests \
338+
-vvv runtime
339+
340+
341+
342+
.PHONY: all pre-build clean install uninstall packaging-debian-build test test-unit test-runtime
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
page_ref: "@ARK_PROJECT__VARIANT@/termux/termux-core-package/docs/@ARK_DOC__VERSION@/developer/test/index.md"
3+
---
4+
5+
# termux-core-package Test Docs
6+
7+
<!-- @ARK_DOCS__HEADER_PLACEHOLDER@ -->
8+
9+
[`termux-core-tests`](https://github.com/termux/termux-core-package/blob/master/tests/termux-core-tests.in) can be used to run tests for [`termux-core`](https://github.com/termux/termux-core-package) can be tested with and other external Termux packages.
10+
11+
To show help, run `"${TERMUX__PREFIX:-$PREFIX}/libexec/installed-tests/termux-core/termux-core-tests" --help`.
12+
13+
&nbsp;
14+
15+
To run all tests, run `"${TERMUX__PREFIX:-$PREFIX}/libexec/installed-tests/termux-core/termux-core-tests" -vv all`. You can optionally run only `unit` or `runtime` tests as well.
16+
- The `unit` tests test different units/components of libraries and executables.
17+
- The `runtime` tests test the runtime execution of libraries and executables.
18+
19+
&nbsp;
20+
21+
Two variants of each test binary is compiled.
22+
- With `fsanitize` enabled with the `-fsanitize=address -fsanitize-recover=address -fno-omit-frame-pointer` flags that contain `-fsanitize` in filename
23+
- Without `fsanitize` enabled that contain `-nofsanitize` in filename.
24+
25+
This is requires because `fsanitize` does not work on all Android versions/architectures properly and may crash with false positives with the `AddressSantizier: SEGV on unknown address` error, like Android `7` (always crashes) and `x86_64` (requires loop to trigger as occasional crash), even for a source file compiled with an empty `main()` function.
26+
27+
To enable `AddressSantizier` while running `termux-core-tests`, pass `-f`. To also enable `LeakSanitizer`, pass `-l` as well, but if it is not supported on current device, the `termux-core-tests` will error out with `AddressSantizier: detect_leaks is not supported on this platform`.
28+
29+
If you get `CANNOT LINK EXECUTABLE *: library "libclang_rt.asan-aarch64.so" not found`, like on Android `7`, you will need to install the `libcompiler-rt` package to get the `libclang_rt.asan-aarch64.so` dynamic library required for `AddressSantizier` if passing `-f`. Export library file path with `export LD_LIBRARY_PATH="${TERMUX__PREFIX:-$PREFIX}/lib/clang/17/lib/linux"` before running tests.
30+
31+
&nbsp;
32+
33+
By default, `termux-core-tests` also runs tests other external Termux packages/commands to ensure proper functioning of the Termux execution environment. To only run `termux-core` package specific tests, pass `--only-termux-core-tests`. To run tests for specific packages only, pass a regex with `--test-packages-filter=<filter>` option.
34+
35+
&nbsp;
36+
37+
Make sure to grant Termux app `Draw over apps` permission as otherwise `termux-am` tests will fail.
38+
39+
---
40+
41+
&nbsp;
42+
43+
44+
45+
46+
47+
## Help
48+
49+
```
50+
termux-core-tests can be used to run tests for 'termux-core' and
51+
other external Termux packages.
52+
53+
54+
Usage:
55+
termux-core-tests [command_options] <command>
56+
57+
Available commands:
58+
unit Run unit tests.
59+
runtime Run runtime on-device tests.
60+
all Run all tests.
61+
62+
Available command_options:
63+
[ -h | --help ] Display this help screen.
64+
[ --version ] Display version.
65+
[ -q | --quiet ] Set log level to 'OFF'.
66+
[ -v | -vv | -vvv | -vvvvv ]
67+
Set log level to 'DEBUG', 'VERBOSE',
68+
'VVERBOSE' and 'VVVERBOSE'.
69+
[ -f ] Use fsanitize binaries for AddressSanitizer.
70+
[ -l ] Detect memory leaks with LeakSanitizer.
71+
Requires '-f' to be passed.
72+
[ --no-clean ] Do not clean test files on failure.
73+
[ --only-termux-core-tests ]
74+
Only run 'termux-core' package tests.
75+
[ --test-names-filter=<filter> ]
76+
Regex to filter which tests to run by
77+
test name.
78+
[ --test-packages-filter=<filter> ]
79+
Regex to filter which tests to run by
80+
package names. By default tests for
81+
both 'termux-core' and external Termux
82+
packages are run.
83+
[ --tests-path=<path> ] The path to installed-tests directory.
84+
```
85+
86+
---
87+
88+
&nbsp;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!@TERMUX__PREFIX@/bin/bash
2+
# shellcheck shell=bash
3+
4+
5+
##
6+
# `termux_core_project__runtime_script_tests__main`
7+
##
8+
termux_core_project__runtime_script_tests__main() {
9+
10+
TERMUX_CORE__TESTS__LOG_TAG="@TERMUX__LNAME@-core-prj.rs-tests"
11+
12+
termux_core__tests__log 4 "main()"
13+
14+
15+
# Run tests.
16+
termux_core_project__runtime_script_tests__run_tests || return $?
17+
18+
return 0
19+
}
20+
21+
##
22+
# `termux_core_project__runtime_script_tests__run_tests`
23+
##
24+
termux_core_project__runtime_script_tests__run_tests() {
25+
26+
termux_core__tests__log 2 "runTests(start)"
27+
28+
TERMUX_CORE__TESTS__TESTS_COUNT="0"
29+
TERMUX_CORE__TESTS__LOG_EMPTY_LINE_AFTER_SCRIPT_TEST="true"
30+
31+
32+
33+
termux_core__tests__log 2 "runTests(end): $TERMUX_CORE__TESTS__TESTS_COUNT tests completed"
34+
35+
return 0
36+
}

tests/termux-core-project_tests.in

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!@TERMUX__PREFIX@/bin/bash
2+
# shellcheck shell=bash
3+
4+
##
5+
# `termux_core_project__tests__main`
6+
##
7+
termux_core_project__tests__main() {
8+
9+
TERMUX_CORE__TESTS__LOG_TAG="@TERMUX__LNAME@-core-prj.tests"
10+
11+
termux_core__tests__log 4 "main()"
12+
13+
# Set `TERMUX_CORE__TESTS__TESTS_PATH` used by compiled c tests.
14+
if [[ ! "$TERMUX_CORE__TESTS__TESTS_PATH" =~ $TERMUX_CORE__TESTS__REGEX__ROOTFS_OR_ABSOLUTE_PATH ]]; then
15+
termux_core__tests__log_error "The TERMUX_CORE__TESTS__TESTS_PATH '$TERMUX_CORE__TESTS__TESTS_PATH' is either not set or is not an absolute path"
16+
return 1
17+
fi
18+
19+
20+
# Run runtime tests.
21+
if [[ "$TERMUX_CORE__TESTS__RUN_RUNTIME_TESTS" == "true" ]]; then
22+
termux_core_project__runtime_tests__run_command || return $?
23+
fi
24+
25+
return 0
26+
27+
}
28+
29+
30+
31+
##
32+
# `termux_core_project__runtime_tests__run_command`
33+
##
34+
termux_core_project__runtime_tests__run_command() {
35+
36+
termux_core__tests__log 4 "Running 'runtime' tests"
37+
38+
(
39+
termux_core_project__runtime_script_tests__run_command || exit $?
40+
) || return $?
41+
42+
return 0
43+
44+
}
45+
46+
##
47+
# `termux_core_project__runtime_script_tests__run_command`
48+
##
49+
termux_core_project__runtime_script_tests__run_command() {
50+
51+
local return_value
52+
53+
local tests_start_time; tests_start_time="$(date "+%s")" || return $?
54+
55+
termux_core__tests__log 1 "Running 'termux-core-project_runtime-script-tests'"
56+
57+
(
58+
# shellcheck source=tests/scripts/termux-core-project_runtime-script-tests.in
59+
termux_core__tests__source_file_from_path \
60+
"$TERMUX_CORE__TESTS__TESTS_PATH/scripts/termux-core-project_runtime-script-tests" || exit $?
61+
62+
termux_core_project__runtime_script_tests__main
63+
)
64+
return_value=$?
65+
if [ $return_value -ne 0 ]; then
66+
termux_core__tests__log_error "'termux-core-project_runtime-script-tests' failed"
67+
return $return_value
68+
fi
69+
70+
termux_core__tests__log 2 "'termux-core-project_runtime-script-tests' completed in \
71+
$(termux_core__tests__print_elapsed_time "$tests_start_time")"
72+
73+
return 0
74+
75+
}

0 commit comments

Comments
 (0)