1
1
"""Tests for g's CLI package."""
2
2
3
+ import enum
4
+ import pathlib
3
5
import subprocess
4
6
import typing as t
5
7
from unittest .mock import patch
@@ -20,25 +22,95 @@ def get_output(
20
22
return exc .output
21
23
22
24
25
+ class EnvFlag (enum .Enum ):
26
+ """Environmental conditions to simulate in test case."""
27
+
28
+ Git = "Git" # Inside a git directory (like this repo)
29
+ Empty = "Empty" # Empty directory (e.g. `tmp_path`)
30
+
31
+
23
32
@pytest .mark .parametrize (
24
33
("argv_args" , "expect_cmd" ),
25
34
[
26
35
(["g" ], "git" ),
27
36
(["g" , "--help" ], "git --help" ),
28
37
],
29
38
)
39
+ class CommandLineTestFixture (t .NamedTuple ):
40
+ """Test fixture for CLI params, environment, and expected result."""
41
+
42
+ # pytest internal
43
+ test_id : str
44
+
45
+ # env data
46
+ env : EnvFlag
47
+
48
+ # test data
49
+ argv_args : t .List [str ]
50
+
51
+ # results
52
+ expect_cmd : t .Optional [str ]
53
+
54
+
55
+ TEST_FIXTURES : t .List [CommandLineTestFixture ] = [
56
+ CommandLineTestFixture (
57
+ test_id = "g-cmd-inside-git-dir" ,
58
+ env = EnvFlag .Git ,
59
+ argv_args = ["g" ],
60
+ expect_cmd = "git" ,
61
+ ),
62
+ CommandLineTestFixture (
63
+ test_id = "g-cmd-help-inside-git-dir" ,
64
+ env = EnvFlag .Git ,
65
+ argv_args = ["g --help" ],
66
+ expect_cmd = "git --help" ,
67
+ ),
68
+ CommandLineTestFixture (
69
+ test_id = "g-cmd-inside-empty-dir" ,
70
+ env = EnvFlag .Empty ,
71
+ argv_args = ["g" ],
72
+ expect_cmd = None ,
73
+ ),
74
+ CommandLineTestFixture (
75
+ test_id = "g-cmd-help-inside-empty-dir" ,
76
+ env = EnvFlag .Empty ,
77
+ argv_args = ["g --help" ],
78
+ expect_cmd = None ,
79
+ ),
80
+ ]
81
+
82
+
83
+ @pytest .mark .parametrize (
84
+ list (CommandLineTestFixture ._fields ),
85
+ TEST_FIXTURES ,
86
+ ids = [f .test_id for f in TEST_FIXTURES ],
87
+ )
30
88
def test_command_line (
31
89
# capsys: pytest.CaptureFixture[str],
90
+ test_id : str ,
91
+ env : EnvFlag ,
32
92
argv_args : t .List [str ],
33
- expect_cmd : str ,
93
+ expect_cmd : t .Optional [str ],
94
+ monkeypatch : pytest .MonkeyPatch ,
95
+ tmp_path : pathlib .Path ,
34
96
) -> None :
35
97
"""Basic CLI usage."""
36
98
from g import sys as gsys
37
99
100
+ if env == EnvFlag .Git :
101
+ pass
102
+ elif env == EnvFlag .Empty :
103
+ monkeypatch .chdir (str (tmp_path ))
104
+
38
105
with patch .object (gsys , "argv" , argv_args ):
39
106
proc = run (wait = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
40
- assert proc is not None
41
- assert proc .stdout is not None
42
- captured = proc .stdout .read ()
107
+ if expect_cmd is None :
108
+ assert proc is None
109
+ else :
110
+ assert proc is not None
111
+ assert proc .stdout is not None
112
+ captured = proc .stdout .read ()
43
113
44
- assert captured == get_output (expect_cmd , shell = True , stderr = subprocess .STDOUT )
114
+ assert captured == get_output (
115
+ expect_cmd , shell = True , stderr = subprocess .STDOUT
116
+ )
0 commit comments