Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dotenv tests #778

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,14 @@
inherit config;
};
};

# run tests with `nix eval .#tests`
tests =
let
results = import ./tests/dotenv/tests.nix { inherit nixpkgs; };
in
if results == [ ]
then "all tests passed"
else throw (builtins.toJSON results);
};
}
13 changes: 13 additions & 0 deletions src/modules/integrations/dotenv-functions.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{ lib }:
rec {
parseLine = line:
let
parts = builtins.match "(.+) *= *(.+)" line;
in
if (!builtins.isNull parts) && (builtins.length parts) == 2 then
{ name = builtins.elemAt parts 0; value = builtins.elemAt parts 1; }
else
null;

parseEnvFile = content: builtins.listToAttrs (lib.filter (x: !builtins.isNull x) (map parseLine (lib.splitString "\n" content)));
}
13 changes: 3 additions & 10 deletions src/modules/integrations/dotenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@ let
dotenvPath = config.devenv.root + "/" + cfg.filename;

dotenvFound = lib.pathExists dotenvPath;
parseLine = line:
let
parts = builtins.match "(.+) *= *(.+)" line;
in
if (!builtins.isNull parts) && (builtins.length parts) == 2 then
{ name = builtins.elemAt parts 0; value = builtins.elemAt parts 1; }
else
null;

parseEnvFile = content: builtins.listToAttrs (lib.filter (x: !builtins.isNull x) (map parseLine (lib.splitString "\n" content)));
dlib = import ./dotenv-functions.nix { inherit lib; };
parseLine = dlib.parseLine;
parseEnvFile = dlib.parseEnvFile;
in
{
options.dotenv = {
Expand Down
8 changes: 8 additions & 0 deletions tests/dotenv/math.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ nixpkgs }:
let
inherit (nixpkgs.lib) mod;
in
{
# Returns true if integer is even.
isEven = x: (mod x 2) == 0;
}
199 changes: 199 additions & 0 deletions tests/dotenv/tests.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
{ nixpkgs }:
let
inherit (nixpkgs.lib) runTests;
math = import ./math.nix { inherit nixpkgs; };
dotenv = import ./../../src/modules/integrations/dotenv-functions.nix { lib = nixpkgs.lib; };
in
runTests {
testIsEven_1 = {
expr = math.isEven 2;
expected = true;
};

testIsEven_2 = {
expr = math.isEven (-3);
expected = false;
};

# (u"", []),
testEmptyConfigEmpty = {
expr = dotenv.parseEnvFile "";
expected = { };
};
# (u"a=b", [Binding(key=u"a", value=u"b", original=Original(string=u"a=b", line=1), error=False)]),
testSimpleAB = {
expr = dotenv.parseEnvFile "a=b";
expected = { "a" = "b"; };
};
# https://github.com/cachix/devenv/issues/772
testJsonQuotedCorrectly = {
expr = dotenv.parseEnvFile "a='{\"a\":\"b\"}'";
expected = { "a" = "{\"a\":\"b\"}"; };
};

}

# python dotenv tests
# @pytest.mark.parametrize("test_input,expected", [

# (u"'a'=b", [Binding(key=u"a", value=u"b", original=Original(string=u"'a'=b", line=1), error=False)]),
# (u"[=b", [Binding(key=u"[", value=u"b", original=Original(string=u"[=b", line=1), error=False)]),
# (u" a = b ", [Binding(key=u"a", value=u"b", original=Original(string=u" a = b ", line=1), error=False)]),
# (u"export a=b", [Binding(key=u"a", value=u"b", original=Original(string=u"export a=b", line=1), error=False)]),
# (
# u" export 'a'=b",
# [Binding(key=u"a", value=u"b", original=Original(string=u" export 'a'=b", line=1), error=False)],
# ),
# (u"# a=b", [Binding(key=None, value=None, original=Original(string=u"# a=b", line=1), error=False)]),
# (u"a=b#c", [Binding(key=u"a", value=u"b#c", original=Original(string=u"a=b#c", line=1), error=False)]),
# (
# u'a=b #c',
# [Binding(key=u"a", value=u"b", original=Original(string=u"a=b #c", line=1), error=False)],
# ),
# (
# u'a=b\t#c',
# [Binding(key=u"a", value=u"b", original=Original(string=u"a=b\t#c", line=1), error=False)],
# ),
# (
# u"a=b c",
# [Binding(key=u"a", value=u"b c", original=Original(string=u"a=b c", line=1), error=False)],
# ),
# (
# u"a=b\tc",
# [Binding(key=u"a", value=u"b\tc", original=Original(string=u"a=b\tc", line=1), error=False)],
# ),
# (
# u"a=b c",
# [Binding(key=u"a", value=u"b c", original=Original(string=u"a=b c", line=1), error=False)],
# ),
# (
# u"a=b\u00a0 c",
# [Binding(key=u"a", value=u"b\u00a0 c", original=Original(string=u"a=b\u00a0 c", line=1), error=False)],
# ),
# (
# u"a=b c ",
# [Binding(key=u"a", value=u"b c", original=Original(string=u"a=b c ", line=1), error=False)],
# ),
# (
# u"a='b c '",
# [Binding(key=u"a", value=u"b c ", original=Original(string=u"a='b c '", line=1), error=False)],
# ),
# (
# u'a="b c "',
# [Binding(key=u"a", value=u"b c ", original=Original(string=u'a="b c "', line=1), error=False)],
# ),
# (
# u"export export_a=1",
# [
# Binding(key=u"export_a", value=u"1", original=Original(string=u"export export_a=1", line=1), error=False)
# ],
# ),
# (
# u"export port=8000",
# [Binding(key=u"port", value=u"8000", original=Original(string=u"export port=8000", line=1), error=False)],
# ),
# (u'a="b\nc"', [Binding(key=u"a", value=u"b\nc", original=Original(string=u'a="b\nc"', line=1), error=False)]),
# (u"a='b\nc'", [Binding(key=u"a", value=u"b\nc", original=Original(string=u"a='b\nc'", line=1), error=False)]),
# (u'a="b\nc"', [Binding(key=u"a", value=u"b\nc", original=Original(string=u'a="b\nc"', line=1), error=False)]),
# (u'a="b\\nc"', [Binding(key=u"a", value=u'b\nc', original=Original(string=u'a="b\\nc"', line=1), error=False)]),
# (u"a='b\\nc'", [Binding(key=u"a", value=u'b\\nc', original=Original(string=u"a='b\\nc'", line=1), error=False)]),
# (u'a="b\\"c"', [Binding(key=u"a", value=u'b"c', original=Original(string=u'a="b\\"c"', line=1), error=False)]),
# (u"a='b\\'c'", [Binding(key=u"a", value=u"b'c", original=Original(string=u"a='b\\'c'", line=1), error=False)]),
# (u"a=à", [Binding(key=u"a", value=u"à", original=Original(string=u"a=à", line=1), error=False)]),
# (u'a="à"', [Binding(key=u"a", value=u"à", original=Original(string=u'a="à"', line=1), error=False)]),
# (
# u'no_value_var',
# [Binding(key=u'no_value_var', value=None, original=Original(string=u"no_value_var", line=1), error=False)],
# ),
# (u'a: b', [Binding(key=None, value=None, original=Original(string=u"a: b", line=1), error=True)]),
# (
# u"a=b\nc=d",
# [
# Binding(key=u"a", value=u"b", original=Original(string=u"a=b\n", line=1), error=False),
# Binding(key=u"c", value=u"d", original=Original(string=u"c=d", line=2), error=False),
# ],
# ),
# (
# u"a=b\rc=d",
# [
# Binding(key=u"a", value=u"b", original=Original(string=u"a=b\r", line=1), error=False),
# Binding(key=u"c", value=u"d", original=Original(string=u"c=d", line=2), error=False),
# ],
# ),
# (
# u"a=b\r\nc=d",
# [
# Binding(key=u"a", value=u"b", original=Original(string=u"a=b\r\n", line=1), error=False),
# Binding(key=u"c", value=u"d", original=Original(string=u"c=d", line=2), error=False),
# ],
# ),
# (
# u'a=\nb=c',
# [
# Binding(key=u"a", value=u'', original=Original(string=u'a=\n', line=1), error=False),
# Binding(key=u"b", value=u'c', original=Original(string=u"b=c", line=2), error=False),
# ]
# ),
# (
# u"\n\n",
# [
# Binding(key=None, value=None, original=Original(string=u"\n\n", line=1), error=False),
# ]
# ),
# (
# u"a=b\n\n",
# [
# Binding(key=u"a", value=u"b", original=Original(string=u"a=b\n", line=1), error=False),
# Binding(key=None, value=None, original=Original(string=u"\n", line=2), error=False),
# ]
# ),
# (
# u'a=b\n\nc=d',
# [
# Binding(key=u"a", value=u"b", original=Original(string=u"a=b\n", line=1), error=False),
# Binding(key=u"c", value=u"d", original=Original(string=u"\nc=d", line=2), error=False),
# ]
# ),
# (
# u'a="\nb=c',
# [
# Binding(key=None, value=None, original=Original(string=u'a="\n', line=1), error=True),
# Binding(key=u"b", value=u"c", original=Original(string=u"b=c", line=2), error=False),
# ]
# ),
# (
# u'# comment\na="b\nc"\nd=e\n',
# [
# Binding(key=None, value=None, original=Original(string=u"# comment\n", line=1), error=False),
# Binding(key=u"a", value=u"b\nc", original=Original(string=u'a="b\nc"\n', line=2), error=False),
# Binding(key=u"d", value=u"e", original=Original(string=u"d=e\n", line=4), error=False),
# ],
# ),
# (
# u'a=b\n# comment 1',
# [
# Binding(key="a", value="b", original=Original(string=u"a=b\n", line=1), error=False),
# Binding(key=None, value=None, original=Original(string=u"# comment 1", line=2), error=False),
# ],
# ),
# (
# u'# comment 1\n# comment 2',
# [
# Binding(key=None, value=None, original=Original(string=u"# comment 1\n", line=1), error=False),
# Binding(key=None, value=None, original=Original(string=u"# comment 2", line=2), error=False),
# ],
# ),
# (
# u'uglyKey[%$=\"S3cr3t_P4ssw#rD\" #\na=b',
# [
# Binding(key=u'uglyKey[%$',
# value=u'S3cr3t_P4ssw#rD',
# original=Original(string=u"uglyKey[%$=\"S3cr3t_P4ssw#rD\" #\n", line=1), error=False),
# Binding(key=u"a", value=u"b", original=Original(string=u'a=b', line=2), error=False),
# ],
# ),
# ])
# def test_parse_stream(test_input, expected):
# result = parse_stream(io.StringIO(test_input))

# assert list(result) == expected