diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a972f4d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/build/install/* +/build/output/* +*.deb diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..10bb910 --- /dev/null +++ b/LICENSE @@ -0,0 +1,10 @@ +Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ + + + +Files: + * +Comment: + The `termux-core-package` repository is released under the `MIT` license, unless specified + differently in a file/directory or in any additional `Files` sections below. +License: [MIT](licenses/termux__termux-core-package__MIT.md) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7a4364a --- /dev/null +++ b/Makefile @@ -0,0 +1,512 @@ +export TERMUX_CORE_PKG__VERSION := 0.1.0 +export TERMUX_CORE_PKG__ARCH +export TERMUX_CORE_PKG__INSTALL_PREFIX + +export TERMUX__NAME := Termux# Default value: `Termux` +export TERMUX__LNAME := termux# Default value: `termux` + +export TERMUX__REPOS_HOST_ORG_NAME := termux# Default value: `termux` +export TERMUX__REPOS_HOST_ORG_URL := https://github.com/$(TERMUX__REPOS_HOST_ORG_NAME)# Default value: `https://github.com/termux` + +export TERMUX_APP__NAME := Termux# Default value: `Termux` +export TERMUX_APP__PACKAGE_NAME := com.termux# Default value: `com.termux` +export TERMUX_APP__DATA_DIR := /data/data/$(TERMUX_APP__PACKAGE_NAME)# Default value: `/data/data/com.termux` + +export TERMUX__ROOTFS := $(TERMUX_APP__DATA_DIR)/files# Default value: `/data/data/com.termux/files` +export TERMUX__HOME := $(TERMUX__ROOTFS)/home# Default value: `/data/data/com.termux/files/home` +export TERMUX__PREFIX := $(TERMUX__ROOTFS)/usr# Default value: `/data/data/com.termux/files/usr` +export TERMUX__PREFIX__BIN_DIR := $(TERMUX__PREFIX)/bin# Default value: `/data/data/com.termux/files/usr/bin` +export TERMUX__PREFIX__INCLUDE_DIR := $(TERMUX__PREFIX)/include# Default value: `/data/data/com.termux/files/usr/include` +export TERMUX__PREFIX__LIB_DIR := $(TERMUX__PREFIX)/lib# Default value: `/data/data/com.termux/files/usr/lib` +export TERMUX__PREFIX__TMP_DIR := $(TERMUX__PREFIX)/tmp# Default value: `/data/data/com.termux/files/usr/tmp` + +export TERMUX_ENV__S_ROOT := TERMUX_# Default value: `TERMUX_` +export TERMUX_ENV__SS_TERMUX := _# Default value: `_` +export TERMUX_ENV__S_TERMUX := $(TERMUX_ENV__S_ROOT)$(TERMUX_ENV__SS_TERMUX)# Default value: `TERMUX__` +export TERMUX_ENV__SS_TERMUX_APP := APP__# Default value: `APP__` +export TERMUX_ENV__S_TERMUX_APP := $(TERMUX_ENV__S_ROOT)$(TERMUX_ENV__SS_TERMUX_APP)# Default value: `TERMUX_APP__` +export TERMUX_ENV__SS_TERMUX_API_APP := API_APP__# Default value: `API_APP__` +export TERMUX_ENV__S_TERMUX_API_APP := $(TERMUX_ENV__S_ROOT)$(TERMUX_ENV__SS_TERMUX_API_APP)# Default value: `TERMUX_API_APP__` +export TERMUX_ENV__SS_TERMUX_ROOTFS := ROOTFS__# Default value: `ROOTFS__` +export TERMUX_ENV__S_TERMUX_ROOTFS := $(TERMUX_ENV__S_ROOT)$(TERMUX_ENV__SS_TERMUX_ROOTFS)# Default value: `TERMUX_ROOTFS__` +export TERMUX_ENV__SS_TERMUX_CORE := CORE__# Default value: `CORE__` +export TERMUX_ENV__S_TERMUX_CORE := $(TERMUX_ENV__S_ROOT)$(TERMUX_ENV__SS_TERMUX_CORE)# Default value: `TERMUX_CORE__` +export TERMUX_ENV__SS_TERMUX_CORE__TESTS := CORE__TESTS__# Default value: `CORE__TESTS__` +export TERMUX_ENV__S_TERMUX_CORE__TESTS := $(TERMUX_ENV__S_ROOT)$(TERMUX_ENV__SS_TERMUX_CORE__TESTS)# Default value: `TERMUX_CORE__TESTS__` +export TERMUX_ENV__SS_TERMUX_EXEC__TESTS := EXEC__TESTS__# Default value: `EXEC__TESTS__` +export TERMUX_ENV__S_TERMUX_EXEC__TESTS := $(TERMUX_ENV__S_ROOT)$(TERMUX_ENV__SS_TERMUX_EXEC__TESTS)# Default value: `TERMUX_EXEC__TESTS__` + +export TERMUX_APP__NAMESPACE := $(TERMUX_APP__PACKAGE_NAME)# Default value: `com.termux` +export TERMUX_APP__SHELL_ACTIVITY__COMPONENT_NAME := $(TERMUX_APP__PACKAGE_NAME)/$(TERMUX_APP__NAMESPACE).app.TermuxActivity# Default value: `com.termux/com.termux.app.TermuxActivity` +export TERMUX_APP__SHELL_SERVICE__COMPONENT_NAME := $(TERMUX_APP__PACKAGE_NAME)/$(TERMUX_APP__NAMESPACE).app.TermuxService# Default value: `com.termux/com.termux.app.TermuxService` + +export TERMUX_PKGS__REPO_NAME := termux-packages# Default value: `termux-packages` +export TERMUX_PKGS__REPO_URL := $(TERMUX__REPOS_HOST_ORG_URL)/$(TERMUX_PKGS__REPO_NAME)# Default value: `https://github.com/termux/termux-packages` +export TERMUX_PKGS__BUILD__REPO_ROOT_DIR + +export TERMUX_CORE_PKG__REPO_NAME := termux-core-package# Default value: `termux-core-package` +export TERMUX_CORE_PKG__REPO_URL := $(TERMUX__REPOS_HOST_ORG_URL)/$(TERMUX_CORE_PKG__REPO_NAME)# Default value: `https://github.com/termux/termux-core-package` +export TERMUX_CORE__TERMUX_REPLACE_TERMUX_CORE_SRC_SCRIPTS_FILE := $(TERMUX_PKGS__BUILD__REPO_ROOT_DIR)/packages/termux-core/build/scripts/termux-replace-termux-core-src-scripts + + + +# Check if TERMUX_PKGS__BUILD__REPO_ROOT_DIR and TERMUX_CORE__TERMUX_REPLACE_TERMUX_CORE_SRC_SCRIPTS_FILE are set and exist +ifeq ($(TERMUX_PKGS__BUILD__REPO_ROOT_DIR),) + $(error The TERMUX_PKGS__BUILD__REPO_ROOT_DIR variable is not set) +else ifneq ($(patsubst /%,,$(TERMUX_PKGS__BUILD__REPO_ROOT_DIR)),) + $(error The TERMUX_PKGS__BUILD__REPO_ROOT_DIR variable '$(TERMUX_PKGS__BUILD__REPO_ROOT_DIR)' is not set to an absolute path) +else ifneq ($(shell test -d "$(TERMUX_PKGS__BUILD__REPO_ROOT_DIR)" && echo 1 || echo 0), 1) + $(error The termux-pacakges build repo directory does not exist at TERMUX_PKGS__BUILD__REPO_ROOT_DIR '$(TERMUX_PKGS__BUILD__REPO_ROOT_DIR)' path) +else ifneq ($(shell test -f "$(TERMUX_CORE__TERMUX_REPLACE_TERMUX_CORE_SRC_SCRIPTS_FILE)" && echo 1 || echo 0), 1) + $(error The 'replace-termux-core-src-scripts' file does not exist at TERMUX_CORE__TERMUX_REPLACE_TERMUX_CORE_SRC_SCRIPTS_FILE '$(TERMUX_CORE__TERMUX_REPLACE_TERMUX_CORE_SRC_SCRIPTS_FILE)' path) +endif + + + +# If architecture not set, find it for the compiler based on which +# predefined architecture macro is defined. The `shell` function +# replaces newlines with a space and a literal space cannot be entered +# in a makefile as its used as a splitter, hence $(SPACE) variable is +# created and used for matching. +ifeq ($(TERMUX_CORE_PKG__ARCH),) + export override PREDEFINED_MACROS := $(shell $(CC) -x c /dev/null -dM -E) + override EMPTY := + override SPACE := $(EMPTY) $(EMPTY) + ifneq (,$(findstring $(SPACE)#define __i686__ 1$(SPACE),$(SPACE)$(PREDEFINED_MACROS)$(SPACE))) + override TERMUX_CORE_PKG__ARCH := i686 + else ifneq (,$(findstring $(SPACE)#define __x86_64__ 1$(SPACE),$(SPACE)$(PREDEFINED_MACROS)$(SPACE))) + override TERMUX_CORE_PKG__ARCH := x86_64 + else ifneq (,$(findstring $(SPACE)#define __aarch64__ 1$(SPACE),$(SPACE)$(PREDEFINED_MACROS)$(SPACE))) + override TERMUX_CORE_PKG__ARCH := aarch64 + else ifneq (,$(findstring $(SPACE)#define __arm__ 1$(SPACE),$(SPACE)$(PREDEFINED_MACROS)$(SPACE))) + override TERMUX_CORE_PKG__ARCH := arm + else + $(error Unsupported package arch) + endif +endif + + +export override IS_ON_DEVICE_BUILD := $(shell test -f "/system/bin/app_process" && echo 1 || echo 0) + + + +export override BUILD_DIR := build# Default value: `build` + +export override BUILD_OUTPUT_DIR := $(BUILD_DIR)/output# Default value: `build/output` + +export override TMP_BUILD_OUTPUT_DIR := $(BUILD_OUTPUT_DIR)/tmp# Default value: `build/output/tmp` + +export override PREFIX_BUILD_OUTPUT_DIR := $(BUILD_OUTPUT_DIR)/usr# Default value: `build/output/usr` +export override BIN_BUILD_OUTPUT_DIR := $(PREFIX_BUILD_OUTPUT_DIR)/bin# Default value: `build/output/usr/bin` +export override LIB_BUILD_OUTPUT_DIR := $(PREFIX_BUILD_OUTPUT_DIR)/lib# Default value: `build/output/usr/lib` +export override LIBEXEC_BUILD_OUTPUT_DIR := $(PREFIX_BUILD_OUTPUT_DIR)/libexec# Default value: `build/output/usr/libexec` +export override TESTS_BUILD_OUTPUT_DIR := $(LIBEXEC_BUILD_OUTPUT_DIR)/installed-tests/termux-core# Default value: `build/output/usr/libexec/installed-tests/termux-core` + +export override PACKAGING_BUILD_OUTPUT_DIR := $(BUILD_OUTPUT_DIR)/packaging# Default value: `build/output/packaging` +export override DEBIAN_PACKAGING_BUILD_OUTPUT_DIR := $(PACKAGING_BUILD_OUTPUT_DIR)/debian# Default value: `build/output/packaging/debian` + + + +export override BUILD_INSTALL_DIR := $(BUILD_DIR)/install# Default value: `build/install` +export override PREFIX_BUILD_INSTALL_DIR := $(BUILD_INSTALL_DIR)/usr# Default value: `build/install/usr` + +ifeq ($(TERMUX_CORE_PKG__INSTALL_PREFIX),) + ifeq ($(DESTDIR)$(PREFIX),) + override TERMUX_CORE_PKG__INSTALL_PREFIX := $(TERMUX__PREFIX) + else + override TERMUX_CORE_PKG__INSTALL_PREFIX := $(DESTDIR)$(PREFIX) + endif +endif +export TERMUX_CORE_PKG__INSTALL_PREFIX + + + +# Escape `&`, `\` and `/` characters to be used as replacement string +# in sed `s/regexp/replacement/` expression. +# The `\` character needs to be escaped twice since used in command +# string with double quoted arguments, which are then passed to `sh` +# as a single quoted argument. +# - https://stackoverflow.com/a/29613573/14686958 +export override TERMUX_PKGS__REPO_URL__ESCAPED := $(shell printf '%s' "$(TERMUX_PKGS__REPO_URL)" | sed -e '$$!{:a;N;$$!ba;}; s/[\]/\\\\&/g; s/[&/]/\\&/g; s/\n/\\&/g') +export override TERMUX_CORE_PKG__REPO_URL__ESCAPED := $(shell printf '%s' "$(TERMUX_CORE_PKG__REPO_URL)" | sed -e '$$!{:a;N;$$!ba;}; s/[\]/\\\\&/g; s/[&/]/\\&/g; s/\n/\\&/g') + + +export override TERMUX__CONSTANTS__MACRO_FLAGS := \ + -DTERMUX_CORE_PKG__VERSION=\"$(TERMUX_CORE_PKG__VERSION)\" \ + -DTERMUX__NAME=\"$(TERMUX__NAME)\" \ + -DTERMUX__LNAME=\"$(TERMUX__LNAME)\" \ + -DTERMUX_APP__DATA_DIR=\"$(TERMUX_APP__DATA_DIR)\" \ + -DTERMUX__ROOTFS=\"$(TERMUX__ROOTFS)\" \ + -DTERMUX__PREFIX=\"$(TERMUX__PREFIX)\" \ + -DTERMUX__PREFIX__BIN_DIR=\"$(TERMUX__PREFIX__BIN_DIR)\" \ + -DTERMUX__PREFIX__TMP_DIR=\"$(TERMUX__PREFIX__TMP_DIR)\" \ + -DTERMUX_ENV__S_TERMUX=\"$(TERMUX_ENV__S_TERMUX)\" \ + -DTERMUX_ENV__S_TERMUX_APP=\"$(TERMUX_ENV__S_TERMUX_APP)\" \ + -DTERMUX_ENV__S_TERMUX_ROOTFS=\"$(TERMUX_ENV__S_TERMUX_ROOTFS)\" \ + -DTERMUX_ENV__S_TERMUX_CORE__TESTS=\"$(TERMUX_ENV__S_TERMUX_CORE__TESTS)\" + +export override TERMUX__CONSTANTS__SED_ARGS := \ + -e "s%[@]TERMUX_CORE_PKG__VERSION[@]%$(TERMUX_CORE_PKG__VERSION)%g" \ + -e "s%[@]TERMUX_CORE_PKG__ARCH[@]%$(TERMUX_CORE_PKG__ARCH)%g" \ + -e "s%[@]TERMUX__LNAME[@]%$(TERMUX__LNAME)%g" \ + -e "s%[@]TERMUX_APP__NAME[@]%$(TERMUX_APP__NAME)%g" \ + -e "s%[@]TERMUX_APP__PACKAGE_NAME[@]%$(TERMUX_APP__PACKAGE_NAME)%g" \ + -e "s%[@]TERMUX_APP__DATA_DIR[@]%$(TERMUX_APP__DATA_DIR)%g" \ + -e "s%[@]TERMUX__ROOTFS[@]%$(TERMUX__ROOTFS)%g" \ + -e "s%[@]TERMUX__HOME[@]%$(TERMUX__HOME)%g" \ + -e "s%[@]TERMUX__PREFIX[@]%$(TERMUX__PREFIX)%g" \ + -e "s%[@]TERMUX_ENV__S_ROOT[@]%$(TERMUX_ENV__S_ROOT)%g" \ + -e "s%[@]TERMUX_ENV__S_TERMUX[@]%$(TERMUX_ENV__S_TERMUX)%g" \ + -e "s%[@]TERMUX_ENV__S_TERMUX_APP[@]%$(TERMUX_ENV__S_TERMUX_APP)%g" \ + -e "s%[@]TERMUX_ENV__S_TERMUX_API_APP[@]%$(TERMUX_ENV__S_TERMUX_API_APP)%g" \ + -e "s%[@]TERMUX_ENV__S_TERMUX_CORE[@]%$(TERMUX_ENV__S_TERMUX_CORE)%g" \ + -e "s%[@]TERMUX_ENV__S_TERMUX_CORE__TESTS[@]%$(TERMUX_ENV__S_TERMUX_CORE__TESTS)%g" \ + -e "s%[@]TERMUX_ENV__S_TERMUX_EXEC__TESTS[@]%$(TERMUX_ENV__S_TERMUX_EXEC__TESTS)%g" \ + -e "s%[@]TERMUX_APP__NAMESPACE[@]%$(TERMUX_APP__NAMESPACE)%g" \ + -e "s%[@]TERMUX_APP__SHELL_ACTIVITY__COMPONENT_NAME[@]%$(TERMUX_APP__SHELL_ACTIVITY__COMPONENT_NAME)%g" \ + -e "s%[@]TERMUX_APP__SHELL_SERVICE__COMPONENT_NAME[@]%$(TERMUX_APP__SHELL_SERVICE__COMPONENT_NAME)%g" \ + -e "s%[@]TERMUX_PKGS__REPO_URL[@]%$(TERMUX_PKGS__REPO_URL__ESCAPED)%g" \ + -e "s%[@]TERMUX_CORE_PKG__REPO_URL[@]%$(TERMUX_CORE_PKG__REPO_URL__ESCAPED)%g" + +define replace-termux-constants + sed $(TERMUX__CONSTANTS__SED_ARGS) "$1.in" > "$2/$$(basename "$1")" +endef + + + +export override CFLAGS_DEFAULT := +export override CPPFLAGS_DEFAULT := +export override LDFLAGS_DEFAULT := + +override CPPFLAGS_DEFAULT += -isystem$(TERMUX__PREFIX__INCLUDE_DIR) +override LDFLAGS_DEFAULT += -L$(TERMUX__PREFIX__LIB_DIR) + +ifeq ($(TERMUX_CORE_PKG__ARCH),arm) + # "We recommend using the -mthumb compiler flag to force the generation of 16-bit Thumb-2 instructions". + # - https://developer.android.com/ndk/guides/standalone_toolchain.html#abi_compatibility + override CFLAGS_DEFAULT += -march=armv7-a -mfpu=neon -mfloat-abi=softfp -mthumb + override LDFLAGS_DEFAULT += -march=armv7-a +else ifeq ($(TERMUX_CORE_PKG__ARCH),i686) + # From $NDK/docs/CPU-ARCH-ABIS.html: + override CFLAGS_DEFAULT += -march=i686 -msse3 -mstackrealign -mfpmath=sse + # i686 seem to explicitly require '-fPIC'. + # - https://github.com/termux/termux-packages/issues/7215#issuecomment-906154438 + override CFLAGS_DEFAULT += -fPIC +endif + +# - https://github.com/termux/termux-packages/commit/b997c4ea +ifeq ($(IS_ON_DEVICE_BUILD), 0) + override LDFLAGS_DEFAULT += -Wl,-rpath=$(TERMUX__PREFIX__LIB_DIR) +endif + +# Android 7 started to support DT_RUNPATH (but not DT_RPATH). +override LDFLAGS_DEFAULT += -Wl,--enable-new-dtags + +# Avoid linking extra (unneeded) libraries. +override LDFLAGS_DEFAULT += -Wl,--as-needed + +# Basic hardening. +override LDFLAGS_DEFAULT += -Wl,-z,relro,-z,now + + +# Set default flags if building with make directly without termux-pacakges build infrastructure. +CFLAGS ?= $(CFLAGS_DEFAULT) +CXXFLAGS ?= $(CFLAGS_DEFAULT) +CPPFLAGS ?= $(CPPFLAGS_DEFAULT) +LDFLAGS ?= $(LDFLAGS_DEFAULT) + +# Force optimize for speed and do basic hardening. +export override CFLAGS_FORCE := -Wall -Wextra -Werror -Wshadow -O2 -D_FORTIFY_SOURCE=2 -fstack-protector-strong + +CFLAGS += $(CFLAGS_FORCE) +CXXFLAGS += $(CFLAGS_FORCE) + +FSANTIZE_FLAGS += -fsanitize=address -fsanitize-recover=address -fno-omit-frame-pointer + + + +override LIBTERMUX_CORE__NOS__C__SOURCE_FILES := \ + lib/termux-core_nos_c_tre/src/android/shell/command/environment/AndroidShellEnvironment.c \ + lib/termux-core_nos_c_tre/src/TermuxCoreLibraryConfig.c \ + lib/termux-core_nos_c_tre/src/data/AssertUtils.c \ + lib/termux-core_nos_c_tre/src/data/DataUtils.c \ + lib/termux-core_nos_c_tre/src/logger/FileLoggerImpl.c \ + lib/termux-core_nos_c_tre/src/logger/Logger.c \ + lib/termux-core_nos_c_tre/src/logger/StandardLoggerImpl.c \ + lib/termux-core_nos_c_tre/src/security/SecurityUtils.c \ + lib/termux-core_nos_c_tre/src/termux/file/TermuxFile.c \ + lib/termux-core_nos_c_tre/src/termux/shell/command/environment/TermuxShellEnvironment.c \ + lib/termux-core_nos_c_tre/src/termux/shell/command/environment/termux_core/TermuxCoreShellEnvironment.c \ + lib/termux-core_nos_c_tre/src/unix/file/UnixFileUtils.c \ + lib/termux-core_nos_c_tre/src/unix/os/process/UnixForkUtils.c \ + lib/termux-core_nos_c_tre/src/unix/os/process/UnixSafeStrerror.c \ + lib/termux-core_nos_c_tre/src/unix/os/process/UnixSignalUtils.c \ + lib/termux-core_nos_c_tre/src/unix/os/selinux/UnixSeLinuxUtils.c \ + lib/termux-core_nos_c_tre/src/unix/shell/command/environment/UnixShellEnvironment.c + +override LIBTERMUX_CORE__NOS__C__OBJECT_FILES := $(patsubst lib/%.c,$(TMP_BUILD_OUTPUT_DIR)/lib/%.o,$(LIBTERMUX_CORE__NOS__C__SOURCE_FILES)) + +override LIBTERMUX_CORE__NOS__C__CPPFLAGS := $(CPPFLAGS) -I "lib/termux-core_nos_c_tre/include" + +override LIBTERMUX_CORE__NOS__C__TESTS_BUILD_OUTPUT_DIR := $(TESTS_BUILD_OUTPUT_DIR)/lib/termux-core_nos_c_tre + + + +override LIBTERMUX_CORE__NOS__CXX__SOURCE_FILES := \ + lib/termux-core_nos_cxx_tre/src/TermuxCoreLibraryConfig.cxx + +override LIBTERMUX_CORE__NOS__CXX__OBJECT_FILES := $(patsubst lib/%.cxx,$(TMP_BUILD_OUTPUT_DIR)/lib/%.o,$(LIBTERMUX_CORE__NOS__CXX__SOURCE_FILES)) + +override LIBTERMUX_CORE__NOS__CXX__CPPFLAGS := $(CPPFLAGS) -I "lib/termux-core_nos_c_tre/include" -I "lib/termux-core_nos_cxx_tre/include" + + + +override TERMUX_CORE__MAIN_APP__TESTS_BUILD_OUTPUT_DIR := $(TESTS_BUILD_OUTPUT_DIR)/app/main + + + +# The `-L` flag must come before `$LDFLAGS`, otherwise old library +# installed in system library directory from previous builds +# will get used instead of newly built one in `$LIB_BUILD_OUTPUT_DIR`. +# The `-fvisibility=hidden` flag is passed so that no internal +# functions are exported. All exported functions must explicitly enable +# `default` visibility with `__attribute__((visibility("default")))`, +# like for the `main()` function. +# The `-Wl,--exclude-libs=ALL` flag is passed so that symbols from +# the `libtermux-core_nos_c_tre.a` static library linked are not exported. +# Run `nm --demangle --defined-only --extern-only ` to +# find exported symbols. +override TERMUX_CORE_EXECUTABLE__C__BUILD_COMMAND := \ + $(CC) $(CFLAGS) $(LIBTERMUX_CORE__NOS__C__CPPFLAGS) \ + -L$(LIB_BUILD_OUTPUT_DIR) $(LDFLAGS) -Wl,--exclude-libs=ALL \ + $(TERMUX__CONSTANTS__MACRO_FLAGS) \ + -fPIE -pie -fvisibility=hidden + +# The `-l` flags must be passed after object files for proper linking. +# The order of libraries matters too and any dependencies of a library +# must come after it. +override TERMUX_CORE_EXECUTABLE__C__POST_LDFLAGS := -l:libtermux-core_nos_c_tre.a + + + +# - https://www.gnu.org/software/make/manual/html_node/Parallel-Disable.html +.NOTPARALLEL: + +all: | pre-build build-termux-core-main-app build-libtermux-core_nos_c_tre build-libtermux-core_nos_cxx_tre + @printf "\ntermux-core-package: %s\n" "Building packaging/debian/*" + @mkdir -p $(DEBIAN_PACKAGING_BUILD_OUTPUT_DIR) + find packaging/debian -mindepth 1 -maxdepth 1 -type f -name "*.in" -exec sh -c \ + 'sed $(TERMUX__CONSTANTS__SED_ARGS) "$$1" > $(DEBIAN_PACKAGING_BUILD_OUTPUT_DIR)/"$$(basename "$$1" | sed "s/\.in$$//")"' sh "{}" \; + find $(DEBIAN_PACKAGING_BUILD_OUTPUT_DIR) -mindepth 1 -maxdepth 1 -type f \ + -regextype posix-extended -regex "^.*/(postinst|postrm|preinst|prerm)$$" \ + -exec chmod 700 {} \; + find $(DEBIAN_PACKAGING_BUILD_OUTPUT_DIR) -mindepth 1 -maxdepth 1 -type f \ + -regextype posix-extended -regex "^.*/(config|conffiles|templates|triggers|clilibs|fortran_mod|runit|shlibs|starlibs|symbols)$$" \ + -exec chmod 600 {} \; + + + @printf "\ntermux-core-package: %s\n\n" "Build termux-core-package successful" + +pre-build: | clean + @printf "termux-core-package: %s\n" "Building termux-core-package" + @mkdir -p $(BUILD_OUTPUT_DIR) + @mkdir -p $(TMP_BUILD_OUTPUT_DIR) + +build-termux-core-main-app: + @printf "\ntermux-core-package: %s\n" "Building app/main" + + @printf "\ntermux-core-package: %s\n" "Building app/main/scripts/*" + @mkdir -p $(BIN_BUILD_OUTPUT_DIR) + find app/main/scripts -type f -name "*.in" -exec sh -c \ + 'sed $(TERMUX__CONSTANTS__SED_ARGS) "$$1" > $(BIN_BUILD_OUTPUT_DIR)/"$$(basename "$$1" | sed "s/\.in$$//")"' sh "{}" \; + find $(BIN_BUILD_OUTPUT_DIR) -maxdepth 1 -type f -exec chmod 700 "{}" \; + find app/main/scripts -type l -exec cp -a "{}" $(BIN_BUILD_OUTPUT_DIR)/ \; + "$(TERMUX_CORE__TERMUX_REPLACE_TERMUX_CORE_SRC_SCRIPTS_FILE)" $(BIN_BUILD_OUTPUT_DIR)/* + + + @printf "\ntermux-core-package: %s\n" "Building app/main/tests/*" + @mkdir -p $(TERMUX_CORE__MAIN_APP__TESTS_BUILD_OUTPUT_DIR) + find app/main/tests -maxdepth 1 -type f -name "*.in" -print0 | xargs -0 -n1 sh -c \ + 'output_file="$(TERMUX_CORE__MAIN_APP__TESTS_BUILD_OUTPUT_DIR)/$$(printf "%s" "$$0" | sed -e "s|^app/main/tests/||" -e "s/\.in$$//")" && mkdir -p "$$(dirname "$$output_file")" && sed $(TERMUX__CONSTANTS__SED_ARGS) "$$0" > "$$output_file"' + find $(TERMUX_CORE__MAIN_APP__TESTS_BUILD_OUTPUT_DIR) -maxdepth 1 -type f -exec chmod 700 "{}" \; + + @printf "\ntermux-core-package: %s\n" "Building app/main/tests/scripts/*" + @mkdir -p $(TERMUX_CORE__MAIN_APP__TESTS_BUILD_OUTPUT_DIR)/scripts + find app/main/tests/scripts -type f -name "*.in" -print0 | xargs -0 -n1 sh -c \ + 'output_file="$(TERMUX_CORE__MAIN_APP__TESTS_BUILD_OUTPUT_DIR)/scripts/$$(printf "%s" "$$0" | sed -e "s|^app/main/tests/scripts/||" -e "s/\.in$$//")" && mkdir -p "$$(dirname "$$output_file")" && sed $(TERMUX__CONSTANTS__SED_ARGS) "$$0" > "$$output_file"' + find $(TERMUX_CORE__MAIN_APP__TESTS_BUILD_OUTPUT_DIR)/scripts -type f -exec chmod 700 "{}" \; + +build-libtermux-core_nos_c_tre: + @printf "\ntermux-core-package: %s\n" "Building lib/termux-core_nos_c_tre" + @mkdir -p $(LIB_BUILD_OUTPUT_DIR) + + @printf "\ntermux-core-package: %s\n" "Building lib/termux-core_nos_c_tre/lib/*.o" + for source_file in $(LIBTERMUX_CORE__NOS__C__SOURCE_FILES); do \ + mkdir -p "$$(dirname "$(TMP_BUILD_OUTPUT_DIR)/$$source_file")" || exit $$?; \ + $(CC) -c $(CFLAGS) $(LIBTERMUX_CORE__NOS__C__CPPFLAGS) \ + $(TERMUX__CONSTANTS__MACRO_FLAGS) \ + -fPIC -fvisibility=default \ + -o $(TMP_BUILD_OUTPUT_DIR)/"$$(echo "$$source_file" | sed -E "s/(.*)\.c$$/\1.o/")" \ + "$$source_file" || exit $$?; \ + done + + @# `nm --demangle --dynamic --defined-only --extern-only /home/builder/.termux-build/termux-core/src/build/output/usr/lib/libtermux-core_nos_c_tre.so` + @printf "\ntermux-core-package: %s\n" "Building lib/libtermux-core_nos_c_tre.so" + $(CC) $(CFLAGS) $(LIBTERMUX_CORE__NOS__C__CPPFLAGS) \ + $(LDFLAGS) \ + $(TERMUX__CONSTANTS__MACRO_FLAGS) \ + -fPIC -shared -fvisibility=default \ + -o $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_nos_c_tre.so \ + $(LIBTERMUX_CORE__NOS__C__OBJECT_FILES) + + @printf "\ntermux-core-package: %s\n" "Building lib/libtermux-core_nos_c_tre.a" + $(AR) rcs $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_nos_c_tre.a $(LIBTERMUX_CORE__NOS__C__OBJECT_FILES) + + + + @printf "\ntermux-core-package: %s\n" "Building lib/termux-core_nos_c_tre/tests/*" + @mkdir -p $(LIBTERMUX_CORE__NOS__C__TESTS_BUILD_OUTPUT_DIR) + + + @printf "\ntermux-core-package: %s\n" "Building lib/termux-core_nos_c_tre/tests/libtermux-core_nos_c_tre_tests" + $(call replace-termux-constants,lib/termux-core_nos_c_tre/tests/libtermux-core_nos_c_tre_tests,$(LIBTERMUX_CORE__NOS__C__TESTS_BUILD_OUTPUT_DIR)) + chmod 700 $(LIBTERMUX_CORE__NOS__C__TESTS_BUILD_OUTPUT_DIR)/libtermux-core_nos_c_tre_tests + + + @printf "\ntermux-core-package: %s\n" "Building lib/termux-core_nos_c_tre/tests/bin/libtermux-core_nos_c_tre_unit-binary-tests" + @mkdir -p $(LIBTERMUX_CORE__NOS__C__TESTS_BUILD_OUTPUT_DIR)/bin + + @# `nm --demangle --defined-only --extern-only /home/builder/.termux-build/termux-core/src/build/output/usr/libexec/installed-tests/termux-core/lib/termux-core_nos_c_tre/bin/libtermux-core_nos_c_tre_unit-binary-tests-fsanitize` + $(TERMUX_CORE_EXECUTABLE__C__BUILD_COMMAND) -O0 -g \ + $(FSANTIZE_FLAGS) \ + -o $(LIBTERMUX_CORE__NOS__C__TESTS_BUILD_OUTPUT_DIR)/bin/libtermux-core_nos_c_tre_unit-binary-tests-fsanitize \ + lib/termux-core_nos_c_tre/tests/src/libtermux-core_nos_c_tre_unit-binary-tests.c \ + $(TERMUX_CORE_EXECUTABLE__C__POST_LDFLAGS) + + @# `nm --demangle --defined-only --extern-only /home/builder/.termux-build/termux-core/src/build/output/usr/libexec/installed-tests/termux-core/lib/termux-core_nos_c_tre/bin/libtermux-core_nos_c_tre_unit-binary-tests-nofsanitize` + $(TERMUX_CORE_EXECUTABLE__C__BUILD_COMMAND) -O0 -g \ + -o $(LIBTERMUX_CORE__NOS__C__TESTS_BUILD_OUTPUT_DIR)/bin/libtermux-core_nos_c_tre_unit-binary-tests-nofsanitize \ + lib/termux-core_nos_c_tre/tests/src/libtermux-core_nos_c_tre_unit-binary-tests.c \ + $(TERMUX_CORE_EXECUTABLE__C__POST_LDFLAGS) + +build-libtermux-core_nos_cxx_tre: + @printf "\ntermux-core-package: %s\n" "Building lib/termux-core_nos_cxx_tre" + @mkdir -p $(LIB_BUILD_OUTPUT_DIR) + + @printf "\ntermux-core-package: %s\n" "Building lib/termux-core_nos_cxx_tre/lib/*.o" + for source_file in $(LIBTERMUX_CORE__NOS__CXX__SOURCE_FILES); do \ + mkdir -p "$$(dirname "$(TMP_BUILD_OUTPUT_DIR)/$$source_file")" || exit $$?; \ + $(CXX) -c $(CXXFLAGS) $(LIBTERMUX_CORE__NOS__CXX__CPPFLAGS) \ + $(TERMUX__CONSTANTS__MACRO_FLAGS) \ + -fPIC -fvisibility=default \ + -o $(TMP_BUILD_OUTPUT_DIR)/"$$(echo "$$source_file" | sed -E "s/(.*)\.cxx$$/\1.o/")" \ + "$$source_file" || exit $$?; \ + done + + @# `nm --demangle --dynamic --defined-only --extern-only /home/builder/.termux-build/termux-core/src/build/output/usr/lib/libtermux-core_nos_cxx_tre.so` + @printf "\ntermux-core-package: %s\n" "Building lib/libtermux-core_nos_cxx_tre.so" + $(CXX) $(CXXFLAGS) $(LIBTERMUX_CORE__NOS__CXX__CPPFLAGS) \ + -L$(LIB_BUILD_OUTPUT_DIR) $(LDFLAGS) -Wl,--exclude-libs=ALL \ + $(TERMUX__CONSTANTS__MACRO_FLAGS) \ + -fPIC -shared -fvisibility=default \ + -o $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_nos_cxx_tre.so \ + $(LIBTERMUX_CORE__NOS__CXX__OBJECT_FILES) \ + -l:libtermux-core_nos_c_tre.a + + @printf "\ntermux-core-package: %s\n" "Building lib/libtermux-core_nos_cxx_tre.a" + $(AR) rcs $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_nos_cxx_tre.a $(LIBTERMUX_CORE__NOS__CXX__OBJECT_FILES) + + + +clean: + rm -rf $(BUILD_OUTPUT_DIR) + +install: + @printf "termux-core-package: %s\n" "Installing termux-core-package in $(TERMUX_CORE_PKG__INSTALL_PREFIX)" + install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/bin + install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include + install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib + install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec + + + find $(BIN_BUILD_OUTPUT_DIR) -maxdepth 1 \( -type f -o -type l \) -exec cp -a "{}" $(TERMUX_CORE_PKG__INSTALL_PREFIX)/bin/ \; + + + rm -rf $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core + install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core/termux + + cp -a lib/termux-core_nos_c_tre/include/termux/termux_core__nos__c $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core/termux/termux_core__nos__c + install $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_nos_c_tre.so $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_nos_c_tre.so + install $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_nos_c_tre.a $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_nos_c_tre.a + + cp -a lib/termux-core_nos_cxx_tre/include/termux/termux_core__nos__cxx $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core/termux/termux_core__nos__cxx + install $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_nos_cxx_tre.so $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_nos_cxx_tre.so + install $(LIB_BUILD_OUTPUT_DIR)/libtermux-core_nos_cxx_tre.a $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_nos_cxx_tre.a + + find $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core -type d -exec chmod 700 {} \; + find $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core -type f -exec chmod 600 {} \; + + + rm -rf $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec/installed-tests/termux-core + install -d $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec/installed-tests + cp -a $(TESTS_BUILD_OUTPUT_DIR) $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec/installed-tests/termux-core + + @printf "\ntermux-core-package: %s\n\n" "Install termux-core-package successful" + +uninstall: + @printf "termux-core-package: %s\n" "Uninstalling termux-core-package from $(TERMUX_CORE_PKG__INSTALL_PREFIX)" + + find app/main/scripts \( -type f -o -type l \) -exec sh -c \ + 'rm -f "$(TERMUX_CORE_PKG__INSTALL_PREFIX)/bin/$$(basename "$$1" | sed "s/\.in$$//")"' sh "{}" \; + + rm -rf $(TERMUX_CORE_PKG__INSTALL_PREFIX)/include/termux-core + + + rm -f $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_nos_c_tre.so + rm -f $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_nos_c_tre.a + + rm -f $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_nos_cxx_tre.so + rm -f $(TERMUX_CORE_PKG__INSTALL_PREFIX)/lib/libtermux-core_nos_cxx_tre.a + + + rm -rf $(TERMUX_CORE_PKG__INSTALL_PREFIX)/libexec/installed-tests/termux-core + + @printf "\ntermux-core-package: %s\n\n" "Uninstall termux-core-package successful" + + + +packaging-debian-build: all + termux-create-package $(DEBIAN_PACKAGING_BUILD_OUTPUT_DIR)/termux-core-package.json + + + +test: all + $(MAKE) TERMUX_CORE_PKG__INSTALL_PREFIX=$(PREFIX_BUILD_INSTALL_DIR) install + + @printf "\ntermux-core-package: %s\n" "Executing termux-core-package tests" + bash $(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-core/app/main/termux-core-tests \ + --tests-path="$(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-core" \ + --only-termux-core-tests \ + -vvv all + +test-unit: all + $(MAKE) TERMUX_CORE_PKG__INSTALL_PREFIX=$(PREFIX_BUILD_INSTALL_DIR) install + + @printf "\ntermux-core-package: %s\n" "Executing termux-core-package unit tests" + bash $(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-core/app/main/termux-core-tests \ + --tests-path="$(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-core" \ + --only-termux-core-tests \ + -vvv unit + +test-runtime: all + $(MAKE) TERMUX_CORE_PKG__INSTALL_PREFIX=$(PREFIX_BUILD_INSTALL_DIR) install + + @printf "\ntermux-core-package: %s\n" "Executing termux-core-package runtime tests" + bash $(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-core/app/main/termux-core-tests \ + --tests-path="$(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-core" \ + --only-termux-core-tests \ + -vvv runtime + + + +.PHONY: all pre-build build-termux-core-main-app build-libtermux-core_nos_c_tre build-libtermux-core_nos_cxx_tre clean install uninstall packaging-debian-build test test-unit test-runtime diff --git a/README.md b/README.md index f89ee43..13a5c20 100644 --- a/README.md +++ b/README.md @@ -1 +1,23 @@ # termux-core-package + +The [`termux-core`](https://github.com/termux/termux-core-package) package provides utils and libraries for Termux core. It provides core functionality for the Termux app that must exist in all Termux execution environments including forks, and is used by other Termux packages and also 3rd party packages. + +### Contents + +- [Project](#project) + +--- + +  + + + + + +## Project + +**Check the `termux-core-package` project info [here](site/pages/en/projects/index.md), including `docs` and `releases` info.** + +--- + +  diff --git a/app/main/scripts/termux/shell/command/environment/termux-apps-info-app-version-name b/app/main/scripts/termux/shell/command/environment/termux-apps-info-app-version-name new file mode 120000 index 0000000..e0100dc --- /dev/null +++ b/app/main/scripts/termux/shell/command/environment/termux-apps-info-app-version-name @@ -0,0 +1 @@ +termux-apps-info-app-version-name.bash \ No newline at end of file diff --git a/app/main/scripts/termux/shell/command/environment/termux-apps-info-app-version-name.bash.in b/app/main/scripts/termux/shell/command/environment/termux-apps-info-app-version-name.bash.in new file mode 100755 index 0000000..5beabc5 --- /dev/null +++ b/app/main/scripts/termux/shell/command/environment/termux-apps-info-app-version-name.bash.in @@ -0,0 +1,165 @@ +#!@TERMUX__PREFIX@/bin/bash +# shellcheck shell=bash + +if [ -z "${BASH_VERSION:-}" ]; then + echo "The 'termux-apps-info-app-version-name.bash' script must be run from a 'bash' shell."; return 64 2>/dev/null|| exit 64 # EX__USAGE +fi + +## +# `termux_core__bash__termux_apps_info_app_version_name__show_help` +## +termux_core__bash__termux_apps_info_app_version_name__show_help() { + + cat <<'HELP_EOF' +termux-apps-info-app-version-name.bash can be used to get/unset +variable values of the app version name environment variables +`*_APP__APP_VERSION_NAME` for Termux app `TERMUX_APP__`, its plugin +apps `TERMUX_*_APP__` and external apps `*_APP__` app scoped that +exist in the `termux-apps-info.env` file, with support for validation +of values. + + +Usage: + termux-apps-info-app-version-name.bash + + +Available commands: + get-value Get app version name value from + Termux scoped variable. + unset-value Unset Termux scoped variable value + for app version name. + + + +get-value: + termux-apps-info-app-version-name.bash get-value [] \ + \ + + +Available command options: + [ --skip-sourcing ] + Skip sourcing of `termux-apps-info.env` file + before getting value. By default, the + '--skip-sourcing-if-cur-app-var' flag is passed + to 'termux-apps-info-env-variable.bash' instead. + [ --extended-validator= ] + The extended validator to pass to + 'termux-scoped-env-variable.bash' for validation + of app version name value. By default, the value + must start with a number. + + + +unset-value: + termux-apps-info-app-version-name.bash unset-value \ + + + + +The `get-value` command type returns the value for the Termux scoped +environment variable generated for the app version name depending on +the variable scope passed and and the sub name as `APP_VERSION_NAME`. +The value is read from the environment if app scope of the variable +to get is the same as the app scope of the current app set in the +`$TERMUX_ENV__S_APP` environment variable, otherwise +`termux-apps-info.env` file is sourced first before reading the value +unless the `--skip-sourcing` flag is passed. If the environment +variable is not set (like in case app is not installed) or is set to a +valid app version name starting with a number (default) or matches the +custom validator passed with the `--extended-validator` argument, then +call will return with exit code `0`. If its set, but not to a valid +app version name, then the call will return with exit code +`81` (`C_EX__NOT_FOUND`). + +The `unset-value` command type unsets the value of the Termux scoped +environment variable generated for the app version name by running the +`unset` command. + +The `unset-value` command type is not available if executing the +`termux-apps-info-app-version-name.bash` script as that will not have +any effect on the calling process environment and is only available if +the script is sourced and the +`termux_core__bash__termux_apps_info_app_version_name` function is +called. + +Check the help of `termux-apps-info-env-variable.sh` and +`termux-scoped-env-variable.sh` commands for info on the arguments for +the `get-value` and `unset-value` command types. + + + +**See Also:** +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/site/pages/en/projects/docs/usage/utils/termux/shell/command/environment/termux-apps-info-app-version-name.md +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/app/main/scripts/termux/shell/command/environment/termux-apps-info-app-version-name.bash.in +- @TERMUX_PKGS__REPO_URL@/blob/master/packages/termux-core/app/main/scripts/termux/shell/command/environment/termux_core__bash__termux_apps_info_app_version_name +. +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/site/pages/en/projects/docs/usage/utils/termux/shell/command/environment/termux-apps-info-env-variable.md +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable.bash.in +- @TERMUX_PKGS__REPO_URL@/blob/master/packages/termux-core/app/main/scripts/termux/shell/command/environment/termux_core__bash__termux_apps_info_env_variable +. +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/site/pages/en/projects/docs/usage/utils/termux/shell/command/environment/termux-scoped-env-variable.md +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable.bash.in +- @TERMUX_PKGS__REPO_URL@/blob/master/packages/termux-core/app/main/scripts/termux/shell/command/environment/termux_core__bash__termux_scoped_env_variable +HELP_EOF + +} + +## +# `termux_core__bash__termux_apps_info_app_version_name__main` [``] +## +termux_core__bash__termux_apps_info_app_version_name__main() { + + local return_value + + if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then + termux_core__bash__termux_apps_info_app_version_name__show_help || return $? + return 0 + elif [ "$1" = "--version" ]; then + echo "@TERMUX_CORE_PKG__VERSION@" || return $? + return 0 + elif [ "$1" = "unset-value" ]; then + echo "The '$1' command cannot be run if executing the 'termux-apps-info-app-version-name.bash' script." 1>&2 + return 80 # C_EX__UNSUPPORTED + else + termux_core__bash__termux_apps_info_app_version_name "$@" + return_value=$? + if [ $return_value -eq 64 ]; then # EX__USAGE + echo "" + termux_core__bash__termux_apps_info_app_version_name__show_help + fi + return $return_value + fi + +} + +##### @TERMUX_CORE__BASH__TERMUX_APPS_INFO_APP_VERSION_NAME@ to be replaced at build time. ##### + + + +## +# Check if script is sourced. +# - https://stackoverflow.com/a/28776166/14686958 +# - https://stackoverflow.com/a/29835459/14686958 +# +# To source the `termux-apps-info-app-version-name.bash` file in `$PATH` +# (with `.` or `source` command), run the following commands. +# The `command -v` command is used to find the location of the script +# file instead of directly using the `.`/`source` command to prevent +# sourcing of a (malicious) file in the current working directory with +# the same name instead of the one in `$PATH`. +# A separate function is used to source so that arguments passed to +# calling script/function are not passed to the sourced script. +# Replace `exit` with `return` if running inside a function. +# ```shell +# source_file_from_path() { local source_file="${1:-}"; [ $# -gt 0 ] && shift 1; local source_path; if source_path="$(command -v "$source_file")" && [ -n "$source_path" ]; then source "$source_path" || return $?; else echo "Failed to find the '$source_file' file to source." 1>&2; return 1; fi; } +# source_file_from_path "termux-apps-info-app-version-name.bash" || exit $? +# ``` +## + +# If script is sourced, return with success, otherwise call main function. +if (return 0 2>/dev/null); then + return 0 # EX__SUCCESS +else + termux_core__bash__termux_apps_info_app_version_name__main "$@" + exit $? +fi diff --git a/app/main/scripts/termux/shell/command/environment/termux-apps-info-app-version-name.sh.in b/app/main/scripts/termux/shell/command/environment/termux-apps-info-app-version-name.sh.in new file mode 100755 index 0000000..2721334 --- /dev/null +++ b/app/main/scripts/termux/shell/command/environment/termux-apps-info-app-version-name.sh.in @@ -0,0 +1,192 @@ +#!@TERMUX__PREFIX@/bin/sh +# shellcheck shell=sh +# shellcheck disable=SC3043 + +## +# `termux_core__sh__termux_apps_info_app_version_name__show_help` +## +termux_core__sh__termux_apps_info_app_version_name__show_help() { + + cat <<'HELP_EOF' +termux-apps-info-app-version-name.sh can be used to get/unset +variable values of the app version name environment variables +`*_APP__APP_VERSION_NAME` for Termux app `TERMUX_APP__`, its plugin +apps `TERMUX_*_APP__` and external apps `*_APP__` app scoped that +exist in the `termux-apps-info.env` file, with support for validation +of values. + + +Usage: + termux-apps-info-app-version-name.sh + + +Available commands: + get-value Get app version name value from + Termux scoped variable. + unset-value Unset Termux scoped variable value + for app version name. + + + +get-value: + termux-apps-info-app-version-name.sh get-value [] \ + \ + + +Available command options: + [ --skip-sourcing ] + Skip sourcing of `termux-apps-info.env` file + before getting value. By default, the + '--skip-sourcing-if-cur-app-var' flag is passed + to 'termux-apps-info-env-variable.sh' instead. + [ --posix-validator= ] + The posix validator to pass to + 'termux-scoped-env-variable.sh' for validation + of app version name value. By default, the value + must start with a number. + + + +unset-value: + termux-apps-info-app-version-name.sh unset-value \ + + + + +The `get-value` command type returns the value for the Termux scoped +environment variable generated for the app version name depending on +the variable scope passed and and the sub name as `APP_VERSION_NAME`. +The value is read from the environment if app scope of the variable +to get is the same as the app scope of the current app set in the +`$TERMUX_ENV__S_APP` environment variable, otherwise +`termux-apps-info.env` file is sourced first before reading the value +unless the `--skip-sourcing` flag is passed. If the environment +variable is not set (like in case app is not installed) or is set to a +valid app version name starting with a number (default) or matches the +custom validator passed with the `--posix-validator` argument, then +call will return with exit code `0`. If its set, but not to a valid +app version name, then the call will return with exit code +`81` (`C_EX__NOT_FOUND`). + +The `unset-value` command type unsets the value of the Termux scoped +environment variable generated for the app version name by running the +`unset` command. + +The `unset-value` command type is not available if executing the +`termux-apps-info-app-version-name.sh` script as that will not have +any effect on the calling process environment and is only available if +the script is sourced and the +`termux_core__sh__termux_apps_info_app_version_name` function is +called. + +Check the help of `termux-apps-info-env-variable.sh` and +`termux-scoped-env-variable.sh` commands for info on the arguments for +the `get-value` and `unset-value` command types. + + + +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/site/pages/en/projects/docs/usage/utils/termux/shell/command/environment/termux-apps-info-app-version-name.md +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/app/main/scripts/termux/shell/command/environment/termux-apps-info-app-version-name.sh.in +- @TERMUX_PKGS__REPO_URL@/blob/master/packages/termux-core/app/main/scripts/termux/shell/command/environment/termux_core__sh__termux_apps_info_app_version_name +. +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/site/pages/en/projects/docs/usage/utils/termux/shell/command/environment/termux-apps-info-env-variable.md +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable.sh.in +- @TERMUX_PKGS__REPO_URL@/blob/master/packages/termux-core/app/main/scripts/termux/shell/command/environment/termux_core__sh__termux_apps_info_env_variable +. +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/site/pages/en/projects/docs/usage/utils/termux/shell/command/environment/termux-scoped-env-variable.md +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable.sh.in +- @TERMUX_PKGS__REPO_URL@/blob/master/packages/termux-core/app/main/scripts/termux/shell/command/environment/termux_core__sh__termux_scoped_env_variable +HELP_EOF + +} + +## +# `termux_core__sh__termux_apps_info_app_version_name__main` [``] +## +termux_core__sh__termux_apps_info_app_version_name__main() { + + local return_value + + if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then + termux_core__sh__termux_apps_info_app_version_name__show_help || return $? + return 0 + elif [ "$1" = "--version" ]; then + echo "@TERMUX_CORE_PKG__VERSION@" || return $? + return 0 + elif [ "$1" = "unset-value" ]; then + echo "The '$1' command cannot be run if executing the 'termux-apps-info-app-version-name.sh' script." 1>&2 + return 80 # C_EX__UNSUPPORTED + else + termux_core__sh__termux_apps_info_app_version_name "$@" + return_value=$? + if [ $return_value -eq 64 ]; then # EX__USAGE + echo "" + termux_core__sh__termux_apps_info_app_version_name__show_help + fi + return $return_value + fi + +} + +##### @TERMUX_CORE__SH__TERMUX_APPS_INFO_APP_VERSION_NAME@ to be replaced at build time. ##### + + + +## +# Check if script is sourced. +# - https://stackoverflow.com/a/28776166/14686958 +# - https://stackoverflow.com/a/29835459/14686958 +# +# To source the `termux-apps-info-app-version-name.sh` file in `$PATH` +# (with `.` command), run the following commands. +# The `command -v` command is used to find the location of the script +# file instead of directly using the `.`/`source` command to prevent +# sourcing of a (malicious) file in the current working directory with +# the same name instead of the one in `$PATH`. +# A separate function is used to source so that arguments passed to +# calling script/function are not passed to the sourced script. +# Passing the `--sourcing-script` argument is necessary if sourcing +# from a `sh` shell script so that script `main` function is not run +# as there is no POSIX way to detect if current script is being +# sourced from another script as `${0##*/}` expansion in the +# "all other shells" case will contain name of the script that is +# sourcing the current script instead of the shell name. +# The `--not-sourcing-script` flag can be passed in case of executing +# the script for efficiency as that will skip the logic of checking if +# script is being sourced or not. +# Replace `exit` with `return` if running inside a function. +# ```shell +# source_file_from_path() { local source_file="${1:-}"; [ $# -gt 0 ] && shift 1; local source_path; if source_path="$(command -v "$source_file")" && [ -n "$source_path" ]; then . "$source_path" || return $?; else echo "Failed to find the '$source_file' file to source." 1>&2; return 1; fi; } +# source_file_from_path "termux-apps-info-app-version-name.sh" --sourcing-script || exit $? +# ``` +## +if [ "${1:-}" = "--sourcing-script" ]; then + TERMUX_CORE__SH__TAIAVN__SOURCING_SCRIPT="true"; shift 1; +elif [ "${1:-}" = "--not-sourcing-script" ]; then + TERMUX_CORE__SH__TAIAVN__SOURCING_SCRIPT="false"; shift 1; +else + TERMUX_CORE__SH__TAIAVN__SOURCING_SCRIPT="false" + if [ -n "${ZSH_EVAL_CONTEXT:-}" ]; then + case "$ZSH_EVAL_CONTEXT" in *:file) TERMUX_CORE__SH__TAIAVN__SOURCING_SCRIPT="true";; esac + elif [ -n "${KSH_VERSION:-}" ]; then + # shellcheck disable=SC2296 + case "$KSH_VERSION" in # mksh, like on Android, will throw `${.sh.file}: bad substitution`. + *"MIRBSD KSH"*) case "${0##*/}" in sh|ksh) TERMUX_CORE__SH__TAIAVN__SOURCING_SCRIPT="true";; esac;; + *) [ "$(cd "$(dirname -- "$0")" && pwd -P)/$(basename -- "$0")" != "$(cd "$(dirname -- "${.sh.file}")" && pwd -P)/$(basename -- "${.sh.file}")" ] && TERMUX_CORE__SH__TAIAVN__SOURCING_SCRIPT="true";; + esac + elif [ -n "${BASH_VERSION:-}" ]; then + (return 0 2>/dev/null) && TERMUX_CORE__SH__TAIAVN__SOURCING_SCRIPT="true" + else + # For all other shells, examine `$0` for known shell binary filenames. + # Detects `sh` and `dash`, add additional shell filenames as needed. + case "${0##*/}" in sh|-sh|dash|-dash) TERMUX_CORE__SH__TAIAVN__SOURCING_SCRIPT="true";; esac + fi +fi + +# If script is sourced, return with success, otherwise call main function. +if [ "$TERMUX_CORE__SH__TAIAVN__SOURCING_SCRIPT" = "true" ]; then + return 0 # EX__SUCCESS +else + termux_core__sh__termux_apps_info_app_version_name__main "$@" + exit $? +fi diff --git a/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable b/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable new file mode 120000 index 0000000..6fcba08 --- /dev/null +++ b/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable @@ -0,0 +1 @@ +termux-apps-info-env-variable.bash \ No newline at end of file diff --git a/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable.bash.in b/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable.bash.in new file mode 100755 index 0000000..bb7863b --- /dev/null +++ b/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable.bash.in @@ -0,0 +1,161 @@ +#!@TERMUX__PREFIX@/bin/bash +# shellcheck shell=bash + +if [ -z "${BASH_VERSION:-}" ]; then + echo "The 'termux-apps-info-env-variable.bash' script must be run from a 'bash' shell."; return 64 2>/dev/null|| exit 64 # EX__USAGE +fi + +## +# `termux_core__bash__termux_apps_info_env_variable__show_help` +## +termux_core__bash__termux_apps_info_env_variable__show_help() { + + cat <<'HELP_EOF' +termux-apps-info-env-variable.bash can be used to source the +`termux-apps-info.env` file into the current environment or get +variable values of Termux app `TERMUX_APP__`, its plugin apps +`TERMUX_*_APP__` and external apps `*_APP__` app scoped environment +variables that exist in the `termux-apps-info.env` file, with +support for fallback values and validation of values. + + +Usage: + termux-apps-info-env-variable.bash + + +Available commands: + source-env Source the apps info environment. + get-value Get Termux scoped variable value. + + + +source-env: + termux-apps-info-env-variable.bash source-env \ + \ + + + + +get-value: + termux-apps-info-env-variable.bash get-value [] \ + \ + \ + [] + +Available command options: + [ --ensure-sourcing ] + Ensure sourcing of `termux-apps-info.env` file + before getting value. + [ --skip-sourcing ] + Skip sourcing of `termux-apps-info.env` file + before getting value. + [ --skip-sourcing-if-cur-app-var ] + Skip sourcing of `termux-apps-info.env` file + before getting value if app scope of the variable + to get is the same as the app scope of the + current app set in the `$TERMUX_ENV__S_APP` + environment variable. + + + +The `source-env` command type sources the `termux-apps-info.env` file +into the current environment. If the file does not exist, then the +call will return with exit code `69` (`EX__UNAVAILABLE`). + +The `get-value` command type returns the value for the Termux scoped +environment variable generated depending on the variable scope and sub +name passed. First its value is read from the environment after +optionally sourcing the `termux-apps-info.env` file depending on +`--*-sourcing*` flags, followed by reading the optional values +passed as `default_values`, and whichever value first matches the +`extended_validator` is returned and if no value matches, then the +call will return with exit code `81` (`C_EX__NOT_FOUND`). +If the file does not exist and `--ensure-sourcing` flag was passed, +then the call will return with exit code `69` (`EX__UNAVAILABLE`). +The `scoped_var_scope_mode` argument must be set, and for scope mode +(`s=`) and sub scope mode (`ss=`), the values must end with `_APP__`, +and for the component name mode (`cn=`), the value must end with +`-app`. + +The `source-env` command type is not available if executing the +`termux-apps-info-env-variable.bash` script as that will not have any +effect on the calling process environment and is only available if +the script is sourced and the +`termux_core__bash__termux_apps_info_env_variable` function is called. + +Check the help of `termux-scoped-env-variable.bash` command for info +on the arguments for the `get-value` command type. + + + +**See Also:** +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/site/pages/en/projects/docs/usage/utils/termux/shell/command/environment/termux-apps-info-env-variable.md +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable.bash.in +- @TERMUX_PKGS__REPO_URL@/blob/master/packages/termux-core/app/main/scripts/termux/shell/command/environment/termux_core__bash__termux_apps_info_env_variable +. +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/site/pages/en/projects/docs/usage/utils/termux/shell/command/environment/termux-scoped-env-variable.md +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable.bash.in +- @TERMUX_PKGS__REPO_URL@/blob/master/packages/termux-core/app/main/scripts/termux/shell/command/environment/termux_core__bash__termux_scoped_env_variable +HELP_EOF + +} + +## +# `termux_core__bash__termux_apps_info_env_variable__main` [``] +## +termux_core__bash__termux_apps_info_env_variable__main() { + + local return_value + + if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then + termux_core__bash__termux_apps_info_env_variable__show_help || return $? + return 0 + elif [ "$1" = "--version" ]; then + echo "@TERMUX_CORE_PKG__VERSION@" || return $? + return 0 + elif [ "$1" = "source-env" ]; then + echo "The '$1' command cannot be run if executing the 'termux-apps-info-env-variable.bash' script." 1>&2 + return 80 # C_EX__UNSUPPORTED + else + termux_core__bash__termux_apps_info_env_variable "$@" + return_value=$? + if [ $return_value -eq 64 ]; then # EX__USAGE + echo "" + termux_core__bash__termux_apps_info_env_variable__show_help + fi + return $return_value + fi + +} + +##### @TERMUX_CORE__BASH__TERMUX_APPS_INFO_ENV_VARIABLE@ to be replaced at build time. ##### + + + +## +# Check if script is sourced. +# - https://stackoverflow.com/a/28776166/14686958 +# - https://stackoverflow.com/a/29835459/14686958 +# +# To source the `termux-apps-info-env-variable.bash` file in `$PATH` +# (with `.` or `source` command), run the following commands. +# The `command -v` command is used to find the location of the script +# file instead of directly using the `.`/`source` command to prevent +# sourcing of a (malicious) file in the current working directory with +# the same name instead of the one in `$PATH`. +# A separate function is used to source so that arguments passed to +# calling script/function are not passed to the sourced script. +# Replace `exit` with `return` if running inside a function. +# ```shell +# source_file_from_path() { local source_file="${1:-}"; [ $# -gt 0 ] && shift 1; local source_path; if source_path="$(command -v "$source_file")" && [ -n "$source_path" ]; then source "$source_path" || return $?; else echo "Failed to find the '$source_file' file to source." 1>&2; return 1; fi; } +# source_file_from_path "termux-apps-info-env-variable.bash" || exit $? +# ``` +## + +# If script is sourced, return with success, otherwise call main function. +if (return 0 2>/dev/null); then + return 0 # EX__SUCCESS +else + termux_core__bash__termux_apps_info_env_variable__main "$@" + exit $? +fi diff --git a/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable.sh.in b/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable.sh.in new file mode 100755 index 0000000..4037bdd --- /dev/null +++ b/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable.sh.in @@ -0,0 +1,189 @@ +#!@TERMUX__PREFIX@/bin/sh +# shellcheck shell=sh +# shellcheck disable=SC3043 + +## +# `termux_core__sh__termux_apps_info_env_variable__show_help` +## +termux_core__sh__termux_apps_info_env_variable__show_help() { + + cat <<'HELP_EOF' +termux-apps-info-env-variable.sh can be used to source the +`termux-apps-info.env` file into the current environment or get +variable values of Termux app `TERMUX_APP__`, its plugin apps +`TERMUX_*_APP__` and external apps `*_APP__` app scoped environment +variables that exist in the `termux-apps-info.env` file, with +support for fallback values and validation of values. + + +Usage: + termux-apps-info-env-variable.sh + + +Available commands: + source-env Source the apps info environment. + get-value Get Termux scoped variable value. + + + +source-env: + termux-apps-info-env-variable.sh source-env \ + \ + + + + +get-value: + termux-apps-info-env-variable.sh get-value [] \ + \ + \ + [] + +Available command options: + [ --ensure-sourcing ] + Ensure sourcing of `termux-apps-info.env` file + before getting value. + [ --skip-sourcing ] + Skip sourcing of `termux-apps-info.env` file + before getting value. + [ --skip-sourcing-if-cur-app-var ] + Skip sourcing of `termux-apps-info.env` file + before getting value if app scope of the variable + to get is the same as the app scope of the + current app set in the `$TERMUX_ENV__S_APP` + environment variable. + + + +The `source-env` command type sources the `termux-apps-info.env` file +into the current environment. If the file does not exist, then the +call will return with exit code `69` (`EX__UNAVAILABLE`). + +The `get-value` command type returns the value for the Termux scoped +environment variable generated depending on the variable scope and sub +name passed. First its value is read from the environment after +optionally sourcing the `termux-apps-info.env` file depending on +`--*-sourcing*` flags, followed by reading the optional values +passed as `default_values`, and whichever value first matches the +`posix_validator` is returned and if no value matches, then the +call will return with exit code `81` (`C_EX__NOT_FOUND`). +If the file does not exist and `--ensure-sourcing` flag was passed, +then the call will return with exit code `69` (`EX__UNAVAILABLE`). +The `scoped_var_scope_mode` argument must be set, and for scope mode +(`s=`) and sub scope mode (`ss=`), the values must end with `_APP__`, +and for the component name mode (`cn=`), the value must end with +`-app`. + +The `source-env` command type is not available if executing the +`termux-apps-info-env-variable.sh` script as that will not have any +effect on the calling process environment and is only available if +the script is sourced and the +`termux_core__sh__termux_apps_info_env_variable` function is called. + +Check the help of `termux-scoped-env-variable.sh` command for info +on the arguments for the `get-value` command type. + + + +**See Also:** +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/site/pages/en/projects/docs/usage/utils/termux/shell/command/environment/termux-apps-info-env-variable.md +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable.sh.in +- @TERMUX_PKGS__REPO_URL@/blob/master/packages/termux-core/app/main/scripts/termux/shell/command/environment/termux_core__sh__termux_apps_info_env_variable +. +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/site/pages/en/projects/docs/usage/utils/termux/shell/command/environment/termux-scoped-env-variable.md +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable.sh.in +- @TERMUX_PKGS__REPO_URL@/blob/master/packages/termux-core/app/main/scripts/termux/shell/command/environment/termux_core__sh__termux_scoped_env_variable +HELP_EOF + +} + +## +# `termux_core__sh__termux_apps_info_env_variable__main` [``] +## +termux_core__sh__termux_apps_info_env_variable__main() { + + local return_value + + if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then + termux_core__sh__termux_apps_info_env_variable__show_help || return $? + return 0 + elif [ "$1" = "--version" ]; then + echo "@TERMUX_CORE_PKG__VERSION@" || return $? + return 0 + elif [ "$1" = "source-env" ]; then + echo "The '$1' command cannot be run if executing the 'termux-apps-info-env-variable.sh' script." 1>&2 + return 80 # C_EX__UNSUPPORTED + else + termux_core__sh__termux_apps_info_env_variable "$@" + return_value=$? + if [ $return_value -eq 64 ]; then # EX__USAGE + echo "" + termux_core__sh__termux_apps_info_env_variable__show_help + fi + return $return_value + fi + +} + +##### @TERMUX_CORE__SH__TERMUX_APPS_INFO_ENV_VARIABLE@ to be replaced at build time. ##### + + + +## +# Check if script is sourced. +# - https://stackoverflow.com/a/28776166/14686958 +# - https://stackoverflow.com/a/29835459/14686958 +# +# To source the `termux-apps-info-env-variable.sh` file in `$PATH` +# (with `.` command), run the following commands. +# The `command -v` command is used to find the location of the script +# file instead of directly using the `.`/`source` command to prevent +# sourcing of a (malicious) file in the current working directory with +# the same name instead of the one in `$PATH`. +# A separate function is used to source so that arguments passed to +# calling script/function are not passed to the sourced script. +# Passing the `--sourcing-script` argument is necessary if sourcing +# from a `sh` shell script so that script `main` function is not run +# as there is no POSIX way to detect if current script is being +# sourced from another script as `${0##*/}` expansion in the +# "all other shells" case will contain name of the script that is +# sourcing the current script instead of the shell name. +# The `--not-sourcing-script` flag can be passed in case of executing +# the script for efficiency as that will skip the logic of checking if +# script is being sourced or not. +# Replace `exit` with `return` if running inside a function. +# ```shell +# source_file_from_path() { local source_file="${1:-}"; [ $# -gt 0 ] && shift 1; local source_path; if source_path="$(command -v "$source_file")" && [ -n "$source_path" ]; then . "$source_path" || return $?; else echo "Failed to find the '$source_file' file to source." 1>&2; return 1; fi; } +# source_file_from_path "termux-apps-info-env-variable.sh" --sourcing-script || exit $? +# ``` +## +if [ "${1:-}" = "--sourcing-script" ]; then + TERMUX_CORE__SH__TAIEV__SOURCING_SCRIPT="true"; shift 1; +elif [ "${1:-}" = "--not-sourcing-script" ]; then + TERMUX_CORE__SH__TAIEV__SOURCING_SCRIPT="false"; shift 1; +else + TERMUX_CORE__SH__TAIEV__SOURCING_SCRIPT="false" + if [ -n "${ZSH_EVAL_CONTEXT:-}" ]; then + case "$ZSH_EVAL_CONTEXT" in *:file) TERMUX_CORE__SH__TAIEV__SOURCING_SCRIPT="true";; esac + elif [ -n "${KSH_VERSION:-}" ]; then + # shellcheck disable=SC2296 + case "$KSH_VERSION" in # mksh, like on Android, will throw `${.sh.file}: bad substitution`. + *"MIRBSD KSH"*) case "${0##*/}" in sh|ksh) TERMUX_CORE__SH__TAIEV__SOURCING_SCRIPT="true";; esac;; + *) [ "$(cd "$(dirname -- "$0")" && pwd -P)/$(basename -- "$0")" != "$(cd "$(dirname -- "${.sh.file}")" && pwd -P)/$(basename -- "${.sh.file}")" ] && TERMUX_CORE__SH__TAIEV__SOURCING_SCRIPT="true";; + esac + elif [ -n "${BASH_VERSION:-}" ]; then + (return 0 2>/dev/null) && TERMUX_CORE__SH__TAIEV__SOURCING_SCRIPT="true" + else + # For all other shells, examine `$0` for known shell binary filenames. + # Detects `sh` and `dash`, add additional shell filenames as needed. + case "${0##*/}" in sh|-sh|dash|-dash) TERMUX_CORE__SH__TAIEV__SOURCING_SCRIPT="true";; esac + fi +fi + +# If script is sourced, return with success, otherwise call main function. +if [ "$TERMUX_CORE__SH__TAIEV__SOURCING_SCRIPT" = "true" ]; then + return 0 # EX__SUCCESS +else + termux_core__sh__termux_apps_info_env_variable__main "$@" + exit $? +fi diff --git a/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable b/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable new file mode 120000 index 0000000..1e31042 --- /dev/null +++ b/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable @@ -0,0 +1 @@ +termux-scoped-env-variable.bash \ No newline at end of file diff --git a/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable.bash.in b/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable.bash.in new file mode 100755 index 0000000..1e13939 --- /dev/null +++ b/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable.bash.in @@ -0,0 +1,280 @@ +#!@TERMUX__PREFIX@/bin/bash +# shellcheck shell=bash + +if [ -z "${BASH_VERSION:-}" ]; then + echo "The 'termux-scoped-env-variable.bash' script must be run from a 'bash' shell."; return 64 2>/dev/null|| exit 64 # EX__USAGE +fi + +## +# `termux_core__bash__termux_scoped_env_variable__show_help` +## +termux_core__bash__termux_scoped_env_variable__show_help() { + + cat <<'HELP_EOF' +termux-scoped-env-variable.bash can be used to get/set/unset variable +names and values for `TERMUX*__` and other scoped environment +variables exported by different Termux runtime components, with +support for fallback values and validation of values. + + +Usage: + termux-scoped-env-variable.bash + + +Available commands: + get-name Get Termux scoped variable name. + get-value Get Termux scoped variable value. + set-value Set Termux scoped variable value. + unset-value Unset Termux scoped variable value. + + + +get-name: + termux-scoped-env-variable.bash get-name \ + \ + + + + +get-value: + termux-scoped-env-variable.bash get-value \ + \ + \ + [] + + + +set-value: + termux-scoped-env-variable.bash set-value \ + \ + + + + +unset-value: + termux-scoped-env-variable.bash unset-value \ + + + + +Available arguments: +- `output_mode`: The output mode for the command. + If set to a valid environment variable name (like `TERMUX__VAR`) or + a bash array variable (like `TERMUX__ARRAY[0]`), then output will be + set to the variable. + Otherwise, it must be set to `>` so that output is printed to + `stdout`, or set to `-` so that output is not printed at all and + only exit code is returned. +- `scoped_var_scope_mode`: The scope mode of the scoped environment + variable name to get/set/unset. +- `scoped_var_sub_name`: The sub name of the scoped environment + variable name to get/set/unset after the scope name. +- `extended_validator`: The extended validator for whether a + particular variable value is valid and should be returned or not. +- `default_values`: One or more fallback default values. This may + optionally be a placeholder value that gets replaced during build + time. +- `value_to_set`: The value to set to the scoped environment variable. + + + +The `get-name` command type returns the name of the Termux scoped +environment variable generated depending on the variable scope and sub +name passed. + +The `get-value` command type returns the value for the Termux scoped +environment variable generated depending on the variable scope and sub +name passed. First its value is read from the environment, followed by +reading the optional values passed as `default_values`, and whichever +value first matches the `extended_validator` is returned and if no +value matches, then the call will return with exit code +`81` (`C_EX__NOT_FOUND`). If the `scoped_var_scope_mode` argument is +not set, then the environment variable is not read and only any +default values passed are checked. + +The `set-value` command type sets the value of the Termux scoped +environment variable generated to the value passed as `value_to_set`. + +The `unset-value` command type unsets the value of the Termux scoped +environment variable generated by running the `unset` command. + +The `set-value` and `unset-value` command types are not available if +executing the `termux-scoped-env-variable.bash` script as they will +not have any effect on the calling process environment and are only +available if the script is sourced and the +`termux_core__bash__termux_scoped_env_variable` function is called. + + + +The scoped environment variable name to get/set/unset is generated +based on the `scoped_var_scope_mode` and `scoped_var_sub_name` +arguments. +The `scoped_var_scope_mode` argument must be equal to one of +the following formats: +- `s=`: The variable name is generated as per + `${scoped_var_scope_name}${scoped_var_sub_name}`. The root scope + is assumed to be part of the `scoped_var_scope_name` argument and + Termux root scope is not prefixed to it. This can be used if + `scoped_var_scope_name` was already generated with the Termux root + scope or if getting a variable for an external app under a + different root scope. +- `ss=`: The variable name is generated as per + `${root_scope_name}${scoped_var_sub_scope_name}${scoped_var_sub_name}`, + where the `root_scope_name` is the value set in the + `$TERMUX_ENV__S_ROOT` environment variable, with a fallback to the + build time placeholder`@TERMUX_ENV__S_ROOT@`, and then to the + hardcoded root scope value `TERMUX_` for the upstream Termux app. +- `cn=`: The variable name is generated as + per the component name passed from the list below, where the value + in the bracket defines the sub scope value used for variable name + generation. + - `cn=termux` (`ss="_"`) + - `cn=termux-app` (`ss="APP__"`) + - `cn=termux-api-app` (`ss="API_APP__"`) + - `cn=termux-float-app` (`ss="FLOAT_APP__"`) + - `cn=termux-gui-app` (`ss="GUI_APP__"`) + - `cn=termux-tasker-app` (`ss="TASKER_APP__"`) + - `cn=termux-widget-app` (`ss="WIDGET_APP__"`) + - `cn=termux-x11-app` (`ss="X11_APP__"`) + - `cn=termux-core` (`ss="CORE__"`) + - `cn=termux-exec` (`ss="EXEC__"`) + +Optionally, only the `scoped_var_scope_mode` argument may be +passed with the `scoped_var_sub_name` argument as an empty string. +This can be used for the `get-name` command and a +`ss=` formatted argument to get the +full scope name with the Termux root scope prefixed to the sub scope. + + + +The `extended_validator` argument must be equal to one of the +following formats. The `value` refers to the values that are checked +for a match, like defined by the Termux scoped environment variable +or the `default_values`. +- `?`: Matches `value` if it is set and not empty. +- `*`: Matches `value` if it is set, or defined but empty. +- `r+=`: Matches `value` if it matches the ``. +- `r-=`: Matches `value` if it does not match the ``. +- `c+=`: Matches `value` if `` passed the `value` as + an argument exits with exit code `0`. +- `c-=`: Matches `value` if `` passed the `value` as + an argument does not exit with exit code `0`. + +For the `r+=` and `r-=` formats, the leading `r+=` or +`r-=` characters are removed from `extended_validator` and +remaining part is matched against the `value` with the bash `=~` if +conditional. The `` argument must be a valid POSIX extended +regular expression (`REG_EXTENDED`). +For example, the argument `r+='^((/)|((/[^/]+)+))$'` would match a +`value` that is a valid absolute path (including rootfs `/`). +For more info, check: +- https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#index-_005b_005b +- https://www.man7.org/linux/man-pages/man3/regex.3.html +- https://www.man7.org/linux/man-pages/man7/regex.7.html + +For the `c+=` and `c-=` formats, the leading `c+=` +or `c-=` characters are removed from `extended_validator` and +remaining part is passed as an argument to `` and executed. +The `` string must be set to an executable in `$PATH`, +or a path to an executable, or a function name in the current +execution environment, optionally followed with arguments. +This is checked with the `command -v` command and if that fails, then +the first argument is checked to see if its an absolute or relative +path to an executable (`+x`) file. For more info, check: +- https://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html + +The `value` is passed as the last argument to ``, or the +first if `` itself does not have any argument. +Any arguments in `` itself should be quoted with single or +double quotes if they have whitespace (IFS) characters, like +`c+='value_validator "some arg"`. +The `stdout` and `stderr` streams are not redirected to +`/dev/null` when executing the command, and must be manually done +in the function if required or the quiet `-q` options be used for +commands like `grep`, otherwise the `stdout` of the command will +pollute the normal result output for the matched value if it is to be +printed to `stdout`. +For example, the arguments `c+='value_validator'` and +`c+='value_validator some_arg'` would match the `value` if the +commands `value_validator "value"` and +`value_validator some_arg "value"` respectively would exit with exit +code `0`. +Note that if `termux-scoped-env-variable.bash` is being +executed, instead of the +`termux_core__bash__termux_scoped_env_variable` local function +being called, any function in the `` argument must first be +exported with the `export -f ` command to be defined in +the executed child process, assuming if executing from a `bash` shell. +For more info, check: +- https://www.gnu.org/software/bash/manual/bash.html#Shell-Functions +- https://www.gnu.org/software/bash/manual/bash.html#index-export + + + +**See Also:** +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/site/pages/en/projects/docs/usage/utils/termux/shell/command/environment/termux-scoped-env-variable.md +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable.bash.in +- @TERMUX_PKGS__REPO_URL@/blob/master/packages/termux-core/app/main/scripts/termux/shell/command/environment/termux_core__bash__termux_scoped_env_variable +HELP_EOF + +} + +## +# `termux_core__bash__termux_scoped_env_variable__main` [``] +## +termux_core__bash__termux_scoped_env_variable__main() { + + local return_value + + if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then + termux_core__bash__termux_scoped_env_variable__show_help || return $? + return 0 + elif [ "$1" = "--version" ]; then + echo "@TERMUX_CORE_PKG__VERSION@" || return $? + return 0 + elif [ "$1" = "set-value" ] || [ "$1" = "unset-value" ]; then + echo "The '$1' command cannot be run if executing the 'termux-scoped-env-variable.bash' script." 1>&2 + return 80 # C_EX__UNSUPPORTED + else + termux_core__bash__termux_scoped_env_variable "$@" + return_value=$? + if [ $return_value -eq 64 ]; then # EX__USAGE + echo "" + termux_core__bash__termux_scoped_env_variable__show_help + fi + return $return_value + fi + +} + +##### @TERMUX_CORE__BASH__TERMUX_SCOPED_ENV_VARIABLE@ to be replaced at build time. ##### + + + +## +# Check if script is sourced. +# - https://stackoverflow.com/a/28776166/14686958 +# - https://stackoverflow.com/a/29835459/14686958 +# +# To source the `termux-scoped-env-variable.bash` file in `$PATH` +# (with `.` or `source` command), run the following commands. +# The `command -v` command is used to find the location of the script +# file instead of directly using the `.`/`source` command to prevent +# sourcing of a (malicious) file in the current working directory with +# the same name instead of the one in `$PATH`. +# A separate function is used to source so that arguments passed to +# calling script/function are not passed to the sourced script. +# Replace `exit` with `return` if running inside a function. +# ```shell +# source_file_from_path() { local source_file="${1:-}"; [ $# -gt 0 ] && shift 1; local source_path; if source_path="$(command -v "$source_file")" && [ -n "$source_path" ]; then source "$source_path" || return $?; else echo "Failed to find the '$source_file' file to source." 1>&2; return 1; fi; } +# source_file_from_path "termux-scoped-env-variable.bash" || exit $? +# ``` +## + +# If script is sourced, return with success, otherwise call main function. +if (return 0 2>/dev/null); then + return 0 # EX__SUCCESS +else + termux_core__bash__termux_scoped_env_variable__main "$@" + exit $? +fi diff --git a/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable.sh.in b/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable.sh.in new file mode 100755 index 0000000..4721136 --- /dev/null +++ b/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable.sh.in @@ -0,0 +1,313 @@ +#!@TERMUX__PREFIX@/bin/sh +# shellcheck shell=sh +# shellcheck disable=SC3043 + +## +# `termux_core__sh__termux_scoped_env_variable__show_help` +## +termux_core__sh__termux_scoped_env_variable__show_help() { + + cat <<'HELP_EOF' +termux-scoped-env-variable.sh can be used to get/set/unset variable +names and values for `TERMUX*__` and other scoped environment +variables exported by different Termux runtime components, with +support for fallback values and validation of values. + + +Usage: + termux-scoped-env-variable.sh + + +Available commands: + get-name Get Termux scoped variable name. + get-value Get Termux scoped variable value. + set-value Set Termux scoped variable value. + unset-value Unset Termux scoped variable value. + + + +get-name: + termux-scoped-env-variable.sh get-name \ + \ + + + + +get-value: + termux-scoped-env-variable.sh get-value \ + \ + \ + [] + + + +set-value: + termux-scoped-env-variable.sh set-value \ + \ + + + + +unset-value: + termux-scoped-env-variable.sh unset-value \ + + + + +Available arguments: +- `output_mode`: The output mode for the command. + If set to a valid environment variable name (like `TERMUX__VAR`), + then output will be set to the variable. + Otherwise, it must be set to `>` so that output is printed to + `stdout`, or set to `-` so that output is not printed at all and + only exit code is returned. +- `scoped_var_scope_mode`: The scope mode of the scoped environment + variable name to get/set/unset. +- `scoped_var_sub_name`: The sub name of the scoped environment + variable name to get/set/unset after the scope name. +- `posix_validator`: The posix validator for whether a + particular variable value is valid and should be returned or not. +- `default_values`: One or more fallback default values. This may + optionally be a placeholder value that gets replaced during build + time. +- `value_to_set`: The value to set to the scoped environment variable. + + + +The `get-name` command type returns the name of the Termux scoped +environment variable generated depending on the variable scope and sub +name passed. + +The `get-value` command type returns the value for the Termux scoped +environment variable generated depending on the variable scope and sub +name passed. First its value is read from the environment, followed by +reading the optional values passed as `default_values`, and whichever +value first matches the `posix_validator` is returned and if no +value matches, then the call will return with exit code +`81` (`C_EX__NOT_FOUND`). If the `scoped_var_scope_mode` argument is +not set, then the environment variable is not read and only any +default values passed are checked. + +The `set-value` command type sets the value of the Termux scoped +environment variable generated to the value passed as `value_to_set`. + +The `unset-value` command type unsets the value of the Termux scoped +environment variable generated by running the `unset` command. + +The `set-value` and `unset-value` command types are not available if +executing the `termux-scoped-env-variable.sh` script as they will +not have any effect on the calling process environment and are only +available if the script is sourced and the +`termux_core__sh__termux_scoped_env_variable` function is called. + + + +The scoped environment variable name to get/set/unset is generated +based on the `scoped_var_scope_mode` and `scoped_var_sub_name` +arguments. +The `scoped_var_scope_mode` argument must be equal to one of +the following formats: +- `s=`: The variable name is generated as per + `${scoped_var_scope_name}${scoped_var_sub_name}`. The root scope + is assumed to be part of the `scoped_var_scope_name` argument and + Termux root scope is not prefixed to it. This can be used if + `scoped_var_scope_name` was already generated with the Termux root + scope or if getting a variable for an external app under a + different root scope. +- `ss=`: The variable name is generated as per + `${root_scope_name}${scoped_var_sub_scope_name}${scoped_var_sub_name}`, + where the `root_scope_name` is the value set in the + `$TERMUX_ENV__S_ROOT` environment variable, with a fallback to the + build time placeholder`@TERMUX_ENV__S_ROOT@`, and then to the + hardcoded root scope value `TERMUX_` for the upstream Termux app. +- `cn=`: The variable name is generated as + per the component name passed from the list below, where the value + in the bracket defines the sub scope value used for variable name + generation. + - `cn=termux` (`ss="_"`) + - `cn=termux-app` (`ss="APP__"`) + - `cn=termux-api-app` (`ss="API_APP__"`) + - `cn=termux-float-app` (`ss="FLOAT_APP__"`) + - `cn=termux-gui-app` (`ss="GUI_APP__"`) + - `cn=termux-tasker-app` (`ss="TASKER_APP__"`) + - `cn=termux-widget-app` (`ss="WIDGET_APP__"`) + - `cn=termux-x11-app` (`ss="X11_APP__"`) + - `cn=termux-core` (`ss="CORE__"`) + - `cn=termux-exec` (`ss="EXEC__"`) + +Optionally, only the `scoped_var_scope_mode` argument may be +passed with the `scoped_var_sub_name` argument as an empty string. +This can be used for the `get-name` command and a +`ss=` formatted argument to get the +full scope name with the Termux root scope prefixed to the sub scope. + + + +The `posix_validator` argument must be equal to one of the +following formats. The `value` refers to the values that are checked +for a match, like defined by the Termux scoped environment variable +or the `default_values`. +- `?`: Matches `value` if it is set and not empty. +- `*`: Matches `value` if it is set, or defined but empty. +- `p+=`: Matches `value` if it matches the ``. +- `p-=`: Matches `value` if it does not match the ``. +- `c+=`: Matches `value` if `` passed the `value` as + an argument exits with exit code `0`. +- `c-=`: Matches `value` if `` passed the `value` as + an argument does not exit with exit code `0`. + +For the `p+=pattern` and `p-=pattern` formats, the leading `p+=` or +`p-=` characters are removed from `posix_validator` +and remaining part is used as is as the pattern for a shell `case` +construct that is dynamically generated using the `eval` command to +match against the `value`. Since `eval` command is used, **DO NOT** +pass untrusted code as `posix_validator` and only a valid +`case` construct pattern must be passed. For example, the argument +`p+='/*'` would match a `value` that is a valid absolute path +(including rootfs `/`). For more info, check: +- https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04_05 +- https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13 + + +For the `c+=` and `c-=` formats, the leading `c+=` +or `c-=` characters are removed from `posix_validator` and +remaining part is passed as an argument to `` and executed. +The `` string must be set to an executable in `$PATH`, +or a path to an executable, or a function name in the current +execution environment, optionally followed with arguments. +This is checked with the `command -v` command and if that fails, then +the first argument is checked to see if its an absolute or relative +path to an executable (`+x`) file. For more info, check: +- https://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html + +The `value` is passed as the last argument to ``, or the +first if `` itself does not have any argument. +Arguments in `` should be separated with a space ` ` +character but themselves must not contain any whitespace (IFS) +characters as that will result in them splitting into +multiple arguments, since `` string is executed without +quotes like `$command`. Any quotes surrounding the arguments will be +treated literally. +The `stdout` and `stderr` streams are not redirected to +`/dev/null` when executing the command, and must be manually done +in the function if required or the quiet `-q` options be used for +commands like `grep`, otherwise the `stdout` of the command will +pollute the normal result output for the matched value if it is to be +printed to `stdout`. +For example, the arguments `c+='value_validator'` and +`c+='value_validator some_arg'` would match the `value` if the +commands `value_validator "value"` and +`value_validator some_arg "value"` respectively would exit with exit +code `0`. +Note that if `termux-scoped-env-variable.sh` is being +executed, instead of the +`termux_core__sh__termux_scoped_env_variable` local function +being called, any function in the `` argument will not be +defined in the executed child process, as POSIX shells do not +support exporting functions, like `bash` does with the +`export -f ` command. If a function needs to be passed, +the `termux_core__sh__termux_scoped_env_variable` function should +be sourced and executed in the same shell environment/process as +the validator function. +For more info, check: +- https://www.gnu.org/software/bash/manual/bash.html#Shell-Functions +- https://www.gnu.org/software/bash/manual/bash.html#index-export + + + +**See Also:** +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/site/pages/en/projects/docs/usage/utils/termux/shell/command/environment/termux-scoped-env-variable.md +- @TERMUX_CORE_PKG__REPO_URL@/blob/master/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable.sh.in +- @TERMUX_PKGS__REPO_URL@/blob/master/packages/termux-core/app/main/scripts/termux/shell/command/environment/termux_core__sh__termux_scoped_env_variable +HELP_EOF + +} + +## +# `termux_core__sh__termux_scoped_env_variable__main` [``] +## +termux_core__sh__termux_scoped_env_variable__main() { + + local return_value + + if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then + termux_core__sh__termux_scoped_env_variable__show_help || return $? + return 0 + elif [ "$1" = "--version" ]; then + echo "@TERMUX_CORE_PKG__VERSION@" || return $? + return 0 + elif [ "$1" = "set-value" ] || [ "$1" = "unset-value" ]; then + echo "The '$1' command cannot be run if executing the 'termux-scoped-env-variable.sh' script." 1>&2 + return 80 # C_EX__UNSUPPORTED + else + termux_core__sh__termux_scoped_env_variable "$@" + return_value=$? + if [ $return_value -eq 64 ]; then # EX__USAGE + echo "" + termux_core__sh__termux_scoped_env_variable__show_help + fi + return $return_value + fi + +} + +##### @TERMUX_CORE__SH__TERMUX_SCOPED_ENV_VARIABLE@ to be replaced at build time. ##### + +## +# Check if script is sourced. +# - https://stackoverflow.com/a/28776166/14686958 +# - https://stackoverflow.com/a/29835459/14686958 +# +# To source the `termux-scoped-env-variable.sh` file in `$PATH` +# (with `.` command), run the following commands. +# The `command -v` command is used to find the location of the script +# file instead of directly using the `.`/`source` command to prevent +# sourcing of a (malicious) file in the current working directory with +# the same name instead of the one in `$PATH`. +# A separate function is used to source so that arguments passed to +# calling script/function are not passed to the sourced script. +# Passing the `--sourcing-script` argument is necessary if sourcing +# from a `sh` shell script so that script `main` function is not run +# as there is no POSIX way to detect if current script is being +# sourced from another script as `${0##*/}` expansion in the +# "all other shells" case will contain name of the script that is +# sourcing the current script instead of the shell name. +# The `--not-sourcing-script` flag can be passed in case of executing +# the script for efficiency as that will skip the logic of checking if +# script is being sourced or not. +# Replace `exit` with `return` if running inside a function. +# ```shell +# source_file_from_path() { local source_file="${1:-}"; [ $# -gt 0 ] && shift 1; local source_path; if source_path="$(command -v "$source_file")" && [ -n "$source_path" ]; then . "$source_path" || return $?; else echo "Failed to find the '$source_file' file to source." 1>&2; return 1; fi; } +# source_file_from_path "termux-scoped-env-variable.sh" --sourcing-script || exit $? +# ``` +## +if [ "${1:-}" = "--sourcing-script" ]; then + TERMUX_CORE__SH__TSEV__SOURCING_SCRIPT="true"; shift 1; +elif [ "${1:-}" = "--not-sourcing-script" ]; then + TERMUX_CORE__SH__TSEV__SOURCING_SCRIPT="false"; shift 1; +else + TERMUX_CORE__SH__TSEV__SOURCING_SCRIPT="false" + if [ -n "${ZSH_EVAL_CONTEXT:-}" ]; then + case "$ZSH_EVAL_CONTEXT" in *:file) TERMUX_CORE__SH__TSEV__SOURCING_SCRIPT="true";; esac + elif [ -n "${KSH_VERSION:-}" ]; then + # shellcheck disable=SC2296 + case "$KSH_VERSION" in # mksh, like on Android, will throw `${.sh.file}: bad substitution`. + *"MIRBSD KSH"*) case "${0##*/}" in sh|ksh) TERMUX_CORE__SH__TSEV__SOURCING_SCRIPT="true";; esac;; + *) [ "$(cd "$(dirname -- "$0")" && pwd -P)/$(basename -- "$0")" != "$(cd "$(dirname -- "${.sh.file}")" && pwd -P)/$(basename -- "${.sh.file}")" ] && TERMUX_CORE__SH__TSEV__SOURCING_SCRIPT="true";; + esac + elif [ -n "${BASH_VERSION:-}" ]; then + (return 0 2>/dev/null) && TERMUX_CORE__SH__TSEV__SOURCING_SCRIPT="true" + else + # For all other shells, examine `$0` for known shell binary filenames. + # Detects `sh` and `dash`, add additional shell filenames as needed. + case "${0##*/}" in sh|-sh|dash|-dash) TERMUX_CORE__SH__TSEV__SOURCING_SCRIPT="true";; esac + fi +fi + +# If script is sourced, return with success, otherwise call main function. +if [ "$TERMUX_CORE__SH__TSEV__SOURCING_SCRIPT" = "true" ]; then + return 0 # EX__SUCCESS +else + termux_core__sh__termux_scoped_env_variable__main "$@" + exit $? +fi diff --git a/app/main/tests/scripts/termux-core-main-app_runtime-script-tests.in b/app/main/tests/scripts/termux-core-main-app_runtime-script-tests.in new file mode 100644 index 0000000..837f466 --- /dev/null +++ b/app/main/tests/scripts/termux-core-main-app_runtime-script-tests.in @@ -0,0 +1,45 @@ +#!@TERMUX__PREFIX@/bin/bash +# shellcheck shell=bash + + +## +# `termux_core_main_app__runtime_script_tests__main` +## +termux_core_main_app__runtime_script_tests__main() { + + TERMUX_CORE__TESTS__LOG_TAG="@TERMUX__LNAME@-core-main-app.rs-tests" + + termux_core__tests__log 4 "main()" + + + # Run tests. + termux_core_main_app__runtime_script_tests__run_tests || return $? + + return 0 +} + +## +# `termux_core_main_app__runtime_script_tests__run_tests` +## +termux_core_main_app__runtime_script_tests__run_tests() { + + termux_core__tests__log 2 "runTests(start)" + + TERMUX_CORE__TESTS__TESTS_COUNT="0" + TERMUX_CORE__TESTS__LOG_EMPTY_LINE_AFTER_SCRIPT_TEST="true" + + + + ( + # shellcheck source=app/main/tests/scripts/termux/shell/command/environment/termux-shell-command-environment_runtime-script-tests.in + termux_core__tests__source_file_from_path \ + "$TERMUX_CORE__TESTS__TESTS_PATH/app/main/scripts/termux/shell/command/environment/termux-shell-command-environment_runtime-script-tests" || exit $? + TermuxShellCommandEnvioronment_runTests || exit $? + ) || return $? + + + + termux_core__tests__log 2 "runTests(end): $TERMUX_CORE__TESTS__TESTS_COUNT tests completed" + + return 0 +} diff --git a/app/main/tests/scripts/termux/shell/command/environment/termux-apps-info-app-version-name_runtime-script-tests.in b/app/main/tests/scripts/termux/shell/command/environment/termux-apps-info-app-version-name_runtime-script-tests.in new file mode 100644 index 0000000..cce34fd --- /dev/null +++ b/app/main/tests/scripts/termux/shell/command/environment/termux-apps-info-app-version-name_runtime-script-tests.in @@ -0,0 +1,788 @@ +#!@TERMUX__PREFIX@/bin/bash +# shellcheck shell=bash + +## +# `termux_core__termux_apps_info_app_version_name__run_tests` +## +termux_core__termux_apps_info_app_version_name__run_tests() { + + termux_core__tests__log 3 "termux_core__termux_apps_info_app_version_name__run_tests()" + + termux_core__sh__termux_apps_info_app_version_name__run_tests || return $? + termux_core__bash__termux_apps_info_app_version_name__run_tests || return $? + + return 0 +} + + + + + +termux_core__sh__termux_apps_info_app_version_name__run_tests() { + + termux_core__tests__log 3 "termux_core__sh__termux_apps_info_app_version_name()" + + TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME="termux_core__sh__termux_apps_info_app_version_name__run_test" + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_VAR_TEST + + termux_core__set_termux_core_test_scoped_env_variables__default || return $? + termux_core__sh__termux_apps_info_app_version_name__run_tests_for_scope || return $? + + termux_core__set_termux_core_test_scoped_env_variables__foo || return $? + termux_core__sh__termux_apps_info_app_version_name__run_tests_for_scope || return $? + + return 0 + +} + +termux_core__sh__termux_apps_info_app_version_name__run_tests_for_scope() { + + TERMUX_ENV__S_ROOT="$TERMUX_CORE__TESTS__TERMUX_ENV__S_ROOT" + + + termux_core__termux_apps_info_app_version_name__test__usage "usage" \ + "APP__" "STRING" || return $? + + + + # `TERMUX_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-app_app-version-name" \ + "APP__" "termux-app" "--posix-validator=\"p+=''|termux-[0-9]*\"" || return $? + + + + # `TERMUX_API_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-api-app_app-version-name" \ + "API_APP__" "termux-api-app" "--posix-validator=\"p+=''|termux-[0-9]*\"" || return $? + + + + # `TERMUX_FLOAT_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-float-app_app-version-name" \ + "FLOAT_APP__" "termux-float-app" "--posix-validator=\"p+=''|termux-[0-9]*\"" || return $? + + + + # `TERMUX_GUI_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-gui-app_app-version-name" \ + "GUI_APP__" "termux-gui-app" "--posix-validator=\"p+=''|termux-[0-9]*\"" || return $? + + + + # `TERMUX_TASKER_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-tasker-app_app-version-name" \ + "TASKER_APP__" "termux-tasker-app" "--posix-validator=\"p+=''|termux-[0-9]*\"" || return $? + + + + # `TERMUX_WIDGET_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-widget-app_app-version-name" \ + "WIDGET_APP__" "termux-widget-app" "--posix-validator=\"p+=''|termux-[0-9]*\"" || return $? + + + + # `TERMUX_X11_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-x11-app_app-version-name" \ + "X11_APP__" "termux-x11-app" "--posix-validator=\"p+=''|termux-[0-9]*\"" || return $? + + return 0 + +} + +termux_core__sh__termux_apps_info_app_version_name__run_test() { + + termux_core__tests__validate_argument_count ge $# 7 termux_core__sh__termux_apps_info_app_version_name__run_test "$@" || return $? + + local test_name="$1" + local pre_test_commands="$2" + local termux_apps_info_env_file_export="$3" + local termux_apps_info_env_file_content="$4" + local expected_exit_code="$5" + local expected_output_regex="$6" + local termux_core__sh__taiavn_command_type="$7" + + local termux_apps_info_env_file_create_command + local termux_apps_info_env_file_export_command + local termux_apps_info_env_file_path + local termux_core__sh__taiavn_call_command + local termux_core__sh__taiavn_source_command + local termux_core__sh__taiavn_source_output_mode_arg="" + local termux_core__sh__taiavn_source_print_command="" + + if [[ "$termux_core__sh__taiavn_command_type" == "get-value" ]]; then + termux_core__tests__validate_argument_count eq $# 10 \ + "'termux_core__sh__termux_apps_info_app_version_name__run_test' for the '$termux_core__sh__taiavn_command_type' command" "$@" || return $? + + local termux_core__sh__taiavn_options="$8" + local termux_core__sh__taiavn_source_output_mode="$9" + local termux_core__sh__taiavn_args="${10}" + + if [[ "$termux_core__sh__taiavn_source_output_mode" == "_" ]]; then + termux_core__sh__taiavn_source_output_mode="output_variable" + fi + + termux_core__sh__taiavn_source_output_mode_arg=" '$termux_core__sh__taiavn_source_output_mode'" + + if [[ "$termux_core__sh__taiavn_source_output_mode" != "-" ]] && \ + [[ "$termux_core__sh__taiavn_source_output_mode" != ">" ]]; then + termux_core__sh__taiavn_source_print_command="${NL}printf '%s' \"\${$termux_core__sh__taiavn_source_output_mode:-}\"" + fi + elif [[ "$termux_core__sh__taiavn_command_type" == "unset-value" ]]; then + termux_core__tests__validate_argument_count eq $# 10 \ + "'termux_core__sh__termux_apps_info_app_version_name__run_test' for the '$termux_core__sh__taiavn_command_type' command" "$@" || return $? + + local termux_core__sh__taiavn_options="$8" + local termux_core__sh__taiavn_source_print_var="$9" + local termux_core__sh__taiavn_args="${10}" + + if [[ "$termux_core__sh__taiavn_source_print_var" != "-" ]]; then + termux_core__sh__taiavn_source_print_command="${NL}printf '%s' \"\${$termux_core__sh__taiavn_source_print_var:-}\"" + fi + else + local termux_core__sh__taiavn_args="" + fi + + + # shellcheck disable=SC2016 + termux_core__sh__taiavn_source_command='source_file_from_path() { local source_file="${1:-}"; [ $# -gt 0 ] && shift 1; local source_path; if source_path="$(command -v "$source_file")" && [ -n "$source_path" ]; then . "$source_path" || return $?; else echo "Failed to find the '\''$source_file'\'' file to source." 1>&2; return 1; fi; }'"${NL}${NL}"' +source_file_from_path "termux-apps-info-app-version-name.sh" --sourcing-script || exit $?' + + termux_apps_info_env_file_path="$TERMUX_CORE__TESTS__TMPDIR_PATH/termux-apps-info.env" + if [[ "$termux_apps_info_env_file_export" == "true" ]]; then + termux_apps_info_env_file_create_command="printf '%s\n' '${termux_apps_info_env_file_content//\'/\'\\\'\'}' > '${termux_apps_info_env_file_path//\'/\'\\\'\'}'" + get_export_scoped_variable_command termux_apps_info_env_file_export_command "$TERMUX_ENV__S_ROOT" "CORE__" "APPS_INFO_ENV_FILE" "$termux_apps_info_env_file_path" || return $? + else + termux_apps_info_env_file_create_command="printf '%s' '' > '${termux_apps_info_env_file_path//\'/\'\\\'\'}'" + get_unset_scoped_variable_command termux_apps_info_env_file_export_command "$TERMUX_ENV__S_ROOT" "CORE__" "APPS_INFO_ENV_FILE" || return $? + fi + + pre_test_commands="export TERMUX_ENV__S_ROOT='${TERMUX_ENV__S_ROOT//\'/\'\\\'\'}';${NL} +unset TERMUX_ENV__S_APP;${NL}${NL}\ +${termux_apps_info_env_file_create_command}${NL}${NL}${termux_apps_info_env_file_export_command}${NL}${NL}\ +${pre_test_commands:+"$pre_test_commands${NL}${NL}"}" + + termux_core__sh__taiavn_options="${termux_core__sh__taiavn_options:+"$termux_core__sh__taiavn_options "}" + + if [[ "$termux_core__sh__taiavn_command_type" == "get-value" ]]; then + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST:-}" != "true" ]]; then + termux_core__sh__taiavn_call_command="termux-apps-info-app-version-name.sh" + termux_core__tests__run_script_test "termux_core__sh__taiavn__${test_name}__exec-stdout" \ + "#!/bin/sh${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__sh__taiavn_call_command '$termux_core__sh__taiavn_command_type' ${termux_core__sh__taiavn_options}'>' $termux_core__sh__taiavn_args" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST:-}" != "true" ]]; then + termux_core__sh__taiavn_call_command="${termux_core__sh__taiavn_source_command}${NL}${NL}termux_core__sh__termux_apps_info_app_version_name" + termux_core__tests__run_script_test "termux_core__sh__taiavn__${test_name}__source-stdout" \ + "#!/bin/sh${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__sh__taiavn_call_command '$termux_core__sh__taiavn_command_type' ${termux_core__sh__taiavn_options}'>' $termux_core__sh__taiavn_args" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + fi + + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_VAR_TEST:-}" != "true" ]]; then + termux_core__sh__taiavn_call_command="${termux_core__sh__taiavn_source_command}${NL}${NL}termux_core__sh__termux_apps_info_app_version_name" + termux_core__tests__run_script_test "termux_core__sh__taiavn__${test_name}__source-var" \ + "#!/bin/sh${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__sh__taiavn_call_command '$termux_core__sh__taiavn_command_type' ${termux_core__sh__taiavn_options}$termux_core__sh__taiavn_source_output_mode_arg $termux_core__sh__taiavn_args || exit \$?$termux_core__sh__taiavn_source_print_command" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + + return 0 + +} + + + +termux_core__bash__termux_apps_info_app_version_name__run_tests() { + + termux_core__tests__log 3 "termux_core__bash__termux_apps_info_app_version_name()" + + TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME="termux_core__bash__termux_apps_info_app_version_name__run_test" + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_VAR_TEST + + termux_core__set_termux_core_test_scoped_env_variables__default || return $? + termux_core__bash__termux_apps_info_app_version_name__run_tests_for_scope || return $? + + termux_core__set_termux_core_test_scoped_env_variables__foo || return $? + termux_core__bash__termux_apps_info_app_version_name__run_tests_for_scope || return $? + + return 0 + +} + +termux_core__bash__termux_apps_info_app_version_name__run_tests_for_scope() { + + TERMUX_ENV__S_ROOT="$TERMUX_CORE__TESTS__TERMUX_ENV__S_ROOT" + + + termux_core__termux_apps_info_app_version_name__test__usage "usage" \ + "APP__" "STRING" || return $? + + + + # `TERMUX_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-app_app-version-name" \ + "APP__" "termux-app" "--extended-validator=\"r+=^(()|(termux-[0-9].*))$\"" || return $? + + + + # `TERMUX_API_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-api-app_app-version-name" \ + "API_APP__" "termux-api-app" "--extended-validator=\"r+=^(()|(termux-[0-9].*))$\"" || return $? + + + + # `TERMUX_FLOAT_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-float-app_app-version-name" \ + "FLOAT_APP__" "termux-float-app" "--extended-validator=\"r+=^(()|(termux-[0-9].*))$\"" || return $? + + + + # `TERMUX_GUI_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-gui-app_app-version-name" \ + "GUI_APP__" "termux-gui-app" "--extended-validator=\"r+=^(()|(termux-[0-9].*))$\"" || return $? + + + + # `TERMUX_TASKER_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-tasker-app_app-version-name" \ + "TASKER_APP__" "termux-tasker-app" "--extended-validator=\"r+=^(()|(termux-[0-9].*))$\"" || return $? + + + + # `TERMUX_WIDGET_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-widget-app_app-version-name" \ + "WIDGET_APP__" "termux-widget-app" "--extended-validator=\"r+=^(()|(termux-[0-9].*))$\"" || return $? + + + + # `TERMUX_X11_APP__APP_VERSION_NAME`. + termux_core__termux_apps_info_app_version_name__test__app_version_name "termux-x11-app_app-version-name" \ + "X11_APP__" "termux-x11-app" "--extended-validator=\"r+=^(()|(termux-[0-9].*))$\"" || return $? + + return 0 + +} + +termux_core__bash__termux_apps_info_app_version_name__run_test() { + + termux_core__tests__validate_argument_count ge $# 7 termux_core__bash__termux_apps_info_app_version_name__run_test "$@" || return $? + + local test_name="$1" + local pre_test_commands="$2" + local termux_apps_info_env_file_export="$3" + local termux_apps_info_env_file_content="$4" + local expected_exit_code="$5" + local expected_output_regex="$6" + local termux_core__bash__taiavn_command_type="$7" + + local termux_apps_info_env_file_create_command + local termux_apps_info_env_file_export_command + local termux_apps_info_env_file_path + local termux_core__bash__taiavn_call_command + local termux_core__bash__taiavn_source_command + local termux_core__bash__taiavn_source_output_mode_arg="" + local termux_core__bash__taiavn_source_print_command="" + + if [[ "$termux_core__bash__taiavn_command_type" == "get-value" ]]; then + termux_core__tests__validate_argument_count eq $# 10 \ + "'termux_core__bash__termux_apps_info_app_version_name__run_test' for the '$termux_core__bash__taiavn_command_type' command" "$@" || return $? + + local termux_core__bash__taiavn_options="$8" + local termux_core__bash__taiavn_source_output_mode="$9" + local termux_core__bash__taiavn_args="${10}" + + if [[ "$termux_core__bash__taiavn_source_output_mode" == "_" ]]; then + termux_core__bash__taiavn_source_output_mode="output_variable" + fi + + termux_core__bash__taiavn_source_output_mode_arg=" '$termux_core__bash__taiavn_source_output_mode'" + + if [[ "$termux_core__bash__taiavn_source_output_mode" != "-" ]] && \ + [[ "$termux_core__bash__taiavn_source_output_mode" != ">" ]]; then + termux_core__bash__taiavn_source_print_command="${NL}printf '%s' \"\${$termux_core__bash__taiavn_source_output_mode:-}\"" + fi + elif [[ "$termux_core__bash__taiavn_command_type" == "unset-value" ]]; then + termux_core__tests__validate_argument_count eq $# 10 \ + "'termux_core__bash__termux_apps_info_app_version_name__run_test' for the '$termux_core__bash__taiavn_command_type' command" "$@" || return $? + + local termux_core__bash__taiavn_options="$8" + local termux_core__bash__taiavn_source_print_var="$9" + local termux_core__bash__taiavn_args="${10}" + + if [[ "$termux_core__bash__taiavn_source_print_var" != "-" ]]; then + termux_core__bash__taiavn_source_print_command="${NL}printf '%s' \"\${$termux_core__bash__taiavn_source_print_var:-}\"" + fi + else + local termux_core__bash__taiavn_args="" + fi + + + # shellcheck disable=SC2016 + termux_core__bash__taiavn_source_command='source_file_from_path() { local source_file="${1:-}"; [ $# -gt 0 ] && shift 1; local source_path; if source_path="$(command -v "$source_file")" && [ -n "$source_path" ]; then source "$source_path" || return $?; else echo "Failed to find the '\''$source_file'\'' file to source." 1>&2; return 1; fi; }'"${NL}${NL}"' +source_file_from_path "termux-apps-info-app-version-name.bash" || exit $?' + + termux_apps_info_env_file_path="$TERMUX_CORE__TESTS__TMPDIR_PATH/termux-apps-info.env" + if [[ "$termux_apps_info_env_file_export" == "true" ]]; then + termux_apps_info_env_file_create_command="printf '%s\n' '${termux_apps_info_env_file_content//\'/\'\\\'\'}' > '${termux_apps_info_env_file_path//\'/\'\\\'\'}'" + get_export_scoped_variable_command termux_apps_info_env_file_export_command "$TERMUX_ENV__S_ROOT" "CORE__" "APPS_INFO_ENV_FILE" "$termux_apps_info_env_file_path" || return $? + else + termux_apps_info_env_file_create_command="printf '%s' '' > '${termux_apps_info_env_file_path//\'/\'\\\'\'}'" + get_unset_scoped_variable_command termux_apps_info_env_file_export_command "$TERMUX_ENV__S_ROOT" "CORE__" "APPS_INFO_ENV_FILE" || return $? + fi + + pre_test_commands="export TERMUX_ENV__S_ROOT='${TERMUX_ENV__S_ROOT//\'/\'\\\'\'}';${NL} +unset TERMUX_ENV__S_APP;${NL}${NL}\ +${termux_apps_info_env_file_create_command}${NL}${NL}${termux_apps_info_env_file_export_command}${NL}${NL}\ +${pre_test_commands:+"$pre_test_commands${NL}${NL}"}" + + termux_core__bash__taiavn_options="${termux_core__bash__taiavn_options:+"$termux_core__bash__taiavn_options "}" + + if [[ "$termux_core__bash__taiavn_command_type" == "get-value" ]]; then + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST:-}" != "true" ]]; then + termux_core__bash__taiavn_call_command="termux-apps-info-app-version-name.bash" + termux_core__tests__run_script_test "termux_core__bash__taiavn__${test_name}__exec-stdout" \ + "#!/bin/bash${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__bash__taiavn_call_command '$termux_core__bash__taiavn_command_type' ${termux_core__bash__taiavn_options}'>' $termux_core__bash__taiavn_args" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST:-}" != "true" ]]; then + termux_core__bash__taiavn_call_command="${termux_core__bash__taiavn_source_command}${NL}${NL}termux_core__bash__termux_apps_info_app_version_name" + termux_core__tests__run_script_test "termux_core__bash__taiavn__${test_name}__source-stdout" \ + "#!/bin/bash${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__bash__taiavn_call_command '$termux_core__bash__taiavn_command_type' ${termux_core__bash__taiavn_options}'>' $termux_core__bash__taiavn_args" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + fi + + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_VAR_TEST:-}" != "true" ]]; then + termux_core__bash__taiavn_call_command="${termux_core__bash__taiavn_source_command}${NL}${NL}termux_core__bash__termux_apps_info_app_version_name" + termux_core__tests__run_script_test "termux_core__bash__taiavn__${test_name}__source-var" \ + "#!/bin/bash${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__bash__taiavn_call_command '$termux_core__bash__taiavn_command_type' ${termux_core__bash__taiavn_options}$termux_core__bash__taiavn_source_output_mode_arg $termux_core__bash__taiavn_args || exit \$?$termux_core__bash__taiavn_source_print_command" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + + return 0 + +} + + + +termux_core__termux_apps_info_app_version_name__test__usage() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 3 termux_core__termux_apps_info_app_version_name__test__usage "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + + local env_command + local env_file + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_command_unset" \ + "" \ + "false" "" \ + 64 "^The command '' passed to \ +'termux_core__([a-z_]+)__termux_apps_info_app_version_name' is not valid\..*$" \ + "" "" - "" || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_command_invalid" \ + "" \ + "false" "" \ + 64 "^The command 'get-value1' passed to \ +'termux_core__([a-z_]+)__termux_apps_info_app_version_name' is not valid\..*$" \ + get-value1 "" - "" || return $? + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_command_args_invalid-less" \ + "" \ + "false" "" \ + 64 "^Invalid argument count 1 for the 'get-value' command\. The \ +'termux_core__([a-z_]+)__termux_apps_info_app_version_name' function expects 2 arguments\..*$" \ + get-value "" - "" || return $? + + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_none" \ + "" \ + "false" "" \ + 64 "^The output_mode '' argument passed to \ +'termux_core__([a-z_]+)__termux_apps_info_app_version_name' is not a valid environment variable name, or equal to \`>\` or \`-\`\..*$" \ + get-value "" "" 'ss="APP__"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_invalid" \ + "" \ + "false" "" \ + 64 "^The output_mode '1foo' argument passed to \ +'termux_core__([a-z_]+)__termux_apps_info_app_version_name' is not a valid environment variable name, or equal to \`>\` or \`-\`\..*$" \ + get-value "" "1foo" 'ss="APP__"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "APP_VERSION_NAME" "0.1.0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_output-mode-stdout_value-valid" \ + "$env_command" \ + "false" "" \ + 0 "^0.\1\.0$" \ + get-value "--skip-sourcing" ">" 'ss="APP__"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "APP_VERSION_NAME" "0.1.0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_output-mode-dash_value-valid" \ + "$env_command" \ + "false" "" \ + 0 "^$" \ + get-value "--skip-sourcing" "-" 'ss="APP__"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}DIFF_APP__" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "BAR_APP__" "APP_VERSION_NAME" "0.2.0" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "BAR_APP__" "APP_VERSION_NAME" "0.1.0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_var-already-set_value-set" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^0.\1\.0$" \ + get-value "" "${TERMUX_ENV__S_ROOT}BAR_APP__APP_VERSION_NAME" 'ss="BAR_APP__"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}DIFF_APP__" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "BAR_APP__" "APP_VERSION_NAME" "0.2.0" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "BAR_APP__" "APP_VERSION_NAME" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_var-already-set_value-empty" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "" "${TERMUX_ENV__S_ROOT}BAR_APP__APP_VERSION_NAME" 'ss="BAR_APP__"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}DIFF_APP__" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "BAR_APP__" "APP_VERSION_NAME" "0.2.0" || return $? + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "BAR_APP__" "APP_VERSION_NAME" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_var-already-set_value-unset" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "" "${TERMUX_ENV__S_ROOT}BAR_APP__APP_VERSION_NAME" 'ss="BAR_APP__"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope_none" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode '' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "" _ '""' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope_invalid-with-no-arg" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode 't=' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "" _ 't=' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope_invalid-with-arg" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode 't=_' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "" _ 't="_"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_component-name_invalid-with-no-arg" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode 'cn=' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "" _ 'cn=' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_component-name_invalid-with-arg" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode 'cn=foo' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "" _ 'cn="foo"' || return $? + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-s_no-arg" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode 's=' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "" _ 's=""' || return $? + + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "1" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-s_var-root-scope-invalid" \ + "$env_command" \ + "false" "" \ + 0 "^$" \ + get-value "--skip-sourcing" _ 's="BAR_APP__"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-s_var-sub-scope-invalid" \ + "$env_command" \ + "false" "" \ + 64 "^The name of the variable to unset 'BAR#_APP__APP_VERSION_NAME' generated in \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name\..*$" \ + get-value "" _ 's="BAR#_APP__"' || return $? + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_no-arg" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode 'ss=' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "" _ 'ss=""' || return $? + + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "1" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_var-root-scope-invalid" \ + "$env_command" \ + "false" "" \ + 1 "^The TERMUX_ENV__S_ROOT environment variable value '1' while running \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name\..*$" \ + get-value "" _ 'ss="APP__"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_var-sub-scope-invalid" \ + "$env_command" \ + "false" "" \ + 64 "^The name of the variable to unset 'BAR_X#_APP__APP_VERSION_NAME' generated in \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name\..*$" \ + get-value "" _ 'ss="X#_APP__"' || return $? + + return 0 + +} + + + +termux_core__termux_apps_info_app_version_name__test__app_version_name() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 4 termux_core__termux_apps_info_app_version_name__test__app_version_name "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local scoped_var_scope_mode="$3" + local termux_core__validator__taiavn_args="$4" + + local env_command + local env_file + local is_termux_app_scope + local test_type + local termux_core__taiavn__scope_args + local termux_version_env_command + local unset_termux_version_env_command="unset TERMUX_ENV__S_ROOT;" + + local -A test_types=() + test_types["scope-s"]='s="'"${TERMUX_ENV__S_ROOT}${var_sub_scope_name}"'"' + test_types["scope-ss"]='ss="'"${var_sub_scope_name}"'"' + test_types["component-name"]='cn="'"${scoped_var_scope_mode}"'"' + + for test_type in "${!test_types[@]}"; do + termux_core__taiavn__scope_args="${test_types["$test_type"]}" + + if [[ "$termux_core__taiavn__scope_args" = ss="APP__" ]] || [[ "$termux_core__taiavn__scope_args" = cn="termux-app" ]]; then + is_termux_app_scope="true" + termux_version_env_command="unset TERMUX_VERSION;" + else + is_termux_app_scope="false" + termux_version_env_command="" + fi + + # `--skip-sourcing`. + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" "0.1.0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing_${test_type}_env-unset" \ + "${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "--skip-sourcing" _ "${termux_core__taiavn__scope_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" "" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" "0.1.0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing_${test_type}_env-empty" \ + "${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "--skip-sourcing" _ "${termux_core__taiavn__scope_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" "0.2.0" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" "0.1.0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing_${test_type}_env-set" \ + "${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^0\.2\.0$" \ + get-value "--skip-sourcing" _ "${termux_core__taiavn__scope_args}" || return $? + + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" "termux-0.2.0" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" "termux-0.1.0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing_${test_type}_env-set_custom_validator" \ + "${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^termux-0\.2\.0$" \ + get-value "--skip-sourcing ${termux_core__validator__taiavn_args}" _ "${termux_core__taiavn__scope_args}" || return $? + + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" "0.1.0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_unset-value_main-variable-set" \ + "${termux_version_env_command}${NL}${env_command}" \ + "false" "" \ + 0 "^$" \ + unset-value "" "${TERMUX_ENV__S_ROOT}${var_sub_scope_name}APP_VERSION_NAME" "${termux_core__taiavn__scope_args}" || return $? + + + # `TERMUX_VERSION`. + if [[ "$is_termux_app_scope" == "true" ]]; then + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" "0.1.0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing_${test_type}_termux_version-unset_env-unset" \ + "${unset_termux_version_env_command}${NL}${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "--skip-sourcing" _ "${termux_core__taiavn__scope_args}" || return $? + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" "0.1.0" || return $? + env_command+="${NL}export TERMUX_VERSION='0.2.0';" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing_${test_type}_termux_version-set_env-set" \ + "${unset_termux_version_env_command}${NL}${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^0\.2\.0$" \ + get-value "--skip-sourcing" _ "${termux_core__taiavn__scope_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "APP_VERSION_NAME" "0.1.0" || return $? + env_command+="${NL}export TERMUX_VERSION='0.2.0';" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_unset-value_termux_version-variable-set" \ + "${termux_version_env_command}${NL}${env_command}" \ + "false" "" \ + 0 "^$" \ + unset-value "" "TERMUX_VERSION" "${termux_core__taiavn__scope_args}" || return $? + fi + + + + # `--skip-sourcing-if-cur-app-var`. + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}${var_sub_scope_name}" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "0.2.0" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "0.1.0" || return $? + [[ "$is_termux_app_scope" == "true" ]] && env_command+="${NL}export TERMUX_VERSION='0.3.0';" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing-if-cur-app-var_${test_type}_env-set_file-set_same-app" \ + "${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^0\.2\.0$" \ + get-value "" _ "${termux_core__taiavn__scope_args}" || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}DIFF_APP__" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "0.2.0" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "0.1.0" || return $? + [[ "$is_termux_app_scope" == "true" ]] && env_command+="${NL}export TERMUX_VERSION='0.3.0';" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing-if-cur-app-var_${test_type}_env-set_file-set_different-app" \ + "${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^0\.1\.0$" \ + get-value "" _ "${termux_core__taiavn__scope_args}" || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}DIFF_APP__" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "0.2.0" || return $? + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing-if-cur-app-var_${test_type}_env-set_file-unset_different-app" \ + "${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "" _ "${termux_core__taiavn__scope_args}" || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}${var_sub_scope_name}" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "b" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "a" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing-if-cur-app-var_${test_type}_env-invalid_file-invalid_same-app" \ + "${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiavn__scope_args}" || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}DIFF_APP__" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "b" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "a" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing-if-cur-app-var_${test_type}_env-invalid_file-invalid_different-app" \ + "${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiavn__scope_args}" || return $? + + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}DIFF_APP__" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "termux-0.2.0" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "termux-0.1.0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing-if-cur-app-var_${test_type}_env-set_file-set_different-app_custom_validator" \ + "${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^termux-0\.1\.0$" \ + get-value "${termux_core__validator__taiavn_args}" _ "${termux_core__taiavn__scope_args}" || return $? + + + # `TERMUX_VERSION`. + if [[ "$is_termux_app_scope" == "true" ]]; then + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}${var_sub_scope_name}" || return $? + append_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "0.2.0" || return $? + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" || return $? + env_command+="${NL}export TERMUX_VERSION='0.1.0';" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing-if-cur-app-var_${test_type}_termux_version-set_env-unset_file-unset_same-app" \ + "${unset_termux_version_env_command}${NL}${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^0\.1\.0$" \ + get-value "" _ "${termux_core__taiavn__scope_args}" || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}${var_sub_scope_name}" || return $? + append_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "0.2.0" || return $? + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing-if-cur-app-var_${test_type}_termux_version-unset_env-unset_file-unset_same-app" \ + "${unset_termux_version_env_command}${NL}${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "" _ "${termux_core__taiavn__scope_args}" || return $? + + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}DIFF_APP__" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "0.2.0" || return $? + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" || return $? + env_command+="${NL}export TERMUX_VERSION='0.1.0';" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing-if-cur-app-var_${test_type}_termux_version-set_env-set_file-unset_different-app" \ + "${unset_termux_version_env_command}${NL}${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^0\.1\.0$" \ + get-value "" _ "${termux_core__taiavn__scope_args}" || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}DIFF_APP__" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" "0.2.0" || return $? + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "${var_sub_scope_name}" "APP_VERSION_NAME" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_skip-sourcing-if-cur-app-var_${test_type}_termux_version-unset_env-set_file-unset_different-app" \ + "${unset_termux_version_env_command}${NL}${termux_version_env_command}${NL}${env_command}" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "" _ "${termux_core__taiavn__scope_args}" || return $? + fi + done + + return 0 + +} diff --git a/app/main/tests/scripts/termux/shell/command/environment/termux-apps-info-env-variable_runtime-script-tests.in b/app/main/tests/scripts/termux/shell/command/environment/termux-apps-info-env-variable_runtime-script-tests.in new file mode 100644 index 0000000..1464427 --- /dev/null +++ b/app/main/tests/scripts/termux/shell/command/environment/termux-apps-info-env-variable_runtime-script-tests.in @@ -0,0 +1,1185 @@ +#!@TERMUX__PREFIX@/bin/bash +# shellcheck shell=bash + +## +# `termux_core__termux_apps_info_env_variable__run_tests` +## +termux_core__termux_apps_info_env_variable__run_tests() { + + termux_core__tests__log 3 "termux_core__termux_apps_info_env_variable__run_tests()" + + termux_core__sh__termux_apps_info_env_variable__run_tests || return $? + termux_core__bash__termux_apps_info_env_variable__run_tests || return $? + + return 0 +} + + + + + +termux_core__sh__termux_apps_info_env_variable__run_tests() { + + termux_core__tests__log 3 "termux_core__sh__termux_apps_info_env_variable()" + + TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME="termux_core__sh__termux_apps_info_env_variable__run_test" + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_VAR_TEST + + termux_core__set_termux_core_test_scoped_env_variables__default || return $? + termux_core__sh__termux_apps_info_env_variable__run_tests_for_scope || return $? + + termux_core__set_termux_core_test_scoped_env_variables__foo || return $? + termux_core__sh__termux_apps_info_env_variable__run_tests_for_scope || return $? + + return 0 + +} + +termux_core__sh__termux_apps_info_env_variable__run_tests_for_scope() { + + local test_type + local termux_core__taiev__scope_args + + TERMUX_ENV__S_ROOT="$TERMUX_CORE__TESTS__TERMUX_ENV__S_ROOT" + + + termux_core__termux_apps_info_env_variable__test__usage "usage" \ + "APP__" "STRING" || return $? + + + + local -A test_types=() + test_types["scope-s"]='s="'"${TERMUX_ENV__S_ROOT}APP__"'"' + test_types["scope-ss"]='ss="'APP__'"' + test_types["component-name"]='cn="termux-app"' + for test_type in "${!test_types[@]}"; do + termux_core__taiev__scope_args="${test_types["$test_type"]}" + + # `TERMUX_APP__STRING_DEFINED`. + termux_core__termux_apps_info_env_variable__test__termux_string_defined "termux-string-defined_${test_type}" \ + "APP__" "STRING_DEFINED" \ + "$termux_core__taiev__scope_args"' "STRING_DEFINED"' '"*"' || return $? + + + + # `TERMUX_APP__STRING_SET`. + termux_core__termux_apps_info_env_variable__test__termux_string_set "termux-string-set_${test_type}" \ + "APP__" "STRING_SET" \ + "$termux_core__taiev__scope_args"' "STRING_SET"' '"?"' || return $? + + + + # `TERMUX_APP__TARGET_SDK`. + termux_core__termux_apps_info_env_variable__test__termux_app__target_sdk "termux-app-target-sdk_${test_type}" \ + "APP__" "TARGET_SDK" \ + "$termux_core__taiev__scope_args"' "TARGET_SDK"' || return $? + + + # `TERMUX_APP__DATA_DIR`. + termux_core__termux_apps_info_env_variable__test__termux_app__data_dir "termux-app-data-dir_${test_type}" \ + "APP__" "DATA_DIR" \ + "$termux_core__taiev__scope_args"' "DATA_DIR"' 'p+="/*[!/]"' \ + "$TERMUX_CORE__TESTS__TERMUX_APP__DATA_DIR" || return $? + done + + return 0 + +} + +termux_core__sh__termux_apps_info_env_variable__run_test() { + + termux_core__tests__validate_argument_count ge $# 7 termux_core__sh__termux_apps_info_env_variable__run_test "$@" || return $? + + local test_name="$1" + local pre_test_commands="$2" + local termux_apps_info_env_file_export="$3" + local termux_apps_info_env_file_content="$4" + local expected_exit_code="$5" + local expected_output_regex="$6" + local termux_core__sh__taiev_command_type="$7" + + local termux_apps_info_env_file_create_command + local termux_apps_info_env_file_export_command + local termux_apps_info_env_file_path + local termux_core__sh__taiev_call_command + local termux_core__sh__taiev_source_command + local termux_core__sh__taiev_source_output_mode_arg="" + local termux_core__sh__taiev_source_print_command="" + + if [[ "$termux_core__sh__taiev_command_type" == "source-env" ]]; then + termux_core__tests__validate_argument_count eq $# 10 \ + "'termux_core__sh__termux_apps_info_env_variable__run_test' for the '$termux_core__sh__taiev_command_type' command" "$@" || return $? + + local termux_core__sh__taiev_options="$8" + local termux_core__sh__taiev_source_print_var="$9" + local termux_core__sh__taiev_args="${10}" + + if [[ "$termux_core__sh__taiev_source_print_var" != "-" ]]; then + termux_core__sh__taiev_source_print_command="${NL}printf '%s' \"\${$termux_core__sh__taiev_source_print_var:-}\"" + fi + elif [[ "$termux_core__sh__taiev_command_type" == "get-value" ]]; then + termux_core__tests__validate_argument_count eq $# 10 \ + "'termux_core__sh__termux_apps_info_env_variable__run_test' for the '$termux_core__sh__taiev_command_type' command" "$@" || return $? + + local termux_core__sh__taiev_options="$8" + local termux_core__sh__taiev_source_output_mode="$9" + local termux_core__sh__taiev_args="${10}" + + if [[ "$termux_core__sh__taiev_source_output_mode" == "_" ]]; then + termux_core__sh__taiev_source_output_mode="output_variable" + fi + + termux_core__sh__taiev_source_output_mode_arg=" '$termux_core__sh__taiev_source_output_mode'" + + if [[ "$termux_core__sh__taiev_source_output_mode" != "-" ]] && \ + [[ "$termux_core__sh__taiev_source_output_mode" != ">" ]]; then + termux_core__sh__taiev_source_print_command="${NL}printf '%s' \"\${$termux_core__sh__taiev_source_output_mode:-}\"" + fi + else + local termux_core__sh__taiev_args="" + fi + + + # shellcheck disable=SC2016 + termux_core__sh__taiev_source_command='source_file_from_path() { local source_file="${1:-}"; [ $# -gt 0 ] && shift 1; local source_path; if source_path="$(command -v "$source_file")" && [ -n "$source_path" ]; then . "$source_path" || return $?; else echo "Failed to find the '\''$source_file'\'' file to source." 1>&2; return 1; fi; }'"${NL}${NL}"' +source_file_from_path "termux-apps-info-env-variable.sh" --sourcing-script || exit $?' + + termux_apps_info_env_file_path="$TERMUX_CORE__TESTS__TMPDIR_PATH/termux-apps-info.env" + if [[ "$termux_apps_info_env_file_export" == "true" ]]; then + termux_apps_info_env_file_create_command="printf '%s\n' '${termux_apps_info_env_file_content//\'/\'\\\'\'}' > '${termux_apps_info_env_file_path//\'/\'\\\'\'}' || exit \$?" + get_export_scoped_variable_command termux_apps_info_env_file_export_command "$TERMUX_ENV__S_ROOT" "CORE__" "APPS_INFO_ENV_FILE" "$termux_apps_info_env_file_path" || return $? + else + termux_apps_info_env_file_create_command="printf '%s' '' > '${termux_apps_info_env_file_path//\'/\'\\\'\'}' || exit \$?" + get_unset_scoped_variable_command termux_apps_info_env_file_export_command "$TERMUX_ENV__S_ROOT" "CORE__" "APPS_INFO_ENV_FILE" || return $? + fi + + pre_test_commands="export TERMUX_ENV__S_ROOT='${TERMUX_ENV__S_ROOT//\'/\'\\\'\'}';${NL} +unset TERMUX_ENV__S_APP;${NL}${NL}\ +${termux_apps_info_env_file_create_command}${NL}${NL}${termux_apps_info_env_file_export_command}${NL}${NL}\ +${pre_test_commands:+"$pre_test_commands${NL}${NL}"}" + + termux_core__sh__taiev_options="${termux_core__sh__taiev_options:+"$termux_core__sh__taiev_options "}" + + if [[ "$termux_core__sh__taiev_command_type" == "get-value" ]]; then + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST:-}" != "true" ]]; then + termux_core__sh__taiev_call_command="termux-apps-info-env-variable.sh" + termux_core__tests__run_script_test "termux_core__sh__taiev__${test_name}__exec-stdout" \ + "#!/bin/sh${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__sh__taiev_call_command '$termux_core__sh__taiev_command_type' ${termux_core__sh__taiev_options}'>' $termux_core__sh__taiev_args" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST:-}" != "true" ]]; then + termux_core__sh__taiev_call_command="${termux_core__sh__taiev_source_command}${NL}${NL}termux_core__sh__termux_apps_info_env_variable" + termux_core__tests__run_script_test "termux_core__sh__taiev__${test_name}__source-stdout" \ + "#!/bin/sh${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__sh__taiev_call_command '$termux_core__sh__taiev_command_type' ${termux_core__sh__taiev_options}'>' $termux_core__sh__taiev_args" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + fi + + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_VAR_TEST:-}" != "true" ]]; then + termux_core__sh__taiev_call_command="${termux_core__sh__taiev_source_command}${NL}${NL}termux_core__sh__termux_apps_info_env_variable" + termux_core__tests__run_script_test "termux_core__sh__taiev__${test_name}__source-var" \ + "#!/bin/sh${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__sh__taiev_call_command '$termux_core__sh__taiev_command_type' ${termux_core__sh__taiev_options}$termux_core__sh__taiev_source_output_mode_arg $termux_core__sh__taiev_args || exit \$?$termux_core__sh__taiev_source_print_command" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + + return 0 + +} + + + +termux_core__bash__termux_apps_info_env_variable__run_tests() { + + termux_core__tests__log 3 "termux_core__bash__termux_apps_info_env_variable()" + + TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME="termux_core__bash__termux_apps_info_env_variable__run_test" + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_VAR_TEST + + termux_core__set_termux_core_test_scoped_env_variables__default || return $? + termux_core__bash__termux_apps_info_env_variable__run_tests_for_scope || return $? + + termux_core__set_termux_core_test_scoped_env_variables__foo || return $? + termux_core__bash__termux_apps_info_env_variable__run_tests_for_scope || return $? + + return 0 + +} + +termux_core__bash__termux_apps_info_env_variable__run_tests_for_scope() { + + local test_type + local termux_core__taiev__scope_args + + TERMUX_ENV__S_ROOT="$TERMUX_CORE__TESTS__TERMUX_ENV__S_ROOT" + + + termux_core__termux_apps_info_env_variable__test__usage "usage" \ + "APP__" "STRING" || return $? + + + + local -A test_types=() + test_types["scope-s"]='s="'"${TERMUX_ENV__S_ROOT}APP__"'"' + test_types["scope-ss"]='ss="'APP__'"' + test_types["component-name"]='cn="termux-app"' + for test_type in "${!test_types[@]}"; do + termux_core__taiev__scope_args="${test_types["$test_type"]}" + + # `TERMUX_APP__STRING_DEFINED`. + termux_core__termux_apps_info_env_variable__test__termux_string_defined "termux-string-defined_${test_type}" \ + "APP__" "STRING_DEFINED" \ + "$termux_core__taiev__scope_args"' "STRING_DEFINED"' '"*"' || return $? + + + + # `TERMUX_APP__STRING_SET`. + termux_core__termux_apps_info_env_variable__test__termux_string_set "termux-string-set_${test_type}" \ + "APP__" "STRING_SET" \ + "$termux_core__taiev__scope_args"' "STRING_SET"' '"?"' || return $? + + + + # `TERMUX_APP__TARGET_SDK`. + termux_core__termux_apps_info_env_variable__test__termux_app__target_sdk "termux-app-target-sdk_${test_type}" \ + "APP__" "TARGET_SDK" \ + "$termux_core__taiev__scope_args"' "TARGET_SDK"' || return $? + + + # `TERMUX_APP__DATA_DIR`. + termux_core__termux_apps_info_env_variable__test__termux_app__data_dir "termux-app-data-dir_${test_type}" \ + "APP__" "DATA_DIR" \ + "$termux_core__taiev__scope_args"' "DATA_DIR"' 'r+="^(/[^/]+)+$"' \ + "$TERMUX_CORE__TESTS__TERMUX_APP__DATA_DIR" || return $? + done + + return 0 + +} + +termux_core__bash__termux_apps_info_env_variable__run_test() { + + termux_core__tests__validate_argument_count ge $# 7 termux_core__bash__termux_apps_info_env_variable__run_test "$@" || return $? + + local test_name="$1" + local pre_test_commands="$2" + local termux_apps_info_env_file_export="$3" + local termux_apps_info_env_file_content="$4" + local expected_exit_code="$5" + local expected_output_regex="$6" + local termux_core__bash__taiev_command_type="$7" + + local termux_apps_info_env_file_create_command + local termux_apps_info_env_file_export_command + local termux_apps_info_env_file_path + local termux_core__bash__taiev_call_command + local termux_core__bash__taiev_source_command + local termux_core__bash__taiev_source_output_mode_arg="" + local termux_core__bash__taiev_source_print_command="" + + if [[ "$termux_core__bash__taiev_command_type" == "source-env" ]]; then + termux_core__tests__validate_argument_count eq $# 10 \ + "'termux_core__bash__termux_apps_info_env_variable__run_test' for the '$termux_core__bash__taiev_command_type' command" "$@" || return $? + + local termux_core__bash__taiev_options="$8" + local termux_core__bash__taiev_source_print_var="$9" + local termux_core__bash__taiev_args="${10}" + + if [[ "$termux_core__bash__taiev_source_print_var" != "-" ]]; then + termux_core__bash__taiev_source_print_command="${NL}printf '%s' \"\${$termux_core__bash__taiev_source_print_var:-}\"" + fi + elif [[ "$termux_core__bash__taiev_command_type" == "get-value" ]]; then + termux_core__tests__validate_argument_count eq $# 10 \ + "'termux_core__bash__termux_apps_info_env_variable__run_test' for the '$termux_core__bash__taiev_command_type' command" "$@" || return $? + + local termux_core__bash__taiev_options="$8" + local termux_core__bash__taiev_source_output_mode="$9" + local termux_core__bash__taiev_args="${10}" + + if [[ "$termux_core__bash__taiev_source_output_mode" == "_" ]]; then + termux_core__bash__taiev_source_output_mode="output_variable" + fi + + termux_core__bash__taiev_source_output_mode_arg=" '$termux_core__bash__taiev_source_output_mode'" + + if [[ "$termux_core__bash__taiev_source_output_mode" != "-" ]] && \ + [[ "$termux_core__bash__taiev_source_output_mode" != ">" ]]; then + termux_core__bash__taiev_source_print_command="${NL}printf '%s' \"\${$termux_core__bash__taiev_source_output_mode:-}\"" + fi + else + local termux_core__bash__taiev_args="" + fi + + + # shellcheck disable=SC2016 + termux_core__bash__taiev_source_command='source_file_from_path() { local source_file="${1:-}"; [ $# -gt 0 ] && shift 1; local source_path; if source_path="$(command -v "$source_file")" && [ -n "$source_path" ]; then source "$source_path" || return $?; else echo "Failed to find the '\''$source_file'\'' file to source." 1>&2; return 1; fi; }'"${NL}${NL}"' +source_file_from_path "termux-apps-info-env-variable.bash" || exit $?' + + termux_apps_info_env_file_path="$TERMUX_CORE__TESTS__TMPDIR_PATH/termux-apps-info.env" + if [[ "$termux_apps_info_env_file_export" == "true" ]]; then + termux_apps_info_env_file_create_command="printf '%s\n' '${termux_apps_info_env_file_content//\'/\'\\\'\'}' > '${termux_apps_info_env_file_path//\'/\'\\\'\'}' || exit \$?" + get_export_scoped_variable_command termux_apps_info_env_file_export_command "$TERMUX_ENV__S_ROOT" "CORE__" "APPS_INFO_ENV_FILE" "$termux_apps_info_env_file_path" || return $? + else + termux_apps_info_env_file_create_command="printf '%s' '' > '${termux_apps_info_env_file_path//\'/\'\\\'\'}' || exit \$?" + get_unset_scoped_variable_command termux_apps_info_env_file_export_command "$TERMUX_ENV__S_ROOT" "CORE__" "APPS_INFO_ENV_FILE" || return $? + fi + + pre_test_commands="export TERMUX_ENV__S_ROOT='${TERMUX_ENV__S_ROOT//\'/\'\\\'\'}';${NL} +unset TERMUX_ENV__S_APP;${NL}${NL}\ +${termux_apps_info_env_file_create_command}${NL}${NL}${termux_apps_info_env_file_export_command}${NL}${NL}\ +${pre_test_commands:+"$pre_test_commands${NL}${NL}"}" + + termux_core__bash__taiev_options="${termux_core__bash__taiev_options:+"$termux_core__bash__taiev_options "}" + + if [[ "$termux_core__bash__taiev_command_type" == "get-value" ]]; then + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST:-}" != "true" ]]; then + termux_core__bash__taiev_call_command="termux-apps-info-env-variable.bash" + termux_core__tests__run_script_test "termux_core__bash__taiev__${test_name}__exec-stdout" \ + "#!/bin/bash${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__bash__taiev_call_command '$termux_core__bash__taiev_command_type' ${termux_core__bash__taiev_options}'>' $termux_core__bash__taiev_args" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST:-}" != "true" ]]; then + termux_core__bash__taiev_call_command="${termux_core__bash__taiev_source_command}${NL}${NL}termux_core__bash__termux_apps_info_env_variable" + termux_core__tests__run_script_test "termux_core__bash__taiev__${test_name}__source-stdout" \ + "#!/bin/bash${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__bash__taiev_call_command '$termux_core__bash__taiev_command_type' ${termux_core__bash__taiev_options}'>' $termux_core__bash__taiev_args" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + fi + + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_VAR_TEST:-}" != "true" ]]; then + termux_core__bash__taiev_call_command="${termux_core__bash__taiev_source_command}${NL}${NL}termux_core__bash__termux_apps_info_env_variable" + termux_core__tests__run_script_test "termux_core__bash__taiev__${test_name}__source-var" \ + "#!/bin/bash${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__bash__taiev_call_command '$termux_core__bash__taiev_command_type' ${termux_core__bash__taiev_options}$termux_core__bash__taiev_source_output_mode_arg $termux_core__bash__taiev_args || exit \$?$termux_core__bash__taiev_source_print_command" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + + return 0 + +} + + + +termux_core__termux_apps_info_env_variable__test__usage() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 3 termux_core__termux_apps_info_env_variable__test__usage "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + + local env_command + local env_file + local test_type + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_command_unset" \ + "" \ + "false" "" \ + 64 "^The command '' passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + "" "" - "" || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_command_invalid" \ + "" \ + "false" "" \ + 64 "^The command 'get-value1' passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value1 "" - "" || return $? + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_source-env_command_args_invalid-more" \ + "" \ + "false" "" \ + 64 "^Invalid argument count 1 for the 'source-env' command\. The \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' function expects 0 arguments\..*$" \ + "source-env" "" - "1" || return $? + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_command_args_invalid-1" \ + "" \ + "false" "" \ + 64 "^Invalid argument count 1 for the 'get-value' command\. The \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' function expects minimum 4 arguments\..*$" \ + get-value "" - "" || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_command_args_invalid-less" \ + "" \ + "false" "" \ + 64 "^Invalid argument count 3 for the 'get-value' command\. The \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' function expects minimum 4 arguments\..*$" \ + get-value "" - '2 3' || return $? + + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_none" \ + "" \ + "false" "" \ + 64 "^The output_mode '' argument passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name, or equal to \`>\` or \`-\`\..*$" \ + get-value "--skip-sourcing" "" 'ss="APP__" "TEST" "*"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_invalid" \ + "" \ + "false" "" \ + 64 "^The output_mode '1foo' argument passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name, or equal to \`>\` or \`-\`\..*$" \ + get-value "--skip-sourcing" "1foo" 'ss="APP__" "TEST" "*"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "TEST" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_output-mode-stdout_value-valid" \ + "$env_command" \ + "false" "" \ + 0 "^foo$" \ + get-value "--skip-sourcing" ">" 'ss="APP__" "TEST" "*"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "TEST" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_output-mode-dash_value-valid" \ + "$env_command" \ + "false" "" \ + 0 "^$" \ + get-value "--skip-sourcing" "-" 'ss="APP__" "TEST" "*"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "BAR_APP__" "TEST" "bar" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "BAR_APP__" "TEST" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_source-env_value-set" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^foo$" \ + source-env "" "${TERMUX_ENV__S_ROOT}BAR_APP__TEST" "" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "BAR_APP__" "TEST" "bar" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "BAR_APP__" "TEST" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_source-env_value-empty" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^$" \ + source-env "" "${TERMUX_ENV__S_ROOT}BAR_APP__TEST" "" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "BAR_APP__" "TEST" "bar" || return $? + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "BAR_APP__" "TEST" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_source-env_value-unset" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^$" \ + source-env "" "${TERMUX_ENV__S_ROOT}BAR_APP__TEST" "" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "BAR_APP__" "TEST" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_source-env_value-none" \ + "$env_command" \ + "true" "" \ + 0 "^bar$" \ + source-env "" "${TERMUX_ENV__S_ROOT}BAR_APP__TEST" "" || return $? + + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "BAR_APP__" "TEST" "bar" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "BAR_APP__" "TEST" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_var-already-set_value-set" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^foo$" \ + get-value "" "${TERMUX_ENV__S_ROOT}BAR_APP__TEST" 'ss="BAR_APP__" "TEST" "*"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "BAR_APP__" "TEST" "bar" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "BAR_APP__" "TEST" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_var-already-set_value-empty" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "" "${TERMUX_ENV__S_ROOT}BAR_APP__TEST" 'ss="BAR_APP__" "TEST" "*"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "BAR_APP__" "TEST" "bar" || return $? + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "BAR_APP__" "TEST" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_var-already-set_value-unset" \ + "$env_command" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" "${TERMUX_ENV__S_ROOT}BAR_APP__TEST" 'ss="BAR_APP__" "TEST" "*"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope_none-with-no-def" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode '' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "--skip-sourcing" _ '"" "TEST" "?"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope_none-with-def" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode '' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "--skip-sourcing" _ '"" "TEST" "?" "foo"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope_invalid-with-no-arg" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode 't=' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "--skip-sourcing" _ 't= "TEST" "*"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope_invalid-with-arg" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode 't=_' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "--skip-sourcing" _ 't="_" "TEST" "*"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_component-name_invalid-with-no-arg" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode 'cn=' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "--skip-sourcing" _ 'cn= "TEST" "*"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_component-name_unsupported-app" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode 'cn=bar-app' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + get-value "--skip-sourcing" _ 'cn="bar-app" "TEST" "*"' || return $? + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-s_no-arg" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode 's=' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "--skip-sourcing" _ 's="" "TEST" "*"' || return $? + + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "1" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-s_var-root-scope-invalid" \ + "$env_command" \ + "false" "" \ + 81 "^Failed to find a valid value .*$" \ + get-value "--skip-sourcing" _ 's="BAR_APP__" "TEST" "*"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-s_var-sub-scope-invalid" \ + "$env_command" \ + "false" "" \ + 64 "^The name of the variable to get 'BAR#_APP__TEST' generated in \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name\..*$" \ + get-value "--skip-sourcing" _ 's="BAR#_APP__" "TEST" "*"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-s_var-sub-name-invalid" \ + "$env_command" \ + "false" "" \ + 64 "^The name of the variable to get 'BAR_APP__#TEST' generated in \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name\..*$" \ + get-value "--skip-sourcing" _ 's="BAR_APP__" "#TEST" "*"' || return $? + + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + append_export_scoped_variable_command env_command "BAR_" "APP__" "VAR" "bar" || return $? + append_export_scoped_variable_command env_command "BAZ_" "APP__" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-s_custom-set" \ + "$env_command" \ + "false" "" \ + 0 "^baz$" \ + get-value "--skip-sourcing" _ 's="BAZ_APP__" "VAR" "*"' || return $? + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_no-arg" \ + "" \ + "false" "" \ + 64 "^The scoped_var_scope_mode 'ss=' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_apps_info_env_variable' is not valid\..*$" \ + get-value "" _ 'ss="" "TEST" "*"' || return $? + + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "1" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_var-root-scope-invalid" \ + "$env_command" \ + "false" "" \ + 1 "^The TERMUX_ENV__S_ROOT environment variable value '1' while running \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name\..*$" \ + get-value "--skip-sourcing" _ 'ss="APP__" "TEST" "*"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_var-sub-scope-invalid" \ + "$env_command" \ + "false" "" \ + 64 "^The name of the variable to get 'BAR_X#_APP__TEST' generated in \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name\..*$" \ + get-value "--skip-sourcing" _ 'ss="X#_APP__" "TEST" "*"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_var-sub-name-invalid" \ + "$env_command" \ + "false" "" \ + 64 "^The name of the variable to get 'BAR_APP__#TEST' generated in \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name\..*$" \ + get-value "--skip-sourcing" _ 'ss="APP__" "#TEST" "*"' || return $? + + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + append_export_scoped_variable_command env_command "BAR_" "APP__" "VAR" "bar" || return $? + append_export_scoped_variable_command env_command "BAZ_" "APP__" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_custom-set" \ + "$env_command" \ + "false" "" \ + 0 "^bar$" \ + get-value "--skip-sourcing" _ 'ss="APP__" "VAR" "*"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + append_export_scoped_variable_command env_command "BAR_" "APP__" "VAR" "bar" || return $? + append_export_scoped_variable_command env_command "BAZ_" "APP__" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_custom-unset" \ + "$env_command" \ + "false" "" \ + 81 "^Failed to find a valid value .*$" \ + get-value "--skip-sourcing" _ 'ss="FOO_APP__" "VAR" "*"' || return $? + + + + local -A test_types=() + test_types["scope-s"]='s="'"${TERMUX_ENV__S_ROOT}APP__"'"' + test_types["scope-ss"]='ss="'APP__'"' + test_types["component-name"]='cn="termux-app"' + + for test_type in "${!test_types[@]}"; do + termux_core__taiavn__scope_args="${test_types["$test_type"]}" + + # `--skip-sourcing`. + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_skip-sourcing_env-unset" \ + "$env_command" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "--skip-sourcing" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_skip-sourcing_env-empty" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "--skip-sourcing" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "bar" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_skip-sourcing_env-set" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^bar$" \ + get-value "--skip-sourcing" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + + + # `--skip-sourcing-if-cur-app-var`. + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}APP__" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "bar" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_skip-sourcing-if-cur-app-var_env-set_file-set_same-app" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^bar$" \ + get-value "--skip-sourcing-if-cur-app-var" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}DIFF_APP__" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "bar" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_skip-sourcing-if-cur-app-var_env-set_file-set_different-app" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^baz$" \ + get-value "--skip-sourcing-if-cur-app-var" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_APP" "${TERMUX_ENV__S_ROOT}DIFF_APP__" || return $? + append_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "bar" || return $? + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_skip-sourcing-if-cur-app-var_env-set_file-unset_different-app" \ + "$env_command" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "--skip-sourcing-if-cur-app-var" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + + + # `--source`. + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" || return $? + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_source_env-unset_file-unset" \ + "$env_command" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_source_env-unset_file-empty" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_source_env-unset_file-set" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^baz$" \ + get-value "" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "" || return $? + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_source_env-empty_file-unset" \ + "$env_command" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_source_env-empty_file-empty" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_source_env-empty_file-set" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^baz$" \ + get-value "" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "bar" || return $? + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_source_env-set_file-unset" \ + "$env_command" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "bar" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_source_env-set_file-empty" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "bar" || return $? + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "APP__" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_${test_type}_source_env-set_file-set" \ + "$env_command" \ + "true" "$env_file" \ + 0 "^baz$" \ + get-value "" _ "${termux_core__taiavn__scope_args}"' "VAR" "*"' || return $? + done + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_invalid-with-no-arg" \ + "" \ + "false" "" \ + 64 "^The validator command 'unknown_func' \ +not found while running 'termux_core__([a-z_]+)__termux_scoped_env_variable' \ +that is set in the ([a-z_]+)_validator 'c\+=unknown_func' argument\..*$" \ + get-value "" _ 'ss="'"$var_sub_scope_name"'" "TEST" c+="unknown_func"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_invalid-with-arg" \ + "" \ + "false" "" \ + 64 "^The validator command 'unknown_func' \ +not found while running 'termux_core__([a-z_]+)__termux_scoped_env_variable' \ +that is set in the ([a-z_]+)_validator 'c\+=unknown_func arg1' argument\..*$" \ + get-value "" _ 'ss="'"$var_sub_scope_name"'" "TEST" c+="unknown_func arg1"' || return $? + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_no-command-arg_file-valid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"foo\" ]; }" \ + "true" "$env_file" \ + 0 "^foo$" \ + get-value "" _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable'" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_no-command-arg_file-invalid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"foo\" ]; }" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable'" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_no-command-arg_file-invalid_def-valid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"foo\" ]; }" \ + "true" "$env_file" \ + 0 "^foo$" \ + get-value "" _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable' foo" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_no-command-arg_file-invalid_def-invalid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"foo\" ]; }" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable' baz" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-arg_file-valid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + "true" "$env_file" \ + 0 "^foo$" \ + get-value "" _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable value'" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-arg_file-invalid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable value'" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-arg_file-invalid_def-valid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + "true" "$env_file" \ + 0 "^foo$" \ + get-value "" _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable value' foo" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-arg_file-invalid_def-invalid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable value' baz" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-arg_arg-type-invalid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable name'" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + return 0 + +} + + + +termux_core__termux_apps_info_env_variable__test__termux_string_defined() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 5 termux_core__termux_apps_info_env_variable__test__termux_string_defined "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + local termux_core__taiev__scope_args="$4" + local termux_core__taiev__validator_and_values_args="$5" + + local env_file + + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-unset" \ + "" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__taiev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-empty" \ + "" \ + "true" "$env_file" \ + 0 "^$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__taiev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-set" \ + "" \ + "true" "$env_file" \ + 0 "^foo$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__taiev__validator_and_values_args}" || return $? + + return 0 + +} + +termux_core__termux_apps_info_env_variable__test__termux_string_set() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 5 termux_core__termux_apps_info_env_variable__test__termux_string_set "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + local termux_core__taiev__scope_args="$4" + local termux_core__taiev__validator_and_values_args="$5" + + local env_file + + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-unset" \ + "" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__taiev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-empty" \ + "" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__taiev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-set" \ + "" \ + "true" "$env_file" \ + 0 "^foo$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__taiev__validator_and_values_args}" || return $? + + return 0 + +} + +termux_core__termux_apps_info_env_variable__test__termux_app__target_sdk() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 4 termux_core__termux_apps_info_env_variable__test__termux_app__target_sdk "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + local termux_core__taiev__scope_args="$4" + + local termux_core__mode__taiev_arg="c+='is_valid_target_sdk'" + + # shellcheck disable=SC2016 + local is_valid_target_sdk_impl='is_valid_target_sdk() { case "${1:-}" in '\'\''|*[!0-9]*|0[0-9]*) return 1;; esac; [ "$1" -le 10000 ]; }' + + local env_file + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-unset" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-empty" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-valid_0" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 0 "^0$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "1" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-valid_1" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 0 "^1$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "9" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-valid_9" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 0 "^9$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "10" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-valid_10" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 0 "^10$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "11" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-valid_11" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 0 "^11$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "99" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-valid_99" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 0 "^99$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "999" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-valid_999" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 0 "^999$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "1000" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-valid_1000" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 0 "^1000$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "9999" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-valid_9999" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 0 "^9999$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "10000" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-valid_10000" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 0 "^10000$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "-0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-invalid_-0" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "01" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-invalid_01" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "100000" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-invalid_100000" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "1000000" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-invalid_1000000" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "uuu" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-invalid_uuu" \ + "${is_valid_target_sdk_impl}" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__mode__taiev_arg}" || return $? + + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + return 0 + +} + +termux_core__termux_apps_info_env_variable__test__termux_app__data_dir() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 6 termux_core__termux_apps_info_env_variable__test__termux_app__data_dir "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + local termux_core__taiev__scope_args="$4" + local termux_core__taiev__validator_and_values_args="$5" + local termux_app__data_dir="$6" + + local env_file + local termux_app__data_dir_escaped + + termux_app__data_dir_escaped="$(termux_core__tests__escape_string_for_regex "$termux_app__data_dir")" || return $? + + get_unset_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-unset" \ + "" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__taiev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-empty" \ + "" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__taiev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "$termux_app__data_dir" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-valid-path" \ + "" \ + "true" "$env_file" \ + 0 "^${termux_app__data_dir_escaped}$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__taiev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_file "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" ".$termux_app__data_dir" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_file-invalid-path" \ + "" \ + "true" "$env_file" \ + 81 "^Failed to find a valid value .*$" \ + get-value "" _ "${termux_core__taiev__scope_args} ${termux_core__taiev__validator_and_values_args}" || return $? + + return 0 + +} diff --git a/app/main/tests/scripts/termux/shell/command/environment/termux-scoped-env-variable_runtime-script-tests.in b/app/main/tests/scripts/termux/shell/command/environment/termux-scoped-env-variable_runtime-script-tests.in new file mode 100644 index 0000000..71054c3 --- /dev/null +++ b/app/main/tests/scripts/termux/shell/command/environment/termux-scoped-env-variable_runtime-script-tests.in @@ -0,0 +1,1701 @@ +#!@TERMUX__PREFIX@/bin/bash +# shellcheck shell=bash + +## +# `termux_core__termux_scoped_env_variable__run_tests` +## +termux_core__termux_scoped_env_variable__run_tests() { + + termux_core__tests__log 3 "termux_core__termux_scoped_env_variable__run_tests()" + + termux_core__sh__termux_scoped_env_variable__run_tests || return $? + termux_core__bash__termux_scoped_env_variable__run_tests || return $? + + return 0 +} + + + + + +termux_core__sh__termux_scoped_env_variable__run_tests() { + + termux_core__tests__log 3 "termux_core__sh__termux_scoped_env_variable()" + + TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME="termux_core__sh__termux_scoped_env_variable__run_test" + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_VAR_TEST + + termux_core__set_termux_core_test_scoped_env_variables__default || return $? + termux_core__sh__termux_scoped_env_variable__run_tests_for_scope || return $? + + termux_core__set_termux_core_test_scoped_env_variables__foo || return $? + termux_core__sh__termux_scoped_env_variable__run_tests_for_scope || return $? + + return 0 + +} + +termux_core__sh__termux_scoped_env_variable__run_tests_for_scope() { + + local test_type + local termux_core__tsev__scope_args + + TERMUX_ENV__S_ROOT="$TERMUX_CORE__TESTS__TERMUX_ENV__S_ROOT" + + + termux_core__termux_scoped_env_variable__test__usage "usage" \ + "_" "STRING" || return $? + + termux_core__sh__termux_scoped_env_variable__test__usage "extended-usage" \ + "_" "STRING" || return $? + + + local -A test_types=() + test_types["scope-s"]='s="'"${TERMUX_ENV__S_ROOT}_"'"' + test_types["scope-ss"]='ss="'_'"' + test_types["component-name"]='cn="termux"' + for test_type in "${!test_types[@]}"; do + termux_core__tsev__scope_args="${test_types["$test_type"]}" + + # `TERMUX__STRING_DEFINED`. + termux_core__termux_scoped_env_variable__test__termux_string_defined "termux-string-defined_${test_type}" \ + "_" "STRING_DEFINED" \ + "$termux_core__tsev__scope_args"' "STRING_DEFINED"' '"*"' || return $? + + + + # `TERMUX__STRING_SET`. + termux_core__termux_scoped_env_variable__test__termux_string_set "termux-string-set_${test_type}" \ + "_" "STRING_SET" \ + "$termux_core__tsev__scope_args"' "STRING_SET"' '"?"' || return $? + + + + # `TERMUX__PACKAGE_NAME`. + termux_core__termux_scoped_env_variable__test__termux__package_name "termux-package-name_${test_type}_regex" \ + "_" "PACKAGE_NAME" \ + "$termux_core__tsev__scope_args"' "PACKAGE_NAME"' 'p-="*[!a-zA-Z0-9_.]*"' \ + "$TERMUX_CORE__TESTS__TERMUX__PACKAGE_NAME" \ + "false" "" || return $? + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + # shellcheck disable=SC2016 + termux_core__termux_scoped_env_variable__test__termux__package_name "termux-package-name_${test_type}_command" \ + "_" "PACKAGE_NAME" \ + "$termux_core__tsev__scope_args"' "PACKAGE_NAME"' 'c+="is_valid_android_app_package_name"' \ + "$TERMUX_CORE__TESTS__TERMUX__PACKAGE_NAME" \ + "true" 'is_valid_android_app_package_name() { printf "%s\n" "${1:-}" | grep -qE '\''^(()|([a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)+))$'\'' && [ "${#1}" -le 255 ]; }' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + + + # `TERMUX__ROOTFS`. + termux_core__termux_scoped_env_variable__test__termux_path "termux-rootfs_${test_type}" \ + "_" "ROOTFS" \ + "$termux_core__tsev__scope_args"' "ROOTFS"' 'p+="/|/*[!/]"' \ + "$TERMUX_CORE__TESTS__TERMUX__ROOTFS" || return $? + + termux_core__termux_scoped_env_variable__test__termux_path "termux-rootfs-as-android-rootfs_${test_type}" \ + "_" "ROOTFS" \ + "$termux_core__tsev__scope_args"' "ROOTFS"' 'p+="/|/*[!/]"' \ + "/" || return $? + + + # `TERMUX__HOME`. + termux_core__termux_scoped_env_variable__test__termux_path "termux-home_${test_type}" \ + "_" "HOME" \ + "$termux_core__tsev__scope_args"' "HOME"' 'p+="/*[!/]"' \ + "$TERMUX_CORE__TESTS__TERMUX__HOME" || return $? + + + # `TERMUX__PREFIX`. + termux_core__termux_scoped_env_variable__test__termux_path "termux-prefix_${test_type}" \ + "_" "PREFIX" \ + "$termux_core__tsev__scope_args"' "PREFIX"' 'p+="/*[!/]"' \ + "$TERMUX_CORE__TESTS__TERMUX__PREFIX" || return $? + + + + # `TERMUX__USER_ID`. + termux_core__termux_scoped_env_variable__test__termux__user_id "termux-user-id_${test_type}" \ + "_" "USER_ID" \ + "$termux_core__tsev__scope_args"' "USER_ID"' 'p-="'\'\''|*[!0-9]*|0[0-9]*|????*"' || return $? + done + + local -A test_types=() + test_types["scope-s"]='s="'"${TERMUX_ENV__S_ROOT}APP__"'"' + test_types["scope-ss"]='ss="'APP__'"' + test_types["component-name"]='cn="termux-app"' + for test_type in "${!test_types[@]}"; do + termux_core__tsev__scope_args="${test_types["$test_type"]}" + + # `TERMUX_APP__PACKAGE_NAME`. + termux_core__termux_scoped_env_variable__test__termux_app__package_name "termux-app-package-name_${test_type}_regex" \ + "APP__" "PACKAGE_NAME" \ + "$termux_core__tsev__scope_args"' "PACKAGE_NAME"' 'p-="'\'\''|*[!a-zA-Z0-9_.]*"' \ + "$TERMUX_CORE__TESTS__TERMUX__PACKAGE_NAME" \ + "false" "" || return $? + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + # shellcheck disable=SC2016. + termux_core__termux_scoped_env_variable__test__termux_app__package_name "termux-app-package-name_${test_type}_command" \ + "APP__" "PACKAGE_NAME" \ + "$termux_core__tsev__scope_args"' "PACKAGE_NAME"' 'c+="is_valid_android_app_package_name"' \ + "$TERMUX_CORE__TESTS__TERMUX__PACKAGE_NAME" \ + "true" 'is_valid_android_app_package_name() { printf "%s\n" "${1:-}" | grep -qE '\''^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)+$'\'' && [ "${#1}" -le 255 ]; }' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + done + + + + local -A test_types=() + test_types["scope-s"]='s="'"${TERMUX_ENV__S_ROOT}EXEC__"'"' + test_types["scope-ss"]='ss="'EXEC__'"' + test_types["component-name"]='cn="termux-exec"' + for test_type in "${!test_types[@]}"; do + termux_core__tsev__scope_args="${test_types["$test_type"]}" + + # `TERMUX_EXEC__PROC_SELF_EXE`. + # We cannot use `PROC_SELF_EXE` itself as `libtermux-exec` may modify/unset. + termux_core__termux_scoped_env_variable__test__termux_exec__proc_self_exe "termux-exec-proc-self-exe_${test_type}" \ + "EXEC__" "PROC_SELF_EXE_TEST" \ + "$termux_core__tsev__scope_args"' "PROC_SELF_EXE_TEST"' 'p+="'\'\''|/*[!/]"' \ + "$TERMUX_CORE__TESTS__TERMUX__PREFIX/bin/foo" || return $? + + done + + return 0 + +} + + + +termux_core__sh__termux_scoped_env_variable__test__usage() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 3 termux_core__sh__termux_scoped_env_variable__test__usage "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + + local env_command + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_pattern-match-positive_env-valid" \ + "${env_command}" \ + 0 "^foo$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' p+='foo|bar'" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_pattern-match-positive_env-invalid" \ + "${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' p+='foo|bar'" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_pattern-match-positive_env-invalid_def-valid" \ + "${env_command}" \ + 0 "^bar$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' p+='foo|bar' bar" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_pattern-match-positive_env-invalid_def-invalid" \ + "${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' p+='foo|bar' qux" || return $? + + + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_pattern-match-negative_env-valid" \ + "${env_command}" \ + 0 "^baz$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' p-='foo|bar'" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_pattern-match-negative_env-invalid" \ + "${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' p-='foo|bar'" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_pattern-match-negative_env-invalid_def-valid" \ + "${env_command}" \ + 0 "^baz$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' p-='foo|bar' baz" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_pattern-match-negative_env-invalid_def-invalid" \ + "${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' p-='foo|bar' bar" || return $? + + return 0 + +} + + + +termux_core__sh__termux_scoped_env_variable__run_test() { + + termux_core__tests__validate_argument_count ge $# 5 termux_core__sh__termux_scoped_env_variable__run_test "$@" || return $? + + local test_name="$1" + local pre_test_commands="$2" + local expected_exit_code="$3" + local expected_output_regex="$4" + local termux_core__sh__tsev_command_type="$5" + + local termux_core__sh__tsev_call_command + local termux_core__sh__tsev_source_command + local termux_core__sh__tsev_source_output_mode_arg="" + local termux_core__sh__tsev_source_print_command="" + + if [[ "$termux_core__sh__tsev_command_type" == "get-name" ]] || \ + [[ "$termux_core__sh__tsev_command_type" == "get-value" ]]; then + termux_core__tests__validate_argument_count eq $# 7 \ + "'termux_core__sh__termux_scoped_env_variable__run_test' for the '$termux_core__sh__tsev_command_type' command" "$@" || return $? + + local termux_core__sh__tsev_source_output_mode="$6" + local termux_core__sh__tsev_args="$7" + + if [[ "$termux_core__sh__tsev_source_output_mode" == "_" ]]; then + termux_core__sh__tsev_source_output_mode="output_variable" + fi + + termux_core__sh__tsev_source_output_mode_arg=" '$termux_core__sh__tsev_source_output_mode'" + + if [[ "$termux_core__sh__tsev_source_output_mode" != "-" ]] && \ + [[ "$termux_core__sh__tsev_source_output_mode" != ">" ]]; then + termux_core__sh__tsev_source_print_command="${NL}printf '%s' \"\${$termux_core__sh__tsev_source_output_mode:-}\"" + fi + elif [[ "$termux_core__sh__tsev_command_type" == "set-value" ]] || \ + [[ "$termux_core__sh__tsev_command_type" == "unset-value" ]]; then + termux_core__tests__validate_argument_count eq $# 7 \ + "'termux_core__sh__termux_scoped_env_variable__run_test' for the '$termux_core__sh__tsev_command_type' command" "$@" || return $? + + local termux_core__sh__tsev_source_print_var="$6" + local termux_core__sh__tsev_args="$7" + + if [[ "$termux_core__sh__tsev_source_print_var" != "-" ]]; then + termux_core__sh__tsev_source_print_command="${NL}printf '%s' \"\${$termux_core__sh__tsev_source_print_var:-}\"" + fi + else + local termux_core__sh__tsev_args="" + fi + + + pre_test_commands="export TERMUX_ENV__S_ROOT='${TERMUX_ENV__S_ROOT//\'/\'\\\'\'}'${NL}${NL}${pre_test_commands:+"$pre_test_commands${NL}${NL}"}" + + # shellcheck disable=SC2016 + termux_core__sh__tsev_source_command='source_file_from_path() { local source_file="${1:-}"; [ $# -gt 0 ] && shift 1; local source_path; if source_path="$(command -v "$source_file")" && [ -n "$source_path" ]; then . "$source_path" || return $?; else echo "Failed to find the '\''$source_file'\'' file to source." 1>&2; return 1; fi; }'"${NL}${NL}"' +source_file_from_path "termux-scoped-env-variable.sh" --sourcing-script || exit $?' + + if [[ "$termux_core__sh__tsev_command_type" == "get-name" ]] || \ + [[ "$termux_core__sh__tsev_command_type" == "get-value" ]]; then + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST:-}" != "true" ]]; then + termux_core__sh__tsev_call_command="termux-scoped-env-variable.sh" + termux_core__tests__run_script_test "termux_core__sh__tsev__${test_name}__exec-stdout" \ + "#!/bin/sh${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__sh__tsev_call_command '$termux_core__sh__tsev_command_type' '>' $termux_core__sh__tsev_args" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST:-}" != "true" ]]; then + termux_core__sh__tsev_call_command="${termux_core__sh__tsev_source_command}${NL}${NL}termux_core__sh__termux_scoped_env_variable" + termux_core__tests__run_script_test "termux_core__sh__tsev__${test_name}__source-stdout" \ + "#!/bin/sh${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__sh__tsev_call_command '$termux_core__sh__tsev_command_type' '>' $termux_core__sh__tsev_args" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + fi + + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_VAR_TEST:-}" != "true" ]]; then + termux_core__sh__tsev_call_command="${termux_core__sh__tsev_source_command}${NL}${NL}termux_core__sh__termux_scoped_env_variable" + termux_core__tests__run_script_test "termux_core__sh__tsev__${test_name}__source-var" \ + "#!/bin/sh${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__sh__tsev_call_command '$termux_core__sh__tsev_command_type'$termux_core__sh__tsev_source_output_mode_arg $termux_core__sh__tsev_args || exit \$?$termux_core__sh__tsev_source_print_command" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + + return 0 + +} + + + + + +termux_core__bash__termux_scoped_env_variable__run_tests() { + + termux_core__tests__log 3 "termux_core__bash__termux_scoped_env_variable()" + + TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME="termux_core__bash__termux_scoped_env_variable__run_test" + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_VAR_TEST + + termux_core__set_termux_core_test_scoped_env_variables__default || return $? + termux_core__bash__termux_scoped_env_variable__run_tests_for_scope || return $? + + termux_core__set_termux_core_test_scoped_env_variables__foo || return $? + termux_core__bash__termux_scoped_env_variable__run_tests_for_scope || return $? + + return 0 + +} + +termux_core__bash__termux_scoped_env_variable__run_tests_for_scope() { + + local test_type + local termux_core__tsev__scope_args + + TERMUX_ENV__S_ROOT="$TERMUX_CORE__TESTS__TERMUX_ENV__S_ROOT" + + + # Basic usage and errors. + termux_core__termux_scoped_env_variable__test__usage "usage" \ + "_" "STRING" || return $? + + termux_core__bash__termux_scoped_env_variable__test__usage "extended-usage" \ + "_" "STRING" || return $? + + + + local -A test_types=() + test_types["scope-s"]='s="'"${TERMUX_ENV__S_ROOT}_"'"' + test_types["scope-ss"]='ss="'_'"' + test_types["component-name"]='cn="termux"' + for test_type in "${!test_types[@]}"; do + termux_core__tsev__scope_args="${test_types["$test_type"]}" + + # `TERMUX__STRING_DEFINED`. + termux_core__termux_scoped_env_variable__test__termux_string_defined "termux-string-defined_${test_type}" \ + "_" "STRING_DEFINED" \ + "$termux_core__tsev__scope_args"' "STRING_DEFINED"' '"*"' || return $? + + + + # `TERMUX__STRING_SET`. + termux_core__termux_scoped_env_variable__test__termux_string_set "termux-string-set_${test_type}" \ + "_" "STRING_SET" \ + "$termux_core__tsev__scope_args"' "STRING_SET"' '"?"' || return $? + + + + # `TERMUX__PACKAGE_NAME`. + termux_core__termux_scoped_env_variable__test__termux__package_name "termux-package-name_${test_type}_regex" \ + "_" "PACKAGE_NAME" \ + "$termux_core__tsev__scope_args"' "PACKAGE_NAME"' 'r+="^(()|([a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)+))$"' \ + "$TERMUX_CORE__TESTS__TERMUX__PACKAGE_NAME" \ + "false" "" || return $? + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + # shellcheck disable=SC2016 + termux_core__termux_scoped_env_variable__test__termux__package_name "termux-package-name_${test_type}_command" \ + "_" "PACKAGE_NAME" \ + "$termux_core__tsev__scope_args"' "PACKAGE_NAME"' 'c+="is_valid_android_app_package_name"' \ + "$TERMUX_CORE__TESTS__TERMUX__PACKAGE_NAME" \ + "true" 'is_valid_android_app_package_name() { printf "%s\n" "${1:-}" | grep -qE '\''^(()|([a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)+))$'\'' && [ "${#1}" -le 255 ]; }' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + + + # `TERMUX__ROOTFS`. + termux_core__termux_scoped_env_variable__test__termux_path "termux-rootfs_${test_type}" \ + "_" "ROOTFS" \ + "$termux_core__tsev__scope_args"' "ROOTFS"' 'r+="^((/)|((/[^/]+)+))$"' \ + "$TERMUX_CORE__TESTS__TERMUX__ROOTFS" || return $? + + termux_core__termux_scoped_env_variable__test__termux_path "termux-rootfs-as-android-rootfs_${test_type}" \ + "_" "ROOTFS" \ + "$termux_core__tsev__scope_args"' "ROOTFS"' 'r+="^((/)|((/[^/]+)+))$"' \ + "/" || return $? + + + # `TERMUX__HOME`. + termux_core__termux_scoped_env_variable__test__termux_path "termux-home_${test_type}" \ + "_" "HOME" \ + "$termux_core__tsev__scope_args"' "HOME"' 'r+="^(/[^/]+)+$"' \ + "$TERMUX_CORE__TESTS__TERMUX__HOME" || return $? + + + # `TERMUX__PREFIX`. + termux_core__termux_scoped_env_variable__test__termux_path "termux-prefix_${test_type}" \ + "_" "PREFIX" \ + "$termux_core__tsev__scope_args"' "PREFIX"' 'r+="^(/[^/]+)+$"' \ + "$TERMUX_CORE__TESTS__TERMUX__PREFIX" || return $? + + + + # `TERMUX__USER_ID`. + termux_core__termux_scoped_env_variable__test__termux__user_id "termux-user-id_${test_type}" \ + "_" "USER_ID" \ + "$termux_core__tsev__scope_args"' "USER_ID"' 'r+="^(([0-9])|([1-9][0-9]{0,2}))$"' || return $? + done + + + + local -A test_types=() + test_types["scope-s"]='s="'"${TERMUX_ENV__S_ROOT}APP__"'"' + test_types["scope-ss"]='ss="'APP__'"' + test_types["component-name"]='cn="termux-app"' + for test_type in "${!test_types[@]}"; do + termux_core__tsev__scope_args="${test_types["$test_type"]}" + + # `TERMUX_APP__PACKAGE_NAME`. + termux_core__termux_scoped_env_variable__test__termux_app__package_name "termux-app-package-name_${test_type}_regex" \ + "APP__" "PACKAGE_NAME" \ + "$termux_core__tsev__scope_args"' "PACKAGE_NAME"' 'r+="^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)+$"' \ + "$TERMUX_CORE__TESTS__TERMUX__PACKAGE_NAME" \ + "false" "" || return $? + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + # shellcheck disable=SC2016 + termux_core__termux_scoped_env_variable__test__termux_app__package_name "termux-app-package-name_${test_type}_command" \ + "APP__" "PACKAGE_NAME" \ + "$termux_core__tsev__scope_args"' "PACKAGE_NAME"' 'c+="is_valid_android_app_package_name"' \ + "$TERMUX_CORE__TESTS__TERMUX__PACKAGE_NAME" \ + "true" 'is_valid_android_app_package_name() { printf "%s\n" "${1:-}" | grep -qE '\''^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)+$'\'' && [ "${#1}" -le 255 ]; }' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + done + + + + local -A test_types=() + test_types["scope-s"]='s="'"${TERMUX_ENV__S_ROOT}EXEC__"'"' + test_types["scope-ss"]='ss="'EXEC__'"' + test_types["component-name"]='cn="termux-exec"' + for test_type in "${!test_types[@]}"; do + termux_core__tsev__scope_args="${test_types["$test_type"]}" + + # `TERMUX_EXEC__PROC_SELF_EXE`. + # We cannot use `PROC_SELF_EXE` itself as `libtermux-exec` may modify/unset. + termux_core__termux_scoped_env_variable__test__termux_exec__proc_self_exe "termux-exec-proc-self-exe_${test_type}" \ + "EXEC__" "PROC_SELF_EXE_TEST" \ + "$termux_core__tsev__scope_args"' "PROC_SELF_EXE_TEST"' 'r+="^(()|((/[^/]+)+))$"' \ + "$TERMUX_CORE__TESTS__TERMUX__PREFIX/bin/foo" || return $? + done + + return 0 + +} + + + +termux_core__bash__termux_scoped_env_variable__test__usage() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 3 termux_core__bash__termux_scoped_env_variable__test__usage "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + + local env_command + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_regex-match-positive_env-valid" \ + "${env_command}" \ + 0 "^foo$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' r+='^foo|bar$'" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_regex-match-positive_env-invalid" \ + "${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' r+='^foo|bar$'" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_regex-match-positive_env-invalid_def-valid" \ + "${env_command}" \ + 0 "^bar$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' r+='^foo|bar$' bar" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_regex-match-positive_env-invalid_def-invalid" \ + "${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' r+='^foo|bar$' qux" || return $? + + + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_regex-match-negative_env-valid" \ + "${env_command}" \ + 0 "^baz$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' r-='^foo|bar$'" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_regex-match-negative_env-invalid" \ + "${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' r-='^foo|bar$'" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_regex-match-negative_env-invalid_def-valid" \ + "${env_command}" \ + 0 "^baz$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' r-='^foo|bar$' baz" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_regex-match-negative_env-invalid_def-invalid" \ + "${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' r-='^foo|bar$' bar" || return $? + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-quoted-arg_env-valid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + 0 "^foo$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable \"value\"'" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-quoted-arg_env-invalid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable \"value\"'" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-quoted-arg_env-invalid_def-valid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + 0 "^foo$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable \"value\"' foo" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-quoted-arg_env-invalid_def-invalid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable \"value\"' baz" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-arg_quoted-arg-type-invalid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable \"name\"'" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + return 0 + +} + + + +termux_core__bash__termux_scoped_env_variable__run_test() { + + termux_core__tests__validate_argument_count ge $# 5 termux_core__bash__termux_scoped_env_variable__run_test "$@" || return $? + + local test_name="$1" + local pre_test_commands="$2" + local expected_exit_code="$3" + local expected_output_regex="$4" + local termux_core__bash__tsev_command_type="$5" + + local termux_core__bash__tsev_call_command + local termux_core__bash__tsev_source_command + local termux_core__bash__tsev_source_output_mode_arg="" + local termux_core__bash__tsev_source_print_command="" + + if [[ "$termux_core__bash__tsev_command_type" == "get-name" ]] || \ + [[ "$termux_core__bash__tsev_command_type" == "get-value" ]]; then + termux_core__tests__validate_argument_count eq $# 7 \ + "'termux_core__bash__termux_scoped_env_variable__run_test' for the '$termux_core__bash__tsev_command_type' command" "$@" || return $? + + local termux_core__bash__tsev_source_output_mode="$6" + local termux_core__bash__tsev_args="$7" + + if [[ "$termux_core__bash__tsev_source_output_mode" == "_" ]]; then + termux_core__bash__tsev_source_output_mode="output_variable" + fi + + termux_core__bash__tsev_source_output_mode_arg=" '$termux_core__bash__tsev_source_output_mode'" + + if [[ "$termux_core__bash__tsev_source_output_mode" != "-" ]] && \ + [[ "$termux_core__bash__tsev_source_output_mode" != ">" ]]; then + termux_core__bash__tsev_source_print_command="${NL}printf '%s' \"\${$termux_core__bash__tsev_source_output_mode:-}\"" + fi + elif [[ "$termux_core__bash__tsev_command_type" == "set-value" ]] || \ + [[ "$termux_core__bash__tsev_command_type" == "unset-value" ]]; then + termux_core__tests__validate_argument_count eq $# 7 \ + "'termux_core__bash__termux_scoped_env_variable__run_test' for the '$termux_core__bash__tsev_command_type' command" "$@" || return $? + + local termux_core__bash__tsev_source_print_var="$6" + local termux_core__bash__tsev_args="$7" + + if [[ "$termux_core__bash__tsev_source_print_var" != "-" ]]; then + termux_core__bash__tsev_source_print_command="${NL}printf '%s' \"\${$termux_core__bash__tsev_source_print_var:-}\"" + fi + else + local termux_core__bash__tsev_args="" + fi + + + pre_test_commands="export TERMUX_ENV__S_ROOT='${TERMUX_ENV__S_ROOT//\'/\'\\\'\'}'${NL}${NL}${pre_test_commands:+"$pre_test_commands${NL}${NL}"}" + + # shellcheck disable=SC2016 + termux_core__bash__tsev_source_command='source_file_from_path() { local source_file="${1:-}"; [ $# -gt 0 ] && shift 1; local source_path; if source_path="$(command -v "$source_file")" && [ -n "$source_path" ]; then source "$source_path" || return $?; else echo "Failed to find the '\''$source_file'\'' file to source." 1>&2; return 1; fi; }'"${NL}${NL}"' +source_file_from_path "termux-scoped-env-variable.bash" || exit $?' + + if [[ "$termux_core__bash__tsev_command_type" == "get-name" ]] || \ + [[ "$termux_core__bash__tsev_command_type" == "get-value" ]]; then + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST:-}" != "true" ]]; then + termux_core__bash__tsev_call_command="termux-scoped-env-variable.bash" + termux_core__tests__run_script_test "termux_core__bash__tsev__${test_name}__exec-stdout" \ + "#!/bin/bash${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__bash__tsev_call_command '$termux_core__bash__tsev_command_type' '>' $termux_core__bash__tsev_args" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST:-}" != "true" ]]; then + termux_core__bash__tsev_call_command="${termux_core__bash__tsev_source_command}${NL}${NL}termux_core__bash__termux_scoped_env_variable" + termux_core__tests__run_script_test "termux_core__bash__tsev__${test_name}__source-stdout" \ + "#!/bin/bash${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__bash__tsev_call_command '$termux_core__bash__tsev_command_type' '>' $termux_core__bash__tsev_args" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + fi + + if [[ "${TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_VAR_TEST:-}" != "true" ]]; then + termux_core__bash__tsev_call_command="${termux_core__bash__tsev_source_command}${NL}${NL}termux_core__bash__termux_scoped_env_variable" + termux_core__tests__run_script_test "termux_core__bash__tsev__${test_name}__source-var" \ + "#!/bin/bash${NL}${NL}set -e -u${NL}${NL}${pre_test_commands}$termux_core__bash__tsev_call_command '$termux_core__bash__tsev_command_type'$termux_core__bash__tsev_source_output_mode_arg $termux_core__bash__tsev_args || exit \$?$termux_core__bash__tsev_source_print_command" \ + "$expected_exit_code" "$expected_output_regex" || return $? + fi + + return 0 + +} + + + + + +termux_core__termux_scoped_env_variable__test__usage() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 3 termux_core__termux_scoped_env_variable__test__usage "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + + local env_command + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_command_unset" \ + "" \ + 64 "^The command '' passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + "" - "" || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_command_invalid" \ + "" \ + 64 "^The command 'get-value1' passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + get-value1 - "" || return $? + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_command_args_invalid-1" \ + "" \ + 64 "^Invalid argument count 1 for the 'get-name' command\. The \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' function expects 3 arguments\..*$" \ + get-name - "" || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_command_args_invalid-less" \ + "" \ + 64 "^Invalid argument count 2 for the 'get-name' command\. The \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' function expects 3 arguments\..*$" \ + get-name - '2' || return $? + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_command_args_invalid-1" \ + "" \ + 64 "^Invalid argument count 1 for the 'get-value' command\. The \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' function expects minimum 4 arguments\..*$" \ + get-value - "" || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_command_args_invalid-less" \ + "" \ + 64 "^Invalid argument count 3 for the 'get-value' command\. The \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' function expects minimum 4 arguments\..*$" \ + get-value - '2 3' || return $? + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_set-value_command_args_invalid-1" \ + "" \ + 64 "^Invalid argument count 1 for the 'set-value' command\. The \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' function expects 3 arguments\..*$" \ + set-value - "1" || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_set-value_command_args_invalid-less" \ + "" \ + 64 "^Invalid argument count 2 for the 'set-value' command\. The \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' function expects 3 arguments\..*$" \ + set-value - '1 2' || return $? + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_unset-value_command_args_invalid-less" \ + "" \ + 64 "^Invalid argument count 1 for the 'unset-value' command\. The \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' function expects 2 arguments\..*$" \ + unset-value - "1" || return $? + + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_none" \ + "" \ + 64 "^The output_mode '' argument passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name, or equal to \`>\` or \`-\`\..*$" \ + get-value "" 'ss="_" "TEST" "*"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_invalid" \ + "" \ + 64 "^The output_mode '1foo' argument passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name, or equal to \`>\` or \`-\`\..*$" \ + get-value "1foo" 'ss="_" "TEST" "*"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_var-to-set_output-mode-stdout_value-valid" \ + "" \ + 0 "^${TERMUX_ENV__S_ROOT}_TEST$" \ + get-name ">" 'ss="_" "TEST"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "_" "TEST" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_output-mode-stdout_value-valid" \ + "$env_command" \ + 0 "^foo$" \ + get-value ">" 'ss="_" "TEST" "*"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_var-to-set_output-mode-dash_value-valid" \ + "" \ + 0 "^$" \ + get-name "-" 'ss="_" "TEST"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "_" "TEST" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_output-mode-dash_value-valid" \ + "$env_command" \ + 0 "^$" \ + get-value "-" 'ss="_" "TEST" "*"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "_" "TEST" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_var-to-set_var-already-set_value-set" \ + "$env_command" \ + 0 "^${TERMUX_ENV__S_ROOT}_TEST$" \ + get-name "${TERMUX_ENV__S_ROOT}_TEST" 'ss="_" "TEST"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "_" "TEST" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_var-to-set_var-already-set_value-unset" \ + "$env_command" \ + 0 "^${TERMUX_ENV__S_ROOT}_TEST$" \ + get-name "${TERMUX_ENV__S_ROOT}_TEST" 'ss="_" "TEST"' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "_" "TEST" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_var-already-set_value-set" \ + "$env_command${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"foo\" ]; }" \ + 0 "^foo$" \ + get-value "${TERMUX_ENV__S_ROOT}_TEST" 'ss="_" "TEST" c+="is_valid_scoped_variable" foo' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "_" "TEST" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_var-to-set_var-already-set_value-unset" \ + "$env_command${NL}${NL}is_valid_scoped_variable() { [ -z \"\$1\" ] || [ \"\$1\" = \"foo\" ]; }" \ + 0 "^$" \ + get-value "${TERMUX_ENV__S_ROOT}_TEST" 'ss="_" "TEST" c+="is_valid_scoped_variable" ""' || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST TERMUX_CORE__TESTS__TEST_FUNC__SKIP_SOURCE_STDOUT_TEST + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_scope_invalid-with-no-arg" \ + "" \ + 64 "^The scoped_var_scope_mode 't=' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + get-name _ 't= "TEST"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_scope_invalid-with-arg" \ + "" \ + 64 "^The scoped_var_scope_mode 't=_' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + get-name _ 't="_" "TEST"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_scope_none" \ + "" \ + 64 "^The scoped_var_scope_mode argument for the variable to get passed for the 'get-name' command to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not set\..*$" \ + get-name _ '"" "TEST"' || return $? + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope_none-with-no-def" \ + "" \ + 81 "^Failed to find a valid value.*$" \ + get-value _ '"" "TEST" "?"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope_none-with-def" \ + "" \ + 0 "^foo$" \ + get-value _ '"" "TEST" "?" "foo"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope_invalid-with-no-arg" \ + "" \ + 64 "^The scoped_var_scope_mode 't=' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + get-value _ 't= "TEST" "*"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope_invalid-with-arg" \ + "" \ + 64 "^The scoped_var_scope_mode 't=_' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + get-value _ 't="_" "TEST" "*"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_component-name_invalid-with-no-arg" \ + "" \ + 64 "^The scoped_var_scope_mode 'cn=' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + get-value _ 'cn= "TEST" "*"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_component-name_unsupported-app" \ + "" \ + 64 "^The scoped_var_scope_mode 'cn=bar-app' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + get-value _ 'cn="bar-app" "TEST" "*"' || return $? + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_set-value_scope_invalid-with-no-arg" \ + "" \ + 64 "^The scoped_var_scope_mode 't=' argument for the variable to set passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + set-value - 't= "TEST" "foo"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_set-value_scope_invalid-with-arg" \ + "" \ + 64 "^The scoped_var_scope_mode 't=_' argument for the variable to set passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + set-value - 't="_" "TEST" "foo"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_set-value_scope_none" \ + "" \ + 64 "^The scoped_var_scope_mode argument for the variable to set passed for the 'set-value' command to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not set\..*$" \ + set-value - '"" "TEST" "foo"' || return $? + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_unset-value_scope_invalid-with-no-arg" \ + "" \ + 64 "^The scoped_var_scope_mode 't=' argument for the variable to unset passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + unset-value - 't= "TEST"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_unset-value_scope_invalid-with-arg" \ + "" \ + 64 "^The scoped_var_scope_mode 't=_' argument for the variable to unset passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + unset-value - 't="_" "TEST"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_unset-value_scope_none" \ + "" \ + 64 "^The scoped_var_scope_mode argument for the variable to unset passed for the 'unset-value' command to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not set\..*$" \ + unset-value - '"" "TEST"' || return $? + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-s_no-arg" \ + "" \ + 64 "^The scoped_var_scope_mode 's=' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + get-value _ 's="" "TEST" "*"' || return $? + + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "1" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-s_var-root-scope-invalid" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ 's="_" "TEST" "*"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-s_var-sub-scope-invalid" \ + "$env_command" \ + 64 "^The name of the variable to get '#TEST' generated in \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name\..*$" \ + get-value _ 's="#" "TEST" "*"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-s_var-sub-name-invalid" \ + "$env_command" \ + 64 "^The name of the variable to get '_#TEST' generated in \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name\..*$" \ + get-value _ 's="_" "#TEST" "*"' || return $? + + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + append_export_scoped_variable_command env_command "BAR_" "_" "VAR" "bar" || return $? + append_export_scoped_variable_command env_command "BAZ_" "_" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-s_custom-set" \ + "$env_command" \ + 0 "^baz$" \ + get-value _ 's="BAZ__" "VAR" "*"' || return $? + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_no-arg" \ + "" \ + 64 "^The scoped_var_scope_mode 'ss=' argument for the variable to get passed to \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not valid\..*$" \ + get-value _ 'ss="" "TEST" "*"' || return $? + + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "1" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_var-root-scope-invalid" \ + "$env_command" \ + 1 "^The TERMUX_ENV__S_ROOT environment variable value '1' while running \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name\..*$" \ + get-value _ 'ss="_" "TEST" "*"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_var-sub-scope-invalid" \ + "$env_command" \ + 64 "^The name of the variable to get 'BAR_#TEST' generated in \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name\..*$" \ + get-value _ 'ss="#" "TEST" "*"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_var-sub-name-invalid" \ + "$env_command" \ + 64 "^The name of the variable to get 'BAR__#TEST' generated in \ +'termux_core__([a-z_]+)__termux_scoped_env_variable' is not a valid environment variable name\..*$" \ + get-value _ 'ss="_" "#TEST" "*"' || return $? + + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + append_export_scoped_variable_command env_command "BAR_" "_" "VAR" "bar" || return $? + append_export_scoped_variable_command env_command "BAZ_" "_" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_custom-set" \ + "$env_command" \ + 0 "^bar$" \ + get-value _ 'ss="_" "VAR" "*"' || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + append_export_scoped_variable_command env_command "BAR_" "_" "VAR" "bar" || return $? + append_export_scoped_variable_command env_command "BAZ_" "_" "VAR" "baz" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_scope-ss_custom-unset" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ 'ss="FOO_SUB__" "VAR" "*"' || return $? + + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_invalid-with-no-arg" \ + "" \ + 64 "^The validator command 'unknown_func' \ +not found while running 'termux_core__([a-z_]+)__termux_scoped_env_variable' \ +that is set in the ([a-z_]+)_validator 'c\+=unknown_func' argument\..*$" \ + get-value _ 'ss="'"$var_sub_scope_name"'" "TEST" c+="unknown_func"' || return $? + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_invalid-with-arg" \ + "" \ + 64 "^The validator command 'unknown_func' \ +not found while running 'termux_core__([a-z_]+)__termux_scoped_env_variable' \ +that is set in the ([a-z_]+)_validator 'c\+=unknown_func arg1' argument\..*$" \ + get-value _ 'ss="'"$var_sub_scope_name"'" "TEST" c+="unknown_func arg1"' || return $? + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_no-command-arg_env-valid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"foo\" ]; }" \ + 0 "^foo$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable'" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_no-command-arg_env-invalid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"foo\" ]; }" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable'" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_no-command-arg_env-invalid_def-valid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"foo\" ]; }" \ + 0 "^foo$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable' foo" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_no-command-arg_env-invalid_def-invalid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"foo\" ]; }" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable' baz" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-arg_env-valid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + 0 "^foo$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable value'" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-arg_env-invalid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable value'" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-arg_env-invalid_def-valid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + 0 "^foo$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable value' foo" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-arg_env-invalid_def-invalid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable value' baz" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + + TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST="true" + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "bar" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_validator-command_with-command-arg_arg-type-invalid" \ + "${env_command}${NL}${NL}is_valid_scoped_variable() { [ \"\$1\" = \"value\" ] && [ \"\$2\" = \"foo\" ]; }" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "ss='$var_sub_scope_name' '$var_sub_name' c+='is_valid_scoped_variable name'" || return $? + unset TERMUX_CORE__TESTS__TEST_FUNC__SKIP_EXEC_TEST + + + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_set-value_scope-s_empty" \ + "$env_command" \ + 0 "^$" \ + set-value "${TERMUX_ENV__S_ROOT}${var_sub_scope_name}${var_sub_name}" "s='${TERMUX_ENV__S_ROOT}${var_sub_scope_name}' '$var_sub_name' ''" || return $? + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_set-value_scope-s_set" \ + "$env_command" \ + 0 "^foo$" \ + set-value "${TERMUX_ENV__S_ROOT}${var_sub_scope_name}${var_sub_name}" "s='${TERMUX_ENV__S_ROOT}${var_sub_scope_name}' '$var_sub_name' foo" || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + append_unset_scoped_variable_command env_command "BAR_" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_set-value_custom-scope-s_set" \ + "$env_command" \ + 0 "^bar$" \ + set-value "BAR_${var_sub_scope_name}${var_sub_name}" "s='BAR_${var_sub_scope_name}' '$var_sub_name' bar" || return $? + + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_set-value_scope-ss_empty" \ + "$env_command" \ + 0 "^$" \ + set-value "${TERMUX_ENV__S_ROOT}${var_sub_scope_name}${var_sub_name}" "ss='$var_sub_scope_name' '$var_sub_name' ''" || return $? + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_set-value_scope-ss_set" \ + "$env_command" \ + 0 "^foo$" \ + set-value "${TERMUX_ENV__S_ROOT}${var_sub_scope_name}${var_sub_name}" "ss='$var_sub_scope_name' '$var_sub_name' foo" || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + append_unset_scoped_variable_command env_command "BAR_" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_set-value_custom-scope-ss_set" \ + "$env_command" \ + 0 "^bar$" \ + set-value "BAR_${var_sub_scope_name}${var_sub_name}" "ss='${var_sub_scope_name}' '$var_sub_name' bar" || return $? + + + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_unset-value_scope-s_unset" \ + "$env_command" \ + 0 "^$" \ + unset-value "${TERMUX_ENV__S_ROOT}${var_sub_scope_name}${var_sub_name}" "s='${TERMUX_ENV__S_ROOT}${var_sub_scope_name}' '$var_sub_name'" || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + append_unset_scoped_variable_command env_command "BAR_" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_unset-value_custom-scope-s_unset" \ + "$env_command" \ + 0 "^$" \ + unset-value "BAR_${var_sub_scope_name}${var_sub_name}" "s='BAR_${var_sub_scope_name}' '$var_sub_name'" || return $? + + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_unset-value_scope-ss_empty" \ + "$env_command" \ + 0 "^$" \ + unset-value "${TERMUX_ENV__S_ROOT}${var_sub_scope_name}${var_sub_name}" "ss='$var_sub_scope_name' '$var_sub_name'" || return $? + + get_export_scoped_variable_command env_command "TERMUX_" "ENV__" "S_ROOT" "BAR_" || return $? + append_unset_scoped_variable_command env_command "BAR_" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_unset-value_custom-scope-ss_unset" \ + "$env_command" \ + 0 "^$" \ + unset-value "BAR_${var_sub_scope_name}${var_sub_name}" "ss='${var_sub_scope_name}' '$var_sub_name'" || return $? + + return 0 + +} + + + +termux_core__termux_scoped_env_variable__test__termux_string_defined() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 5 termux_core__termux_scoped_env_variable__test__termux_string_defined "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + local termux_core__tsev__scope_args="$4" + local termux_core__tsev__validator_and_values_args="$5" + + local env_command + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-unset" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-empty" \ + "$env_command" \ + 0 "^$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-set" \ + "$env_command" \ + 0 "^foo$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + return 0 + +} + +termux_core__termux_scoped_env_variable__test__termux_string_set() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 5 termux_core__termux_scoped_env_variable__test__termux_string_set "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + local termux_core__tsev__scope_args="$4" + local termux_core__tsev__validator_and_values_args="$5" + + local env_command + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_valid" \ + "" \ + 0 "^${TERMUX_ENV__S_ROOT}${var_sub_scope_name}${var_sub_name}$" \ + get-name _ "${termux_core__tsev__scope_args}" || return $? + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-unset" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-empty" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-set" \ + "$env_command" \ + 0 "^foo$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + return 0 + +} + +termux_core__termux_scoped_env_variable__test__termux__package_name() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 8 termux_core__termux_scoped_env_variable__test__termux__package_name "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + local termux_core__tsev__scope_args="$4" + local termux_core__tsev__validator_and_values_args="$5" + local termux__package_name="$6" + local run_length_tests="$7" + local pre_test_commands="$8" + + local env_command + local termux__package_name_escaped + + termux__package_name_escaped="$(termux_core__tests__escape_string_for_regex "$termux__package_name")" || return $? + pre_test_commands="${pre_test_commands:+"$pre_test_commands${NL}${NL}"}" + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_valid" \ + "${pre_test_commands}" \ + 0 "^${TERMUX_ENV__S_ROOT}${var_sub_scope_name}${var_sub_name}$" \ + get-name _ "${termux_core__tsev__scope_args}" || return $? + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-unset" \ + "${pre_test_commands}${env_command}" \ + 0 "^$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-empty" \ + "${pre_test_commands}${env_command}" \ + 0 "^$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "$termux__package_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid-chars" \ + "${pre_test_commands}${env_command}" \ + 0 "^${termux__package_name_escaped}$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "#$termux__package_name#" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid-chars" \ + "${pre_test_commands}${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + if [[ "$run_length_tests" == "true" ]]; then + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo.$(printf 'x%.0s' {1..251})" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid-length" \ + "${pre_test_commands}${env_command}" \ + 0 "^foo\.$(printf 'x%.0s' {1..251})$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo.$(printf 'x%.0s' {1..252})" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid-length" \ + "${pre_test_commands}${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + fi + + return 0 + +} + +termux_core__termux_scoped_env_variable__test__termux_app__package_name() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 8 termux_core__termux_scoped_env_variable__test__termux_app__package_name "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + local termux_core__tsev__scope_args="$4" + local termux_core__tsev__validator_and_values_args="$5" + local termux__package_name="$6" + local run_length_tests="$7" + local pre_test_commands="$8" + + local env_command + local termux__package_name_escaped + + termux__package_name_escaped="$(termux_core__tests__escape_string_for_regex "$termux__package_name")" || return $? + pre_test_commands="${pre_test_commands:+"$pre_test_commands${NL}${NL}"}" + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_valid" \ + "${pre_test_commands}" \ + 0 "^${TERMUX_ENV__S_ROOT}${var_sub_scope_name}${var_sub_name}$" \ + get-name _ "${termux_core__tsev__scope_args}" || return $? + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-unset" \ + "${pre_test_commands}${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-empty" \ + "${pre_test_commands}${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "$termux__package_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid-chars" \ + "${pre_test_commands}${env_command}" \ + 0 "^${termux__package_name_escaped}$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" @"TERMUX_APP__PACKAGE_NAME"@ || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid-chars" \ + "${pre_test_commands}${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + if [[ "$run_length_tests" == "true" ]]; then + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo.$(printf 'x%.0s' {1..251})" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid-length" \ + "${pre_test_commands}${env_command}" \ + 0 "^foo\.$(printf 'x%.0s' {1..251})$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "foo.$(printf 'x%.0s' {1..252})" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid-length" \ + "${pre_test_commands}${env_command}" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + fi + + return 0 + +} + + + +termux_core__termux_scoped_env_variable__test__termux_path() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 6 termux_core__termux_scoped_env_variable__test__termux_path "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + local termux_core__tsev__scope_args="$4" + local termux_core__tsev__validator_and_values_args="$5" + local termux_path="$6" + + local env_command + local termux_path_escaped + local termux_path_prefix + local termux_path_prefix_escaped + + termux_path_escaped="$(termux_core__tests__escape_string_for_regex "$termux_path")" || return $? + + if [[ "$termux_path" != "/" ]]; then + termux_path_prefix="$termux_path" + termux_path_prefix_escaped="$(termux_core__tests__escape_string_for_regex "$termux_path_prefix")" || return $? + else + termux_path_prefix="" + termux_path_prefix_escaped="" + fi + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_valid" \ + "" \ + 0 "^${TERMUX_ENV__S_ROOT}${var_sub_scope_name}${var_sub_name}$" \ + get-name _ "${termux_core__tsev__scope_args}" || return $? + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-unset_def-none" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "$termux_path" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid_def-none" \ + "$env_command" \ + 0 "^${termux_path_escaped}$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" ".$termux_path" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid_def-none" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-unset_def-valid" \ + "$env_command" \ + 0 "^${termux_path_escaped}$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}"' "'"${termux_path}"'"' || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" ".$termux_path" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid_def-valid" \ + "$env_command" \ + 0 "^${termux_path_prefix_escaped}/def$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}"' "'"${termux_path_prefix}/def"'"' || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" ".$termux_path" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid_def-invalid" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}"' "'".${termux_path_prefix}/def"'"' || return $? + + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-unset_def1-valid_def2-valid" \ + "$env_command" \ + 0 "^${termux_path_prefix_escaped}/def1$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}"' "'"${termux_path_prefix}/def1"'" "'"${termux_path_prefix}/def2"'"' || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "$termux_path" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid_def1-valid_def2-valid" \ + "$env_command" \ + 0 "^${termux_path_escaped}$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}"' "'"${termux_path_prefix}/def1"'" "'"${termux_path_prefix}/def2"'"' || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" ".$termux_path" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid_def1-valid_def2-valid" \ + "$env_command" \ + 0 "^${termux_path_prefix_escaped}/def1$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}"' "'"${termux_path_prefix}/def1"'" "'"${termux_path_prefix}/def2"'"' || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" ".$termux_path" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid_def1-invalid_def2-valid" \ + "$env_command" \ + 0 "^${termux_path_prefix_escaped}/def2$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}"' "'".${termux_path_prefix}/def1"'" "'"${termux_path_prefix}/def2"'"' || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" ".$termux_path" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid_def1-invalid_def2-invalid" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}"' "'".${termux_path_prefix}/def1"'" ".'"${termux_path_prefix}/def2"'"' || return $? + + return 0 + +} + + +termux_core__termux_scoped_env_variable__test__termux__user_id() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 5 termux_core__termux_scoped_env_variable__test__termux__user_id "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + local termux_core__tsev__scope_args="$4" + local termux_core__tsev__validator_and_values_args="$5" + + local env_command + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_get-name_valid" \ + "" \ + 0 "^${TERMUX_ENV__S_ROOT}${var_sub_scope_name}${var_sub_name}$" \ + get-name _ "${termux_core__tsev__scope_args}" || return $? + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-unset" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-empty" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid-0" \ + "$env_command" \ + 0 "^0$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "1" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid-1" \ + "$env_command" \ + 0 "^1$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "9" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid-9" \ + "$env_command" \ + 0 "^9$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "10" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid-10" \ + "$env_command" \ + 0 "^10$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "11" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid-11" \ + "$env_command" \ + 0 "^11$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "99" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid-99" \ + "$env_command" \ + 0 "^99$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "999" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid-999" \ + "$env_command" \ + 0 "^999$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "-0" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid--0" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "01" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid-01" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "1000" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid-1000" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "10000" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid-10000" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "uuu" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid-uuu" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-unset_def1-valid" \ + "$env_command" \ + 0 "^0$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args} 0" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-empty_def1-valid" \ + "$env_command" \ + 0 "^0$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args} 0" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "10000" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid-10000_def1-valid" \ + "$env_command" \ + 0 "^0$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args} 0" || return $? + + return 0 + +} + + +termux_core__termux_scoped_env_variable__test__termux_exec__proc_self_exe() { + + termux_core__tests__log 4 "${1}()" + + termux_core__tests__validate_argument_count eq $# 6 termux_core__termux_scoped_env_variable__test__termux_exec__proc_self_exe "$@" || return $? + + local test_label="$1" + local var_sub_scope_name="$2" + local var_sub_name="$3" + local termux_core__tsev__scope_args="$4" + local termux_core__tsev__validator_and_values_args="$5" + local termux_executable_path="$6" + + local env_command + local termux_executable_path_escaped + + termux_executable_path_escaped="$(termux_core__tests__escape_string_for_regex "$termux_executable_path")" || return $? + + + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-name_valid" \ + "" \ + 0 "^${TERMUX_ENV__S_ROOT}${var_sub_scope_name}${var_sub_name}$" \ + get-name _ "${termux_core__tsev__scope_args}" || return $? + + get_unset_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-unset" \ + "$env_command" \ + 0 "^$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-empty" \ + "$env_command" \ + 0 "^$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "$termux_executable_path" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid-executable-path" \ + "$env_command" \ + 0 "^${termux_executable_path_escaped}$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" ".$termux_executable_path" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid-executable-path" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "/" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid-android-rootfs" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "//" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-invalid-android-rootfs-duplicate-path-sep" \ + "$env_command" \ + 81 "^Failed to find a valid value .*$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + get_export_scoped_variable_command env_command "$TERMUX_ENV__S_ROOT" "$var_sub_scope_name" "$var_sub_name" "/foo" || return $? + "$TERMUX_CORE__TESTS__TEST_FUNC__FUNC_NAME" "${test_label}_get-value_env-valid-android-rootfs-subpath" \ + "$env_command" \ + 0 "^/foo$" \ + get-value _ "${termux_core__tsev__scope_args} ${termux_core__tsev__validator_and_values_args}" || return $? + + return 0 + +} diff --git a/app/main/tests/scripts/termux/shell/command/environment/termux-shell-command-environment_runtime-script-tests.in b/app/main/tests/scripts/termux/shell/command/environment/termux-shell-command-environment_runtime-script-tests.in new file mode 100644 index 0000000..c549277 --- /dev/null +++ b/app/main/tests/scripts/termux/shell/command/environment/termux-shell-command-environment_runtime-script-tests.in @@ -0,0 +1,103 @@ +#!@TERMUX__PREFIX@/bin/bash +# shellcheck shell=bash + +## +# `TermuxShellCommandEnvioronment_runTests` +## +TermuxShellCommandEnvioronment_runTests() { + + termux_core__tests__log 3 "TermuxShellCommandEnvioronment_runTests()" + + if [[ "$TERMUX_CORE__TESTS__SKIP_TERMUX_CORE_ENV_VARIABLE_TESTS" != "true" ]]; then + local tests_start_time; tests_start_time="$(date "+%s")" || return $? + + local tests_count__old_value="$TERMUX_CORE__TESTS__TESTS_COUNT" + TERMUX_CORE__TESTS__TESTS_COUNT="0" + + test__termux_scoped_env_variable || return $? + test__termux_apps_info_env_variable || return $? + test__termux_apps_info_app_version_name || return $? + + termux_core__tests__log 3 "All $TERMUX_CORE__TESTS__TESTS_COUNT 'termux_scooed_env' tests successful in \ +$(termux_core__tests__print_elapsed_time "$tests_start_time")" + + TERMUX_CORE__TESTS__TESTS_COUNT="$((tests_count__old_value + TERMUX_CORE__TESTS__TESTS_COUNT))" + fi + + return 0 +} + + + + + +## +# `test__termux_scoped_env_variable` +## +test__termux_scoped_env_variable() { + + ( + # shellcheck source=app/main/tests/scripts/termux/shell/command/environment/termux-scoped-env-variable_runtime-script-tests.in + termux_core__tests__source_file_from_path \ + "$TERMUX_CORE__TESTS__TESTS_PATH/app/main/scripts/termux/shell/command/environment/termux-scoped-env-variable_runtime-script-tests" || exit $? + termux_core__termux_scoped_env_variable__run_tests || exit $? + ) || return $? + + return 0 + +} + +## +# `test__termux_apps_info_env_variable` +## +test__termux_apps_info_env_variable() { + + ( + # shellcheck source=app/main/tests/scripts/termux/shell/command/environment/termux-apps-info-env-variable_runtime-script-tests.in + termux_core__tests__source_file_from_path \ + "$TERMUX_CORE__TESTS__TESTS_PATH/app/main/scripts/termux/shell/command/environment/termux-apps-info-env-variable_runtime-script-tests" || exit $? + termux_core__termux_apps_info_env_variable__run_tests || exit $? + ) || return $? + + return 0 + +} + +## +# `test__termux_apps_info_app_version_name` +## +test__termux_apps_info_app_version_name() { + + ( + # shellcheck source=app/main/tests/scripts/termux/shell/command/environment/termux-apps-info-app-version-name_runtime-script-tests.in + termux_core__tests__source_file_from_path \ + "$TERMUX_CORE__TESTS__TESTS_PATH/app/main/scripts/termux/shell/command/environment/termux-apps-info-app-version-name_runtime-script-tests" || exit $? + termux_core__termux_apps_info_app_version_name__run_tests || exit $? + ) || return $? + + return 0 + +} + + +termux_core__set_termux_core_test_scoped_env_variables__default() { + + TERMUX_CORE__TESTS__TERMUX_ENV__S_ROOT="$TERMUX_ENV__S_ROOT" + TERMUX_CORE__TESTS__TERMUX__PACKAGE_NAME="$TERMUX_APP__PACKAGE_NAME" + TERMUX_CORE__TESTS__TERMUX_APP__DATA_DIR="$TERMUX_APP__DATA_DIR" + TERMUX_CORE__TESTS__TERMUX__ROOTFS="$TERMUX__ROOTFS" + TERMUX_CORE__TESTS__TERMUX__HOME="$TERMUX__HOME" + TERMUX_CORE__TESTS__TERMUX__PREFIX="$TERMUX__PREFIX" + +} + +termux_core__set_termux_core_test_scoped_env_variables__foo() { + + TERMUX_CORE__TESTS__TERMUX_ENV__S_ROOT="FOO_" + TERMUX_CORE__TESTS__TERMUX__PACKAGE_NAME="com.foo" + TERMUX_CORE__TESTS__TERMUX_APP__DATA_DIR="/data/data/com.foo" + TERMUX_CORE__TESTS__TERMUX__ROOTFS="$TERMUX_CORE__TESTS__TERMUX_APP__DATA_DIR/termux/rootfs/0" + TERMUX_CORE__TESTS__TERMUX__HOME="$TERMUX_CORE__TESTS__TERMUX__ROOTFS/home" + TERMUX_CORE__TESTS__TERMUX__PREFIX="$TERMUX_CORE__TESTS__TERMUX__ROOTFS/usr" + +} diff --git a/app/main/tests/termux-core-main-app_tests.in b/app/main/tests/termux-core-main-app_tests.in new file mode 100644 index 0000000..c68f3b3 --- /dev/null +++ b/app/main/tests/termux-core-main-app_tests.in @@ -0,0 +1,75 @@ +#!@TERMUX__PREFIX@/bin/bash +# shellcheck shell=bash + +## +# `termux_core_main_app__tests__main` +## +termux_core_main_app__tests__main() { + + TERMUX_CORE__TESTS__LOG_TAG="@TERMUX__LNAME@-core-main-app.tests" + + termux_core__tests__log 4 "main()" + + # Set `TERMUX_CORE__TESTS__TESTS_PATH` used by compiled c tests. + if [[ ! "$TERMUX_CORE__TESTS__TESTS_PATH" =~ $TERMUX_CORE__TESTS__REGEX__ROOTFS_OR_ABSOLUTE_PATH ]]; then + 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" + return 1 + fi + + + # Run runtime tests. + if [[ "$TERMUX_CORE__TESTS__RUN_RUNTIME_TESTS" == "true" ]]; then + termux_core_main_app__runtime_tests__run_command || return $? + fi + + return 0 + +} + + + +## +# `termux_core_main_app__runtime_tests__run_command` +## +termux_core_main_app__runtime_tests__run_command() { + + termux_core__tests__log 4 "Running 'runtime' tests" + + ( + termux_core_main_app__runtime_script_tests__run_command || exit $? + ) || return $? + + return 0 + +} + +## +# `termux_core_main_app__runtime_script_tests__run_command` +## +termux_core_main_app__runtime_script_tests__run_command() { + + local return_value + + local tests_start_time; tests_start_time="$(date "+%s")" || return $? + + termux_core__tests__log 1 "Running 'termux-core-main-app_runtime-script-tests'" + + ( + # shellcheck source=app/main/tests/scripts/termux-core-main-app_runtime-script-tests.in + termux_core__tests__source_file_from_path \ + "$TERMUX_CORE__TESTS__TESTS_PATH/app/main/scripts/termux-core-main-app_runtime-script-tests" || exit $? + + termux_core_main_app__runtime_script_tests__main + ) + return_value=$? + if [ $return_value -ne 0 ]; then + termux_core__tests__log_error "'termux-core-main-app_runtime-script-tests' failed" + return $return_value + fi + + termux_core__tests__log 2 "'termux-core-main-app_runtime-script-tests' completed in \ +$(termux_core__tests__print_elapsed_time "$tests_start_time")" + + return 0 + +} diff --git a/app/main/tests/termux-core-tests.in b/app/main/tests/termux-core-tests.in new file mode 100644 index 0000000..f44ee19 --- /dev/null +++ b/app/main/tests/termux-core-tests.in @@ -0,0 +1,1018 @@ +#!@TERMUX__PREFIX@/bin/bash +# shellcheck shell=bash + +if [ -z "${BASH_VERSION:-}" ]; then + echo "The 'termux-core-tests' script must be run from a 'bash' shell."; return 64 2>/dev/null|| exit 64 # EX__USAGE +fi + +termux_core__tests__init() { + +TERMUX_CORE__TESTS__LOG_LEVEL___N="@TERMUX_ENV__S_TERMUX_CORE__TESTS@LOG_LEVEL" +termux_core__tests__copy_variable TERMUX_CORE__TESTS__LOG_LEVEL "$TERMUX_CORE__TESTS__LOG_LEVEL___N" || return $? + +TERMUX_CORE__TESTS__MAX_LOG_LEVEL=5 # Default: `5` (VVVERBOSE=5) +{ [[ ! "${TERMUX_CORE__TESTS__LOG_LEVEL:-}" =~ ^[0-9]+$ ]] || [[ "$TERMUX_CORE__TESTS__LOG_LEVEL" -gt "$TERMUX_CORE__TESTS__MAX_LOG_LEVEL" ]]; } && \ +TERMUX_CORE__TESTS__LOG_LEVEL=1 # Default: `1` (OFF=0, NORMAL=1, DEBUG=2, VERBOSE=3, VVERBOSE=4 and VVVERBOSE=5) +TERMUX_CORE__TESTS__LOG_TAG="" # Default: `` + +TERMUX_CORE__TESTS__COMMAND_TYPE_ID="" # Default: `` +TERMUX_CORE__TESTS__COMMAND_TYPE_NOOP="false" # Default: `false` + +NL=$'\n' + +TERMUX_CORE__TESTS__DETECT_LEAKS=0 # Default: `0` +TERMUX_CORE__TESTS__USE_FSANITIZE_BUILDS="false" # Default: `false` +TERMUX_CORE__TESTS__NO_CLEAN="false" # Default: `false` +[[ ! "${TERMUX_CORE__TESTS__SKIP_TERMUX_CORE_ENV_VARIABLE_TESTS:-}" =~ ^true|false$ ]] && \ +TERMUX_CORE__TESTS__SKIP_TERMUX_CORE_ENV_VARIABLE_TESTS="false" # Default: `false` +TERMUX_CORE__TESTS__TEST_NAMES_FILTER="" +TERMUX_CORE__TESTS__TEST_PACKAGES_FILTER="^.*$" # Default: `^.*$` + +TERMUX_CORE__TESTS__TESTS_COUNT="0" +TERMUX_CORE__TESTS__LOG_EMPTY_LINE_AFTER_SCRIPT_TEST="false" + +TERMUX_CORE__TESTS__NAME_MAX=255 +TERMUX_CORE__TESTS__REGEX__ABSOLUTE_PATH='^(/[^/]+)+$' +TERMUX_CORE__TESTS__REGEX__ROOTFS_OR_ABSOLUTE_PATH='^((/)|((/[^/]+)+))$' +TERMUX_CORE__TESTS__REGEX__APP_PACKAGE_NAME="^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)+$" +TERMUX_CORE__TESTS__REGEX__UNSIGNED_INT='^[0-9]+$' + + + +# Set `TERMUX_*` variables to environment variables exported by +# Termux app, otherwise default to build time placeholders. +# This is done to support scoped and dynamic variables design. +# The `TERMUX_ENV__*` variables still use build time placeholders. + +TERMUX_ENV__S_ROOT="@TERMUX_ENV__S_ROOT@" + + +TERMUX_APP__NAME___N="@TERMUX_ENV__S_TERMUX_APP@NAME" +termux_core__tests__copy_variable TERMUX_APP__NAME "$TERMUX_APP__NAME___N" || return $? +[[ -z "$TERMUX_APP__NAME" ]] && \ +TERMUX_APP__NAME="@TERMUX_APP__NAME@" + +TERMUX_APP__PACKAGE_NAME___N="@TERMUX_ENV__S_TERMUX_APP@PACKAGE_NAME" +termux_core__tests__copy_variable TERMUX_APP__PACKAGE_NAME "$TERMUX_APP__PACKAGE_NAME___N" || return $? +{ [[ ! "$TERMUX_APP__PACKAGE_NAME" =~ $TERMUX_CORE__TESTS__REGEX__APP_PACKAGE_NAME ]] || \ + [ "${#TERMUX_APP__PACKAGE_NAME}" -gt $TERMUX_CORE__TESTS__NAME_MAX ]; } && \ +TERMUX_APP__PACKAGE_NAME="@TERMUX_APP__PACKAGE_NAME@" + +TERMUX_APP__DATA_DIR___N="@TERMUX_ENV__S_TERMUX_APP@DATA_DIR" +termux_core__tests__copy_variable TERMUX_APP__DATA_DIR "$TERMUX_APP__DATA_DIR___N" || return $? +[[ ! "$TERMUX_APP__DATA_DIR" =~ $TERMUX_CORE__TESTS__REGEX__ABSOLUTE_PATH ]] && \ +TERMUX_APP__DATA_DIR="@TERMUX_APP__DATA_DIR@" + + +TERMUX__ROOTFS___N="@TERMUX_ENV__S_TERMUX@ROOTFS" +termux_core__tests__copy_variable TERMUX__ROOTFS "$TERMUX__ROOTFS___N" || return $? +[[ ! "$TERMUX__ROOTFS" =~ $TERMUX_CORE__TESTS__REGEX__ROOTFS_OR_ABSOLUTE_PATH ]] && \ +TERMUX__ROOTFS="@TERMUX__ROOTFS@" + +TERMUX__HOME___N="@TERMUX_ENV__S_TERMUX@HOME" +termux_core__tests__copy_variable TERMUX__HOME "$TERMUX__HOME___N" || return $? +[[ ! "$TERMUX__HOME" =~ $TERMUX_CORE__TESTS__REGEX__ABSOLUTE_PATH ]] && \ +TERMUX__HOME="@TERMUX__HOME@" + +TERMUX__PREFIX___N="@TERMUX_ENV__S_TERMUX@PREFIX" +termux_core__tests__copy_variable TERMUX__PREFIX "$TERMUX__PREFIX___N" || return $? +[[ ! "$TERMUX__PREFIX" =~ $TERMUX_CORE__TESTS__REGEX__ABSOLUTE_PATH ]] && \ +TERMUX__PREFIX="@TERMUX__PREFIX@" + + +TERMUX_EXEC__TESTS__LOG_LEVEL___N="@TERMUX_ENV__S_TERMUX_EXEC__TESTS@LOG_LEVEL" +termux_core__tests__copy_variable TERMUX_EXEC__TESTS__LOG_LEVEL "$TERMUX_EXEC__TESTS__LOG_LEVEL___N" || return $? + + +TERMUX_CORE__TESTS__TESTS_PATH___N="@TERMUX_ENV__S_TERMUX_CORE__TESTS@TESTS_PATH" +TERMUX_CORE__TESTS__TESTS_PATH="$TERMUX__PREFIX/libexec/installed-tests/termux-core" +printf -v "$TERMUX_CORE__TESTS__TESTS_PATH___N" "%s" "$TERMUX_CORE__TESTS__TESTS_PATH" || return $? +export "${TERMUX_CORE__TESTS__TESTS_PATH___N?}" || return $? + + +TERMUX__USER_ID___N="@TERMUX_ENV__S_TERMUX@USER_ID" +termux_core__tests__copy_variable TERMUX__USER_ID "$TERMUX__USER_ID___N" || return $? +TERMUX__USER_ID="${TERMUX__USER_ID:-0}" + + +TERMUX_CORE__APPS_INFO_ENV_FILE___N="@TERMUX_ENV__S_TERMUX_CORE@APPS_INFO_ENV_FILE" +termux_core__tests__copy_variable TERMUX_CORE__APPS_INFO_ENV_FILE "$TERMUX_CORE__APPS_INFO_ENV_FILE___N" || return $? + + + +# Set exit traps. +termux_core__tests__set_traps || return $? + +} + + + +function termux_core__tests__log() { local log_level="${1}"; shift; if [[ $TERMUX_CORE__TESTS__LOG_LEVEL -ge $log_level ]]; then echo "${TERMUX_CORE__TESTS__LOG_TAG:-"@TERMUX__LNAME@-core.tests"}:" "$@"; fi } +function termux_core__tests__log_literal() { local log_level="${1}"; shift; if [[ $TERMUX_CORE__TESTS__LOG_LEVEL -ge $log_level ]]; then echo -e "${TERMUX_CORE__TESTS__LOG_TAG:-"@TERMUX__LNAME@-core.tests"}:" "$@"; fi } +function termux_core__tests__log_error() { echo "${TERMUX_CORE__TESTS__LOG_TAG:-"@TERMUX__LNAME@-core.tests"}:" "$@" 1>&2; } + + +## +# `termux_core__tests__main` [``] +## +termux_core__tests__main() { + + local return_value + + termux_core__tests__init || return $? + + TERMUX_CORE__TESTS__RUN_UNIT_TESTS="false" + TERMUX_CORE__TESTS__RUN_RUNTIME_TESTS="false" + + # Process the command arguments passed to the script. + termux_core__tests__process_script_arguments "$@" || return $? + if [ "$TERMUX_CORE__TESTS__COMMAND_TYPE_NOOP" = "true" ]; then return 0; fi + + + termux_core__tests__log 4 "Running 'termux_core__tests__main'" + + if [[ "$TERMUX_CORE__TESTS__COMMAND_TYPE_ID" == *,* ]] || \ + [[ ",unit,runtime,all," != *",$TERMUX_CORE__TESTS__COMMAND_TYPE_ID,"* ]]; then + termux_core__tests__log_error "Invalid command type id '$TERMUX_CORE__TESTS__COMMAND_TYPE_ID' passed. Must equal 'unit', 'runtime' or 'all'." + return 1 + fi + + + termux_core__tests__log 1 "Running 'termux-core' tests" + + local tests_start_time; tests_start_time="$(date "+%s")" || return $? + + [[ ",unit,all," == *",$TERMUX_CORE__TESTS__COMMAND_TYPE_ID,"* ]] && TERMUX_CORE__TESTS__RUN_UNIT_TESTS="true" + [[ ",runtime,all," == *",$TERMUX_CORE__TESTS__COMMAND_TYPE_ID,"* ]] && TERMUX_CORE__TESTS__RUN_RUNTIME_TESTS="true" + + termux_core__tests__log 5 "$TERMUX_CORE__TESTS__LOG_LEVEL___N='$TERMUX_CORE__TESTS__LOG_LEVEL'" + [[ -n "$TERMUX_CORE__TESTS__TEST_NAMES_FILTER" ]] && termux_core__tests__log 5 "$TERMUX_CORE__TESTS__TEST_NAMES_FILTER='$TERMUX_CORE__TESTS__TEST_NAMES_FILTER'" + termux_core__tests__log 5 "$TERMUX_CORE__TESTS__TESTS_PATH___N='$TERMUX_CORE__TESTS__TESTS_PATH'" + termux_core__tests__log 5 "$TERMUX__USER_ID___N='$TERMUX__USER_ID'" + + + # Set `TERMUX_CORE__TESTS__TESTS_PATH. + if [[ ! "$TERMUX_CORE__TESTS__TESTS_PATH" =~ $TERMUX_CORE__TESTS__REGEX__ROOTFS_OR_ABSOLUTE_PATH ]]; then + 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" + return 1 + fi + + + TERMUX_CORE__TESTS__IS_RUNNING_ON_ANDROID="false" + TERMUX_CORE__TESTS__IS_RUNNING_IN_TERMUX="false" + if [ -f "/system/bin/app_process" ]; then + TERMUX_CORE__TESTS__IS_RUNNING_ON_ANDROID="true" + [ -x "$TERMUX__ROOTFS" ] && TERMUX_CORE__TESTS__IS_RUNNING_IN_TERMUX="true" + fi + + + # Setup variables for runtime tests. + if [[ "$TERMUX_CORE__TESTS__RUN_RUNTIME_TESTS" == "true" ]]; then + if [[ "$TERMUX_CORE__TESTS__IS_RUNNING_IN_TERMUX" != "true" ]]; then + termux_core__tests__log_error "The TERMUX__ROOTFS '$TERMUX__ROOTFS' path not found or is not executable. " + termux_core__tests__log_error "Runtime tests must be run from $TERMUX_APP__NAME app in Android." + return 1 + fi + + + if [[ ! "$TERMUX__USER_ID" =~ ^(([0-9])|([1-9][0-9]{0,2}))$ ]]; then + termux_core__tests__log_error "The TERMUX__USER_ID '$TERMUX__USER_ID' is not set to a valid user id." + return 1 + fi + + + TERMUX_CORE__TESTS__UID="$(id -u)" + return_value=$? + if [ $return_value -ne 0 ]; then + termux_core__tests__log_error "Failed to get uid" + return $return_value + fi + + + ANDROID__BUILD_VERSION_SDK="$(getprop "ro.build.version.sdk")" + if [[ ! "$ANDROID__BUILD_VERSION_SDK" =~ $TERMUX_CORE__TESTS__REGEX__UNSIGNED_INT ]]; then + termux_core__tests__log_error "Failed to get android build version sdk with getprop" + return 1 + fi + + + if [[ ! -d "$TMPDIR" ]]; then + termux_core__tests__log_error "The TMPDIR '$TMPDIR' is either not set or not a directory" + return 1 + fi + + + TERMUX_CORE__TESTS__TMPDIR_PATH="$TMPDIR/termux-core-tests" + + # Ensure test directory is clean and does not contain files from previous run. + rm -rf "$TERMUX_CORE__TESTS__TMPDIR_PATH" || return $? + mkdir -p "$TERMUX_CORE__TESTS__TMPDIR_PATH" || return $? + + # Setup temp directory for exec tests. + # DO NOT modify unless its ensured that no test requires the specified directory hierarchy. + TERMUX_CORE__TESTS__EXEC_TMPDIR_PATH="$TERMUX_CORE__TESTS__TMPDIR_PATH/exec" + mkdir -p "$TERMUX_CORE__TESTS__EXEC_TMPDIR_PATH" || return $? + + TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_NAME="test-script" + TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_PATH="$TERMUX_CORE__TESTS__EXEC_TMPDIR_PATH/$TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_NAME" + fi + + + + # Run tests. + if [[ "termux-core" =~ $TERMUX_CORE__TESTS__TEST_PACKAGES_FILTER ]]; then + termux_core__libtermux_core__nos__c__tests__run_command || return $? + termux_core__termux_core_main_app__tests__run_command || return $? + fi + + termux_core__termux_external_packages__tests__run_command || return $? + + + + termux_core__tests__log 1 "All 'termux-core' tests successful in \ +$(termux_core__tests__print_elapsed_time "$tests_start_time")" + + return 0 + +} + + + +## +# `termux_core__libtermux_core__nos__c__tests__run_command` +## +termux_core__libtermux_core__nos__c__tests__run_command() { + + local return_value + + local tests_start_time; tests_start_time="$(date "+%s")" || return $? + + termux_core__tests__log 1 "Running 'libtermux-core_nos_c_tre_tests'" + + ( + # shellcheck source=lib/termux-core_nos_c_tre/tests/libtermux-core_nos_c_tre_tests.in + termux_core__tests__source_file_from_path \ + "$TERMUX_CORE__TESTS__TESTS_PATH/lib/termux-core_nos_c_tre/libtermux-core_nos_c_tre_tests" || exit $? + + libtermux_core__nos__c__tests__main + ) + return_value=$? + if [ $return_value -ne 0 ]; then + termux_core__tests__log_error "'libtermux-core_nos_c_tre_tests' failed" + return $return_value + fi + + termux_core__tests__log 2 "'libtermux-core_nos_c_tre_tests' completed in \ +$(termux_core__tests__print_elapsed_time "$tests_start_time")" + + return 0 + +} + + + +## +# `termux_core__termux_core_main_app__tests__run_command` +## +termux_core__termux_core_main_app__tests__run_command() { + + local return_value + + local tests_start_time; tests_start_time="$(date "+%s")" || return $? + + termux_core__tests__log 1 "Running 'termux-core-main-app_tests'" + + ( + # shellcheck source=app/main/tests/termux-core-main-app_tests.in + termux_core__tests__source_file_from_path \ + "$TERMUX_CORE__TESTS__TESTS_PATH/app/main/termux-core-main-app_tests" || exit $? + + termux_core_main_app__tests__main + ) + return_value=$? + if [ $return_value -ne 0 ]; then + termux_core__tests__log_error "'termux-core-main-app_tests' failed" + return $return_value + fi + + termux_core__tests__log 2 "'termux-core-main-app_tests' completed in \ +$(termux_core__tests__print_elapsed_time "$tests_start_time")" + + return 0 + +} + + + +## +# `termux_core__termux_external_packages__tests__run_command` +## +termux_core__termux_external_packages__tests__run_command() { + + local return_value + + local tests_start_time; tests_start_time="$(date "+%s")" || return $? + + termux_core__tests__log 1 "Running 'termux-external-packages_tests'" + + ( + # shellcheck source=app/main/tests/termux-external-packages_tests.in + termux_core__tests__source_file_from_path \ + "$TERMUX_CORE__TESTS__TESTS_PATH/app/main/termux-external-packages_tests" || exit $? + + termux_external_packages__tests__main + ) + return_value=$? + if [ $return_value -ne 0 ]; then + termux_core__tests__log_error "'termux-external-packages_tests' failed" + return $return_value + fi + + termux_core__tests__log 2 "'termux-external-packages_tests' completed in \ +$(termux_core__tests__print_elapsed_time "$tests_start_time")" + + return 0 + +} + + + + + +## +# `termux_core__tests__run_script_test` `` [``] \ +# `` +# `` `` \ +# [``] +## +termux_core__tests__run_script_test() { + + local return_value + + local opt; local opt_arg; local OPTARG; local OPTIND; + + local test_file_set_executable="true" + local working_directory="." + local execution_path="" + + if [[ $# -lt 1 ]]; then + termux_core__tests__log_error "Invalid argument count $#. The 'termux_core__tests__run_script_test' command expects at least 1 argument: \ + test_name" + return 1 + fi + + local test_name="$1" + shift 1 + + termux_core__tests__log 5 "$test_name()" + + # Parse options to main command. + while getopts ":-:" opt; do + opt_arg="${OPTARG:-}" + case "${opt}" in + -) + case "${OPTARG}" in *?=*) opt_arg="${OPTARG#*=}";; *) opt_arg="";; esac + case "${OPTARG}" in + no-test-file-set-executable) + test_file_set_executable="false" + ;; + working-dir=?*) + working_directory="$opt_arg" + ;; + working-dir | working-dir=) + termux_core__tests__log_error "No argument set for arg option '--${OPTARG%=*}'." + return 64 # EX__USAGE + ;; + executable-path=?*) + execution_path="$opt_arg" + ;; + executable-path | executable-path=) + termux_core__tests__log_error "No argument set for arg option '--${OPTARG%=*}'." + return 64 # EX__USAGE + ;; + '') + # End of options `--`. + break + ;; + *) + termux_core__tests__log_error "Unknown option: '--${OPTARG:-}'." + return 64 # EX__USAGE + ;; + esac + ;; + \?) + :;; + esac + done + shift $((OPTIND - 1)) # Remove already processed arguments from argument list + + + + if [[ $# -lt 3 ]]; then + termux_core__tests__log_error "Invalid argument count $#. The 'termux_core__tests__run_script_test' command expects at least 4 arguments: \ + test_name test_file_content expected_exit_code expected_output_regex [script_args]" + return 1 + fi + + local test_file_content="$1" + local expected_exit_code="$2" + local expected_output_regex="$3" + shift 3 # Remove args before `script_args` + + local output + local actual_output + local test_failed="false" + + if [[ -n "${TERMUX_CORE__TESTS__TEST_NAMES_FILTER:-}" ]] && [[ ! "$test_name" =~ $TERMUX_CORE__TESTS__TEST_NAMES_FILTER ]]; then + return 0 + fi + + if [[ "${TERMUX_CORE__TESTS__TESTS_COUNT:-}" =~ $TERMUX_CORE__TESTS__REGEX__UNSIGNED_INT ]]; then + TERMUX_CORE__TESTS__TESTS_COUNT=$((TERMUX_CORE__TESTS__TESTS_COUNT + 1)) + fi + + termux_core__tests__log 5 "test_file_path='$TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_PATH'" + if [[ "$test_file_content" == *"${NL}"* ]]; then + termux_core__tests__log 5 "test_file_content=${NL}"'```'"${NL}$test_file_content${NL}"'```' + else + termux_core__tests__log 5 "test_file_content='$test_file_content'" + fi + + if [[ "$test_file_set_executable" != "true" ]]; then + termux_core__tests__log 5 "test_file_set_executable='$test_file_set_executable'" + fi + if [[ "$working_directory" != "." ]]; then + termux_core__tests__log 5 "working_directory='$working_directory'" + fi + if [[ -n "$execution_path" ]]; then + termux_core__tests__log 5 "execution_path='$execution_path'" + fi + + termux_core__tests__log 5 "expected_exit_code='$expected_exit_code'" + termux_core__tests__log 5 "expected_output_regex='$expected_output_regex'" + + # If TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_PATH is not a valid absolute path. + if [[ ! "$TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_PATH" =~ $TERMUX_CORE__TESTS__REGEX__ABSOLUTE_PATH ]]; then + termux_core__tests__log_error "The TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_PATH '$TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_PATH' is not a valid absolute path" + return 1 + fi + + rm -f "$TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_PATH" || return $? + + output="$(printf "%s" "$test_file_content" > "$TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_PATH" 2>&1)" + return_value=$? + if [ $return_value -ne 0 ]; then + printf "%s\n" "$output" 1>&2 + termux_core__tests__log_error "Failed to create the '$TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_PATH' file for the '$test_name' test" + return $return_value + fi + + if [[ "$test_file_set_executable" == "true" ]]; then + output="$(chmod +x "$TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_PATH" 2>&1)" + return_value=$? + if [ $return_value -ne 0 ]; then + printf "%s\n" "$output" 1>&2 + termux_core__tests__log_error "Failed to set the executable bit for the '$TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_PATH' file for the '$test_name' test" + return $return_value + fi + fi + + actual_output="$(cd "$working_directory" && "${execution_path:-$TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_PATH}" "$@" 2>&1)" + actual_exit_code=$? + if [[ -n "$expected_output_regex" ]] && [[ ! "$actual_output" =~ $expected_output_regex ]]; then + termux_core__tests__log_error "FAILED: '$test_name' test" + termux_core__tests__log_error "Expected output_regex does not equal match actual output" + test_failed="true" + elif [ $actual_exit_code != "$expected_exit_code" ]; then + termux_core__tests__log_error "$actual_output" + termux_core__tests__log_error "FAILED: '$test_name' test" + termux_core__tests__log_error "Expected result_code does not equal actual result_code" + test_failed="true" + fi + + if [[ "$test_failed" == "true" ]]; then + if [[ "$test_file_content" == *"${NL}"* ]]; then + termux_core__tests__log_error "test_file_content=${NL}"'```'"${NL}$test_file_content${NL}"'```' + else + termux_core__tests__log_error "test_file_content='$test_file_content'" + fi + termux_core__tests__log_error "actual_exit_code: '$actual_exit_code'" + termux_core__tests__log_error "expected_exit_code: '$expected_exit_code'" + termux_core__tests__log_error "actual_output: '$actual_output'" + termux_core__tests__log_error "expected_output_regex: '$expected_output_regex'" + return 100 + else + #termux_core__tests__log 2 "PASSED" + + # Remove test file so that later tests do not accidentally use it. + rm -f "$TERMUX_CORE__TESTS__SCRIPT_TEST_FILE_PATH" || return $? + + if [[ "$TERMUX_CORE__TESTS__LOG_EMPTY_LINE_AFTER_SCRIPT_TEST" == "true" ]]; then + termux_core__tests__log 5 "" + fi + + return 0 + fi + +} + + + + + +## +# `get_export_scoped_variable_command` `` \ +# `` `` `` \ +# `` +## +get_export_scoped_variable_command() { + + termux_core__tests__validate_argument_count eq $# 5 get_export_scoped_variable_command "$@" || return $? + + local output_variable_name="$1" + local var_root_scope_name="$2" + local var_sub_scope_name="$3" + local var_sub_name="$4" + local var_value="$5" + + printf -v "$output_variable_name" "%s" \ + "export ${var_root_scope_name}${var_sub_scope_name}${var_sub_name}='${var_value//\'/\'\\\'\'}';" || return $? + + return 0 + +} + +## +# `append_export_scoped_variable_command` `` \ +# `` `` `` \ +# `` +## +append_export_scoped_variable_command() { + + termux_core__tests__validate_argument_count eq $# 5 append_export_scoped_variable_command "$@" || return $? + + local output_variable_name="$1" + local var_root_scope_name="$2" + local var_sub_scope_name="$3" + local var_sub_name="$4" + local var_value="$5" + + local __command + + get_export_scoped_variable_command __command "$var_root_scope_name" "$var_sub_scope_name" "$var_sub_name" "$var_value" || return $? + + printf -v "$output_variable_name" "%s" \ + "${!output_variable_name:-}${NL}${__command}" || return $? + + return 0 + +} + + + +## +# `get_unset_scoped_variable_command` `` \ +# `` `` `` +## +get_unset_scoped_variable_command() { + + termux_core__tests__validate_argument_count eq $# 4 get_unset_scoped_variable_command "$@" || return $? + + local output_variable_name="$1" + local var_root_scope_name="$2" + local var_sub_scope_name="$3" + local var_sub_name="$4" + + printf -v "$output_variable_name" "%s" \ + "unset ${var_root_scope_name}${var_sub_scope_name}${var_sub_name};" || return $? + + return 0 + +} + +## +# `append_unset_scoped_variable_command` `` \ +# `` `` `` +## +append_unset_scoped_variable_command() { + + termux_core__tests__validate_argument_count eq $# 4 append_unset_scoped_variable_command "$@" || return $? + + local output_variable_name="$1" + local var_root_scope_name="$2" + local var_sub_scope_name="$3" + local var_sub_name="$4" + + local __command + + get_unset_scoped_variable_command __command "$var_root_scope_name" "$var_sub_scope_name" "$var_sub_name" || return $? + + printf -v "$output_variable_name" "%s" \ + "${!output_variable_name:-}${NL}${__command}" || return $? + + return 0 + +} + + + + + +## +# Source a file under `$PATH`, like under `TERMUX__PREFIX/bin`. +# +# A separate function is used to source so that arguments passed to +# calling script/function are not passed to the sourced script. +# +# +# termux_core__tests__source_file_from_path +## +termux_core__tests__source_file_from_path() { + + local source_file="${1:-}"; [ $# -gt 0 ] && shift 1; + + local source_path + + if source_path="$(command -v "$source_file")" && [ -n "$source_path" ]; then + # shellcheck disable=SC1090 + source "$source_path" || return $? + else + echo "Failed to find the '$source_file' file to source." 1>&2 + return 1 + fi + +} + + + + + +## +# Copy the value of a variable to another variable. +# +# +# **Parameters:** +# `output_variable_name` - The name of the output variable to set. +# `input_variable_name` - The name of the input variable to read. +# +# **Returns:** +# Returns `0` if successful, otherwise returns with a non-zero exit code. +# +# +# `termux_core__tests__copy_variable` `` `` +## +termux_core__tests__copy_variable() { + + local output_variable_name="${1:-}" + local input_variable_name="${2:-}" + + if [[ ! "$output_variable_name" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then + echo "The output_variable_name '$output_variable_name' is not a valid shell variable name while running 'termux_core__tests__copy_variable'." 1>&2 + return 1 + fi + + if [[ ! "$input_variable_name" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then + echo "The input_variable_name '$input_variable_name' is not a valid shell variable name while running 'termux_core__tests__copy_variable'." 1>&2 + return 1 + fi + + eval "$output_variable_name"=\"\$\{"$input_variable_name":-\}\" + +} + + + + + +## +# Escape '\$[](){}|^.?+*' in a string with backslashes so that it can +# be used as a literal string in regex. +# +# +# `termux_core__tests__escape_string_for_regex` `` +## +termux_core__tests__escape_string_for_regex() { + + printf "%s" "$1" | sed -zE -e 's/[][\.|$(){}?+*^]/\\&/g' + +} + + + + + +## +# `termux_core__tests__get_arguments_string` `` `` +## +termux_core__tests__get_arguments_string() { + + local output_variable_name="${1:-}" + shift 1 + + local argument + local __arguments_string="" + local i=1 + + while [ $# -ne 0 ]; do + argument="$1" + __arguments_string="$__arguments_string$i: \`$argument\`" + + if [ $# -gt 1 ]; then + __arguments_string="$__arguments_string$NL" + fi + + i=$((i + 1)) + shift + done + + printf -v "$output_variable_name" "%s" "$__arguments_string" || return $? + +} + +## +# `termux_core__tests__print_arguments_string` `` +## +termux_core__tests__print_arguments_string() { + + local arguments_string="" + termux_core__tests__get_arguments_string arguments_string "$@" || return $? + printf "%s\n" "$arguments_string" + +} + +## +# `termux_core__tests__validate_argument_count` `eq`|`lt`|`le`|`gt`|`ge` `` `` `