|
1 | 1 | import os |
| 2 | +import subprocess |
2 | 3 | import sys |
3 | 4 | from pathlib import Path |
4 | | -from typing import Optional |
| 5 | +from typing import Optional, Sequence |
5 | 6 |
|
6 | 7 | import pytest |
7 | 8 |
|
|
13 | 14 | import sh |
14 | 15 |
|
15 | 16 |
|
| 17 | +def invoke_sub(args: Sequence[str]) -> subprocess.CompletedProcess: |
| 18 | + """ |
| 19 | + Invoke the `dotenv` CLI in a subprocess. |
| 20 | +
|
| 21 | + This is necessary to test subcommands like `dotenv run` that replace the |
| 22 | + current process. |
| 23 | + """ |
| 24 | + |
| 25 | + return subprocess.run( |
| 26 | + ["dotenv", *args], |
| 27 | + capture_output=True, |
| 28 | + text=True, |
| 29 | + ) |
| 30 | + |
| 31 | + |
16 | 32 | @pytest.mark.parametrize( |
17 | 33 | "output_format,content,expected", |
18 | 34 | ( |
@@ -258,3 +274,29 @@ def test_run_with_version(cli): |
258 | 274 |
|
259 | 275 | assert result.exit_code == 0 |
260 | 276 | assert result.output.strip().endswith(__version__) |
| 277 | + |
| 278 | + |
| 279 | +def test_run_with_command_flags(dotenv_path): |
| 280 | + """ |
| 281 | + Check that command flags passed after `dotenv run` are not interpreted. |
| 282 | +
|
| 283 | + Here, we want to run `printenv --version`, not `dotenv --version`. |
| 284 | + """ |
| 285 | + |
| 286 | + result = invoke_sub(["--file", dotenv_path, "run", "printenv", "--version"]) |
| 287 | + |
| 288 | + assert result.returncode == 0 |
| 289 | + assert result.stdout.strip().startswith("printenv ") |
| 290 | + |
| 291 | + |
| 292 | +def test_run_with_dotenv_and_command_flags(cli, dotenv_path): |
| 293 | + """ |
| 294 | + Check that dotenv flags supersede command flags. |
| 295 | + """ |
| 296 | + |
| 297 | + result = invoke_sub( |
| 298 | + ["--version", "--file", dotenv_path, "run", "printenv", "--version"] |
| 299 | + ) |
| 300 | + |
| 301 | + assert result.returncode == 0 |
| 302 | + assert result.stdout.strip().startswith("dotenv, version") |
0 commit comments