|
1 | 1 | import os |
2 | 2 | import subprocess |
3 | | -import sys |
4 | 3 | from pathlib import Path |
5 | 4 | from typing import Optional, Sequence |
6 | 5 |
|
|
9 | 8 | import dotenv |
10 | 9 | from dotenv.cli import cli as dotenv_cli |
11 | 10 | from dotenv.version import __version__ |
12 | | - |
13 | | -if sys.platform != "win32": |
14 | | - import sh |
| 11 | +from tests.test_lib import check_process, run_dotenv |
15 | 12 |
|
16 | 13 |
|
17 | 14 | def invoke_sub(args: Sequence[str]) -> subprocess.CompletedProcess: |
@@ -192,111 +189,109 @@ def test_set_no_file(cli): |
192 | 189 | assert "Missing argument" in result.output |
193 | 190 |
|
194 | 191 |
|
195 | | -@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows") |
196 | 192 | def test_get_default_path(tmp_path): |
197 | | - with sh.pushd(tmp_path): |
198 | | - (tmp_path / ".env").write_text("a=b") |
| 193 | + (tmp_path / ".env").write_text("A=x") |
199 | 194 |
|
200 | | - result = sh.dotenv("get", "a") |
| 195 | + result = run_dotenv(["get", "A"], cwd=tmp_path) |
201 | 196 |
|
202 | | - assert result == "b\n" |
| 197 | + check_process(result, exit_code=0, stdout="x\n") |
203 | 198 |
|
204 | 199 |
|
205 | | -@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows") |
206 | 200 | def test_run(tmp_path): |
207 | | - with sh.pushd(tmp_path): |
208 | | - (tmp_path / ".env").write_text("a=b") |
| 201 | + (tmp_path / ".env").write_text("A=x") |
209 | 202 |
|
210 | | - result = sh.dotenv("run", "printenv", "a") |
| 203 | + result = run_dotenv(["run", "printenv", "A"], cwd=tmp_path) |
211 | 204 |
|
212 | | - assert result == "b\n" |
| 205 | + check_process(result, exit_code=0, stdout="x\n") |
213 | 206 |
|
214 | 207 |
|
215 | | -@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows") |
216 | 208 | def test_run_with_existing_variable(tmp_path): |
217 | | - with sh.pushd(tmp_path): |
218 | | - (tmp_path / ".env").write_text("a=b") |
219 | | - env = dict(os.environ) |
220 | | - env.update({"LANG": "en_US.UTF-8", "a": "c"}) |
| 209 | + (tmp_path / ".env").write_text("A=x") |
| 210 | + env = dict(os.environ) |
| 211 | + env.update({"LANG": "en_US.UTF-8", "A": "y"}) |
221 | 212 |
|
222 | | - result = sh.dotenv("run", "printenv", "a", _env=env) |
| 213 | + result = run_dotenv(["run", "printenv", "A"], cwd=tmp_path, env=env) |
223 | 214 |
|
224 | | - assert result == "b\n" |
| 215 | + check_process(result, exit_code=0, stdout="x\n") |
225 | 216 |
|
226 | 217 |
|
227 | | -@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows") |
228 | 218 | def test_run_with_existing_variable_not_overridden(tmp_path): |
229 | | - with sh.pushd(tmp_path): |
230 | | - (tmp_path / ".env").write_text("a=b") |
231 | | - env = dict(os.environ) |
232 | | - env.update({"LANG": "en_US.UTF-8", "a": "c"}) |
| 219 | + (tmp_path / ".env").write_text("A=x") |
| 220 | + env = dict(os.environ) |
| 221 | + env.update({"LANG": "en_US.UTF-8", "A": "C"}) |
233 | 222 |
|
234 | | - result = sh.dotenv("run", "--no-override", "printenv", "a", _env=env) |
| 223 | + result = run_dotenv( |
| 224 | + ["run", "--no-override", "printenv", "A"], cwd=tmp_path, env=env |
| 225 | + ) |
235 | 226 |
|
236 | | - assert result == "c\n" |
| 227 | + check_process(result, exit_code=0, stdout="C\n") |
237 | 228 |
|
238 | 229 |
|
239 | | -@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows") |
240 | 230 | def test_run_with_none_value(tmp_path): |
241 | | - with sh.pushd(tmp_path): |
242 | | - (tmp_path / ".env").write_text("a=b\nc") |
| 231 | + (tmp_path / ".env").write_text("A=x\nc") |
243 | 232 |
|
244 | | - result = sh.dotenv("run", "printenv", "a") |
| 233 | + result = run_dotenv(["run", "printenv", "A"], cwd=tmp_path) |
245 | 234 |
|
246 | | - assert result == "b\n" |
| 235 | + check_process(result, exit_code=0, stdout="x\n") |
247 | 236 |
|
248 | 237 |
|
249 | | -@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows") |
250 | | -def test_run_with_other_env(dotenv_path): |
251 | | - dotenv_path.write_text("a=b") |
| 238 | +def test_run_with_other_env(dotenv_path, tmp_path): |
| 239 | + dotenv_path.write_text("A=x") |
252 | 240 |
|
253 | | - result = sh.dotenv("--file", dotenv_path, "run", "printenv", "a") |
| 241 | + result = run_dotenv( |
| 242 | + ["--file", str(dotenv_path), "run", "printenv", "A"], |
| 243 | + cwd=tmp_path, |
| 244 | + ) |
254 | 245 |
|
255 | | - assert result == "b\n" |
| 246 | + check_process(result, exit_code=0, stdout="x\n") |
256 | 247 |
|
257 | 248 |
|
258 | | -def test_run_without_cmd(cli): |
259 | | - result = cli.invoke(dotenv_cli, ["run"]) |
| 249 | +def test_run_without_cmd(tmp_path): |
| 250 | + result = run_dotenv(["run"], cwd=tmp_path) |
260 | 251 |
|
261 | | - assert result.exit_code == 2 |
262 | | - assert "Invalid value for '-f'" in result.output |
| 252 | + check_process(result, exit_code=2) |
| 253 | + assert "Invalid value for '-f'" in result.stderr |
263 | 254 |
|
264 | 255 |
|
265 | | -def test_run_with_invalid_cmd(cli): |
266 | | - result = cli.invoke(dotenv_cli, ["run", "i_do_not_exist"]) |
| 256 | +def test_run_with_invalid_cmd(tmp_path): |
| 257 | + result = run_dotenv(["run", "i_do_not_exist"], cwd=tmp_path) |
267 | 258 |
|
268 | | - assert result.exit_code == 2 |
269 | | - assert "Invalid value for '-f'" in result.output |
| 259 | + check_process(result, exit_code=2) |
| 260 | + assert "Invalid value for '-f'" in result.stderr |
270 | 261 |
|
271 | 262 |
|
272 | | -def test_run_with_version(cli): |
273 | | - result = cli.invoke(dotenv_cli, ["--version"]) |
| 263 | +def test_run_with_version(tmp_path): |
| 264 | + result = run_dotenv(["--version"], cwd=tmp_path) |
274 | 265 |
|
275 | | - assert result.exit_code == 0 |
276 | | - assert result.output.strip().endswith(__version__) |
| 266 | + check_process(result, exit_code=0) |
| 267 | + assert result.stdout.strip().endswith(__version__) |
277 | 268 |
|
278 | 269 |
|
279 | | -def test_run_with_command_flags(dotenv_path): |
| 270 | +def test_run_with_command_flags(dotenv_path, tmp_path): |
280 | 271 | """ |
281 | 272 | Check that command flags passed after `dotenv run` are not interpreted. |
282 | 273 |
|
283 | 274 | Here, we want to run `printenv --version`, not `dotenv --version`. |
284 | 275 | """ |
285 | 276 |
|
286 | | - result = invoke_sub(["--file", dotenv_path, "run", "printenv", "--version"]) |
| 277 | + result = run_dotenv( |
| 278 | + ["--file", str(dotenv_path), "run", "printenv", "--version"], |
| 279 | + cwd=tmp_path, |
| 280 | + ) |
287 | 281 |
|
288 | | - assert result.returncode == 0 |
| 282 | + check_process(result, exit_code=0) |
289 | 283 | assert result.stdout.strip().startswith("printenv ") |
290 | 284 |
|
291 | 285 |
|
292 | | -def test_run_with_dotenv_and_command_flags(cli, dotenv_path): |
| 286 | +def test_run_with_dotenv_and_command_flags(dotenv_path, tmp_path): |
293 | 287 | """ |
294 | 288 | Check that dotenv flags supersede command flags. |
295 | 289 | """ |
296 | 290 |
|
297 | | - result = invoke_sub( |
298 | | - ["--version", "--file", dotenv_path, "run", "printenv", "--version"] |
| 291 | + result = run_dotenv( |
| 292 | + ["--version", "--file", str(dotenv_path), "run", "printenv", "--version"], |
| 293 | + cwd=tmp_path, |
299 | 294 | ) |
300 | 295 |
|
301 | | - assert result.returncode == 0 |
| 296 | + check_process(result, exit_code=0) |
302 | 297 | assert result.stdout.strip().startswith("dotenv, version") |
0 commit comments