From f59ab9aba506c1ed149f7093f5543ef021567ebc Mon Sep 17 00:00:00 2001 From: sterni Date: Mon, 12 Apr 2021 19:30:41 +0200 Subject: feat(sterni/nix/fun): add hasEllipsis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As a complementation to builtins.functionArgs this function checks if the function has a set pattern that contains an ellipsis (i. e. `{ [arg, [ arg1, [ … ]]] ... }:`). The implementation of this is pretty cursed however since there is no clean way to do this in vanilla nix: We need to match on the output of builtins.toXML which does try to serialize functions by outputting their argument and information about it (whether it is a normal argument or a attribute set pattern, in the latter case it also serialize every component of the pattern). Change-Id: I0f33721811a3180cec205a0c98e6d92e10e92075 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2950 Tested-by: BuildkiteCI Reviewed-by: sterni --- users/sterni/nix/fun/tests/default.nix | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 users/sterni/nix/fun/tests/default.nix (limited to 'users/sterni/nix/fun/tests') diff --git a/users/sterni/nix/fun/tests/default.nix b/users/sterni/nix/fun/tests/default.nix new file mode 100644 index 0000000000..6492554306 --- /dev/null +++ b/users/sterni/nix/fun/tests/default.nix @@ -0,0 +1,29 @@ +{ depot, ... }: + +let + inherit (depot.nix.runTestsuite) + runTestsuite + it + assertEq + ; + + inherit (depot.users.sterni.nix) + fun + ; + + hasEllipsisTests = it "checks fun.hasEllipsis" [ + (assertEq "Malicious string" false + (fun.hasEllipsis (builtins.toXML ({ foo, ... }: 12)))) + (assertEq "No function" false + (fun.hasEllipsis 23)) + (assertEq "No attribute set pattern" false + (fun.hasEllipsis (a: a + 2))) + (assertEq "No ellipsis" false + (fun.hasEllipsis ({ foo, bar }: foo + bar))) + (assertEq "Ellipsis" true + (fun.hasEllipsis ({ depot, pkgs, ... }: 42))) + ]; +in + runTestsuite "nix.fun" [ + hasEllipsisTests + ] -- cgit 1.4.1