Debian Codegraph @

tests/test_flags.py

raw symbols SCIP
1 import pytest
2 import pexpect
3
4
5 @pytest.fixture
6 def env_feature_config(tmpdir):
7 def _env_feature_config(features):
8 conf = tmpdir.join("config.yaml")
9 conf.write(
10 """
11 targets:
12 test1:
13 features:
14 """
15 )
16 for feature in features:
17 conf.write(f" - {feature}\n", "a")
18 if not features:
19 conf.write(" {}\n", "a")
20
21 return conf
22
23 yield _env_feature_config
24
25
26 def test_with_feature(tmpdir, env_feature_config):
27 conf = env_feature_config(["test"])
28 test = tmpdir.join("test.py")
29 test.write(
30 """
31 import pytest
32
33 @pytest.mark.lg_feature("test")
34 def test(env):
35 assert True
36 """
37 )
38
39 with pexpect.spawn(f"pytest --lg-env {conf} {test}") as spawn:
40 spawn.expect("1 passed")
41 spawn.expect(pexpect.EOF)
42 spawn.close()
43 assert spawn.exitstatus == 0
44
45
46 def test_skip_feature(tmpdir, env_feature_config):
47 conf = env_feature_config(["test"])
48 test = tmpdir.join("test2.py")
49 test.write(
50 """
51 import pytest
52
53 @pytest.mark.lg_feature("test2")
54 def test(env):
55 assert True
56 """
57 )
58
59 with pexpect.spawn(f"pytest --lg-env {conf} {test}") as spawn:
60 spawn.expect("1 skipped")
61 spawn.expect(pexpect.EOF)
62 spawn.close()
63 assert spawn.exitstatus == 0
64
65
66 def test_skip_feature_list(tmpdir, env_feature_config):
67 conf = env_feature_config(["test"])
68 test = tmpdir.join("test2.py")
69 test.write(
70 """
71 import pytest
72
73 @pytest.mark.lg_feature(["test2", "test3"])
74 def test(env):
75 assert True
76 """
77 )
78
79 with pexpect.spawn(f"pytest --lg-env {conf} {test}") as spawn:
80 spawn.expect("1 skipped")
81 spawn.expect(pexpect.EOF)
82 spawn.close()
83 assert spawn.exitstatus == 0
84
85
86 def test_match_feature_list(tmpdir, env_feature_config):
87 conf = env_feature_config(["test1", "test2"])
88 test = tmpdir.join("test2.py")
89 test.write(
90 """
91 import pytest
92
93 @pytest.mark.lg_feature(["test1", "test2"])
94 def test(env):
95 assert True
96 """
97 )
98
99 with pexpect.spawn(f"pytest --lg-env {conf} {test}") as spawn:
100 spawn.expect("1 passed")
101 spawn.expect(pexpect.EOF)
102 spawn.close()
103 assert spawn.exitstatus == 0
104
105
106 def test_match_multi_feature_source(tmpdir, env_feature_config):
107 conf = env_feature_config(["test1", "test2", "test3"])
108 test = tmpdir.join("test.py")
109 test.write(
110 """
111 import pytest
112
113 pytestmark = pytest.mark.lg_feature("test1")
114
115 @pytest.mark.lg_feature("test2")
116 class TestMulti:
117 @pytest.mark.lg_feature("test3")
118 def test(self, env):
119 assert True
120 """
121 )
122
123 with pexpect.spawn(f"pytest --lg-env {conf} {test}") as spawn:
124 spawn.expect("1 passed")
125 spawn.expect(pexpect.EOF)
126 spawn.close()
127 assert spawn.exitstatus == 0
128
129
130 def test_skip_multi_feature_source(tmpdir, env_feature_config):
131 conf = env_feature_config(["test1", "test3"])
132 test = tmpdir.join("test.py")
133 test.write(
134 """
135 import pytest
136
137 pytestmark = pytest.mark.lg_feature("test1")
138
139 @pytest.mark.lg_feature("test2")
140 class TestMulti:
141 @pytest.mark.lg_feature("test3")
142 def test(self, env):
143 assert True
144 """
145 )
146
147 with pexpect.spawn(f"pytest --lg-env {conf} {test}") as spawn:
148 spawn.expect("1 skipped")
149 spawn.expect(pexpect.EOF)
150 spawn.close()
151 assert spawn.exitstatus == 0
152
153
154 @pytest.mark.parametrize(
155 "marker_args_str,error",
156 [
157 ("", "Unexpected number of args/kwargs"),
158 ("'too', 'many'", "Unexpected number of args/kwargs"),
159 ("{'foo': 'bar'}", "Unsupported 'features' argument type"),
160 ],
161 ids=["no args", "too many args", "unsupported arg type"],
162 )
163 def test_lg_feature_unexpected_args(tmpdir, env_feature_config, marker_args_str, error):
164 # features do not matter here, simply generate a valid env config
165 conf = env_feature_config([])
166 test = tmpdir.join("test.py")
167 test.write(
168 f"""
169 import pytest
170
171 @pytest.mark.lg_feature({marker_args_str})
172 def test(self, env):
173 assert True
174 """
175 )
176
177 with pexpect.spawn(f"pytest --lg-env {conf} {test}") as spawn:
178 spawn.expect(error)
179 spawn.expect(pexpect.EOF)
180 spawn.close()
181 # pytest command line usage error leads to exit code 4
182 assert spawn.exitstatus == 4
183
184
185 def test_xfail_feature(tmpdir, env_feature_config):
186 conf = env_feature_config(["test"])
187 test = tmpdir.join("test.py")
188 test.write(
189 """
190 import pytest
191
192 @pytest.mark.lg_xfail_feature("test")
193 def test(env):
194 assert False
195 """
196 )
197
198 with pexpect.spawn(f"pytest --lg-env {conf} {test}") as spawn:
199 spawn.expect("1 xfailed")
200 spawn.expect(pexpect.EOF)
201 assert spawn.exitstatus == 0
202
203
204 def test_no_xfail_feature(tmpdir, env_feature_config):
205 conf = env_feature_config([])
206 test = tmpdir.join("test.py")
207 test.write(
208 """
209 import pytest
210
211 @pytest.mark.lg_xfail_feature("test")
212 def test(env):
213 assert False
214 """
215 )
216
217 with pexpect.spawn(f"pytest --lg-env {conf} {test}") as spawn:
218 spawn.expect("1 failed")
219 spawn.expect(pexpect.EOF)
220 assert spawn.exitstatus == 1
221
222
223 @pytest.mark.parametrize(
224 "marker_args_str,error",
225 [
226 ("", "Unexpected number of arguments"),
227 ("'too', 'many'", "Unexpected number of arguments"),
228 ("{'foo': 'bar'}", "Unsupported 'feature' argument type"),
229 ("'feature', condition='mycondition'", "Unsupported 'condition' argument"),
230 ],
231 ids=["no args", "too many args", "unsupported arg type", "unsupported condition"],
232 )
233 def test_lg_xfail_feature_unexpected_args(tmpdir, env_feature_config, marker_args_str, error):
234 # features do not matter here, simply generate a valid env config
235 conf = env_feature_config([])
236 test = tmpdir.join("test.py")
237 test.write(
238 f"""
239 import pytest
240
241 @pytest.mark.lg_xfail_feature({marker_args_str})
242 def test(self, env):
243 assert True
244 """
245 )
246
247 with pexpect.spawn(f"pytest --lg-env {conf} {test}") as spawn:
248 spawn.expect(error)
249 spawn.expect(pexpect.EOF)
250 spawn.close()
251 # pytest command line usage error leads to exit code 4
252 assert spawn.exitstatus == 4