Files
nixpkgs/lib/tests/strings.sh
Ryan Hendrickson c5ba1a0b3a lib.escapeShellArg: handle paths
Path values should be copied into the store if they're going to be used
in a shell script. In other words, `"my-command ${arg}"` and
`"my-command ${lib.escapeShellArg arg}"` should always do the same thing
if `arg` is a path; it should not be the case that the first is safe and
the second results in an out-of-store reference.
2026-08-24 20:10:20 -04:00

71 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Tests lib/strings.nix
# Run:
# [nixpkgs]$ lib/tests/strings.sh
# or:
# [nixpkgs]$ nix-build lib/tests/release.nix
set -euo pipefail
shopt -s inherit_errexit
# Use
# || die
die() {
echo >&2 "test case failed: " "$@"
exit 1
}
if test -n "${TEST_LIB:-}"; then
nixpkgs="$(dirname "$TEST_LIB")"
else
nixpkgs="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.."; pwd)"
fi
export NIX_PATH=nixpkgs=$nixpkgs
work="$(mktemp -d)"
clean_up() {
rm -rf "$work"
}
trap clean_up EXIT
cd "$work"
expectSuccess() {
local expr=$1
local expectedResultRegex=$2
if ! result=$(nix-instantiate --eval --strict --json \
--expr "with (import <nixpkgs/lib>).strings; $expr" 2>/dev/null); then
die "$expr failed to evaluate, but it was expected to succeed"
fi
if [[ ! "$result" =~ $expectedResultRegex ]]; then
die "$expr == $result, but $expectedResultRegex was expected"
fi
}
expectFailure() {
local expr=$1
local expectedErrorRegex=$2
if result=$(nix-instantiate --eval --strict --json 2>"$work/stderr" \
--expr "with (import <nixpkgs/lib>).strings; $expr"); then
die "$expr evaluated successfully to $result, but it was expected to fail"
fi
if [[ ! "$(<"$work/stderr")" =~ $expectedErrorRegex ]]; then
die "Error was $(<"$work/stderr"), but $expectedErrorRegex was expected"
fi
}
expectFailure 'escapeShellArg /does-not-exist' "error: path '/does-not-exist' does not exist"
# Without this, there would be no need to escape the path.
export NIX_STORE_DIR="$work/store with spaces"
mkdir -p $NIX_STORE_DIR
# Check that escaping happens even with paths.
expectSuccess "escapeShellArg $nixpkgs/lib/tests/strings.sh" "'$NIX_STORE_DIR/.*'"
# Check that store paths are not recopied into the store.
expectSuccess "escapeShellArg (/. + replaceString \"'\" \"\" $result)" "$result"
export -n NIX_STORE_DIR
echo >&2 tests ok