mirror of
https://github.com/NixOS/nix
synced 2025-07-07 06:01:48 +02:00
Merge remote-tracking branch 'upstream/master' into overlayfs-store
This commit is contained in:
commit
c99c80f075
200 changed files with 6007 additions and 1158 deletions
|
@ -34,6 +34,21 @@ nix-build check.nix -A failed --argstr checkBuildId $checkBuildId \
|
|||
[ "$status" = "100" ]
|
||||
if checkBuildTempDirRemoved $TEST_ROOT/log; then false; fi
|
||||
|
||||
test_custom_build_dir() {
|
||||
local customBuildDir="$TEST_ROOT/custom-build-dir"
|
||||
|
||||
# Nix does not create the parent directories, and perhaps it shouldn't try to
|
||||
# decide the permissions of build-dir.
|
||||
mkdir "$customBuildDir"
|
||||
nix-build check.nix -A failed --argstr checkBuildId $checkBuildId \
|
||||
--no-out-link --keep-failed --option build-dir "$TEST_ROOT/custom-build-dir" 2> $TEST_ROOT/log || status=$?
|
||||
[ "$status" = "100" ]
|
||||
[[ 1 == "$(count "$customBuildDir/nix-build-"*)" ]]
|
||||
local buildDir="$customBuildDir/nix-build-"*
|
||||
grep $checkBuildId $buildDir/checkBuildId
|
||||
}
|
||||
test_custom_build_dir
|
||||
|
||||
nix-build check.nix -A deterministic --argstr checkBuildId $checkBuildId \
|
||||
--no-out-link 2> $TEST_ROOT/log
|
||||
checkBuildTempDirRemoved $TEST_ROOT/log
|
||||
|
|
45
tests/functional/chroot-store.sh
Normal file
45
tests/functional/chroot-store.sh
Normal file
|
@ -0,0 +1,45 @@
|
|||
source common.sh
|
||||
|
||||
echo example > $TEST_ROOT/example.txt
|
||||
mkdir -p $TEST_ROOT/x
|
||||
|
||||
export NIX_STORE_DIR=/nix2/store
|
||||
|
||||
CORRECT_PATH=$(cd $TEST_ROOT && nix-store --store ./x --add example.txt)
|
||||
|
||||
[[ $CORRECT_PATH =~ ^/nix2/store/.*-example.txt$ ]]
|
||||
|
||||
PATH1=$(cd $TEST_ROOT && nix path-info --store ./x $CORRECT_PATH)
|
||||
[ $CORRECT_PATH == $PATH1 ]
|
||||
|
||||
PATH2=$(nix path-info --store "$TEST_ROOT/x" $CORRECT_PATH)
|
||||
[ $CORRECT_PATH == $PATH2 ]
|
||||
|
||||
PATH3=$(nix path-info --store "local?root=$TEST_ROOT/x" $CORRECT_PATH)
|
||||
[ $CORRECT_PATH == $PATH3 ]
|
||||
|
||||
# Ensure store info trusted works with local store
|
||||
nix --store $TEST_ROOT/x store info --json | jq -e '.trusted'
|
||||
|
||||
# Test building in a chroot store.
|
||||
if canUseSandbox; then
|
||||
|
||||
flakeDir=$TEST_ROOT/flake
|
||||
mkdir -p $flakeDir
|
||||
|
||||
cat > $flakeDir/flake.nix <<EOF
|
||||
{
|
||||
outputs = inputs: rec {
|
||||
packages.$system.default = import ./simple.nix;
|
||||
};
|
||||
}
|
||||
EOF
|
||||
|
||||
cp simple.nix shell.nix simple.builder.sh config.nix $flakeDir/
|
||||
|
||||
outPath=$(nix build --print-out-paths --no-link --sandbox-paths '/nix? /bin? /lib? /lib64? /usr?' --store $TEST_ROOT/x path:$flakeDir)
|
||||
|
||||
[[ $outPath =~ ^/nix2/store/.*-simple$ ]]
|
||||
|
||||
[[ $(cat $TEST_ROOT/x/nix/store/$(basename $outPath)/hello) = 'Hello World!' ]]
|
||||
fi
|
|
@ -283,6 +283,11 @@ grepQuietInverse() {
|
|||
! grep "$@" > /dev/null
|
||||
}
|
||||
|
||||
# Return the number of arguments
|
||||
count() {
|
||||
echo $#
|
||||
}
|
||||
|
||||
trap onError ERR
|
||||
|
||||
fi # COMMON_VARS_AND_FUNCTIONS_SH_SOURCED
|
||||
|
|
|
@ -43,6 +43,16 @@ export NIX_USER_CONF_FILES=$here/config/nix-with-substituters.conf
|
|||
var=$(nix config show | grep '^substituters =' | cut -d '=' -f 2 | xargs)
|
||||
[[ $var == https://example.com ]]
|
||||
|
||||
# Test that we can include a file.
|
||||
export NIX_USER_CONF_FILES=$here/config/nix-with-include.conf
|
||||
var=$(nix config show | grep '^allowed-uris =' | cut -d '=' -f 2 | xargs)
|
||||
[[ $var == https://github.com/NixOS/nix ]]
|
||||
|
||||
# Test that we can !include a file.
|
||||
export NIX_USER_CONF_FILES=$here/config/nix-with-bang-include.conf
|
||||
var=$(nix config show | grep '^experimental-features =' | cut -d '=' -f 2 | xargs)
|
||||
[[ $var == nix-command ]]
|
||||
|
||||
# Test that it's possible to load config from the environment
|
||||
prev=$(nix config show | grep '^cores' | cut -d '=' -f 2 | xargs)
|
||||
export NIX_CONFIG="cores = 4242"$'\n'"experimental-features = nix-command flakes"
|
||||
|
@ -56,4 +66,4 @@ exp_features=$(nix config show | grep '^experimental-features' | cut -d '=' -f 2
|
|||
# Test that it's possible to retrieve a single setting's value
|
||||
val=$(nix config show | grep '^warn-dirty' | cut -d '=' -f 2 | xargs)
|
||||
val2=$(nix config show warn-dirty)
|
||||
[[ $val == $val2 ]]
|
||||
[[ $val == $val2 ]]
|
1
tests/functional/config/extra-config.conf
Normal file
1
tests/functional/config/extra-config.conf
Normal file
|
@ -0,0 +1 @@
|
|||
allowed-uris = https://github.com/NixOS/nix
|
2
tests/functional/config/nix-with-bang-include.conf
Normal file
2
tests/functional/config/nix-with-bang-include.conf
Normal file
|
@ -0,0 +1,2 @@
|
|||
experimental-features = nix-command
|
||||
!include ./missing-extra-config.conf
|
2
tests/functional/config/nix-with-include.conf
Normal file
2
tests/functional/config/nix-with-include.conf
Normal file
|
@ -0,0 +1,2 @@
|
|||
experimental-features = nix-command
|
||||
include ./extra-config.conf
|
|
@ -43,10 +43,18 @@ path0_=$(nix eval --impure --raw --expr "(builtins.fetchTree git+file://$TEST_RO
|
|||
export _NIX_FORCE_HTTP=1
|
||||
[[ $(tail -n 1 $path0/hello) = "hello" ]]
|
||||
|
||||
# Nuke the cache
|
||||
rm -rf $TEST_HOME/.cache/nix
|
||||
|
||||
# Fetch the default branch.
|
||||
path=$(nix eval --impure --raw --expr "(builtins.fetchGit file://$repo).outPath")
|
||||
[[ $(cat $path/hello) = world ]]
|
||||
|
||||
# Fetch when the cache has packed-refs
|
||||
# Regression test of #8822
|
||||
git -C $TEST_HOME/.cache/nix/gitv3/*/ pack-refs --all
|
||||
path=$(nix eval --impure --raw --expr "(builtins.fetchGit file://$repo).outPath")
|
||||
|
||||
# Fetch a rev from another branch
|
||||
git -C $repo checkout -b devtest
|
||||
echo "different file" >> $TEST_ROOT/git/differentbranch
|
||||
|
@ -251,6 +259,7 @@ path12=$(nix eval --impure --raw --expr "(builtins.fetchGit { url = file://$repo
|
|||
|
||||
# should fail if there is no repo
|
||||
rm -rf $repo/.git
|
||||
rm -rf $TEST_HOME/.cache/nix
|
||||
(! nix eval --impure --raw --expr "(builtins.fetchGit \"file://$repo\").outPath")
|
||||
|
||||
# should succeed for a repo without commits
|
||||
|
|
|
@ -56,6 +56,23 @@ cat > $flake1Dir/flake.nix <<EOF
|
|||
a12 = self.drvCall.outPath;
|
||||
|
||||
a13 = "\${self.drvCall.drvPath}\${self.drvCall.outPath}";
|
||||
|
||||
a14 = with import ./config.nix; let
|
||||
top = mkDerivation {
|
||||
name = "dot-installable";
|
||||
outputs = [ "foo" "out" ];
|
||||
meta.outputsToInstall = [ "out" ];
|
||||
buildCommand = ''
|
||||
mkdir \$foo \$out
|
||||
echo "foo" > \$foo/file
|
||||
echo "out" > \$out/file
|
||||
'';
|
||||
};
|
||||
in top // {
|
||||
foo = top.foo // {
|
||||
outputSpecified = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
EOF
|
||||
|
@ -94,3 +111,10 @@ nix build --json --out-link $TEST_ROOT/result $flake1Dir#a12
|
|||
|
||||
expectStderr 1 nix build --impure --json --out-link $TEST_ROOT/result $flake1Dir#a13 \
|
||||
| grepQuiet "has 2 entries in its context. It should only have exactly one entry"
|
||||
|
||||
# Test accessing output in installables with `.` (foobarbaz.<output>)
|
||||
nix build --json --no-link $flake1Dir#a14.foo | jq --exit-status '
|
||||
(.[0] |
|
||||
(.drvPath | match(".*dot-installable.drv")) and
|
||||
(.outputs | keys == ["foo"]))
|
||||
'
|
||||
|
|
|
@ -93,6 +93,24 @@ foo
|
|||
EOF
|
||||
chmod +x $nonFlakeDir/shebang-comments.sh
|
||||
|
||||
cat > $nonFlakeDir/shebang-different-comments.sh <<EOF
|
||||
#! $(type -P env) nix
|
||||
# some comments
|
||||
// some comments
|
||||
/* some comments
|
||||
* some comments
|
||||
\ some comments
|
||||
% some comments
|
||||
@ some comments
|
||||
-- some comments
|
||||
(* some comments
|
||||
#! nix --offline shell
|
||||
#! nix flake1#fooScript
|
||||
#! nix --no-write-lock-file --command cat
|
||||
foo
|
||||
EOF
|
||||
chmod +x $nonFlakeDir/shebang-different-comments.sh
|
||||
|
||||
cat > $nonFlakeDir/shebang-reject.sh <<EOF
|
||||
#! $(type -P env) nix
|
||||
# some comments
|
||||
|
@ -607,6 +625,7 @@ expectStderr 1 nix flake metadata "$flake2Dir" --no-allow-dirty --reference-lock
|
|||
[[ $($nonFlakeDir/shebang.sh) = "foo" ]]
|
||||
[[ $($nonFlakeDir/shebang.sh "bar") = "foo"$'\n'"bar" ]]
|
||||
[[ $($nonFlakeDir/shebang-comments.sh ) = "foo" ]]
|
||||
[[ "$($nonFlakeDir/shebang-different-comments.sh)" = "$(cat $nonFlakeDir/shebang-different-comments.sh)" ]]
|
||||
[[ $($nonFlakeDir/shebang-inline-expr.sh baz) = "foo"$'\n'"baz" ]]
|
||||
[[ $($nonFlakeDir/shebang-file.sh baz) = "foo"$'\n'"baz" ]]
|
||||
expect 1 $nonFlakeDir/shebang-reject.sh 2>&1 | grepQuiet -F 'error: unsupported unquoted character in nix shebang: *. Use double backticks to escape?'
|
||||
|
|
|
@ -6,8 +6,9 @@ git init "$repo"
|
|||
git -C "$repo" config user.email "you@example.com"
|
||||
git -C "$repo" config user.name "Your Name"
|
||||
|
||||
# Compare Nix's and git's implementation of git hashing
|
||||
try () {
|
||||
hash=$(nix hash path --mode git --format base16 --algo sha1 $TEST_ROOT/hash-path)
|
||||
local hash=$(nix hash path --mode git --format base16 --algo sha1 $TEST_ROOT/hash-path)
|
||||
[[ "$hash" == "$1" ]]
|
||||
|
||||
git -C "$repo" rm -rf hash-path || true
|
||||
|
@ -15,7 +16,7 @@ try () {
|
|||
git -C "$repo" add hash-path
|
||||
git -C "$repo" commit -m "x"
|
||||
git -C "$repo" status
|
||||
hash2=$(git -C "$TEST_ROOT/scratch" rev-parse HEAD:hash-path)
|
||||
local hash2=$(git -C "$TEST_ROOT/scratch" rev-parse HEAD:hash-path)
|
||||
[[ "$hash2" = "$1" ]]
|
||||
}
|
||||
|
||||
|
@ -32,36 +33,45 @@ echo "Run Hello World" > $TEST_ROOT/hash-path/executable
|
|||
chmod +x $TEST_ROOT/hash-path/executable
|
||||
try "e5c0a11a556801a5c9dcf330ca9d7e2c572697f4"
|
||||
|
||||
rm -rf $TEST_ROOT/dummy1
|
||||
echo Hello World! > $TEST_ROOT/dummy1
|
||||
path1=$(nix store add --mode git --hash-algo sha1 $TEST_ROOT/dummy1)
|
||||
hash1=$(nix-store -q --hash $path1)
|
||||
test "$hash1" = "sha256:1brffhvj2c0z6x8qismd43m0iy8dsgfmy10bgg9w11szway2wp9v"
|
||||
# Check Nix added object has matching git hash
|
||||
try2 () {
|
||||
local hashPath="$1"
|
||||
local expected="$2"
|
||||
|
||||
rm -rf $TEST_ROOT/dummy2
|
||||
mkdir -p $TEST_ROOT/dummy2
|
||||
echo Hello World! > $TEST_ROOT/dummy2/hello
|
||||
path2=$(nix store add --mode git --hash-algo sha1 $TEST_ROOT/dummy2)
|
||||
hash2=$(nix-store -q --hash $path2)
|
||||
test "$hash2" = "sha256:1vhv7zxam7x277q0y0jcypm7hwhccbzss81vkdgf0ww5sm2am4y0"
|
||||
local path=$(nix store add --mode git --hash-algo sha1 "$repo/$hashPath")
|
||||
|
||||
rm -rf $TEST_ROOT/dummy3
|
||||
mkdir -p $TEST_ROOT/dummy3
|
||||
mkdir -p $TEST_ROOT/dummy3/dir
|
||||
touch $TEST_ROOT/dummy3/dir/file
|
||||
echo Hello World! > $TEST_ROOT/dummy3/dir/file
|
||||
touch $TEST_ROOT/dummy3/dir/executable
|
||||
chmod +x $TEST_ROOT/dummy3/dir/executable
|
||||
echo Run Hello World! > $TEST_ROOT/dummy3/dir/executable
|
||||
path3=$(nix store add --mode git --hash-algo sha1 $TEST_ROOT/dummy3)
|
||||
hash3=$(nix-store -q --hash $path3)
|
||||
test "$hash3" = "sha256:08y3nm3mvn9qvskqnf13lfgax5lh73krxz4fcjd5cp202ggpw9nv"
|
||||
git -C "$repo" add "$hashPath"
|
||||
git -C "$repo" commit -m "x"
|
||||
git -C "$repo" status
|
||||
local hashFromGit=$(git -C "$repo" rev-parse "HEAD:$hashPath")
|
||||
[[ "$hashFromGit" == "$2" ]]
|
||||
|
||||
rm -rf $TEST_ROOT/dummy3
|
||||
mkdir -p $TEST_ROOT/dummy3
|
||||
mkdir -p $TEST_ROOT/dummy3/dir
|
||||
touch $TEST_ROOT/dummy3/dir/file
|
||||
ln -s './hello/world.txt' $TEST_ROOT/dummy3/dir/symlink
|
||||
path3=$(nix store add --mode git --hash-algo sha1 $TEST_ROOT/dummy3)
|
||||
hash3=$(nix-store -q --hash $path3)
|
||||
test "$hash3" = "sha256:1dwazas8irzpar89s8k2bnp72imfw7kgg4aflhhsfnicg8h428f3"
|
||||
local caFromNix=$(nix path-info --json "$path" | jq -r ".[] | .ca")
|
||||
[[ "fixed:git:sha1:$(nix hash convert --to nix32 "sha1:$hashFromGit")" = "$caFromNix" ]]
|
||||
}
|
||||
|
||||
rm -rf "$repo/dummy1"
|
||||
echo Hello World! > "$repo/dummy1"
|
||||
try2 dummy1 "980a0d5f19a64b4b30a87d4206aade58726b60e3"
|
||||
|
||||
rm -rf "$repo/dummy2"
|
||||
mkdir -p "$repo/dummy2"
|
||||
echo Hello World! > "$repo/dummy2/hello"
|
||||
try2 dummy2 "8b8e43b937854f4083ea56777821abda2799e850"
|
||||
|
||||
rm -rf "$repo/dummy3"
|
||||
mkdir -p "$repo/dummy3"
|
||||
mkdir -p "$repo/dummy3/dir"
|
||||
touch "$repo/dummy3/dir/file"
|
||||
echo Hello World! > "$repo/dummy3/dir/file"
|
||||
touch "$repo/dummy3/dir/executable"
|
||||
chmod +x "$repo/dummy3/dir/executable"
|
||||
echo Run Hello World! > "$repo/dummy3/dir/executable"
|
||||
try2 dummy3 "f227adfaf60d2778aabbf93df6dd061272d2dc85"
|
||||
|
||||
rm -rf "$repo/dummy4"
|
||||
mkdir -p "$repo/dummy4"
|
||||
mkdir -p "$repo/dummy4/dir"
|
||||
touch "$repo/dummy4/dir/file"
|
||||
ln -s './hello/world.txt' "$repo/dummy4/dir/symlink"
|
||||
try2 dummy4 "06f3e789820fc488d602358f03e3a1cbf993bf33"
|
||||
|
|
|
@ -68,8 +68,16 @@ done
|
|||
for i in lang/eval-fail-*.nix; do
|
||||
echo "evaluating $i (should fail)";
|
||||
i=$(basename "$i" .nix)
|
||||
flags="$(
|
||||
if [[ -e "lang/$i.flags" ]]; then
|
||||
sed -e 's/#.*//' < "lang/$i.flags"
|
||||
else
|
||||
# note that show-trace is also set by init.sh
|
||||
echo "--eval --strict --show-trace"
|
||||
fi
|
||||
)"
|
||||
if
|
||||
expectStderr 1 nix-instantiate --eval --strict --show-trace "lang/$i.nix" \
|
||||
expectStderr 1 nix-instantiate $flags "lang/$i.nix" \
|
||||
| sed "s!$(pwd)!/pwd!g" > "lang/$i.err"
|
||||
then
|
||||
diffAndAccept "$i" err err.exp
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
error:
|
||||
… while counting down; n = 10
|
||||
|
||||
… while counting down; n = 9
|
||||
|
||||
… while counting down; n = 8
|
||||
|
||||
… while counting down; n = 7
|
||||
|
||||
… while counting down; n = 6
|
||||
|
||||
… while counting down; n = 5
|
||||
|
||||
… while counting down; n = 4
|
||||
|
||||
… while counting down; n = 3
|
||||
|
||||
… while counting down; n = 2
|
||||
|
||||
… while counting down; n = 1
|
||||
|
||||
(stack trace truncated; use '--show-trace' to show the full, detailed trace)
|
||||
|
||||
error: kaboom
|
|
@ -0,0 +1 @@
|
|||
--eval --strict --no-show-trace
|
|
@ -0,0 +1,9 @@
|
|||
let
|
||||
countDown = n:
|
||||
if n == 0
|
||||
then throw "kaboom"
|
||||
else
|
||||
builtins.addErrorContext
|
||||
"while counting down; n = ${toString n}"
|
||||
("x" + countDown (n - 1));
|
||||
in countDown 10
|
1
tests/functional/lang/eval-okay-baseNameOf.exp
Normal file
1
tests/functional/lang/eval-okay-baseNameOf.exp
Normal file
|
@ -0,0 +1 @@
|
|||
"ok"
|
32
tests/functional/lang/eval-okay-baseNameOf.nix
Normal file
32
tests/functional/lang/eval-okay-baseNameOf.nix
Normal file
|
@ -0,0 +1,32 @@
|
|||
assert baseNameOf "" == "";
|
||||
assert baseNameOf "." == ".";
|
||||
assert baseNameOf ".." == "..";
|
||||
assert baseNameOf "a" == "a";
|
||||
assert baseNameOf "a." == "a.";
|
||||
assert baseNameOf "a.." == "a..";
|
||||
assert baseNameOf "a.b" == "a.b";
|
||||
assert baseNameOf "a.b." == "a.b.";
|
||||
assert baseNameOf "a.b.." == "a.b..";
|
||||
assert baseNameOf "a/" == "a";
|
||||
assert baseNameOf "a/." == ".";
|
||||
assert baseNameOf "a/.." == "..";
|
||||
assert baseNameOf "a/b" == "b";
|
||||
assert baseNameOf "a/b." == "b.";
|
||||
assert baseNameOf "a/b.." == "b..";
|
||||
assert baseNameOf "a/b/c" == "c";
|
||||
assert baseNameOf "a/b/c." == "c.";
|
||||
assert baseNameOf "a/b/c.." == "c..";
|
||||
assert baseNameOf "a/b/c/d" == "d";
|
||||
assert baseNameOf "a/b/c/d." == "d.";
|
||||
assert baseNameOf "a\\b" == "a\\b";
|
||||
assert baseNameOf "C:a" == "C:a";
|
||||
assert baseNameOf "a//b" == "b";
|
||||
|
||||
# It's been like this for close to a decade. We ought to commit to it.
|
||||
# https://github.com/NixOS/nix/pull/582#issuecomment-121014450
|
||||
assert baseNameOf "a//" == "";
|
||||
|
||||
assert baseNameOf ./foo == "foo";
|
||||
assert baseNameOf ./foo/bar == "bar";
|
||||
|
||||
"ok"
|
|
@ -1,22 +0,0 @@
|
|||
source common.sh
|
||||
|
||||
cd $TEST_ROOT
|
||||
|
||||
echo example > example.txt
|
||||
mkdir -p ./x
|
||||
|
||||
NIX_STORE_DIR=$TEST_ROOT/x
|
||||
|
||||
CORRECT_PATH=$(nix-store --store ./x --add example.txt)
|
||||
|
||||
PATH1=$(nix path-info --store ./x $CORRECT_PATH)
|
||||
[ $CORRECT_PATH == $PATH1 ]
|
||||
|
||||
PATH2=$(nix path-info --store "$PWD/x" $CORRECT_PATH)
|
||||
[ $CORRECT_PATH == $PATH2 ]
|
||||
|
||||
PATH3=$(nix path-info --store "local?root=$PWD/x" $CORRECT_PATH)
|
||||
[ $CORRECT_PATH == $PATH3 ]
|
||||
|
||||
# Ensure store info trusted works with local store
|
||||
nix --store ./x store info --json | jq -e '.trusted'
|
|
@ -83,7 +83,7 @@ nix_tests = \
|
|||
export.sh \
|
||||
config.sh \
|
||||
add.sh \
|
||||
local-store.sh \
|
||||
chroot-store.sh \
|
||||
filter-source.sh \
|
||||
misc.sh \
|
||||
dump-db.sh \
|
||||
|
|
|
@ -8,4 +8,4 @@ libplugintest_ALLOW_UNDEFINED := 1
|
|||
|
||||
libplugintest_EXCLUDE_FROM_LIBRARY_LIST := 1
|
||||
|
||||
libplugintest_CXXFLAGS := -I src/libutil -I src/libstore -I src/libexpr -I src/libfetchers
|
||||
libplugintest_CXXFLAGS := $(INCLUDE_libutil) $(INCLUDE_libstore) $(INCLUDE_libexpr) $(INCLUDE_libfetchers)
|
||||
|
|
|
@ -47,6 +47,9 @@ testRepl () {
|
|||
| grep "attribute 'currentSystem' missing"
|
||||
nix repl "${nixArgs[@]}" 2>&1 <<< "builtins.currentSystem" \
|
||||
| grep "$(nix-instantiate --eval -E 'builtins.currentSystem')"
|
||||
|
||||
expectStderr 1 nix repl ${testDir}/simple.nix \
|
||||
| grepQuiet -s "error: path '$testDir/simple.nix' is not a flake"
|
||||
}
|
||||
|
||||
# Simple test, try building a drv
|
||||
|
|
|
@ -8,7 +8,7 @@ test-libstoreconsumer_INSTALL_DIR :=
|
|||
test-libstoreconsumer_SOURCES := \
|
||||
$(wildcard $(d)/*.cc) \
|
||||
|
||||
test-libstoreconsumer_CXXFLAGS += -I src/libutil -I src/libstore
|
||||
test-libstoreconsumer_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libstore)
|
||||
|
||||
test-libstoreconsumer_LIBS = libstore libutil
|
||||
|
||||
|
|
|
@ -189,3 +189,9 @@ nix-env --set $outPath10
|
|||
[ "$(nix-store -q --resolve $profiles/test)" = $outPath10 ]
|
||||
nix-env --set $drvPath10
|
||||
[ "$(nix-store -q --resolve $profiles/test)" = $outPath10 ]
|
||||
|
||||
# Test the case where $HOME contains a symlink.
|
||||
mkdir -p $TEST_ROOT/real-home/alice/.nix-defexpr/channels
|
||||
ln -sfn $TEST_ROOT/real-home $TEST_ROOT/home
|
||||
ln -sfn $(pwd)/user-envs.nix $TEST_ROOT/home/alice/.nix-defexpr/channels/foo
|
||||
HOME=$TEST_ROOT/home/alice nix-env -i foo-0.1
|
||||
|
|
|
@ -158,4 +158,6 @@ in
|
|||
fetch-git = runNixOSTestFor "x86_64-linux" ./fetch-git;
|
||||
|
||||
ca-fd-leak = runNixOSTestFor "x86_64-linux" ./ca-fd-leak;
|
||||
|
||||
gzip-content-encoding = runNixOSTestFor "x86_64-linux" ./gzip-content-encoding.nix;
|
||||
}
|
||||
|
|
|
@ -187,9 +187,14 @@ in
|
|||
client.succeed("nix flake metadata nixpkgs --tarball-ttl 0 >&2")
|
||||
|
||||
# Test fetchTree on a github URL.
|
||||
hash = client.succeed(f"nix eval --raw --expr '(fetchTree {info['url']}).narHash'")
|
||||
hash = client.succeed(f"nix eval --no-trust-tarballs-from-git-forges --raw --expr '(fetchTree {info['url']}).narHash'")
|
||||
assert hash == info['locked']['narHash']
|
||||
|
||||
# Fetching without a narHash should succeed if trust-github is set and fail otherwise.
|
||||
client.succeed(f"nix eval --raw --expr 'builtins.fetchTree github:github:fancy-enterprise/private-flake/{info['revision']}'")
|
||||
out = client.fail(f"nix eval --no-trust-tarballs-from-git-forges --raw --expr 'builtins.fetchTree github:github:fancy-enterprise/private-flake/{info['revision']}' 2>&1")
|
||||
assert "will not fetch unlocked input" in out, "--no-trust-tarballs-from-git-forges did not fail with the expected error"
|
||||
|
||||
# Shut down the web server. The flake should be cached on the client.
|
||||
github.succeed("systemctl stop httpd.service")
|
||||
|
||||
|
|
71
tests/nixos/gzip-content-encoding.nix
Normal file
71
tests/nixos/gzip-content-encoding.nix
Normal file
|
@ -0,0 +1,71 @@
|
|||
# Test that compressed files fetched from server with compressed responses
|
||||
# do not get excessively decompressed.
|
||||
# E.g. fetching a zstd compressed tarball from a server,
|
||||
# which compresses the response with `Content-Encoding: gzip`.
|
||||
# The expected result is that the fetched file is a zstd archive.
|
||||
|
||||
{ lib, config, ... }:
|
||||
|
||||
let
|
||||
pkgs = config.nodes.machine.nixpkgs.pkgs;
|
||||
|
||||
ztdCompressedFile = pkgs.stdenv.mkDerivation {
|
||||
name = "dummy-zstd-compressed-archive";
|
||||
dontUnpack = true;
|
||||
nativeBuildInputs = with pkgs; [ zstd ];
|
||||
buildPhase = ''
|
||||
mkdir archive
|
||||
for _ in {1..100}; do echo "lorem" > archive/file1; done
|
||||
for _ in {1..100}; do echo "ipsum" > archive/file2; done
|
||||
tar --zstd -cf archive.tar.zst archive
|
||||
'';
|
||||
installPhase = ''
|
||||
install -Dm 644 -T archive.tar.zst $out/share/archive
|
||||
'';
|
||||
};
|
||||
|
||||
fileCmd = "${pkgs.file}/bin/file";
|
||||
in
|
||||
|
||||
{
|
||||
name = "gzip-content-encoding";
|
||||
|
||||
nodes =
|
||||
{ machine =
|
||||
{ config, pkgs, ... }:
|
||||
{ networking.firewall.allowedTCPPorts = [ 80 ];
|
||||
|
||||
services.nginx.enable = true;
|
||||
services.nginx.virtualHosts."localhost" =
|
||||
{ root = "${ztdCompressedFile}/share/";
|
||||
# Make sure that nginx really tries to compress the
|
||||
# file on the fly with no regard to size/mime.
|
||||
# http://nginx.org/en/docs/http/ngx_http_gzip_module.html
|
||||
extraConfig = ''
|
||||
gzip on;
|
||||
gzip_types *;
|
||||
gzip_proxied any;
|
||||
gzip_min_length 0;
|
||||
'';
|
||||
};
|
||||
virtualisation.writableStore = true;
|
||||
virtualisation.additionalPaths = with pkgs; [ file ];
|
||||
nix.settings.substituters = lib.mkForce [ ];
|
||||
};
|
||||
};
|
||||
|
||||
# Check that when nix-prefetch-url is used with a zst tarball it does not get decompressed.
|
||||
testScript = { nodes }: ''
|
||||
# fmt: off
|
||||
start_all()
|
||||
|
||||
machine.wait_for_unit("nginx.service")
|
||||
machine.succeed("""
|
||||
# Make sure that the file is properly compressed as the test would be meaningless otherwise
|
||||
curl --compressed -v http://localhost/archive |& tr -s ' ' |& grep --ignore-case 'content-encoding: gzip'
|
||||
archive_path=$(nix-prefetch-url http://localhost/archive --print-path | tail -n1)
|
||||
[[ $(${fileCmd} --brief --mime-type $archive_path) == "application/zstd" ]]
|
||||
tar --zstd -xf $archive_path
|
||||
""")
|
||||
'';
|
||||
}
|
31
tests/unit/libexpr-support/tests/nix_api_expr.hh
Normal file
31
tests/unit/libexpr-support/tests/nix_api_expr.hh
Normal file
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
///@file
|
||||
#include "nix_api_expr.h"
|
||||
#include "nix_api_value.h"
|
||||
#include "tests/nix_api_store.hh"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace nixC {
|
||||
|
||||
class nix_api_expr_test : public nix_api_store_test
|
||||
{
|
||||
protected:
|
||||
|
||||
nix_api_expr_test()
|
||||
{
|
||||
nix_libexpr_init(ctx);
|
||||
state = nix_state_create(nullptr, nullptr, store);
|
||||
value = nix_alloc_value(nullptr, state);
|
||||
}
|
||||
~nix_api_expr_test()
|
||||
{
|
||||
nix_gc_decref(nullptr, value);
|
||||
nix_state_free(state);
|
||||
}
|
||||
|
||||
EvalState * state;
|
||||
Value * value;
|
||||
};
|
||||
|
||||
}
|
|
@ -23,15 +23,18 @@ libexpr-tests_EXTRA_INCLUDES = \
|
|||
-I tests/unit/libexpr-support \
|
||||
-I tests/unit/libstore-support \
|
||||
-I tests/unit/libutil-support \
|
||||
-I src/libexpr \
|
||||
-I src/libfetchers \
|
||||
-I src/libstore \
|
||||
-I src/libutil
|
||||
$(INCLUDE_libexpr) \
|
||||
$(INCLUDE_libexprc) \
|
||||
$(INCLUDE_libfetchers) \
|
||||
$(INCLUDE_libstore) \
|
||||
$(INCLUDE_libstorec) \
|
||||
$(INCLUDE_libutil) \
|
||||
$(INCLUDE_libutilc)
|
||||
|
||||
libexpr-tests_CXXFLAGS += $(libexpr-tests_EXTRA_INCLUDES)
|
||||
|
||||
libexpr-tests_LIBS = \
|
||||
libexpr-test-support libstore-test-support libutils-test-support \
|
||||
libexpr libfetchers libstore libutil
|
||||
libexpr libexprc libfetchers libstore libstorec libutil libutilc
|
||||
|
||||
libexpr-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) -lgmock
|
||||
|
|
100
tests/unit/libexpr/nix_api_expr.cc
Normal file
100
tests/unit/libexpr/nix_api_expr.cc
Normal file
|
@ -0,0 +1,100 @@
|
|||
#include "nix_api_store.h"
|
||||
#include "nix_api_store_internal.h"
|
||||
#include "nix_api_util.h"
|
||||
#include "nix_api_util_internal.h"
|
||||
#include "nix_api_expr.h"
|
||||
#include "nix_api_value.h"
|
||||
|
||||
#include "tests/nix_api_expr.hh"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace nixC {
|
||||
|
||||
TEST_F(nix_api_expr_test, nix_expr_eval_from_string)
|
||||
{
|
||||
nix_expr_eval_from_string(nullptr, state, "builtins.nixVersion", ".", value);
|
||||
nix_value_force(nullptr, state, value);
|
||||
auto result = nix_get_string(nullptr, value);
|
||||
|
||||
ASSERT_STREQ(PACKAGE_VERSION, result);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_expr_test, nix_expr_eval_add_numbers)
|
||||
{
|
||||
nix_expr_eval_from_string(nullptr, state, "1 + 1", ".", value);
|
||||
nix_value_force(nullptr, state, value);
|
||||
auto result = nix_get_int(nullptr, value);
|
||||
|
||||
ASSERT_EQ(2, result);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_expr_test, nix_expr_eval_drv)
|
||||
{
|
||||
auto expr = R"(derivation { name = "myname"; builder = "mybuilder"; system = "mysystem"; })";
|
||||
nix_expr_eval_from_string(nullptr, state, expr, ".", value);
|
||||
ASSERT_EQ(NIX_TYPE_ATTRS, nix_get_type(nullptr, value));
|
||||
|
||||
EvalState * stateFn = nix_state_create(nullptr, nullptr, store);
|
||||
Value * valueFn = nix_alloc_value(nullptr, state);
|
||||
nix_expr_eval_from_string(nullptr, stateFn, "builtins.toString", ".", valueFn);
|
||||
ASSERT_EQ(NIX_TYPE_FUNCTION, nix_get_type(nullptr, valueFn));
|
||||
|
||||
EvalState * stateResult = nix_state_create(nullptr, nullptr, store);
|
||||
Value * valueResult = nix_alloc_value(nullptr, stateResult);
|
||||
nix_value_call(ctx, stateResult, valueFn, value, valueResult);
|
||||
ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(nullptr, valueResult));
|
||||
|
||||
std::string p = nix_get_string(nullptr, valueResult);
|
||||
std::string pEnd = "-myname";
|
||||
ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size()));
|
||||
|
||||
// Clean up
|
||||
nix_gc_decref(nullptr, valueFn);
|
||||
nix_state_free(stateFn);
|
||||
|
||||
nix_gc_decref(nullptr, valueResult);
|
||||
nix_state_free(stateResult);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_expr_test, nix_build_drv)
|
||||
{
|
||||
auto expr = R"(derivation { name = "myname";
|
||||
system = builtins.currentSystem;
|
||||
builder = "/bin/sh";
|
||||
args = [ "-c" "echo foo > $out" ];
|
||||
})";
|
||||
nix_expr_eval_from_string(nullptr, state, expr, ".", value);
|
||||
|
||||
Value * drvPathValue = nix_get_attr_byname(nullptr, value, state, "drvPath");
|
||||
const char * drvPath = nix_get_string(nullptr, drvPathValue);
|
||||
|
||||
std::string p = drvPath;
|
||||
std::string pEnd = "-myname.drv";
|
||||
ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size()));
|
||||
|
||||
StorePath * drvStorePath = nix_store_parse_path(ctx, store, drvPath);
|
||||
ASSERT_EQ(true, nix_store_is_valid_path(ctx, store, drvStorePath));
|
||||
|
||||
Value * outPathValue = nix_get_attr_byname(ctx, value, state, "outPath");
|
||||
const char * outPath = nix_get_string(ctx, outPathValue);
|
||||
|
||||
p = outPath;
|
||||
pEnd = "-myname";
|
||||
ASSERT_EQ(pEnd, p.substr(p.size() - pEnd.size()));
|
||||
ASSERT_EQ(true, drvStorePath->path.isDerivation());
|
||||
|
||||
StorePath * outStorePath = nix_store_parse_path(ctx, store, outPath);
|
||||
ASSERT_EQ(false, nix_store_is_valid_path(ctx, store, outStorePath));
|
||||
|
||||
// TODO figure out why fails.
|
||||
// `make libexpr-tests_RUN` works, but `nix build .` enters an infinite loop
|
||||
/* nix_store_realise(ctx, store, drvStorePath, nullptr, nullptr); */
|
||||
/* auto is_valid_path = nix_store_is_valid_path(ctx, store, outStorePath); */
|
||||
/* ASSERT_EQ(true, is_valid_path); */
|
||||
|
||||
// Clean up
|
||||
nix_store_path_free(drvStorePath);
|
||||
nix_store_path_free(outStorePath);
|
||||
}
|
||||
}
|
63
tests/unit/libexpr/nix_api_external.cc
Normal file
63
tests/unit/libexpr/nix_api_external.cc
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include "nix_api_store.h"
|
||||
#include "nix_api_store_internal.h"
|
||||
#include "nix_api_util.h"
|
||||
#include "nix_api_util_internal.h"
|
||||
#include "nix_api_expr.h"
|
||||
#include "nix_api_expr_internal.h"
|
||||
#include "nix_api_value.h"
|
||||
#include "nix_api_external.h"
|
||||
#include "tests/nix_api_expr.hh"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace nixC {
|
||||
|
||||
class MyExternalValueDesc : public NixCExternalValueDesc
|
||||
{
|
||||
public:
|
||||
MyExternalValueDesc(int x)
|
||||
: _x(x)
|
||||
{
|
||||
print = print_function;
|
||||
showType = show_type_function;
|
||||
typeOf = type_of_function;
|
||||
}
|
||||
|
||||
private:
|
||||
int _x;
|
||||
static void print_function(void * self, nix_printer * printer) {}
|
||||
|
||||
static void show_type_function(void * self, nix_string_return * res) {}
|
||||
|
||||
static void type_of_function(void * self, nix_string_return * res)
|
||||
{
|
||||
MyExternalValueDesc * obj = static_cast<MyExternalValueDesc *>(self);
|
||||
|
||||
std::string type_string = "nix-external<MyExternalValueDesc( ";
|
||||
type_string += std::to_string(obj->_x);
|
||||
type_string += " )>";
|
||||
res->str = &*type_string.begin();
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(nix_api_expr_test, nix_expr_eval_external)
|
||||
{
|
||||
MyExternalValueDesc * external = new MyExternalValueDesc(42);
|
||||
ExternalValue * val = nix_create_external_value(ctx, external, external);
|
||||
nix_init_external(ctx, value, val);
|
||||
|
||||
EvalState * stateResult = nix_state_create(nullptr, nullptr, store);
|
||||
Value * valueResult = nix_alloc_value(nullptr, stateResult);
|
||||
|
||||
EvalState * stateFn = nix_state_create(nullptr, nullptr, store);
|
||||
Value * valueFn = nix_alloc_value(nullptr, stateFn);
|
||||
|
||||
nix_expr_eval_from_string(nullptr, state, "builtins.typeOf", ".", valueFn);
|
||||
|
||||
ASSERT_EQ(NIX_TYPE_EXTERNAL, nix_get_type(nullptr, value));
|
||||
|
||||
nix_value_call(ctx, state, valueFn, value, valueResult);
|
||||
|
||||
ASSERT_STREQ("nix-external<MyExternalValueDesc( 42 )>", nix_get_string(nullptr, valueResult));
|
||||
}
|
||||
}
|
184
tests/unit/libexpr/nix_api_value.cc
Normal file
184
tests/unit/libexpr/nix_api_value.cc
Normal file
|
@ -0,0 +1,184 @@
|
|||
#include "nix_api_store.h"
|
||||
#include "nix_api_store_internal.h"
|
||||
#include "nix_api_util.h"
|
||||
#include "nix_api_util_internal.h"
|
||||
#include "nix_api_expr.h"
|
||||
#include "nix_api_value.h"
|
||||
|
||||
#include "tests/nix_api_expr.hh"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace nixC {
|
||||
|
||||
TEST_F(nix_api_expr_test, nix_value_set_get_int)
|
||||
{
|
||||
ASSERT_EQ(0, nix_get_int(ctx, nullptr));
|
||||
ASSERT_DEATH(nix_get_int(ctx, value), "");
|
||||
|
||||
int myInt = 1;
|
||||
nix_init_int(ctx, value, myInt);
|
||||
|
||||
ASSERT_EQ(myInt, nix_get_int(ctx, value));
|
||||
ASSERT_STREQ("an integer", nix_get_typename(ctx, value));
|
||||
ASSERT_EQ(NIX_TYPE_INT, nix_get_type(ctx, value));
|
||||
}
|
||||
|
||||
TEST_F(nix_api_expr_test, nix_value_set_get_float)
|
||||
{
|
||||
ASSERT_FLOAT_EQ(0.0, nix_get_float(ctx, nullptr));
|
||||
ASSERT_DEATH(nix_get_float(ctx, value), "");
|
||||
|
||||
float myDouble = 1.0;
|
||||
nix_init_float(ctx, value, myDouble);
|
||||
|
||||
ASSERT_FLOAT_EQ(myDouble, nix_get_float(ctx, value));
|
||||
ASSERT_STREQ("a float", nix_get_typename(ctx, value));
|
||||
ASSERT_EQ(NIX_TYPE_FLOAT, nix_get_type(ctx, value));
|
||||
}
|
||||
|
||||
TEST_F(nix_api_expr_test, nix_value_set_get_bool)
|
||||
{
|
||||
ASSERT_EQ(false, nix_get_bool(ctx, nullptr));
|
||||
ASSERT_DEATH(nix_get_bool(ctx, value), "");
|
||||
|
||||
bool myBool = true;
|
||||
nix_init_bool(ctx, value, myBool);
|
||||
|
||||
ASSERT_EQ(myBool, nix_get_bool(ctx, value));
|
||||
ASSERT_STREQ("a Boolean", nix_get_typename(ctx, value));
|
||||
ASSERT_EQ(NIX_TYPE_BOOL, nix_get_type(ctx, value));
|
||||
}
|
||||
|
||||
TEST_F(nix_api_expr_test, nix_value_set_get_string)
|
||||
{
|
||||
ASSERT_EQ(nullptr, nix_get_string(ctx, nullptr));
|
||||
ASSERT_DEATH(nix_get_string(ctx, value), "");
|
||||
|
||||
const char * myString = "some string";
|
||||
nix_init_string(ctx, value, myString);
|
||||
|
||||
ASSERT_STREQ(myString, nix_get_string(ctx, value));
|
||||
ASSERT_STREQ("a string", nix_get_typename(ctx, value));
|
||||
ASSERT_EQ(NIX_TYPE_STRING, nix_get_type(ctx, value));
|
||||
}
|
||||
|
||||
TEST_F(nix_api_expr_test, nix_value_set_get_null)
|
||||
{
|
||||
ASSERT_DEATH(nix_get_typename(ctx, value), "");
|
||||
|
||||
nix_init_null(ctx, value);
|
||||
|
||||
ASSERT_STREQ("null", nix_get_typename(ctx, value));
|
||||
ASSERT_EQ(NIX_TYPE_NULL, nix_get_type(ctx, value));
|
||||
}
|
||||
|
||||
TEST_F(nix_api_expr_test, nix_value_set_get_path)
|
||||
{
|
||||
ASSERT_EQ(nullptr, nix_get_path_string(ctx, nullptr));
|
||||
ASSERT_DEATH(nix_get_path_string(ctx, value), "");
|
||||
|
||||
const char * p = "/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname";
|
||||
nix_init_path_string(ctx, state, value, p);
|
||||
|
||||
ASSERT_STREQ(p, nix_get_path_string(ctx, value));
|
||||
ASSERT_STREQ("a path", nix_get_typename(ctx, value));
|
||||
ASSERT_EQ(NIX_TYPE_PATH, nix_get_type(ctx, value));
|
||||
}
|
||||
|
||||
TEST_F(nix_api_expr_test, nix_build_and_init_list)
|
||||
{
|
||||
ASSERT_EQ(nullptr, nix_get_list_byidx(ctx, nullptr, state, 0));
|
||||
ASSERT_EQ(0, nix_get_list_size(ctx, nullptr));
|
||||
|
||||
ASSERT_DEATH(nix_get_list_byidx(ctx, value, state, 0), "");
|
||||
ASSERT_DEATH(nix_get_list_size(ctx, value), "");
|
||||
|
||||
int size = 10;
|
||||
ListBuilder * builder = nix_make_list_builder(ctx, state, size);
|
||||
|
||||
Value * intValue = nix_alloc_value(ctx, state);
|
||||
nix_init_int(ctx, intValue, 42);
|
||||
nix_list_builder_insert(ctx, builder, 0, intValue);
|
||||
nix_make_list(ctx, builder, value);
|
||||
nix_list_builder_free(builder);
|
||||
|
||||
ASSERT_EQ(42, nix_get_int(ctx, nix_get_list_byidx(ctx, value, state, 0)));
|
||||
ASSERT_EQ(nullptr, nix_get_list_byidx(ctx, value, state, 1));
|
||||
ASSERT_EQ(10, nix_get_list_size(ctx, value));
|
||||
|
||||
ASSERT_STREQ("a list", nix_get_typename(ctx, value));
|
||||
ASSERT_EQ(NIX_TYPE_LIST, nix_get_type(ctx, value));
|
||||
|
||||
// Clean up
|
||||
nix_gc_decref(ctx, intValue);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_expr_test, nix_build_and_init_attr)
|
||||
{
|
||||
ASSERT_EQ(nullptr, nix_get_attr_byname(ctx, nullptr, state, 0));
|
||||
ASSERT_EQ(nullptr, nix_get_attr_byidx(ctx, nullptr, state, 0, nullptr));
|
||||
ASSERT_EQ(nullptr, nix_get_attr_name_byidx(ctx, nullptr, state, 0));
|
||||
ASSERT_EQ(0, nix_get_attrs_size(ctx, nullptr));
|
||||
ASSERT_EQ(false, nix_has_attr_byname(ctx, nullptr, state, "no-value"));
|
||||
|
||||
ASSERT_DEATH(nix_get_attr_byname(ctx, value, state, 0), "");
|
||||
ASSERT_DEATH(nix_get_attr_byidx(ctx, value, state, 0, nullptr), "");
|
||||
ASSERT_DEATH(nix_get_attr_name_byidx(ctx, value, state, 0), "");
|
||||
ASSERT_DEATH(nix_get_attrs_size(ctx, value), "");
|
||||
ASSERT_DEATH(nix_has_attr_byname(ctx, value, state, "no-value"), "");
|
||||
|
||||
int size = 10;
|
||||
const char ** out_name = (const char **) malloc(sizeof(char *));
|
||||
|
||||
BindingsBuilder * builder = nix_make_bindings_builder(ctx, state, size);
|
||||
|
||||
Value * intValue = nix_alloc_value(ctx, state);
|
||||
nix_init_int(ctx, intValue, 42);
|
||||
|
||||
Value * stringValue = nix_alloc_value(ctx, state);
|
||||
nix_init_string(ctx, stringValue, "foo");
|
||||
|
||||
nix_bindings_builder_insert(ctx, builder, "a", intValue);
|
||||
nix_bindings_builder_insert(ctx, builder, "b", stringValue);
|
||||
nix_make_attrs(ctx, value, builder);
|
||||
nix_bindings_builder_free(builder);
|
||||
|
||||
ASSERT_EQ(2, nix_get_attrs_size(ctx, value));
|
||||
|
||||
Value * out_value = nix_get_attr_byname(ctx, value, state, "a");
|
||||
ASSERT_EQ(42, nix_get_int(ctx, out_value));
|
||||
nix_gc_decref(ctx, out_value);
|
||||
|
||||
out_value = nix_get_attr_byidx(ctx, value, state, 0, out_name);
|
||||
ASSERT_EQ(42, nix_get_int(ctx, out_value));
|
||||
ASSERT_STREQ("a", *out_name);
|
||||
nix_gc_decref(ctx, out_value);
|
||||
|
||||
ASSERT_STREQ("a", nix_get_attr_name_byidx(ctx, value, state, 0));
|
||||
|
||||
ASSERT_EQ(true, nix_has_attr_byname(ctx, value, state, "b"));
|
||||
ASSERT_EQ(false, nix_has_attr_byname(ctx, value, state, "no-value"));
|
||||
|
||||
out_value = nix_get_attr_byname(ctx, value, state, "b");
|
||||
ASSERT_STREQ("foo", nix_get_string(ctx, out_value));
|
||||
nix_gc_decref(nullptr, out_value);
|
||||
|
||||
out_value = nix_get_attr_byidx(ctx, value, state, 1, out_name);
|
||||
ASSERT_STREQ("foo", nix_get_string(ctx, out_value));
|
||||
ASSERT_STREQ("b", *out_name);
|
||||
nix_gc_decref(nullptr, out_value);
|
||||
|
||||
ASSERT_STREQ("b", nix_get_attr_name_byidx(ctx, value, state, 1));
|
||||
|
||||
ASSERT_STREQ("a set", nix_get_typename(ctx, value));
|
||||
ASSERT_EQ(NIX_TYPE_ATTRS, nix_get_type(ctx, value));
|
||||
|
||||
// Clean up
|
||||
nix_gc_decref(ctx, intValue);
|
||||
nix_gc_decref(ctx, stringValue);
|
||||
free(out_name);
|
||||
}
|
||||
|
||||
}
|
|
@ -91,7 +91,7 @@ namespace nix {
|
|||
}
|
||||
|
||||
TEST_F(PrimOpTest, getEnv) {
|
||||
setenv("_NIX_UNIT_TEST_ENV_VALUE", "test value", 1);
|
||||
setEnv("_NIX_UNIT_TEST_ENV_VALUE", "test value");
|
||||
auto v = eval("builtins.getEnv \"_NIX_UNIT_TEST_ENV_VALUE\"");
|
||||
ASSERT_THAT(v, IsStringEq("test value"));
|
||||
}
|
||||
|
|
|
@ -79,11 +79,11 @@ TEST_F(ValuePrintingTests, tList)
|
|||
Value vTwo;
|
||||
vTwo.mkInt(2);
|
||||
|
||||
auto list = state.buildList(3);
|
||||
list.elems[0] = &vOne;
|
||||
list.elems[1] = &vTwo;
|
||||
Value vList;
|
||||
state.mkList(vList, 5);
|
||||
vList.bigList.elems[0] = &vOne;
|
||||
vList.bigList.elems[1] = &vTwo;
|
||||
vList.bigList.size = 3;
|
||||
vList.mkList(list);
|
||||
|
||||
test(vList, "[ 1 2 «nullptr» ]");
|
||||
}
|
||||
|
@ -249,12 +249,12 @@ TEST_F(ValuePrintingTests, depthList)
|
|||
Value vNested;
|
||||
vNested.mkAttrs(builder2.finish());
|
||||
|
||||
auto list = state.buildList(3);
|
||||
list.elems[0] = &vOne;
|
||||
list.elems[1] = &vTwo;
|
||||
list.elems[2] = &vNested;
|
||||
Value vList;
|
||||
state.mkList(vList, 5);
|
||||
vList.bigList.elems[0] = &vOne;
|
||||
vList.bigList.elems[1] = &vTwo;
|
||||
vList.bigList.elems[2] = &vNested;
|
||||
vList.bigList.size = 3;
|
||||
vList.mkList(list);
|
||||
|
||||
test(vList, "[ 1 2 { ... } ]", PrintOptions { .maxDepth = 1 });
|
||||
test(vList, "[ 1 2 { nested = { ... }; one = 1; two = 2; } ]", PrintOptions { .maxDepth = 2 });
|
||||
|
@ -539,11 +539,11 @@ TEST_F(ValuePrintingTests, ansiColorsList)
|
|||
Value vTwo;
|
||||
vTwo.mkInt(2);
|
||||
|
||||
auto list = state.buildList(3);
|
||||
list.elems[0] = &vOne;
|
||||
list.elems[1] = &vTwo;
|
||||
Value vList;
|
||||
state.mkList(vList, 5);
|
||||
vList.bigList.elems[0] = &vOne;
|
||||
vList.bigList.elems[1] = &vTwo;
|
||||
vList.bigList.size = 3;
|
||||
vList.mkList(list);
|
||||
|
||||
test(vList,
|
||||
"[ " ANSI_CYAN "1" ANSI_NORMAL " " ANSI_CYAN "2" ANSI_NORMAL " " ANSI_MAGENTA "«nullptr»" ANSI_NORMAL " ]",
|
||||
|
@ -670,11 +670,11 @@ TEST_F(ValuePrintingTests, ansiColorsListRepeated)
|
|||
Value vEmpty;
|
||||
vEmpty.mkAttrs(emptyBuilder.finish());
|
||||
|
||||
auto list = state.buildList(2);
|
||||
list.elems[0] = &vEmpty;
|
||||
list.elems[1] = &vEmpty;
|
||||
Value vList;
|
||||
state.mkList(vList, 3);
|
||||
vList.bigList.elems[0] = &vEmpty;
|
||||
vList.bigList.elems[1] = &vEmpty;
|
||||
vList.bigList.size = 2;
|
||||
vList.mkList(list);
|
||||
|
||||
test(vList,
|
||||
"[ { } " ANSI_MAGENTA "«repeated»" ANSI_NORMAL " ]",
|
||||
|
@ -690,11 +690,11 @@ TEST_F(ValuePrintingTests, listRepeated)
|
|||
Value vEmpty;
|
||||
vEmpty.mkAttrs(emptyBuilder.finish());
|
||||
|
||||
auto list = state.buildList(2);
|
||||
list.elems[0] = &vEmpty;
|
||||
list.elems[1] = &vEmpty;
|
||||
Value vList;
|
||||
state.mkList(vList, 3);
|
||||
vList.bigList.elems[0] = &vEmpty;
|
||||
vList.bigList.elems[1] = &vEmpty;
|
||||
vList.bigList.size = 2;
|
||||
vList.mkList(list);
|
||||
|
||||
test(vList, "[ { } «repeated» ]", PrintOptions { });
|
||||
test(vList,
|
||||
|
@ -750,11 +750,12 @@ TEST_F(ValuePrintingTests, ansiColorsListElided)
|
|||
Value vTwo;
|
||||
vTwo.mkInt(2);
|
||||
|
||||
{
|
||||
auto list = state.buildList(2);
|
||||
list.elems[0] = &vOne;
|
||||
list.elems[1] = &vTwo;
|
||||
Value vList;
|
||||
state.mkList(vList, 4);
|
||||
vList.bigList.elems[0] = &vOne;
|
||||
vList.bigList.elems[1] = &vTwo;
|
||||
vList.bigList.size = 2;
|
||||
vList.mkList(list);
|
||||
|
||||
test(vList,
|
||||
"[ " ANSI_CYAN "1" ANSI_NORMAL " " ANSI_FAINT "«1 item elided»" ANSI_NORMAL " ]",
|
||||
|
@ -762,12 +763,18 @@ TEST_F(ValuePrintingTests, ansiColorsListElided)
|
|||
.ansiColors = true,
|
||||
.maxListItems = 1
|
||||
});
|
||||
}
|
||||
|
||||
Value vThree;
|
||||
vThree.mkInt(3);
|
||||
|
||||
vList.bigList.elems[2] = &vThree;
|
||||
vList.bigList.size = 3;
|
||||
{
|
||||
auto list = state.buildList(3);
|
||||
list.elems[0] = &vOne;
|
||||
list.elems[1] = &vTwo;
|
||||
list.elems[2] = &vThree;
|
||||
Value vList;
|
||||
vList.mkList(list);
|
||||
|
||||
test(vList,
|
||||
"[ " ANSI_CYAN "1" ANSI_NORMAL " " ANSI_FAINT "«2 items elided»" ANSI_NORMAL " ]",
|
||||
|
@ -775,6 +782,7 @@ TEST_F(ValuePrintingTests, ansiColorsListElided)
|
|||
.ansiColors = true,
|
||||
.maxListItems = 1
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
|
|
32
tests/unit/libfetchers/local.mk
Normal file
32
tests/unit/libfetchers/local.mk
Normal file
|
@ -0,0 +1,32 @@
|
|||
check: libfetchers-tests_RUN
|
||||
|
||||
programs += libfetchers-tests
|
||||
|
||||
libfetchers-tests_NAME = libnixfetchers-tests
|
||||
|
||||
libfetchers-tests_ENV := _NIX_TEST_UNIT_DATA=$(d)/data
|
||||
|
||||
libfetchers-tests_DIR := $(d)
|
||||
|
||||
ifeq ($(INSTALL_UNIT_TESTS), yes)
|
||||
libfetchers-tests_INSTALL_DIR := $(checkbindir)
|
||||
else
|
||||
libfetchers-tests_INSTALL_DIR :=
|
||||
endif
|
||||
|
||||
libfetchers-tests_SOURCES := $(wildcard $(d)/*.cc)
|
||||
|
||||
libfetchers-tests_EXTRA_INCLUDES = \
|
||||
-I tests/unit/libstore-support \
|
||||
-I tests/unit/libutil-support \
|
||||
$(INCLUDE_libfetchers) \
|
||||
$(INCLUDE_libstore) \
|
||||
$(INCLUDE_libutil)
|
||||
|
||||
libfetchers-tests_CXXFLAGS += $(libfetchers-tests_EXTRA_INCLUDES)
|
||||
|
||||
libfetchers-tests_LIBS = \
|
||||
libstore-test-support libutil-test-support \
|
||||
libfetchers libstore libutil
|
||||
|
||||
libfetchers-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS)
|
18
tests/unit/libfetchers/public-key.cc
Normal file
18
tests/unit/libfetchers/public-key.cc
Normal file
|
@ -0,0 +1,18 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include "fetchers.hh"
|
||||
#include "json-utils.hh"
|
||||
|
||||
namespace nix {
|
||||
TEST(PublicKey, jsonSerialization) {
|
||||
auto json = nlohmann::json(fetchers::PublicKey { .key = "ABCDE" });
|
||||
|
||||
ASSERT_EQ(json, R"({ "key": "ABCDE", "type": "ssh-ed25519" })"_json);
|
||||
}
|
||||
TEST(PublicKey, jsonDeserialization) {
|
||||
auto pubKeyJson = R"({ "key": "ABCDE", "type": "ssh-ed25519" })"_json;
|
||||
fetchers::PublicKey pubKey = pubKeyJson;
|
||||
|
||||
ASSERT_EQ(pubKey.key, "ABCDE");
|
||||
ASSERT_EQ(pubKey.type, "ssh-ed25519");
|
||||
}
|
||||
}
|
66
tests/unit/libstore-support/tests/nix_api_store.hh
Normal file
66
tests/unit/libstore-support/tests/nix_api_store.hh
Normal file
|
@ -0,0 +1,66 @@
|
|||
#pragma once
|
||||
///@file
|
||||
#include "tests/nix_api_util.hh"
|
||||
|
||||
#include "file-system.hh"
|
||||
|
||||
#include "nix_api_store.h"
|
||||
#include "nix_api_store_internal.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace nixC {
|
||||
class nix_api_store_test : public nix_api_util_context
|
||||
{
|
||||
public:
|
||||
nix_api_store_test()
|
||||
{
|
||||
nix_libstore_init(ctx);
|
||||
init_local_store();
|
||||
};
|
||||
|
||||
~nix_api_store_test() override
|
||||
{
|
||||
nix_store_free(store);
|
||||
|
||||
for (auto & path : fs::recursive_directory_iterator(nixDir)) {
|
||||
fs::permissions(path, fs::perms::owner_all);
|
||||
}
|
||||
fs::remove_all(nixDir);
|
||||
}
|
||||
|
||||
Store * store;
|
||||
std::string nixDir;
|
||||
std::string nixStoreDir;
|
||||
|
||||
protected:
|
||||
void init_local_store()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
// no `mkdtemp` with MinGW
|
||||
auto tmpl = nix::defaultTempDir() + "/tests_nix-store.";
|
||||
for (size_t i = 0; true; ++i) {
|
||||
nixDir = tmpl + std::string { i };
|
||||
if (fs::create_directory(nixDir)) break;
|
||||
}
|
||||
#else
|
||||
auto tmpl = nix::defaultTempDir() + "/tests_nix-store.XXXXXX";
|
||||
nixDir = mkdtemp((char *) tmpl.c_str());
|
||||
#endif
|
||||
|
||||
nixStoreDir = nixDir + "/my_nix_store";
|
||||
|
||||
// Options documented in `nix help-stores`
|
||||
const char * p1[] = {"store", nixStoreDir.c_str()};
|
||||
const char * p2[] = {"state", (new std::string(nixDir + "/my_state"))->c_str()};
|
||||
const char * p3[] = {"log", (new std::string(nixDir + "/my_log"))->c_str()};
|
||||
|
||||
const char ** params[] = {p1, p2, p3, nullptr};
|
||||
|
||||
store = nix_store_open(ctx, "local", params);
|
||||
}
|
||||
};
|
||||
}
|
|
@ -19,13 +19,15 @@ libstore-tests_SOURCES := $(wildcard $(d)/*.cc)
|
|||
libstore-tests_EXTRA_INCLUDES = \
|
||||
-I tests/unit/libstore-support \
|
||||
-I tests/unit/libutil-support \
|
||||
-I src/libstore \
|
||||
-I src/libutil
|
||||
$(INCLUDE_libstore) \
|
||||
$(INCLUDE_libstorec) \
|
||||
$(INCLUDE_libutil) \
|
||||
$(INCLUDE_libutilc)
|
||||
|
||||
libstore-tests_CXXFLAGS += $(libstore-tests_EXTRA_INCLUDES)
|
||||
|
||||
libstore-tests_LIBS = \
|
||||
libstore-test-support libutil-test-support \
|
||||
libstore libutil
|
||||
libstore libstorec libutil libutilc
|
||||
|
||||
libstore-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS)
|
||||
|
|
93
tests/unit/libstore/nix_api_store.cc
Normal file
93
tests/unit/libstore/nix_api_store.cc
Normal file
|
@ -0,0 +1,93 @@
|
|||
#include "nix_api_util.h"
|
||||
#include "nix_api_util_internal.h"
|
||||
#include "nix_api_store.h"
|
||||
#include "nix_api_store_internal.h"
|
||||
|
||||
#include "tests/nix_api_store.hh"
|
||||
|
||||
namespace nixC {
|
||||
|
||||
void observe_string_cb(const char * start, unsigned int n, std::string * user_data)
|
||||
{
|
||||
*user_data = std::string(start);
|
||||
}
|
||||
|
||||
std::string PATH_SUFFIX = "/g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-name";
|
||||
|
||||
TEST_F(nix_api_util_context, nix_libstore_init)
|
||||
{
|
||||
auto ret = nix_libstore_init(ctx);
|
||||
ASSERT_EQ(NIX_OK, ret);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test, nix_store_get_uri)
|
||||
{
|
||||
std::string str;
|
||||
auto ret = nix_store_get_uri(ctx, store, (void *) observe_string_cb, &str);
|
||||
ASSERT_EQ(NIX_OK, ret);
|
||||
ASSERT_STREQ("local", str.c_str());
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test, InvalidPathFails)
|
||||
{
|
||||
nix_store_parse_path(ctx, store, "invalid-path");
|
||||
ASSERT_EQ(ctx->last_err_code, NIX_ERR_NIX_ERROR);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test, ReturnsValidStorePath)
|
||||
{
|
||||
StorePath * result = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str());
|
||||
ASSERT_NE(result, nullptr);
|
||||
ASSERT_STREQ("name", result->path.name().data());
|
||||
ASSERT_STREQ(PATH_SUFFIX.substr(1).c_str(), result->path.to_string().data());
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test, SetsLastErrCodeToNixOk)
|
||||
{
|
||||
nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str());
|
||||
ASSERT_EQ(ctx->last_err_code, NIX_OK);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test, DoesNotCrashWhenContextIsNull)
|
||||
{
|
||||
ASSERT_NO_THROW(nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str()));
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test, get_version)
|
||||
{
|
||||
std::string str;
|
||||
auto ret = nix_store_get_version(ctx, store, (void *) observe_string_cb, &str);
|
||||
ASSERT_EQ(NIX_OK, ret);
|
||||
ASSERT_STREQ(PACKAGE_VERSION, str.c_str());
|
||||
}
|
||||
|
||||
TEST_F(nix_api_util_context, nix_store_open_dummy)
|
||||
{
|
||||
nix_libstore_init(ctx);
|
||||
Store * store = nix_store_open(ctx, "dummy://", nullptr);
|
||||
ASSERT_EQ(NIX_OK, ctx->last_err_code);
|
||||
ASSERT_STREQ("dummy", store->ptr->getUri().c_str());
|
||||
|
||||
std::string str;
|
||||
nix_store_get_version(ctx, store, (void *) observe_string_cb, &str);
|
||||
ASSERT_STREQ("", str.c_str());
|
||||
|
||||
nix_store_free(store);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_util_context, nix_store_open_invalid)
|
||||
{
|
||||
nix_libstore_init(ctx);
|
||||
Store * store = nix_store_open(ctx, "invalid://", nullptr);
|
||||
ASSERT_EQ(NIX_ERR_NIX_ERROR, ctx->last_err_code);
|
||||
ASSERT_EQ(nullptr, store);
|
||||
nix_store_free(store);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_store_test, nix_store_is_valid_path_not_in_store)
|
||||
{
|
||||
StorePath * path = nix_store_parse_path(ctx, store, (nixStoreDir + PATH_SUFFIX).c_str());
|
||||
ASSERT_EQ(false, nix_store_is_valid_path(ctx, store, path));
|
||||
}
|
||||
|
||||
}
|
27
tests/unit/libutil-support/tests/nix_api_util.hh
Normal file
27
tests/unit/libutil-support/tests/nix_api_util.hh
Normal file
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
///@file
|
||||
#include "nix_api_util.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace nixC {
|
||||
|
||||
class nix_api_util_context : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
|
||||
nix_api_util_context()
|
||||
{
|
||||
ctx = nix_c_context_create();
|
||||
nix_libutil_init(ctx);
|
||||
};
|
||||
|
||||
~nix_api_util_context() override
|
||||
{
|
||||
nix_c_context_free(ctx);
|
||||
ctx = nullptr;
|
||||
}
|
||||
|
||||
nix_c_context * ctx;
|
||||
};
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "error.hh"
|
||||
#include "json-utils.hh"
|
||||
|
||||
namespace nix {
|
||||
|
@ -55,4 +56,108 @@ TEST(from_json, vectorOfOptionalInts) {
|
|||
ASSERT_FALSE(vals.at(1).has_value());
|
||||
}
|
||||
|
||||
TEST(valueAt, simpleObject) {
|
||||
auto simple = R"({ "hello": "world" })"_json;
|
||||
|
||||
ASSERT_EQ(valueAt(getObject(simple), "hello"), "world");
|
||||
|
||||
auto nested = R"({ "hello": { "world": "" } })"_json;
|
||||
|
||||
auto & nestedObject = valueAt(getObject(nested), "hello");
|
||||
|
||||
ASSERT_EQ(valueAt(nestedObject, "world"), "");
|
||||
}
|
||||
|
||||
TEST(valueAt, missingKey) {
|
||||
auto json = R"({ "hello": { "nested": "world" } })"_json;
|
||||
|
||||
auto & obj = getObject(json);
|
||||
|
||||
ASSERT_THROW(valueAt(obj, "foo"), Error);
|
||||
}
|
||||
|
||||
TEST(getObject, rightAssertions) {
|
||||
auto simple = R"({ "object": {} })"_json;
|
||||
|
||||
ASSERT_EQ(getObject(valueAt(getObject(simple), "object")), (nlohmann::json::object_t {}));
|
||||
|
||||
auto nested = R"({ "object": { "object": {} } })"_json;
|
||||
|
||||
auto & nestedObject = getObject(valueAt(getObject(nested), "object"));
|
||||
|
||||
ASSERT_EQ(nestedObject, getObject(nlohmann::json::parse(R"({ "object": {} })")));
|
||||
ASSERT_EQ(getObject(valueAt(getObject(nestedObject), "object")), (nlohmann::json::object_t {}));
|
||||
}
|
||||
|
||||
TEST(getObject, wrongAssertions) {
|
||||
auto json = R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })"_json;
|
||||
|
||||
auto & obj = getObject(json);
|
||||
|
||||
ASSERT_THROW(getObject(valueAt(obj, "array")), Error);
|
||||
ASSERT_THROW(getObject(valueAt(obj, "string")), Error);
|
||||
ASSERT_THROW(getObject(valueAt(obj, "int")), Error);
|
||||
ASSERT_THROW(getObject(valueAt(obj, "boolean")), Error);
|
||||
}
|
||||
|
||||
TEST(getArray, rightAssertions) {
|
||||
auto simple = R"({ "array": [] })"_json;
|
||||
|
||||
ASSERT_EQ(getArray(valueAt(getObject(simple), "array")), (nlohmann::json::array_t {}));
|
||||
}
|
||||
|
||||
TEST(getArray, wrongAssertions) {
|
||||
auto json = R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })"_json;
|
||||
|
||||
ASSERT_THROW(getArray(valueAt(json, "object")), Error);
|
||||
ASSERT_THROW(getArray(valueAt(json, "string")), Error);
|
||||
ASSERT_THROW(getArray(valueAt(json, "int")), Error);
|
||||
ASSERT_THROW(getArray(valueAt(json, "boolean")), Error);
|
||||
}
|
||||
|
||||
TEST(getString, rightAssertions) {
|
||||
auto simple = R"({ "string": "" })"_json;
|
||||
|
||||
ASSERT_EQ(getString(valueAt(getObject(simple), "string")), "");
|
||||
}
|
||||
|
||||
TEST(getString, wrongAssertions) {
|
||||
auto json = R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })"_json;
|
||||
|
||||
ASSERT_THROW(getString(valueAt(json, "object")), Error);
|
||||
ASSERT_THROW(getString(valueAt(json, "array")), Error);
|
||||
ASSERT_THROW(getString(valueAt(json, "int")), Error);
|
||||
ASSERT_THROW(getString(valueAt(json, "boolean")), Error);
|
||||
}
|
||||
|
||||
TEST(getInteger, rightAssertions) {
|
||||
auto simple = R"({ "int": 0 })"_json;
|
||||
|
||||
ASSERT_EQ(getInteger(valueAt(getObject(simple), "int")), 0);
|
||||
}
|
||||
|
||||
TEST(getInteger, wrongAssertions) {
|
||||
auto json = R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })"_json;
|
||||
|
||||
ASSERT_THROW(getInteger(valueAt(json, "object")), Error);
|
||||
ASSERT_THROW(getInteger(valueAt(json, "array")), Error);
|
||||
ASSERT_THROW(getInteger(valueAt(json, "string")), Error);
|
||||
ASSERT_THROW(getInteger(valueAt(json, "boolean")), Error);
|
||||
}
|
||||
|
||||
TEST(getBoolean, rightAssertions) {
|
||||
auto simple = R"({ "boolean": false })"_json;
|
||||
|
||||
ASSERT_EQ(getBoolean(valueAt(getObject(simple), "boolean")), false);
|
||||
}
|
||||
|
||||
TEST(getBoolean, wrongAssertions) {
|
||||
auto json = R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })"_json;
|
||||
|
||||
ASSERT_THROW(getBoolean(valueAt(json, "object")), Error);
|
||||
ASSERT_THROW(getBoolean(valueAt(json, "array")), Error);
|
||||
ASSERT_THROW(getBoolean(valueAt(json, "string")), Error);
|
||||
ASSERT_THROW(getBoolean(valueAt(json, "int")), Error);
|
||||
}
|
||||
|
||||
} /* namespace nix */
|
||||
|
|
|
@ -18,11 +18,12 @@ libutil-tests_SOURCES := $(wildcard $(d)/*.cc)
|
|||
|
||||
libutil-tests_EXTRA_INCLUDES = \
|
||||
-I tests/unit/libutil-support \
|
||||
-I src/libutil
|
||||
$(INCLUDE_libutil) \
|
||||
$(INCLUDE_libutilc)
|
||||
|
||||
libutil-tests_CXXFLAGS += $(libutil-tests_EXTRA_INCLUDES)
|
||||
|
||||
libutil-tests_LIBS = libutil-test-support libutil
|
||||
libutil-tests_LIBS = libutil-test-support libutil libutilc
|
||||
|
||||
libutil-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS)
|
||||
|
||||
|
|
145
tests/unit/libutil/nix_api_util.cc
Normal file
145
tests/unit/libutil/nix_api_util.cc
Normal file
|
@ -0,0 +1,145 @@
|
|||
#include "config.hh"
|
||||
#include "args.hh"
|
||||
#include "nix_api_util.h"
|
||||
#include "nix_api_util_internal.h"
|
||||
#include "tests/nix_api_util.hh"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace nixC {
|
||||
|
||||
void observe_string_cb(const char * start, unsigned int n, std::string * user_data)
|
||||
{
|
||||
*user_data = std::string(start);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_util_context, nix_context_error)
|
||||
{
|
||||
std::string err_msg_ref;
|
||||
try {
|
||||
throw nix::Error("testing error");
|
||||
} catch (nix::Error & e) {
|
||||
err_msg_ref = e.what();
|
||||
nix_context_error(ctx);
|
||||
}
|
||||
ASSERT_EQ(ctx->last_err_code, NIX_ERR_NIX_ERROR);
|
||||
ASSERT_EQ(ctx->name, "nix::Error");
|
||||
ASSERT_EQ(*ctx->last_err, err_msg_ref);
|
||||
ASSERT_EQ(ctx->info->msg.str(), "testing error");
|
||||
|
||||
try {
|
||||
throw std::runtime_error("testing exception");
|
||||
} catch (std::exception & e) {
|
||||
err_msg_ref = e.what();
|
||||
nix_context_error(ctx);
|
||||
}
|
||||
ASSERT_EQ(ctx->last_err_code, NIX_ERR_UNKNOWN);
|
||||
ASSERT_EQ(*ctx->last_err, err_msg_ref);
|
||||
}
|
||||
|
||||
TEST_F(nix_api_util_context, nix_set_err_msg)
|
||||
{
|
||||
ASSERT_EQ(ctx->last_err_code, NIX_OK);
|
||||
nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error");
|
||||
ASSERT_EQ(ctx->last_err_code, NIX_ERR_UNKNOWN);
|
||||
ASSERT_EQ(*ctx->last_err, "unknown test error");
|
||||
}
|
||||
|
||||
TEST(nix_api_util, nix_version_get)
|
||||
{
|
||||
ASSERT_EQ(std::string(nix_version_get()), PACKAGE_VERSION);
|
||||
}
|
||||
|
||||
struct MySettings : nix::Config
|
||||
{
|
||||
nix::Setting<std::string> settingSet{this, "empty", "setting-name", "Description"};
|
||||
};
|
||||
|
||||
MySettings mySettings;
|
||||
static nix::GlobalConfig::Register rs(&mySettings);
|
||||
|
||||
TEST_F(nix_api_util_context, nix_setting_get)
|
||||
{
|
||||
ASSERT_EQ(ctx->last_err_code, NIX_OK);
|
||||
std::string setting_value;
|
||||
nix_err result = nix_setting_get(ctx, "invalid-key", (void *) observe_string_cb, &setting_value);
|
||||
ASSERT_EQ(result, NIX_ERR_KEY);
|
||||
|
||||
result = nix_setting_get(ctx, "setting-name", (void *) observe_string_cb, &setting_value);
|
||||
ASSERT_EQ(result, NIX_OK);
|
||||
ASSERT_STREQ("empty", setting_value.c_str());
|
||||
}
|
||||
|
||||
TEST_F(nix_api_util_context, nix_setting_set)
|
||||
{
|
||||
nix_err result = nix_setting_set(ctx, "invalid-key", "new-value");
|
||||
ASSERT_EQ(result, NIX_ERR_KEY);
|
||||
|
||||
result = nix_setting_set(ctx, "setting-name", "new-value");
|
||||
ASSERT_EQ(result, NIX_OK);
|
||||
|
||||
std::string setting_value;
|
||||
result = nix_setting_get(ctx, "setting-name", (void *) observe_string_cb, &setting_value);
|
||||
ASSERT_EQ(result, NIX_OK);
|
||||
ASSERT_STREQ("new-value", setting_value.c_str());
|
||||
}
|
||||
|
||||
TEST_F(nix_api_util_context, nix_err_msg)
|
||||
{
|
||||
// no error
|
||||
EXPECT_THROW(nix_err_msg(nullptr, ctx, NULL), nix::Error);
|
||||
|
||||
// set error
|
||||
nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error");
|
||||
|
||||
// basic usage
|
||||
std::string err_msg = nix_err_msg(NULL, ctx, NULL);
|
||||
ASSERT_EQ(err_msg, "unknown test error");
|
||||
|
||||
// advanced usage
|
||||
unsigned int sz;
|
||||
err_msg = nix_err_msg(nix_c_context_create(), ctx, &sz);
|
||||
ASSERT_EQ(sz, err_msg.size());
|
||||
}
|
||||
|
||||
TEST_F(nix_api_util_context, nix_err_info_msg)
|
||||
{
|
||||
std::string err_info;
|
||||
|
||||
// no error
|
||||
EXPECT_THROW(nix_err_info_msg(NULL, ctx, (void *) observe_string_cb, &err_info), nix::Error);
|
||||
|
||||
try {
|
||||
throw nix::Error("testing error");
|
||||
} catch (...) {
|
||||
nix_context_error(ctx);
|
||||
}
|
||||
nix_err_info_msg(nix_c_context_create(), ctx, (void *) observe_string_cb, &err_info);
|
||||
ASSERT_STREQ("testing error", err_info.c_str());
|
||||
}
|
||||
|
||||
TEST_F(nix_api_util_context, nix_err_name)
|
||||
{
|
||||
std::string err_name;
|
||||
|
||||
// no error
|
||||
EXPECT_THROW(nix_err_name(NULL, ctx, (void *) observe_string_cb, &err_name), nix::Error);
|
||||
|
||||
std::string err_msg_ref;
|
||||
try {
|
||||
throw nix::Error("testing error");
|
||||
} catch (...) {
|
||||
nix_context_error(ctx);
|
||||
}
|
||||
nix_err_name(nix_c_context_create(), ctx, (void *) observe_string_cb, &err_name);
|
||||
ASSERT_EQ(std::string(err_name), "nix::Error");
|
||||
}
|
||||
|
||||
TEST_F(nix_api_util_context, nix_err_code)
|
||||
{
|
||||
ASSERT_EQ(nix_err_code(ctx), NIX_OK);
|
||||
nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error");
|
||||
ASSERT_EQ(nix_err_code(ctx), NIX_ERR_UNKNOWN);
|
||||
}
|
||||
|
||||
}
|
|
@ -151,6 +151,16 @@ namespace nix {
|
|||
ASSERT_EQ(p1, "dir");
|
||||
}
|
||||
|
||||
TEST(baseNameOf, trailingSlashes) {
|
||||
auto p1 = baseNameOf("/dir//");
|
||||
ASSERT_EQ(p1, "dir");
|
||||
}
|
||||
|
||||
TEST(baseNameOf, absoluteNothingSlashNothing) {
|
||||
auto p1 = baseNameOf("//");
|
||||
ASSERT_EQ(p1, "");
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* isInDir
|
||||
* --------------------------------------------------------------------------*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue