Skip to content

Commit

Permalink
nix: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
h7x4 committed Oct 27, 2024
1 parent 313d27e commit 9f548da
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 21 deletions.
104 changes: 92 additions & 12 deletions test/markup/nix/default.expect.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,105 @@
{ stdenv, foo, bar ? <span class="hljs-literal">false</span>, ... }:
{
stdenv,
pkgs <span class="hljs-operator">?</span> <span class="hljs-built_in">import</span> <span class="hljs-symbol">&lt;nixpkgs&gt;</span> {},
home-manager <span class="hljs-operator">?</span> <span class="hljs-built_in">import</span> <span class="hljs-symbol">&lt;a/b/c/home-manager&gt;</span>,
foo <span class="hljs-operator">?</span> <span class="hljs-string">&quot;hello&quot;</span>,
bar <span class="hljs-operator">?</span> <span class="hljs-literal">false</span>,
...
}:

<span class="hljs-comment">/*
* foo
*/</span>

<span class="hljs-keyword">let</span>
<span class="hljs-attr">a</span> = <span class="hljs-number">1</span>; <span class="hljs-comment"># just a comment</span>
<span class="hljs-attr">b</span> = <span class="hljs-literal">null</span>;
<span class="hljs-attr">c</span> = <span class="hljs-built_in">toString</span> <span class="hljs-number">10</span>;
<span class="hljs-keyword">in</span> stdenv.mkDerivation <span class="hljs-keyword">rec</span> {
<span class="hljs-attr">name</span> = <span class="hljs-string">&quot;foo-<span class="hljs-subst">${version}</span>&quot;</span>;
<span class="hljs-attr">version</span> = <span class="hljs-string">&quot;1.3&quot;</span>;
<span class="hljs-attr">a</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; <span class="hljs-comment"># just a comment</span>
<span class="hljs-attr">b</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;
<span class="hljs-attr">c</span> <span class="hljs-operator">=</span> <span class="hljs-built_in">toString</span> <span class="hljs-number">10</span>;

<span class="hljs-attr">configureFlags</span> = [ <span class="hljs-string">&quot;--with-foo2&quot;</span> ] ++ stdenv.lib.optional bar <span class="hljs-string">&quot;--with-foo=<span class="hljs-subst">${ <span class="hljs-keyword">with</span> stdenv.lib; foo }</span>&quot;</span>
<span class="hljs-keyword">inherit</span> (<span class="hljs-built_in">builtins</span>) concatLists;
<span class="hljs-keyword">inherit</span> (lib) genAttrs;
<span class="hljs-keyword">in</span>

<span class="hljs-attr">postInstall</span> = <span class="hljs-string">&#x27;&#x27;
{
<span class="hljs-attr">number</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;
<span class="hljs-attr">floating_point</span> <span class="hljs-operator">=</span> <span class="hljs-number">2.3</span>;

<span class="hljs-attr">dash_usage</span> <span class="hljs-operator">=</span> {
<span class="hljs-attr">negative</span> <span class="hljs-operator">=</span> <span class="hljs-operator">-</span><span class="hljs-number">1</span>;
<span class="hljs-attr">negative_space</span> <span class="hljs-operator">=</span> <span class="hljs-operator">-</span> <span class="hljs-number">1</span>;

<span class="hljs-attr">arithmetic</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-operator">-</span><span class="hljs-number">2</span>;
<span class="hljs-attr">arithmetic2</span> <span class="hljs-operator">=</span> <span class="hljs-operator">-</span><span class="hljs-number">1</span> <span class="hljs-operator">-</span> <span class="hljs-number">2</span>;
<span class="hljs-attr">arithmetic3</span> <span class="hljs-operator">=</span> <span class="hljs-operator">-</span> <span class="hljs-number">1</span> <span class="hljs-operator">-</span><span class="hljs-number">2</span><span class="hljs-operator">-</span><span class="hljs-number">3</span>;
<span class="hljs-attr">arithmetic4</span> <span class="hljs-operator">=</span><span class="hljs-operator">-</span><span class="hljs-number">1</span><span class="hljs-operator">+</span><span class="hljs-operator">-</span><span class="hljs-number">2</span> <span class="hljs-operator">+</span> <span class="hljs-operator">-</span> <span class="hljs-number">3</span>;

<span class="hljs-attr">attrname</span> <span class="hljs-operator">=</span> a-b;
};

<span class="hljs-attr">normal_string</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;asdf&quot;</span>;
<span class="hljs-attr">interpolated_string</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;hello <span class="hljs-subst">${<span class="hljs-built_in">toString</span> <span class="hljs-number">1</span> <span class="hljs-operator">+</span> (<span class="hljs-number">2</span> <span class="hljs-operator">*</span> <span class="hljs-number">3</span>)}</span> world&quot;</span>;
<span class="hljs-attr">escaped_interpolation_string</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;<span class="hljs-char escape_">\$</span>{escaped} and &#x27;&#x27;<span class="hljs-subst">${<span class="hljs-string">&quot;not&quot;</span> <span class="hljs-operator">+</span> <span class="hljs-string">&quot;escaped&quot;</span>}</span>&quot;</span>;
<span class="hljs-attr">indentedString</span> <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;&#x27;
hello
<span class="hljs-subst">${ <span class="hljs-keyword">if</span> <span class="hljs-literal">true</span> <span class="hljs-keyword">then</span> <span class="hljs-string">&quot;--<span class="hljs-subst">${test}</span>&quot;</span> <span class="hljs-keyword">else</span> <span class="hljs-literal">false</span> }</span>
<span class="hljs-char escape_">&#x27;&#x27;$</span>{ escaped }
\<span class="hljs-subst">${<span class="hljs-string">&quot;not&quot;</span> <span class="hljs-operator">+</span> <span class="hljs-string">&quot;escaped&quot;</span>}</span> and <span class="hljs-char escape_">&#x27;&#x27;$</span>{escaped}
<span class="hljs-char escape_">&#x27;&#x27;&#x27;</span>escaped single ticks<span class="hljs-char escape_">&#x27;&#x27;&#x27;</span>
world
&#x27;&#x27;</span>;

<span class="hljs-attr">attrsWithoutSpace</span><span class="hljs-operator">=</span><span class="hljs-string">&quot;hello <span class="hljs-subst">${world}</span>&quot;</span>;

<span class="hljs-attr">concatenatedList</span> <span class="hljs-operator">=</span> [ <span class="hljs-string">&quot;--with-foo2&quot;</span> ] <span class="hljs-operator">++</span> stdenv.lib.optional bar <span class="hljs-string">&quot;--with-foo=<span class="hljs-subst">${ <span class="hljs-keyword">with</span> stdenv.lib; foo }</span>&quot;</span>;

<span class="hljs-attr">paths</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">with</span> lib; {
<span class="hljs-attr">home</span> <span class="hljs-operator">=</span> <span class="hljs-symbol">~/x/y/z</span>;
<span class="hljs-attr">here</span> <span class="hljs-operator">=</span> <span class="hljs-symbol">./x/y/z</span>;
<span class="hljs-attr">up</span> <span class="hljs-operator">=</span> <span class="hljs-symbol">../x/y/z</span>;
<span class="hljs-attr">root</span> <span class="hljs-operator">=</span> <span class="hljs-symbol">/x/y/z</span>;
};

<span class="hljs-attr">long.nested.attr.path</span> <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;&#x27;
<span class="hljs-subst">${
<span class="hljs-comment"># comment inside antiquote</span>
<span class="hljs-comment">/* comment before */</span> <span class="hljs-built_in">toString</span> <span class="hljs-number">1</span> <span class="hljs-comment">/* comment after */</span>
}</span>
&#x27;&#x27;</span>;

<span class="hljs-attr">meta</span> = <span class="hljs-keyword">with</span> stdenv.lib; {
<span class="hljs-attr">homepage</span> = https://nixos.org;
<span class="hljs-attr">someBuiltins</span> <span class="hljs-operator">=</span> [
(<span class="hljs-built_in">removeAttrs</span> { <span class="hljs-attr">x</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; ${<span class="hljs-string">&quot;_&quot;</span> <span class="hljs-operator">+</span> y.z <span class="hljs-keyword">or</span> <span class="hljs-string">&quot;none&quot;</span>} <span class="hljs-operator">=</span> <span class="hljs-number">2</span>; } [ <span class="hljs-string">&quot;x&quot;</span> ])
(<span class="hljs-built_in">builtins.concatLists</span> [ [<span class="hljs-number">1</span>] [<span class="hljs-number">2</span>] ])
(<span class="hljs-built_in">builtins</span>.nonExistent <span class="hljs-string">&quot;hello&quot;</span>)
];

<span class="hljs-attr">someOperators</span> <span class="hljs-operator">=</span> [
(a <span class="hljs-operator">|&gt;</span> b <span class="hljs-operator">&lt;|</span> c)
(x <span class="hljs-operator">||</span> y <span class="hljs-operator">&amp;&amp;</span> z <span class="hljs-operator">-&gt;</span> w)
(x <span class="hljs-operator">&lt;</span> y <span class="hljs-operator">&amp;&amp;</span> y <span class="hljs-operator">&gt;</span> x)
];

<span class="hljs-attr">function</span> <span class="hljs-operator">=</span> <span class="hljs-params">a:</span> <span class="hljs-params">b:</span> a <span class="hljs-operator">//</span> b;
<span class="hljs-attr">unformattedFunction</span> <span class="hljs-operator">=</span> <span class="hljs-params">a :</span> <span class="hljs-params">b :</span> a <span class="hljs-operator">==</span> b;

<span class="hljs-attr">interpolatedAttrs</span> <span class="hljs-operator">=</span> {
${<span class="hljs-literal">null</span>} <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;
x.${y}.z <span class="hljs-operator">=</span> <span class="hljs-number">2</span>;
};

<span class="hljs-attr">invalidAttrs</span> <span class="hljs-operator">=</span> {
<span class="hljs-number">1</span>invalidAttr <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;
<span class="hljs-attr">notAPath</span> <span class="hljs-operator">=</span> <span class="hljs-operator">//</span>;
<span class="hljs-attr">trailingSlashPath</span> <span class="hljs-operator">=</span> <span class="hljs-operator">/</span>asdf<span class="hljs-symbol">/</span>;
<span class="hljs-attr">notAFunction</span> <span class="hljs-operator">=</span> x:x;
};
}

<span class="hljs-comment"># REPL tests</span>

<span class="hljs-meta prompt_">nix-repl&gt;</span> <span class="hljs-number">1</span> <span class="hljs-operator">+</span> <span class="hljs-number">2</span>

<span class="hljs-number">3</span>
<span class="hljs-meta prompt_">nix-repl&gt;</span> <span class="hljs-meta">:b</span> pkgs.writeText <span class="hljs-string">&quot;file.txt&quot;</span> <span class="hljs-string">&quot;content&quot;</span>

This <span class="hljs-built_in">derivation</span> produced the following <span class="hljs-params">outputs:</span>
out <span class="hljs-operator">-&gt;</span> <span class="hljs-symbol">/nix/store/v5a715bk02cgvb0fv9kby0nsyy1prpy2-file.txt</span>
[<span class="hljs-number">2</span> built]
98 changes: 89 additions & 9 deletions test/markup/nix/default.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{ stdenv, foo, bar ? false, ... }:
{
stdenv,
pkgs ? import <nixpkgs> {},
home-manager ? import <a/b/c/home-manager>,
foo ? "hello",
bar ? false,
...
}:

/*
* foo
Expand All @@ -8,18 +15,91 @@ let
a = 1; # just a comment
b = null;
c = toString 10;
in stdenv.mkDerivation rec {
name = "foo-${version}";
version = "1.3";

configureFlags = [ "--with-foo2" ] ++ stdenv.lib.optional bar "--with-foo=${ with stdenv.lib; foo }"
inherit (builtins) concatLists;
inherit (lib) genAttrs;
in

postInstall = ''
{
number = 1;
floating_point = 2.3;

dash_usage = {
negative = -1;
negative_space = - 1;

arithmetic = 1-2;
arithmetic2 = -1 - 2;
arithmetic3 = - 1 -2-3;
arithmetic4 =-1+-2 + - 3;

attrname = a-b;
};

normal_string = "asdf";
interpolated_string = "hello ${toString 1 + (2 * 3)} world";
escaped_interpolation_string = "\${escaped} and ''${"not" + "escaped"}";
indentedString = ''
hello
${ if true then "--${test}" else false }
''${ escaped }
\${"not" + "escaped"} and ''${escaped}
'''escaped single ticks'''
world
'';

attrsWithoutSpace="hello ${world}";

concatenatedList = [ "--with-foo2" ] ++ stdenv.lib.optional bar "--with-foo=${ with stdenv.lib; foo }";

paths = with lib; {
home = ~/x/y/z;
here = ./x/y/z;
up = ../x/y/z;
root = /x/y/z;
};

long.nested.attr.path = ''
${
# comment inside antiquote
/* comment before */ toString 1 /* comment after */
}
'';

meta = with stdenv.lib; {
homepage = https://nixos.org;
someBuiltins = [
(removeAttrs { x = 1; ${"_" + y.z or "none"} = 2; } [ "x" ])
(builtins.concatLists [ [1] [2] ])
(builtins.nonExistent "hello")
];

someOperators = [
(a |> b <| c)
(x || y && z -> w)
(x < y && y > x)
];

function = a: b: a // b;
unformattedFunction = a : b : a == b;

interpolatedAttrs = {
${null} = 1;
x.${y}.z = 2;
};

invalidAttrs = {
1invalidAttr = 1;
notAPath = //;
trailingSlashPath = /asdf/;
notAFunction = x:x;
};
}

# REPL tests

nix-repl> 1 + 2

3
nix-repl> :b pkgs.writeText "file.txt" "content"

This derivation produced the following outputs:
out -> /nix/store/v5a715bk02cgvb0fv9kby0nsyy1prpy2-file.txt
[2 built]

0 comments on commit 9f548da

Please sign in to comment.