set up expression-level tests

This commit is contained in:
Valentin Gagarin 2024-11-27 11:21:59 +01:00
parent 22c7c3091f
commit a8dcc9f298
5 changed files with 74 additions and 23 deletions

View file

@ -30,6 +30,26 @@ Structured content is managed through Nix expressions, and copy is written in [C
- Edit any of the files, see [repository layout](#repository-layout) for guidance - Edit any of the files, see [repository layout](#repository-layout) for guidance
# Testing
As a derivation, e.g. for CI:
```bash
nix-build -A tests
```
In the development shell:
```bash
run-tests
```
Running tests in a loop on source code changes:
```bash
test-loop
```
# Repository layout # Repository layout
- [content](./content) - [content](./content)

View file

@ -14,12 +14,7 @@ let
in in
new // { types = prev.recursiveUpdate prev.types new.types; }; new // { types = prev.recursiveUpdate prev.types new.types; };
lib'' = lib.extend lib'; lib'' = lib.extend lib';
# TODO: update when the PR to expose `pkgs.devmode` is merged
# https://github.com/NixOS/nixpkgs/pull/354556
devmode = pkgs.callPackage "${sources.devmode-reusable}/pkgs/by-name/de/devmode/package.nix" {
buildArgs = "${toString ./.} -A build --show-trace";
open = "/index.html";
};
in in
rec { rec {
lib = lib''; lib = lib'';
@ -38,11 +33,36 @@ rec {
inherit (result.config) build; inherit (result.config) build;
shell = pkgs.mkShellNoCC { shell =
packages = with pkgs; [ let
cmark run-tests = pkgs.writeShellApplication {
npins name = "run-tests";
text = with pkgs; with lib; ''
${getExe nix-unit} ${toString ./tests.nix} "$@"
'';
};
test-loop = pkgs.writeShellApplication {
name = "test-loop";
text = with pkgs; with lib; ''
${getExe watchexec} -w ${toString ./.} -- ${getExe nix-unit} ${toString ./tests.nix}
'';
};
devmode = pkgs.devmode.override {
buildArgs = "${toString ./.} -A build --show-trace";
open = "/index.html";
};
in
pkgs.mkShellNoCC {
packages = [
pkgs.npins
run-tests
test-loop
devmode devmode
]; ];
}; };
tests = with pkgs; with lib; runCommand "run-tests" { } ''
touch $out
${getExe nix-unit} ${./tests.nix} "$@"
'';
} }

View file

@ -104,7 +104,7 @@ rec {
path1 = subpath.components path1'; path1 = subpath.components path1';
prefix1 = with lib; take (length path1 - 1) path1; prefix1 = with lib; take (length path1 - 1) path1;
path2 = subpath.components path2'; path2 = subpath.components path2';
prefix2 = with lib; take (length path1 - 1) path2; prefix2 = with lib; take (length path2 - 1) path2;
commonPrefixLength = with lists; commonPrefixLength = with lists;
findFirstIndex (i: i.fst != i.snd) findFirstIndex (i: i.fst != i.snd)

View file

@ -1,22 +1,22 @@
{ {
"pins": { "pins": {
"devmode-reusable": { "nix-unit": {
"type": "Git", "type": "Git",
"repository": { "repository": {
"type": "GitHub", "type": "GitHub",
"owner": "fricklerhandwerk", "owner": "nix-community",
"repo": "nixpkgs" "repo": "nix-unit"
}, },
"branch": "refactor-devmode", "branch": "main",
"revision": "f0746a6690939987734d6519a2e3daf28ed36d87", "revision": "2071bbb765681ac3d8194ec560c8b27ff2a3b541",
"url": "https://github.com/fricklerhandwerk/nixpkgs/archive/f0746a6690939987734d6519a2e3daf28ed36d87.tar.gz", "url": "https://github.com/nix-community/nix-unit/archive/2071bbb765681ac3d8194ec560c8b27ff2a3b541.tar.gz",
"hash": "011kg3c2mfy7y808llpmq3hf6vv6rlazx8m11w41pnym4kwr3ivz" "hash": "0blz1kcmn9vnr9q3iqp2mv13hv3pdccljmmc54f8j7ybf5v0wgmp"
}, },
"nixpkgs": { "nixpkgs": {
"type": "Channel", "type": "Channel",
"name": "nixpkgs-unstable", "name": "nixpkgs-unstable",
"url": "https://releases.nixos.org/nixpkgs/nixpkgs-24.11pre691017.b69de56fac8c/nixexprs.tar.xz", "url": "https://releases.nixos.org/nixpkgs/nixpkgs-25.05pre711046.8edf06bea5bc/nixexprs.tar.xz",
"hash": "0z32pj0lh5ng2a6cn0qfmka8cynnygckn5615mkaxq2aplkvgzx3" "hash": "1mwsn0rvfm603svrq3pca4c51zlix5gkyr4gl6pxhhq3q6xs5s8y"
} }
}, },
"version": 3 "version": 3

11
website/tests.nix Normal file
View file

@ -0,0 +1,11 @@
# tests written for running with `nix-unit`
# https://github.com/nix-community/nix-unit
let
inherit (import ./. { }) lib;
in
{
test-relativePath = {
expr = with lib; relativePath "bar" "baz";
expected = "./baz";
};
}