tests/functional2: migrate search.sh

Change-Id: Id0f47fc72191d913442ff35d47df1b87ceb9f0be
This commit is contained in:
Commentator2.0
2025-09-19 15:09:22 +02:00
parent 2a622d7de7
commit a25a5739c7
4 changed files with 123 additions and 52 deletions
-1
View File
@@ -114,7 +114,6 @@ functional_tests_scripts = [
'eval.sh',
'repl.sh',
'binary-cache-build-remote.sh',
'search.sh',
'logging.sh',
'export.sh',
'config.sh',
-51
View File
@@ -1,51 +0,0 @@
source common.sh
clearStore
clearCache
(( $(nix search -f search.nix '' hello | wc -l) > 0 ))
# Check descriptions are searched
(( $(nix search -f search.nix '' broken | wc -l) > 0 ))
# Check search that matches nothing
(( $(nix search -f search.nix '' nosuchpackageexists | wc -l) == 0 ))
# Search for multiple arguments
(( $(nix search -f search.nix '' hello empty | wc -l) == 2 ))
# Multiple arguments will not exist
(( $(nix search -f search.nix '' hello broken | wc -l) == 0 ))
# No regex should return an error
(( $(nix search -f search.nix '' | wc -l) == 0 ))
## Search expressions
# Check that empty search string matches all
nix search -f search.nix '' ^ | grepQuiet foo
nix search -f search.nix '' ^ | grepQuiet bar
nix search -f search.nix '' ^ | grepQuiet hello
## Tests for multiple regex/match highlighting
# FIXME: possibly not test this with colour in the future
export CLICOLOR_FORCE=1
e=$'\x1b' # grep doesn't support \e, \033 or even \x1b
# Multiple overlapping regexes
(( $(nix search -f search.nix '' 'oo' 'foo' 'oo' | grep -c "$e\[32;1mfoo$e\\[0;1m") == 1 ))
(( $(nix search -f search.nix '' 'broken b' 'en bar' | grep -c "$e\[32;1mbroken bar$e\\[0m") == 1 ))
# Multiple matches
# Searching for 'o' should yield the 'o' in 'broken bar', the 'oo' in foo and 'o' in hello
(( $(nix search -f search.nix '' 'o' | grep -Eoc "$e\[32;1mo{1,2}$e\[(0|0;1)m") == 3 ))
# Searching for 'b' should yield the 'b' in bar and the two 'b's in 'broken bar'
# NOTE: This does not work with `grep -c` because it counts the two 'b's in 'broken bar' as one matched line
(( $(nix search -f search.nix '' 'b' | grep -Eo "$e\[32;1mb$e\[(0|0;1)m" | wc -l) == 3 ))
## Tests for --exclude
(( $(nix search -f search.nix ^ -e hello | grep -c hello) == 0 ))
(( $(nix search -f search.nix foo ^ --exclude 'foo|bar' | grep -Ec 'foo|bar') == 0 ))
(( $(nix search -f search.nix foo ^ -e foo --exclude bar | grep -Ec 'foo|bar') == 0 ))
[[ $(nix search -f search.nix '' ^ -e bar --json | jq -c 'keys') == '["foo","hello"]' ]]
+123
View File
@@ -0,0 +1,123 @@
import json
import re
import pytest
from functional2.testlib.fixtures.file_helper import CopyFile, with_files
from functional2.testlib.fixtures.nix import Nix
from functional2.testlib.utils import get_global_asset
_files = {
"search.nix": CopyFile("assets/test_search/search.nix"),
"config.nix": get_global_asset("config.nix"),
}
_exclude_args = ["search", "-f", "search.nix"]
_search_args = [*_exclude_args, ""]
@pytest.fixture(autouse=True)
def commands(nix: Nix):
nix.settings.feature("nix-command")
@with_files(_files)
def test_pkg_name(nix: Nix):
res = nix.nix([*_search_args, "hello"]).run().ok()
assert len(res.stdout_plain.splitlines()) > 1
@with_files(_files)
def test_description(nix: Nix):
res = nix.nix([*_search_args, "broken"]).run().ok()
assert len(res.stdout_plain.splitlines()) > 1
@with_files(_files)
def test_no_exist(nix: Nix):
res = nix.nix([*_search_args, "nosuchpackageexits"]).run().expect(1)
assert "error: no results for the given search" in res.stderr_plain
@with_files(_files)
def test_multiple_args_exists(nix: Nix):
res = nix.nix([*_search_args, "hello", "empty"]).run().ok()
assert len(res.stdout_plain.splitlines()) == 2
@with_files(_files)
def test_multiple_args_no_exist(nix: Nix):
res = nix.nix([*_search_args, "hello", "broken"]).run().expect(1)
assert "error: no results for the given search" in res.stderr_plain
@with_files(_files)
def test_no_args(nix: Nix):
res = nix.nix([*_search_args]).run().expect(1)
assert "error: Must provide at least one regex!" in res.stderr_plain
@with_files(_files)
def test_match_all(nix: Nix):
res = nix.nix([*_search_args, "^"]).run().ok()
out = res.stdout_plain
for pkg in ["foo", "bar", "hello"]:
assert pkg in out
# FIXME(Jade): possibly not test this with colour in the future
@with_files(_files)
def test_overlap_1(nix: Nix):
nix.env.set_env("FORCE_COLOR", "1")
res = nix.nix([*_search_args, "oo", "foo", "oo"]).run().ok()
out = res.stdout
assert out == b"* \x1b[0;1m\x1b[32;1mfoo\x1b[0;1m\x1b[0m (5)\n"
@with_files(_files)
def test_overlap_2(nix: Nix):
nix.env.set_env("FORCE_COLOR", "1")
res = nix.nix([*_search_args, "broken b", "en bar"]).run().ok()
out = res.stdout
assert out == b"* \x1b[0;1mbar\x1b[0m (3)\n \x1b[32;1mbroken bar\x1b[0m\n"
@with_files(_files)
def test_multiple_finds_o(nix: Nix):
"""Searching for 'o' should yield the 'o' in 'broken bar', the 'oo' in foo and 'o' in hello"""
nix.env.set_env("FORCE_COLOR", "1")
res = nix.nix([*_search_args, "o"]).run().ok()
out = res.stdout_s
assert len(re.findall(r"\x1b\[32;1mo{1,2}\x1b\[(0|0;1)m", out)) == 3
@with_files(_files)
def test_multiple_finds_b(nix: Nix):
"""Searching for 'b' should yield the 'b' in bar and the two 'b's in 'broken bar'"""
nix.env.set_env("FORCE_COLOR", "1")
res = nix.nix([*_search_args, "b"]).run().ok()
out = res.stdout_s
assert len(re.findall(r"\x1b\[32;1mb\x1b\[(0|0;1)m", out)) == 3
@with_files(_files)
def test_exclude_hello(nix: Nix):
res = nix.nix([*_exclude_args, "", "^", "-e", "hello"]).run().ok()
assert res.stdout_plain.count("hello") == 0
@with_files(_files)
def test_exclude_foo_regex(nix: Nix):
res = nix.nix([*_exclude_args, "foo", "^", "--exclude", "foo|bar"]).run().expect(1)
assert "error: no results for the given search" in res.stderr_plain
@with_files(_files)
def test_exclude_foo_multiple_args(nix: Nix):
res = nix.nix([*_exclude_args, "foo", "^", "-e", "foo", "--exclude", "bar"]).run().expect(1)
assert "error: no results for the given search" in res.stderr_plain
@with_files(_files)
def test_exclude_bar(nix: Nix):
res = nix.nix([*_exclude_args, "", "^", "-e", "bar", "--json"]).run().ok()
assert json.loads(res.stdout_plain).keys() == {"foo", "hello"}