Skip to content

Commit 451986c

Browse files
Merge pull request #12 from gytis-ivaskevicius/test-coverage-improvements
test: expand coverage with DSL edge cases, Lua syntax validation, and module smoke tests
2 parents 71acbf5 + b75935d commit 451986c

25 files changed

Lines changed: 867 additions & 345 deletions

.github/workflows/ci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: CI
2+
on:
3+
push:
4+
branches: [master]
5+
pull_request:
6+
branches: [master]
7+
jobs:
8+
check:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: DeterminateSystems/nix-installer-action@v11
13+
- uses: DeterminateSystems/magic-nix-cache-action@v2
14+
- run: nix flake check

apps.nix

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,71 @@
1-
{ utils, pkgs, lib ? pkgs.lib }:
1+
{
2+
utils,
3+
pkgs,
4+
lib ? pkgs.lib,
5+
}:
26
let
3-
generateMarkdown = optionsFile:
7+
generateMarkdown =
8+
optionsFile:
49
let
510
dsl = import ./lib/dsl.nix { inherit lib; };
6-
options = (pkgs.lib.evalModules {
7-
modules = [
8-
./lib/wrapper.options.nix
9-
./lib/api.options.nix
10-
./lib/lsp.options.nix
11-
./lib/treesitter.options.nix
12-
];
11+
inherit
12+
(
13+
(pkgs.lib.evalModules {
14+
modules = [
15+
./lib/wrapper.options.nix
16+
./lib/api.options.nix
17+
./lib/lsp.options.nix
18+
./lib/treesitter.options.nix
19+
];
1320

14-
specialArgs = { inherit pkgs dsl; };
15-
}).options;
16-
json = lib.filterAttrs (_: v: builtins.elem (toString optionsFile) v.declarations) (pkgs.nixosOptionsDoc { inherit options; }).optionsNix;
17-
parseDefinition = it: if builtins.isString it then it else if it._type == "literalExpression" then it.text else throw "Unknown definition: ${it}";
21+
specialArgs = { inherit pkgs dsl; };
22+
})
23+
)
24+
options
25+
;
26+
json =
27+
lib.filterAttrs (_: v: builtins.elem (toString optionsFile) v.declarations)
28+
(pkgs.nixosOptionsDoc { inherit options; }).optionsNix;
29+
parseDefinition =
30+
it:
31+
if builtins.isString it then
32+
it
33+
else if it._type == "literalExpression" then
34+
it.text
35+
else
36+
throw "Unknown definition: ${it}";
1837
in
19-
pkgs.writeText "options.md" (lib.concatStringsSep "\n\n" (lib.mapAttrsToList
20-
(name: value: ''
21-
## ${builtins.replaceStrings ["<" ">"] [ "\\<" "\\>"] name}
38+
pkgs.writeText "options.md" (
39+
lib.concatStringsSep "
40+
41+
" (
42+
lib.mapAttrsToList (name: value: ''
43+
## ${builtins.replaceStrings [ "<" ">" ] [ "\<" "\>" ] name}
2244
23-
${value.description}
45+
${value.description}
2446
2547
26-
**Type:** ${value.type}
48+
**Type:** ${value.type}
2749
28-
**Default:** `${value.defaultText or parseDefinition (value.default or "")}`
50+
**Default:** `${value.defaultText or parseDefinition (value.default or "")}`
2951
30-
**Example:**
31-
```nix
32-
${parseDefinition (value.example or "")}
33-
```
34-
'')
35-
json));
52+
**Example:**
53+
```nix
54+
${parseDefinition (value.example or "")}
55+
```
56+
'') json
57+
)
58+
);
3659
mkApp = drv: utils.mkApp { inherit drv; };
3760
in
3861
{
39-
generateDocs = mkApp (pkgs.writeShellScriptBin "create-docs.sh" ''
40-
mkdir -p docs
41-
cp -f ${generateMarkdown ./lib/api.options.nix} docs/api.options.md
42-
cp -f ${generateMarkdown ./lib/wrapper.options.nix} docs/wrapper.options.md
43-
cp -f ${generateMarkdown ./lib/lsp.options.nix} docs/lsp.options.md
44-
cp -f ${generateMarkdown ./lib/treesitter.options.nix} docs/treesitter.options.md
45-
'');
62+
generateDocs = mkApp (
63+
pkgs.writeShellScriptBin "create-docs.sh" ''
64+
mkdir -p docs
65+
cp -f ${generateMarkdown ./lib/api.options.nix} docs/api.options.md
66+
cp -f ${generateMarkdown ./lib/wrapper.options.nix} docs/wrapper.options.md
67+
cp -f ${generateMarkdown ./lib/lsp.options.nix} docs/lsp.options.md
68+
cp -f ${generateMarkdown ./lib/treesitter.options.nix} docs/treesitter.options.md
69+
''
70+
);
4671
}

check-utils.nix

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,83 @@
11
systemOrPkgs:
22
let
3-
inherit (builtins) replaceStrings toJSON isAttrs foldl' unsafeDiscardStringContext elemAt match split concatStringsSep isList substring stringLength length attrNames;
3+
inherit (builtins)
4+
replaceStrings
5+
toJSON
6+
isAttrs
7+
foldl'
8+
unsafeDiscardStringContext
9+
elemAt
10+
match
11+
split
12+
concatStringsSep
13+
isList
14+
substring
15+
stringLength
16+
length
17+
attrNames
18+
;
419
system = systemOrPkgs.system or systemOrPkgs;
520
pipe = val: functions: foldl' (x: f: f x) val functions;
621
max = x: y: if x > y then x else y;
722

823
# Minimized copy-paste https://github.com/NixOS/nixpkgs/blob/master/lib/strings.nix#L746-L762
9-
sanitizeDerivationName = string: pipe (toString string) [
10-
# Get rid of string context. This is safe under the assumption that the
11-
# resulting string is only used as a derivation name
12-
unsafeDiscardStringContext
13-
# Strip all leading "."
14-
(x: elemAt (match "\\.*(.*)" x) 0)
15-
# Split out all invalid characters
16-
# https://github.com/NixOS/nix/blob/2.3.2/src/libstore/store-api.cc#L85-L112
17-
# https://github.com/NixOS/nix/blob/2242be83c61788b9c0736a92bb0b5c7bbfc40803/nix-rust/src/store/path.rs#L100-L125
18-
(split "[^[:alnum:]+._?=-]+")
19-
# Replace invalid character ranges with a "-"
20-
(map (s: if isList s then "-" else s))
21-
(concatStringsSep "")
22-
# Limit to 211 characters (minus 4 chars for ".drv")
23-
(x: substring (max (stringLength x - 207) 0) (-1) x)
24-
# If the result is empty, replace it with "?EMPTY?"
25-
(x: if stringLength x == 0 then "?EMPTY?" else x)
26-
];
24+
sanitizeDerivationName =
25+
string:
26+
pipe (toString string) [
27+
# Get rid of string context. This is safe under the assumption that the
28+
# resulting string is only used as a derivation name
29+
unsafeDiscardStringContext
30+
# Strip all leading "."
31+
(x: elemAt (match "\.*(.*)" x) 0)
32+
# Split out all invalid characters
33+
# https://github.com/NixOS/nix/blob/2.3.2/src/libstore/store-api.cc#L85-L112
34+
# https://github.com/NixOS/nix/blob/2242be83c61788b9c0736a92bb0b5c7bbfc40803/nix-rust/src/store/path.rs#L100-L125
35+
(split "[^[:alnum:]+._?=-]+")
36+
# Replace invalid character ranges with a "-"
37+
(map (s: if isList s then "-" else s))
38+
(concatStringsSep "")
39+
# Limit to 211 characters (minus 4 chars for ".drv")
40+
(x: substring (max (stringLength x - 207) 0) (-1) x)
41+
# If the result is empty, replace it with "?EMPTY?"
42+
(x: if stringLength x == 0 then "?EMPTY?" else x)
43+
];
2744

2845
# Minimized version of 'sanitizeDerivationName' function
29-
str = it:
46+
str =
47+
it:
3048
if it == null then
3149
"null"
3250
# TODO: Better solution is needed
3351
else if isAttrs it then
3452
(replaceStrings [ "-" ] [ "" ] (sanitizeDerivationName (toJSON it)))
35-
else sanitizeDerivationName it;
53+
else
54+
sanitizeDerivationName it;
3655

37-
test = name: command: derivation {
38-
inherit system;
39-
name = substring 0 (211 - 33) name;
40-
builder = "/bin/sh";
41-
args = [ "-c" command ];
42-
};
56+
test =
57+
name: command:
58+
derivation {
59+
inherit system;
60+
name = substring 0 (211 - 33) name;
61+
builder = "/bin/sh";
62+
args = [
63+
"-c"
64+
command
65+
];
66+
};
4367
in
4468
{
4569

46-
isEqual = a: b:
47-
if a == b
48-
then test "SUCCESS__${str a}__IS_EQUAL__${str b}" "echo success > $out"
49-
else test "FAILURE__${str a}__NOT_EQUAL__${str b}" "exit 1";
70+
isEqual =
71+
a: b:
72+
if a == b then
73+
test "SUCCESS__${str a}__IS_EQUAL__${str b}" "echo success > $out"
74+
else
75+
test "FAILURE__${str a}__NOT_EQUAL__${str b}" "exit 1";
5076

51-
hasKey = attrset: key:
52-
if attrset ? ${str key}
53-
then test "SUCCESS__${str key}__EXISTS_IN_ATTRSET" "echo success > $out"
54-
else test "FAILURE__${str key}__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_${str(length (attrNames attrset))}" "exit 1";
77+
hasKey =
78+
attrset: key:
79+
if attrset ? ${str key} then
80+
test "SUCCESS__${str key}__EXISTS_IN_ATTRSET" "echo success > $out"
81+
else
82+
test "FAILURE__${str key}__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_${str (length (attrNames attrset))}" "exit 1";
5583
}

0 commit comments

Comments
 (0)