From c32d09bd8398518ee0d5dcf5421a44002a29a36d Mon Sep 17 00:00:00 2001
From: Daniel Barlow <dan@telent.net>
Date: Sun, 2 Mar 2025 21:09:11 +0000
Subject: [PATCH] output-template: run the tests

---
 pkgs/output-template/Makefile            |  3 --
 pkgs/output-template/default.nix         |  7 ++--
 pkgs/output-template/output-template.fnl |  8 +++++
 pkgs/watch-outputs/output-template.fnl   | 44 ------------------------
 4 files changed, 12 insertions(+), 50 deletions(-)
 delete mode 100644 pkgs/output-template/Makefile
 delete mode 100644 pkgs/watch-outputs/output-template.fnl

diff --git a/pkgs/output-template/Makefile b/pkgs/output-template/Makefile
deleted file mode 100644
index b0d6b6c..0000000
--- a/pkgs/output-template/Makefile
+++ /dev/null
@@ -1,3 +0,0 @@
-check:
-	./output-template  '{{' '}}' < example.ini > output
-	diff -u output example.ini.expected
diff --git a/pkgs/output-template/default.nix b/pkgs/output-template/default.nix
index ae7e525..6d60ae9 100644
--- a/pkgs/output-template/default.nix
+++ b/pkgs/output-template/default.nix
@@ -2,6 +2,7 @@
   fetchurl,
   writeFennel,
   fennel,
+  fennelrepl,
   runCommand,
   lua,
   anoia,
@@ -17,9 +18,9 @@ stdenv.mkDerivation {
   src = ./.;
 
   buildInputs = [ lua ];
-  doCheck = true;
-
+  nativeBuildInputs = [ fennelrepl ] ;
   buildPhase = ''
+    fennelrepl --test ./output-template.fnl
     cp -p ${
       writeFennel name {
         packages = [
@@ -27,11 +28,11 @@ stdenv.mkDerivation {
           lualinux
           linotify
         ];
+        macros = [ anoia.dev ];
         mainFunction = "run";
       } ./output-template.fnl
     } ${name}
   '';
-  checkPhase = "make check";
   installPhase = ''
     install -D ${name} $out/bin/${name}
   '';
diff --git a/pkgs/output-template/output-template.fnl b/pkgs/output-template/output-template.fnl
index 754b5e4..2a6fc33 100644
--- a/pkgs/output-template/output-template.fnl
+++ b/pkgs/output-template/output-template.fnl
@@ -41,4 +41,12 @@
         out (substitute (: (io.input) :read "*a") opening closing)]
     (io.write out)))
 
+(import-macros { : define-tests : expect : expect= } :anoia.assert)
+(define-tests
+  (fn slurp [name]
+    (with-open [f (assert (io.open name))] (f:read "*a")))
+  (expect=
+   (pick-values 1 (substitute (slurp "example.ini")  "{{" "}}"))
+   (slurp "example.ini.expected")))
+
 { : run }
diff --git a/pkgs/watch-outputs/output-template.fnl b/pkgs/watch-outputs/output-template.fnl
deleted file mode 100644
index 41ee982..0000000
--- a/pkgs/watch-outputs/output-template.fnl
+++ /dev/null
@@ -1,44 +0,0 @@
-(local svc (require :anoia.svc))
-
-(fn json-escape [s]
-  ;; All Unicode characters may be placed within the quotation marks,
-  ;; except for the characters that MUST be escaped:
-  ;; quotation mark, reverse solidus, and the control characters (U+0000
-  ;; through U+001F). (RFC 8259)
-  (-> s
-      (string.gsub
-       "[\"\b\f\n\r\t]" {
-                         "\b" "\\b"
-                         "\"" "\\\""
-                         "\f" "\\f"
-                         "\n" "\\n"
-                         "\r" "\\r"
-                         "\t" "\\t"
-                         })
-      (string.gsub
-       "([\x00-\x1b])"
-       (fn [x] (string.format "\\u%04X" (string.byte x))))))
-
-
-(fn substitute [text opening closing]
-  (let [delim (.. opening "(.-)" closing)
-        myenv {
-               : string
-               :output
-               (fn [service-path path]
-                 (let [s (assert (svc.open (.. service-path "/.outputs")))]
-                   (s:output path)))
-               :lua_quote #(string.format "%q" $1)
-               :json_quote (fn [x] (.. "\"" (json-escape x) "\""))
-               }]
-    (string.gsub text delim
-                 (fn [x]
-                   (assert ((load (.. "return " x) x :t myenv))
-                           (string.format "missing value for %q" x))))))
-
-(fn run []
-  (let [[opening closing] arg
-        out (substitute (: (io.input) :read "*a") opening closing)]
-    (io.write out)))
-
-{ : run }