aboutsummaryrefslogtreecommitdiffstats
path: root/tests.nix
blob: c7955fbd7006e4d6b50f0b14e45c478cc6144f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{ pkgs ? import sources.nixpkgs { overlays = [ (import ./pkgs { }) ]; }
, sources ? import ./nix/sources.nix
}:
let
  haskellPackages = pkgs.haskell.packages.ghc98;
  makeDocTest = n: i: (import (sources.nixpkgs + "/nixos/lib") { }).runTest {
    name = "doc-test-${n}";
    imports = [{
      nodes =
        {
          machine = { lib, pkgs, nodes, ... }: {
            environment.systemPackages = [ ];
            systemd.services.backend.wantedBy = [ "multi-user.target" ];
            systemd.services.backend.preStart = ''
              export HOME=$(mktemp -d)
              ${pkgs.git}/bin/git config --global user.email "you@example.com"
              ${pkgs.git}/bin/git config --global user.name "Your Name"
              ${pkgs.git}/bin/git init
              ${pkgs.git}/bin/git commit -m init --allow-empty
            '';
            systemd.services.backend.script = ''
              UUID_SEED=0 ${haskellPackages.backend}/bin/backend serve .
            '';
          };
        };
      testScript = ''
        start_all();
        machine.wait_for_unit("backend");
        machine.succeed("${pkgs.bash}/bin/bash ${makeDocTestScript n i}");
      '';

    }];
    hostPkgs = pkgs;
    defaults.networking.firewall.enable = false;
  };
  makeDocTestScript = n: i: pkgs.runCommand "doc-test-script-${n}" { } ''
    set -efu
    export PATH=${pkgs.lib.makeBinPath [
      pkgs.coreutils
      pkgs.glow
      pkgs.jq
      pkgs.pandoc
    ]}
    (
      cat <<'EOF'
    set -efux
    readonly tmp=$(mktemp -d)
    trap 'rm -rf "$tmp"' EXIT
    cd "$tmp"
    export ACMS_CONTENT=$PWD/content # TODO
    export PATH=${pkgs.lib.makeBinPath [
    haskellPackages.cli
    pkgs.jq
    ]}''${PATH+:$PATH}
    EOF
      cat ${i} | pandoc --to json | jq -c '
        .blocks | map(select(.t == "CodeBlock") | .c |
          {
            t: .[0].[1].[0],
            v: .[1]
          }
        ) | .[]
      ' | while read -r l; do
        t=$(echo "$l" | jq -r .t)
        v=$(echo "$l" | jq -r .v)
        if test "$t" = console; then
          cat <<EOF
    LASTOUT=\$(
      $v
      )
    EOF
        else
          cat <<EOF
    test "\$LASTOUT" = '$v'
    EOF
        fi
      done
    ) > $out
  '';
in
{
  get-started-cli = makeDocTest "get-started-cli" ./docs/get-started-cli.md;
}