tests.fetchurl: add flag-appending tests

This commit is contained in:
Yueh-Shun Li
2025-12-17 21:47:39 +08:00
parent 04419e1b96
commit dd7414ba7f
+74
View File
@@ -1,11 +1,85 @@
{
lib,
testers,
fetchurl,
writeShellScriptBin,
jq,
moreutils,
emptyFile,
...
}:
let
testFlagAppending =
args:
testers.invalidateFetcherByDrvHash
(fetchurl.override (previousArgs: {
curl = (
writeShellScriptBin "curl" ''
set -eu -o pipefail
hasFoo=
hasBar=
echo "curl-mock-expecting-flags: get flags: $*" >&2
for arg; do
case "$arg" in
-V|--version)
${lib.getExe previousArgs.curl} "$arg"
exit "$?"
;;
--foo)
echo "curl-mock-expecting-flags: \`--foo' found in the argument list passed to \`curl'." >&2
hasFoo=1
;;
--bar)
echo "curl-mock-expecting-flags: \`--bar' found in the argument list passed to \`curl'." >&2
hasBar=1
;;
esac
done
if [[ -z "$hasFoo" ]]; then
echo "ERROR: curl-mock-expecting-flags: \`--foo' missing in the argument list passed to \`curl'." >&2
fi
if [[ -z "$hasBar" ]]; then
echo "ERROR: curl-mock-expecting-flags: \`--bar' missing in the argument list passed to \`curl'." >&2
fi
if [[ -n "$hasFoo" ]] && [[ -n "$hasBar" ]]; then
touch $out
else
exit 1
fi
''
);
}))
(
{
url = "https://www.example.com/source";
hash = emptyFile.outputHash;
recursiveHash = true; # aligned with emptyFile
}
// args
);
in
{
flag-appending-curlOpts = testFlagAppending {
name = "test-fetchurl-flag-appending-curlOpts";
curlOpts = "--foo --bar";
};
flag-appending-curlOptsList = testFlagAppending {
name = "test-fetchurl-flag-appending-curlOptsList";
curlOptsList = [
"--foo"
"--bar"
];
};
flag-appending-netrcPhase-curlOpts = testFlagAppending {
name = "test-fetchurl-flag-appending-netrcPhase-curlOpts";
netrcPhase = ''
touch netrc
curlOpts="$curlOpts --foo --bar"
'';
};
# Tests that we can send custom headers with spaces in them
header =
let