From 8282c60d742dc6725723370249a341a944575d6c Mon Sep 17 00:00:00 2001 From: Will Dietz Date: Sun, 25 Feb 2018 16:33:17 -0600 Subject: [PATCH 1/4] tests: test nix search behavior --- tests/local.mk | 3 ++- tests/search.nix | 25 +++++++++++++++++++++++++ tests/search.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 tests/search.nix create mode 100644 tests/search.sh diff --git a/tests/local.mk b/tests/local.mk index ec7ebfb0d..9df0adf1b 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -24,7 +24,8 @@ nix_tests = \ brotli.sh \ pure-eval.sh \ check.sh \ - plugins.sh + plugins.sh \ + search.sh # parallel.sh install-tests += $(foreach x, $(nix_tests), tests/$(x)) diff --git a/tests/search.nix b/tests/search.nix new file mode 100644 index 000000000..fea6e7a7a --- /dev/null +++ b/tests/search.nix @@ -0,0 +1,25 @@ +with import ./config.nix; + +{ + hello = mkDerivation rec { + name = "hello-${version}"; + version = "0.1"; + buildCommand = "touch $out"; + meta.description = "Empty file"; + }; + foo = mkDerivation rec { + name = "foo-5"; + buildCommand = '' + mkdir -p $out + echo ${name} > $out/${name} + ''; + }; + bar = mkDerivation rec { + name = "bar-3"; + buildCommand = '' + echo "Does not build successfully" + exit 1 + ''; + meta.description = "broken bar"; + }; +} diff --git a/tests/search.sh b/tests/search.sh new file mode 100644 index 000000000..53a69a0bb --- /dev/null +++ b/tests/search.sh @@ -0,0 +1,42 @@ +source common.sh + +clearStore +clearCache + +# No packages +(( $(NIX_PATH= nix search -u|wc -l) == 0 )) + +# Haven't updated cache, still nothing +(( $(nix search -f search.nix hello|wc -l) == 0 )) +(( $(nix search -f search.nix |wc -l) == 0 )) + +# Update cache, search should work +(( $(nix search -f search.nix -u hello|wc -l) > 0 )) + +# Use cache +(( $(nix search -f search.nix foo|wc -l) > 0 )) +(( $(nix search foo|wc -l) > 0 )) + +# Test --no-cache works +# No results from cache +(( $(nix search --no-cache foo |wc -l) == 0 )) +# Does find results from file pointed at +(( $(nix search -f search.nix --no-cache foo |wc -l) > 0 )) + +# Check descriptions are searched +(( $(nix search broken | wc -l) > 0 )) + +# Check search that matches nothing +(( $(nix search nosuchpackageexists | wc -l) == 0 )) + + +## Search expressions + +# Check that empty search string matches all +EMPTY=$(nix search) +BOL=$(nix search "^") +ALL=$(nix search ".*") + +diff <(printf "%s" $EMPTY) <(printf "%s" $BOL) +# "ALL" highlights differently, not sure preferred behavior +# diff <(printf "%s" $EMPTY) <(printf "%s" $ALL) From 3cac8734acb098488bd232bcee92099410d4d167 Mon Sep 17 00:00:00 2001 From: Will Dietz Date: Sun, 25 Feb 2018 16:32:36 -0600 Subject: [PATCH 2/4] nix search: fix bug where we wrote to cache when shouldn't, breaking This is exposed by the tests added previously, and resolves the error reported in #1892: "expected JSON value". --- src/nix/search.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nix/search.cc b/src/nix/search.cc index 87cdb2d7e..1adde28cb 100644 --- a/src/nix/search.cc +++ b/src/nix/search.cc @@ -237,7 +237,7 @@ struct CmdSearch : SourceExprCommand, MixJSON throw Error("error writing to %s", tmpFile); } - if (rename(tmpFile.c_str(), jsonCacheFileName.c_str()) == -1) + if (writeCache && rename(tmpFile.c_str(), jsonCacheFileName.c_str()) == -1) throw SysError("cannot rename '%s' to '%s'", tmpFile, jsonCacheFileName); } } From 009cf9cd23e5b9198cf8977cca7c823b6b95ad5b Mon Sep 17 00:00:00 2001 From: Will Dietz Date: Sun, 25 Feb 2018 16:38:13 -0600 Subject: [PATCH 3/4] nix search: explicitly handle empty search string, fixes #1893 This is important since this is given as an example. Other patterns containing "empty search string" will still be handled differently on different platforms ("asdf|") but that's less of an issue. --- src/nix/search.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/nix/search.cc b/src/nix/search.cc index 1adde28cb..009dfae9c 100644 --- a/src/nix/search.cc +++ b/src/nix/search.cc @@ -78,6 +78,11 @@ struct CmdSearch : SourceExprCommand, MixJSON { settings.readOnlyMode = true; + // Empty search string should match all packages + // Use "^" here instead of ".*" due to differences in resulting highlighting + // (see #1893 -- libc++ claims empty search string is not in POSIX grammar) + if (re.empty()) re = "^"; + std::regex regex(re, std::regex::extended | std::regex::icase); auto state = getEvalState(); From c577186f5916c90193368492f1c4180a1386febc Mon Sep 17 00:00:00 2001 From: Will Dietz Date: Thu, 1 Mar 2018 13:24:03 -0600 Subject: [PATCH 4/4] tests/search.sh: simplify, don't rely on consistent ordering --- tests/search.sh | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/tests/search.sh b/tests/search.sh index 53a69a0bb..d83427247 100644 --- a/tests/search.sh +++ b/tests/search.sh @@ -33,10 +33,6 @@ clearCache ## Search expressions # Check that empty search string matches all -EMPTY=$(nix search) -BOL=$(nix search "^") -ALL=$(nix search ".*") - -diff <(printf "%s" $EMPTY) <(printf "%s" $BOL) -# "ALL" highlights differently, not sure preferred behavior -# diff <(printf "%s" $EMPTY) <(printf "%s" $ALL) +nix search|grep -q foo +nix search|grep -q bar +nix search|grep -q hello