Skip to content

Commit a7e516b

Browse files
committed
lib/utils: add mkAssertions
1 parent 60adb6c commit a7e516b

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

lib/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ lib.makeExtensible (
8282
ifNonNull'
8383
listToUnkeyedAttrs
8484
literalLua
85+
mkAssertions
8586
mkIfNonNull
8687
mkIfNonNull'
8788
mkRaw

lib/utils.nix

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,37 @@ rec {
173173
in
174174
lib.literalExpression exp;
175175

176+
__messagePrefix = scope: "Nixvim (${scope}):";
177+
/**
178+
Process one or several assertions by prepending the common 'Nixvim (<scope>): ' prefix to messages.
179+
The second argument can either be a list of assertions or a single one.
180+
181+
# Example
182+
183+
```nix
184+
warnings = mkAssertions "plugins.foo" {
185+
assertion = plugins.foo.settings.barIntegration && (!plugins.bar.enable);
186+
message = "`barIntegration` is enabled but the `bar` plugin is not."
187+
}
188+
```
189+
190+
# Type
191+
192+
```
193+
mkAssertions :: String -> List -> List
194+
```
195+
*/
196+
mkAssertions =
197+
scope: assertions:
198+
let
199+
prefix = __messagePrefix scope;
200+
processAssertion = a: {
201+
inherit (a) assertion;
202+
message = "${prefix} ${lib.trim a.message}";
203+
};
204+
in
205+
builtins.map processAssertion (lib.toList assertions);
206+
176207
/**
177208
Convert one or several conditional warnings to a final warning list.
178209
The second argument can either be a list of _conditional warnings_ or a single one.
@@ -195,9 +226,9 @@ rec {
195226
mkWarnings =
196227
scope: warnings:
197228
let
229+
prefix = __messagePrefix scope;
198230
processWarning =
199-
warning:
200-
lib.optional (warning.when or true) "Nixvim (${scope}): ${lib.trim (warning.message or warning)}";
231+
warning: lib.optional (warning.when or true) "${prefix} ${lib.trim (warning.message or warning)}";
201232
in
202233
builtins.concatMap processWarning (lib.toList warnings);
203234

tests/lib-tests.nix

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,38 @@ let
491491
};
492492
};
493493

494+
testMkAssertions = {
495+
expr =
496+
(lib.nixvim.mkAssertions "foo-bar" [
497+
{
498+
assertion = true;
499+
message = "This is the message";
500+
}
501+
{
502+
assertion = false;
503+
message = "Another assertion";
504+
}
505+
])
506+
++ (lib.nixvim.mkAssertions "single-element") {
507+
assertion = true;
508+
message = " Trailing whitespaces ";
509+
};
510+
expected = [
511+
{
512+
assertion = true;
513+
message = "Nixvim (foo-bar): This is the message";
514+
}
515+
{
516+
assertion = false;
517+
message = "Nixvim (foo-bar): Another assertion";
518+
}
519+
{
520+
assertion = true;
521+
message = "Nixvim (single-element): Trailing whitespaces";
522+
}
523+
];
524+
};
525+
494526
testMkWarnings = {
495527
expr =
496528
(lib.nixvim.mkWarnings "foo-bar" [
@@ -506,12 +538,12 @@ let
506538
])
507539
++ (lib.nixvim.mkWarnings "single-element") {
508540
when = true;
509-
message = "Hello";
541+
message = " Trailing whitespaces ";
510542
};
511543
expected = [
512544
"Nixvim (foo-bar): This is the message"
513545
"Nixvim (foo-bar): This is an unconditional warning"
514-
"Nixvim (single-element): Hello"
546+
"Nixvim (single-element): Trailing whitespaces"
515547
];
516548
};
517549
};

0 commit comments

Comments
 (0)