fix builder tests

This commit is contained in:
Tristan 2024-01-04 05:23:06 +00:00
parent 669fccd57e
commit 28f8cfe612
6 changed files with 76 additions and 65 deletions

View file

@ -39,8 +39,8 @@
./testing/elems.test.nix
./testing/site.test.nix
./testing/style.test.nix
./testing/builder.test.nix
];
};
};
}

26
nixite/builder.nix Normal file
View file

@ -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));
}

View file

@ -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}
''
];
}

View file

@ -24,5 +24,4 @@ in
chmod +x $out/bin/nixite-server
''
];
inherit my-site;
}

47
testing/builder.test.nix Normal file
View file

@ -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;
})
])
]

View file

@ -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;