From f4c92f8872006dd95985589a229bb532943d6a89 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Sat, 28 Feb 2026 13:19:13 +0100 Subject: [PATCH] testing: migrate function-trace.sh Change-Id: I44bd28165070f55f21b052bc02ba6f2ea5787a1d --- tests/functional/function-trace.sh | 69 ------------------- tests/functional/meson.build | 1 - tests/functional2/eval/test_function_trace.py | 42 +++++++++++ 3 files changed, 42 insertions(+), 70 deletions(-) delete mode 100755 tests/functional/function-trace.sh create mode 100644 tests/functional2/eval/test_function_trace.py diff --git a/tests/functional/function-trace.sh b/tests/functional/function-trace.sh deleted file mode 100755 index 71f18b67f..000000000 --- a/tests/functional/function-trace.sh +++ /dev/null @@ -1,69 +0,0 @@ -#!/usr/bin/env bash - -source common.sh - -set +x - -expect_trace() { - expr="$1" - expect="$2" - actual=$( - nix-instantiate \ - --trace-function-calls \ - --expr "$expr" 2>&1 \ - | grep "function-trace" \ - | sed -e 's/ [0-9]*$//' \ - || true - ) - - echo -n "Tracing expression '$expr'" - msg=$(diff -swB \ - <(echo "$expect") \ - <(echo "$actual") - ) && result=0 || result=$? - if [ $result -eq 0 ]; then - echo " ok." - else - echo " failed. difference:" - echo "$msg" - return $result - fi -} - -# failure inside a tryEval -expect_trace 'builtins.tryEval (throw "example")' " -function-trace entered «string»:1:1 at -function-trace entered «string»:1:19 at -function-trace exited «string»:1:19 at -function-trace exited «string»:1:1 at -" - -# Missing argument to a formal function -expect_trace '({ x }: x) { }' " -function-trace entered «string»:1:1 at -function-trace exited «string»:1:1 at -" - -# Too many arguments to a formal function -expect_trace '({ x }: x) { x = "x"; y = "y"; }' " -function-trace entered «string»:1:1 at -function-trace exited «string»:1:1 at -" - -# Not enough arguments to a lambda -expect_trace '(x: y: x + y) 1' " -function-trace entered «string»:1:1 at -function-trace exited «string»:1:1 at -" - -# Too many arguments to a lambda -expect_trace '(x: x) 1 2' " -function-trace entered «string»:1:1 at -function-trace exited «string»:1:1 at -" - -# Not a function -expect_trace '1 2' " -function-trace entered «string»:1:1 at -function-trace exited «string»:1:1 at -" diff --git a/tests/functional/meson.build b/tests/functional/meson.build index e1af80e6b..816ee6cdc 100644 --- a/tests/functional/meson.build +++ b/tests/functional/meson.build @@ -92,7 +92,6 @@ functional_tests_scripts = [ 'nix-copy-ssh-ng.sh', 'pre-hook.sh', 'post-hook.sh', - 'function-trace.sh', 'eval-store.sh', 'derivation-json.sh', 'import-derivation.sh', diff --git a/tests/functional2/eval/test_function_trace.py b/tests/functional2/eval/test_function_trace.py new file mode 100644 index 000000000..55eed127a --- /dev/null +++ b/tests/functional2/eval/test_function_trace.py @@ -0,0 +1,42 @@ +import pytest +from testlib.fixtures.nix import Nix + + +class TestFunctionTrace: + @pytest.fixture(autouse=True) + def setup(self, nix: Nix): + self.nix = nix + + def call(self, expr: str) -> list[str]: + trace = ( + self.nix.nix_instantiate(["--trace-function-calls", "--expr", expr]).run().stderr_plain + ) + return [ + " ".join(line.split(" ")[:-1]) + for line in trace.splitlines() + if line.startswith("function-trace") + ] + + def test_try_eval(self): + assert self.call('builtins.tryEval (throw "example")') == [ + "function-trace entered «string»:1:1 at", + "function-trace entered «string»:1:19 at", + "function-trace exited «string»:1:19 at", + "function-trace exited «string»:1:1 at", + ] + + @pytest.mark.parametrize( + "expr", + [ + "({ x }: x) { }", + '({ x }: x) { x = "x"; y = "y"; }', + "(x: y: x + y) 1", + "(x: x) 1 2", + "1 2", + ], + ) + def test_single_call(self, expr: str): + assert self.call(expr) == [ + "function-trace entered «string»:1:1 at", + "function-trace exited «string»:1:1 at", + ]