diff --git a/flake.nix b/flake.nix index 3823004..594f16b 100644 --- a/flake.nix +++ b/flake.nix @@ -39,8 +39,8 @@ ./testing/elems.test.nix ./testing/site.test.nix ./testing/style.test.nix + ./testing/builder.test.nix ]; - }; }; } diff --git a/nixite/builder.nix b/nixite/builder.nix new file mode 100644 index 0000000..c709154 --- /dev/null +++ b/nixite/builder.nix @@ -0,0 +1,26 @@ +rec { + buildSiteTo = prefix: site: + builtins.toString (builtins.attrValues (builtins.mapAttrs (name: content: + if builtins.isString content + then '' + cp ${builtins.toFile name content} ${prefix}/${name} + '' + else if builtins.isPath content + then '' + cp -r ${content} ${prefix}/${name} + '' + else if builtins.isAttrs content && content ? "__toString" + then '' + cp ${builtins.toFile name (toString content)} ${prefix}/${name} + '' + else if builtins.isAttrs content + then '' + mkdir -p ${prefix}/${name} + ${buildSiteTo "${prefix}/${name}" content} + '' + else + throw "Site page must be string, path or attrset, but got ${ + builtins.typeOf content + }: [${toString content}]") + site)); +} diff --git a/nixite/make-site.nix b/nixite/make-site.nix index 3730a78..e3e5092 100644 --- a/nixite/make-site.nix +++ b/nixite/make-site.nix @@ -1,28 +1,5 @@ pkgs: raw: let - buildSiteTo = prefix: site: - builtins.toString (builtins.attrValues (builtins.mapAttrs (name: content: - if builtins.isString content - then '' - cp ${builtins.toFile name content} ${prefix}/${name} - '' - else if builtins.isPath content - then '' - cp -r ${content} ${prefix}/${name} - '' - else if builtins.isAttrs content && content ? "__toString" - then '' - cp ${builtins.toFile name (toString content)} ${prefix}/${name} - '' - else if builtins.isAttrs content - then '' - mkdir -p ${prefix}/${name} - ${buildSiteTo "${prefix}/${name}" content} - '' - else - throw "Site page must be string, path or attrset, but got ${ - builtins.typeOf content - }: [${toString content}]") - site)); + builder = import ./builder.nix; in derivation { builder = "/bin/sh"; @@ -33,7 +10,7 @@ in '' PATH=$PATH:${pkgs.coreutils}/bin mkdir $out - ${buildSiteTo "$out" raw} + ${builder.buildSiteTo "$out" raw} '' ]; } diff --git a/nixite/serve.nix b/nixite/serve.nix index 5b425bd..d2f241c 100644 --- a/nixite/serve.nix +++ b/nixite/serve.nix @@ -24,5 +24,4 @@ in chmod +x $out/bin/nixite-server '' ]; - inherit my-site; } diff --git a/testing/builder.test.nix b/testing/builder.test.nix new file mode 100644 index 0000000..b54c062 --- /dev/null +++ b/testing/builder.test.nix @@ -0,0 +1,47 @@ +{ + describe, + it, + ... +}: let + builder = import ../nixite/builder.nix; +in + with builder; [ + (describe "buildSiteTo" [ + (it "copies all the files" { + actual = buildSiteTo "." { + page = "this is a page"; + subdir = { + page = "this page is in a subdir"; + link = ./src; + }; + }; + expected = '' + cp /nix/store/crirfz0n6f8dgl1si3x7pwyw7fqm0r8l-page ./page + mkdir -p ./subdir + cp -r /nix/store/q95cn7ccixzi9w22aic4bl0ykk40ka7v-src ./subdir/link + cp /nix/store/ic6fyy8wg8r4338a3m5kinmg11igxsyj-page ./subdir/page + + ''; + }) + + (it "throws with a list for a page" { + actual = buildSiteTo "." {page = [];}; + throws = true; + }) + + (it "throws with null for a page" { + actual = buildSiteTo "." {page = null;}; + throws = true; + }) + + (it "throws with a bool for a page" { + actual = buildSiteTo "." {page = true;}; + throws = true; + }) + + (it "throws with a number for a page" { + actual = buildSiteTo "." {page = 5;}; + throws = true; + }) + ]) + ] diff --git a/testing/site.test.nix b/testing/site.test.nix index e7dca66..0faebf8 100644 --- a/testing/site.test.nix +++ b/testing/site.test.nix @@ -235,44 +235,6 @@ in }; })) ]) - (describe "copyTo" [ - (it "copies all the files" { - actual = copyTo "." { - page = "this is a page"; - subdir = { - page = "this page is in a subdir"; - link = ./src; - }; - }; - expected = '' - cp /nix/store/crirfz0n6f8dgl1si3x7pwyw7fqm0r8l-page ./page - mkdir -p ./subdir - cp -r /nix/store/q95cn7ccixzi9w22aic4bl0ykk40ka7v-src ./subdir/link - cp /nix/store/ic6fyy8wg8r4338a3m5kinmg11igxsyj-page ./subdir/page - - ''; - }) - - (it "throws with a list for a page" { - actual = copyTo "." {page = [];}; - throws = true; - }) - - (it "throws with null for a page" { - actual = copyTo "." {page = null;}; - throws = true; - }) - - (it "throws with a bool for a page" { - actual = copyTo "." {page = true;}; - throws = true; - }) - - (it "throws with a number for a page" { - actual = copyTo "." {page = 5;}; - throws = true; - }) - ]) (describe "prepare" [ (it "prepares the site" { actual = prepare {favicon = ./src/favicon.png;} my-site;