forked from theskumar/python-dotenv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
302 lines (207 loc) · 8.22 KB
/
test_cli.py
File metadata and controls
302 lines (207 loc) · 8.22 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import os
import subprocess
import sys
from pathlib import Path
from typing import Optional, Sequence
import pytest
import dotenv
from dotenv.cli import cli as dotenv_cli
from dotenv.version import __version__
if sys.platform != "win32":
import sh
def invoke_sub(args: Sequence[str]) -> subprocess.CompletedProcess:
"""
Invoke the `dotenv` CLI in a subprocess.
This is necessary to test subcommands like `dotenv run` that replace the
current process.
"""
return subprocess.run(
["dotenv", *args],
capture_output=True,
text=True,
)
@pytest.mark.parametrize(
"output_format,content,expected",
(
(None, "x='a b c'", """x=a b c\n"""),
("simple", "x='a b c'", """x=a b c\n"""),
("simple", """x='"a b c"'""", """x="a b c"\n"""),
("simple", '''x="'a b c'"''', """x='a b c'\n"""),
("json", "x='a b c'", """{\n "x": "a b c"\n}\n"""),
("shell", "x='a b c'", "x='a b c'\n"),
("shell", """x='"a b c"'""", """x='"a b c"'\n"""),
("shell", '''x="'a b c'"''', """x=''"'"'a b c'"'"''\n"""),
("shell", "x='a\nb\nc'", "x='a\nb\nc'\n"),
("export", "x='a b c'", """export x='a b c'\n"""),
),
)
def test_list(
cli, dotenv_path, output_format: Optional[str], content: str, expected: str
):
dotenv_path.write_text(content + "\n")
args = ["--file", dotenv_path, "list"]
if format is not None:
args.extend(["--format", output_format])
result = cli.invoke(dotenv_cli, args)
assert (result.exit_code, result.output) == (0, expected)
def test_list_non_existent_file(cli):
result = cli.invoke(dotenv_cli, ["--file", "nx_file", "list"])
assert result.exit_code == 2, result.output
assert "Error opening env file" in result.output
def test_list_not_a_file(cli):
result = cli.invoke(dotenv_cli, ["--file", ".", "list"])
assert result.exit_code == 2, result.output
assert "Error opening env file" in result.output
def test_list_no_file(cli):
result = cli.invoke(dotenv.cli.list_values, [])
assert (result.exit_code, result.output) == (1, "")
def test_get_existing_value(cli, dotenv_path):
dotenv_path.write_text("a=b")
result = cli.invoke(dotenv_cli, ["--file", dotenv_path, "get", "a"])
assert (result.exit_code, result.output) == (0, "b\n")
def test_get_non_existent_value(cli, dotenv_path):
result = cli.invoke(dotenv_cli, ["--file", dotenv_path, "get", "a"])
assert (result.exit_code, result.output) == (1, "")
def test_get_non_existent_file(cli):
result = cli.invoke(dotenv_cli, ["--file", "nx_file", "get", "a"])
assert result.exit_code == 2
assert "Error opening env file" in result.output
def test_get_not_a_file(cli):
result = cli.invoke(dotenv_cli, ["--file", ".", "get", "a"])
assert result.exit_code == 2
assert "Error opening env file" in result.output
def test_unset_existing_value(cli, dotenv_path):
dotenv_path.write_text("a=b")
result = cli.invoke(dotenv_cli, ["--file", dotenv_path, "unset", "a"])
assert (result.exit_code, result.output) == (0, "Successfully removed a\n")
assert dotenv_path.read_text() == ""
def test_unset_non_existent_value(cli, dotenv_path):
result = cli.invoke(dotenv_cli, ["--file", dotenv_path, "unset", "a"])
assert (result.exit_code, result.output) == (1, "")
assert dotenv_path.read_text() == ""
@pytest.mark.parametrize(
"quote_mode,variable,value,expected",
(
("always", "a", "x", "a='x'\n"),
("never", "a", "x", "a=x\n"),
("auto", "a", "x", "a=x\n"),
("auto", "a", "x y", "a='x y'\n"),
("auto", "a", "$", "a='$'\n"),
),
)
def test_set_quote_options(cli, dotenv_path, quote_mode, variable, value, expected):
result = cli.invoke(
dotenv_cli,
[
"--file",
dotenv_path,
"--export",
"false",
"--quote",
quote_mode,
"set",
variable,
value,
],
)
assert (result.exit_code, result.output) == (0, "{}={}\n".format(variable, value))
assert dotenv_path.read_text() == expected
@pytest.mark.parametrize(
"dotenv_path,export_mode,variable,value,expected",
(
(Path(".nx_file"), "true", "a", "x", "export a='x'\n"),
(Path(".nx_file"), "false", "a", "x", "a='x'\n"),
),
)
def test_set_export(cli, dotenv_path, export_mode, variable, value, expected):
result = cli.invoke(
dotenv_cli,
[
"--file",
dotenv_path,
"--quote",
"always",
"--export",
export_mode,
"set",
variable,
value,
],
)
assert (result.exit_code, result.output) == (0, "{}={}\n".format(variable, value))
assert dotenv_path.read_text() == expected
def test_set_non_existent_file(cli):
result = cli.invoke(dotenv.cli.set_value, ["a", "b"])
assert (result.exit_code, result.output) == (1, "")
def test_set_no_file(cli):
result = cli.invoke(dotenv_cli, ["--file", "nx_file", "set"])
assert result.exit_code == 2
assert "Missing argument" in result.output
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
def test_get_default_path(tmp_path):
with sh.pushd(tmp_path):
(tmp_path / ".env").write_text("a=b")
result = sh.dotenv("get", "a")
assert result == "b\n"
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
def test_run(tmp_path):
with sh.pushd(tmp_path):
(tmp_path / ".env").write_text("a=b")
result = sh.dotenv("run", "printenv", "a")
assert result == "b\n"
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
def test_run_with_existing_variable(tmp_path):
with sh.pushd(tmp_path):
(tmp_path / ".env").write_text("a=b")
env = dict(os.environ)
env.update({"LANG": "en_US.UTF-8", "a": "c"})
result = sh.dotenv("run", "printenv", "a", _env=env)
assert result == "b\n"
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
def test_run_with_existing_variable_not_overridden(tmp_path):
with sh.pushd(tmp_path):
(tmp_path / ".env").write_text("a=b")
env = dict(os.environ)
env.update({"LANG": "en_US.UTF-8", "a": "c"})
result = sh.dotenv("run", "--no-override", "printenv", "a", _env=env)
assert result == "c\n"
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
def test_run_with_none_value(tmp_path):
with sh.pushd(tmp_path):
(tmp_path / ".env").write_text("a=b\nc")
result = sh.dotenv("run", "printenv", "a")
assert result == "b\n"
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
def test_run_with_other_env(dotenv_path):
dotenv_path.write_text("a=b")
result = sh.dotenv("--file", dotenv_path, "run", "printenv", "a")
assert result == "b\n"
def test_run_without_cmd(cli):
result = cli.invoke(dotenv_cli, ["run"])
assert result.exit_code == 2
assert "Invalid value for '-f'" in result.output
def test_run_with_invalid_cmd(cli):
result = cli.invoke(dotenv_cli, ["run", "i_do_not_exist"])
assert result.exit_code == 2
assert "Invalid value for '-f'" in result.output
def test_run_with_version(cli):
result = cli.invoke(dotenv_cli, ["--version"])
assert result.exit_code == 0
assert result.output.strip().endswith(__version__)
def test_run_with_command_flags(dotenv_path):
"""
Check that command flags passed after `dotenv run` are not interpreted.
Here, we want to run `printenv --version`, not `dotenv --version`.
"""
result = invoke_sub(["--file", dotenv_path, "run", "printenv", "--version"])
assert result.returncode == 0
assert result.stdout.strip().startswith("printenv ")
def test_run_with_dotenv_and_command_flags(cli, dotenv_path):
"""
Check that dotenv flags supersede command flags.
"""
result = invoke_sub(
["--version", "--file", dotenv_path, "run", "printenv", "--version"]
)
assert result.returncode == 0
assert result.stdout.strip().startswith("dotenv, version")