-
Notifications
You must be signed in to change notification settings - Fork 27
/
dotenv_test.exs
87 lines (77 loc) · 3.07 KB
/
dotenv_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
defmodule DotenvTest do
use ExUnit.Case, async: true
def fixture_dir, do: Path.expand("../fixture", __ENV__.file())
def proj1_dir, do: Path.join(fixture_dir(), "proj1")
def proj2_dir, do: Path.join(fixture_dir(), "proj2")
def proj3_dir, do: Path.join(fixture_dir(), "proj3")
test "parsing a simple dotenv file" do
File.cd!(proj1_dir())
env = Dotenv.load()
assert Dotenv.Env.path(env) == Path.expand(".env", proj1_dir())
assert Dotenv.Env.get(env, "FOO_BAR") == "1234"
assert Dotenv.Env.get(env, "BAZ") == "5678"
assert Dotenv.Env.get(env, "BUZ") == "9999"
assert Dotenv.Env.get(env, "QUX") == "0000"
end
test "parsing a simple dotenv file as found in the real world" do
File.cd!(proj3_dir())
env = Dotenv.load()
assert Dotenv.Env.get(env, "EMPTY") == nil
assert Dotenv.Env.path(env) == Path.expand(".env", proj3_dir())
assert Dotenv.Env.get(env, "EXTERNAL_HOST") == "external.example.com"
assert Dotenv.Env.get(env, "EXTERNAL_PROTOCOL") == "https"
assert Dotenv.Env.get(env, "INTERNAL_HOST") == "internal.example.com"
assert Dotenv.Env.get(env, "INTERNAL_PROTOCOL") == "https"
end
test "it parses values in double quotes" do
File.cd!(proj1_dir())
env = Dotenv.load()
assert Dotenv.Env.get(env, "DOUBLE_QUOTED_VALUE") == "NoDoubleQuotes"
end
test "it parses values in single quotes" do
File.cd!(proj1_dir())
env = Dotenv.load()
assert Dotenv.Env.get(env, "SINGLE_QUOTED_VALUE") == "NoSingleQuotes"
end
test "finding the dotenv from a subdir" do
File.cd!(Path.join(proj1_dir(), "subdir"))
env = Dotenv.load()
assert Dotenv.Env.path(env) == Path.expand(".env", proj1_dir())
assert Dotenv.Env.get(env, "FOO_BAR") == "1234"
assert Dotenv.Env.get(env, "BAZ") == "5678"
assert Dotenv.Env.get(env, "BUZ") == "9999"
assert Dotenv.Env.get(env, "QUX") == "0000"
end
test "loading into system environment" do
import System, only: [get_env: 1]
File.cd!(Path.join(proj1_dir(), "subdir"))
Dotenv.load!()
assert get_env("FOO_BAR") == "1234"
assert get_env("BAZ") == "5678"
assert get_env("BUZ") == "9999"
assert get_env("QUX") == "0000"
end
test "falling back to system environment" do
File.cd!(proj1_dir())
System.put_env("FOO_BAR", "ORIGINAL_FOO_BAR")
System.put_env("BAZZLE", "4321")
env = Dotenv.load()
assert Dotenv.Env.path(env) == Path.expand(".env", proj1_dir())
# .env values take precedence
assert Dotenv.Env.get(env, "FOO_BAR") == "1234"
assert Dotenv.Env.get(env, "BAZZLE") == "4321"
end
test "with explicit file" do
env_path = Path.join(proj2_dir(), ".env")
env = Dotenv.load!(env_path)
assert Dotenv.Env.path(env) == env_path
assert System.get_env("PROJ2_VAR") == "9876"
end
test "with multiple explicit files" do
env_paths = [Path.join(proj1_dir(), ".env"), Path.join(proj2_dir(), ".env")]
env = Dotenv.load!(env_paths)
assert Dotenv.Env.path(env) == env_paths |> Enum.join(":")
assert System.get_env("PROJ2_VAR") == "9876"
assert System.get_env("FOO_BAR") == "PROJ2_FOO_BAR"
end
end