Skip to content

Commit

Permalink
fix dotenv test
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Mar 14, 2024
1 parent 02c48f6 commit 6f91d5a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/modules/integrations/dotenv.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{ pkgs, config, lib, ... }:
{ pkgs, config, lib, self, ... }:

let
cfg = config.dotenv;

normalizeFilenames = filenames: if lib.isList filenames then filenames else [ filenames ];
dotenvFiles = normalizeFilenames cfg.filename;
dotenvPaths = map (filename: config.devenv.root + "/" + filename) dotenvFiles;
dotenvPaths = map (filename: (self + ("/" + filename))) dotenvFiles;

parseLine = line:
let
Expand All @@ -22,16 +22,19 @@ let

createMissingFileMessage = file:
let
exampleExists = builtins.pathExists (file + ".example");
exampleExists = lib.pathExists (file + ".example");
filename = builtins.baseNameOf (toString file);
in
lib.optionalString (!lib.pathExists file) ''
echo "💡 The dotenv file '${file}' was not found."
echo "💡 The dotenv file '${filename}' was not found."
${lib.optionalString exampleExists ''
echo
echo " To create this file, you can copy the example file:"
echo " $ cp ${file}.example ${file}"
echo
echo " $ cp ${filename}.example ${filename}"
echo
''}
'';

in
{
options.dotenv = {
Expand Down Expand Up @@ -61,14 +64,14 @@ in
enterShell = lib.concatStringsSep "\n" (map createMissingFileMessage dotenvPaths);
dotenv.resolved = mergeEnvFiles dotenvPaths;
assertions = [{
assertion = lib.hasPrefix ".env" cfg.filename;
assertion = builtins.all (lib.hasPrefix ".env") dotenvFiles;
message = "The dotenv filename must start with '.env'.";
}];
})
(lib.mkIf (!cfg.enable && !cfg.disableHint) {
enterShell =
let
dotenvFound = lib.any (file: lib.pathExists file) dotenvPaths;
dotenvFound = lib.any lib.pathExists dotenvPaths;
in
lib.optionalString dotenvFound ''
echo "💡 A dotenv file was found, while dotenv integration is currently not enabled."
Expand Down
4 changes: 3 additions & 1 deletion tests/dotenv/.setup.sh
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
echo "{ env.LOCAL = \"1\";}" > devenv.local.nix
echo "{ env.LOCAL = \"1\";}" > devenv.local.nix
echo "FOO=1\nBAR=2\nBAZ=3" > .env
echo "BAZ=5" > .env.bar
1 change: 0 additions & 1 deletion tests/dotenv/bar.env

This file was deleted.

2 changes: 1 addition & 1 deletion tests/dotenv/devenv.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ pkgs, ... }: {
dotenv.enable = true;
dotenv.filename = [ ".env" "bar.env" ];
dotenv.filename = [ ".env" ".env.bar" ];

env.BAR = "1";
}

0 comments on commit 6f91d5a

Please sign in to comment.