From aeb174ac6535a1536619cd04bcaa78c289457504 Mon Sep 17 00:00:00 2001 From: tristan Date: Tue, 2 Jan 2024 11:13:47 +0000 Subject: [PATCH] document throws, run alejandra --- README.md | 4 +++ flake.lock | 24 +++++++++++++++ flake.nix | 18 +++++++---- mytest.nix | 14 +++++++++ testing/default.nix | 2 +- testing/import.nix | 13 ++++---- testing/it.nix | 74 +++++++++++++++++++++++++-------------------- testing/run.nix | 3 +- testing/watch.nix | 1 - 9 files changed, 105 insertions(+), 48 deletions(-) create mode 100644 flake.lock create mode 100644 mytest.nix diff --git a/README.md b/README.md index 15f04e9..b063c07 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ Make a test file: actual = 2 + 2; expected = 5; }) + (it "throws a party" { + actual = throw "a party"; + throws = true; + }) ] ``` diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..1623a6b --- /dev/null +++ b/flake.lock @@ -0,0 +1,24 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1703013332, + "narHash": "sha256-+tFNwMvlXLbJZXiMHqYq77z/RfmpfpiI3yjL6o/Zo9M=", + "path": "/nix/store/50bgi74d890mpkp90w1jwc5g0dw4dccr-source", + "rev": "54aac082a4d9bb5bbc5c4e899603abfb76a3f6d6", + "type": "path" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix index 1bf2323..554d259 100644 --- a/flake.nix +++ b/flake.nix @@ -1,10 +1,16 @@ { description = "A testing library for nix"; - outputs = { self, nixpkgs }: - let - system = "x86_64-linux"; - pkgs = import nixpkgs { inherit system; }; - testing = import ./testing/. { inherit pkgs; }; - in testing; + outputs = { + self, + nixpkgs, + }: let + system = "x86_64-linux"; + pkgs = import nixpkgs {inherit system;}; + tix = import ./testing/. {inherit pkgs;}; + in + tix + // { + packages.x86_64-linux.test = tix.run [./mytest.nix]; + }; } diff --git a/mytest.nix b/mytest.nix new file mode 100644 index 0000000..28c47a3 --- /dev/null +++ b/mytest.nix @@ -0,0 +1,14 @@ +{it, ...}: [ + (it "can add numbers" { + actual = 2 + 2; + expected = 4; + }) + (it "loves big brother" { + actual = 2 + 2; + expected = 5; + }) + (it "won't throw a party" { + actual = "a party"; + throws = true; + }) +] diff --git a/testing/default.nix b/testing/default.nix index 6c2fb01..e0c1e05 100644 --- a/testing/default.nix +++ b/testing/default.nix @@ -1,4 +1,4 @@ -{ pkgs ? import { } }: { +{pkgs ? import {}}: { run = import ./run.nix pkgs; it = import ./it.nix; import = import ./import.nix; diff --git a/testing/import.nix b/testing/import.nix index 3bd8e78..d59f086 100644 --- a/testing/import.nix +++ b/testing/import.nix @@ -1,6 +1,7 @@ -path: -let it = import ./it.nix; -in builtins.trace ("testing " + builtins.baseNameOf path) { - inherit path; - results = (import path { inherit it; }); -} +path: let + it = import ./it.nix; +in + builtins.trace ("testing " + builtins.baseNameOf path) { + inherit path; + results = import path {inherit it;}; + } diff --git a/testing/it.nix b/testing/it.nix index a3cb9d0..cbf5021 100644 --- a/testing/it.nix +++ b/testing/it.nix @@ -1,41 +1,51 @@ -msg: -{ actual, expected ? { }, asString ? false, asJSON ? false -, removeDunders ? false, safeToPrint ? true, throws ? false }: -let +msg: { + actual, + expected ? {}, + asString ? false, + asJSON ? false, + removeDunders ? false, + safeToPrint ? true, + throws ? false, +}: let preProcess = v: - if removeDunders then - undunder v - else if asString then - toString v - else if asJSON then - builtins.toJSON v - else - v; + if removeDunders + then undunder v + else if asString + then toString v + else if asJSON + then builtins.toJSON v + else v; a = preProcess actual; e = preProcess expected; undunder = v: - if builtins.isAttrs v then - builtins.removeAttrs v [ "__toString" "__functor" ] - else - v; + if builtins.isAttrs v + then builtins.removeAttrs v ["__toString" "__functor"] + else v; - out = (if safeToPrint then - builtins.toJSON (undunder (if throws then - (builtins.tryEval actual).value - else { - inherit actual expected; - })) - else - ''{"msg": "cannot be stringified ):"}''); + out = + if safeToPrint + then + builtins.toJSON (undunder ( + if throws + then (builtins.tryEval actual).value + else { + inherit actual expected; + } + )) + else ''{"msg": "cannot be stringified ):"}''; success = - if throws then (builtins.tryEval actual).success == false else (a == e); - -in { - inherit success msg; -} // (if success then - { } -else - builtins.trace "FAILED ${msg}" { inherit actual expected; }) + if throws + then (builtins.tryEval actual).success == false + else (a == e); +in + { + inherit success msg; + } + // ( + if success + then {} + else builtins.trace "FAILED ${msg}" {inherit actual expected;} + ) diff --git a/testing/run.nix b/testing/run.nix index 4c2e459..7399a8d 100644 --- a/testing/run.nix +++ b/testing/run.nix @@ -1,5 +1,4 @@ -pkgs: files: -let +pkgs: files: let test = import ./import.nix; res = map test files; resFile = builtins.toFile "results" (builtins.toJSON res); diff --git a/testing/watch.nix b/testing/watch.nix index dfec78d..3144fcd 100644 --- a/testing/watch.nix +++ b/testing/watch.nix @@ -7,4 +7,3 @@ pkgs.writeShellScriptBin "watch" '' pkill ${pname} done '' -