1313from guppylang_internals .error import GuppyError
1414from guppylang_internals .span import Loc , Span
1515from guppylang_internals .warning import emit_warning
16+ from tests .util import guppy_warning_records
1617
1718file = "public_warning_test.py"
1819
@@ -52,36 +53,18 @@ def register_source() -> None:
5253 DEF_STORE .sources .add_file (file , "line1\n line2\n line3\n line4\n warn()\n line6\n err\n " )
5354
5455
55- def test_definition_check_emits_warning (monkeypatch ):
56- """`GuppyDefinition.check()` inherits warning flushing from `check_single()`.
57-
58- The monkeypatch targets the inner `ENGINE.check()` call to keep the real
59- `@pretty_errors` wrapper in place while synthesizing a warning producer.
60- """
61- definition = make_definition ()
56+ def install_check_warning (monkeypatch ) -> None :
57+ """Synthesize a warning from the inner engine `check()` implementation."""
6258
6359 def fake_check (_def_ids , * , reset = True ) -> None :
6460 del reset
6561 emit_warning (make_warning ())
6662
6763 monkeypatch .setattr (ENGINE , "check" , fake_check )
6864
69- with warnings .catch_warnings (record = True ) as records :
70- warnings .simplefilter ("always" )
71- definition .check ()
72-
73- assert len (records ) == 1
74- assert records [0 ].category is GuppyWarning
75- assert records [0 ].filename == file
76-
7765
78- def test_definition_compile_emits_warning (monkeypatch ):
79- """`GuppyDefinition.compile()` inherits warning flushing from `compile_single()`.
80-
81- The monkeypatch targets the inner `ENGINE._compile()` call so the test still
82- exercises the real top-level wrapper around `compile_single()`.
83- """
84- definition = make_definition ()
66+ def install_compile_warning (monkeypatch ) -> None :
67+ """Synthesize a warning from the inner engine `_compile()` implementation."""
8568
8669 def fake_compile (_def_ids , * , reset = True ):
8770 del reset
@@ -91,57 +74,54 @@ def fake_compile(_def_ids, *, reset=True):
9174
9275 monkeypatch .setattr (ENGINE , "_compile" , fake_compile )
9376
94- with warnings .catch_warnings (record = True ) as records :
95- warnings .simplefilter ("always" )
96- definition .compile ()
97-
98- assert len (records ) == 1
99- assert records [0 ].category is GuppyWarning
100- assert records [0 ].filename == file
101-
10277
103- def test_library_check_emits_warning_once (monkeypatch ):
104- """`GuppyLibrary.check()` should not flush separately for engine subcalls.
105-
106- Unlike the single-definition helpers, this method needs its own outer
107- `diagnostic_report()` because it orchestrates multiple top-level engine calls.
108- """
109- library = GuppyLibrary ([])
110-
111- def fake_check (_def_ids , * , reset = True ) -> None :
112- del reset
113- emit_warning (make_warning ())
114-
115- monkeypatch .setattr (ENGINE , "check" , fake_check )
78+ @pytest .mark .parametrize (
79+ ("install_warning" , "run_entrypoint" ),
80+ [
81+ (
82+ install_check_warning ,
83+ lambda definition : definition .check (),
84+ ),
85+ (
86+ install_compile_warning ,
87+ lambda definition : definition .compile (),
88+ ),
89+ ],
90+ )
91+ def test_single_definition_entrypoints_emit_warning (
92+ monkeypatch , install_warning , run_entrypoint
93+ ):
94+ """Single-definition public entrypoints should flush one warning."""
95+ definition = make_definition ()
96+ install_warning (monkeypatch )
11697
11798 with warnings .catch_warnings (record = True ) as records :
11899 warnings .simplefilter ("always" )
119- library . check ( )
100+ run_entrypoint ( definition )
120101
121- assert len (records ) == 1
102+ guppy_records = guppy_warning_records (records )
103+ assert len (guppy_records ) == 1
104+ assert guppy_records [0 ].filename == file
122105
123106
124107def test_library_compile_emits_warning_once (monkeypatch ):
125- """`GuppyLibrary.compile()` should coalesce flushes across check and compile ."""
108+ """`GuppyLibrary.compile()` should coalesce warnings across its subcalls ."""
126109 library = GuppyLibrary ([])
127-
128- def fake_check (_def_ids , * , reset = True ) -> None :
129- del reset
130- emit_warning (make_warning ())
110+ install_check_warning (monkeypatch )
131111
132112 def fake_compile (_def_ids , * , reset = True ):
133113 del reset
134114 emit_warning (make_warning ())
135115 return SimpleNamespace (package = SimpleNamespace (modules = []))
136116
137- monkeypatch .setattr (ENGINE , "check" , fake_check )
138117 monkeypatch .setattr (ENGINE , "compile" , fake_compile )
139118
140119 with warnings .catch_warnings (record = True ) as records :
141120 warnings .simplefilter ("always" )
142121 library .compile ()
143122
144- assert len (records ) == 1
123+ guppy_records = guppy_warning_records (records )
124+ assert len (guppy_records ) == 1
145125
146126
147127def test_definition_check_discards_warning_on_error (monkeypatch ):
@@ -160,53 +140,29 @@ def fake_check(_def_ids, *, reset=True) -> None:
160140 with pytest .raises (GuppyError ):
161141 definition .check ()
162142
163- assert len (records ) == 0
164-
165-
166- def test_definition_check_rich_warning_emits_stderr (monkeypatch , capsys ):
167- """Rich warnings should add rendered stderr output on top of Python warnings."""
168- definition = make_definition ()
169- register_source ()
170-
171- def fake_check (_def_ids , * , reset = True ) -> None :
172- del reset
173- emit_warning (make_warning ())
174-
175- monkeypatch .setattr (ENGINE , "check" , fake_check )
176-
177- with warnings .catch_warnings (record = True ) as records :
178- warnings .simplefilter ("always" )
179- with rich_warnings ():
180- definition .check ()
181-
182- assert len (records ) == 1
183- err = capsys .readouterr ().err
184- assert "Warning: Public API warning" in err
185- assert "Triggered from a public entrypoint" in err
143+ guppy_records = guppy_warning_records (records )
144+ assert len (guppy_records ) == 0
186145
187146
188147def test_library_compile_rich_warning_emits_stderr_once (monkeypatch , capsys ):
189148 """Rich mode should not duplicate rendered warnings across library subcalls."""
190149 library = GuppyLibrary ([])
191150 register_source ()
192-
193- def fake_check (_def_ids , * , reset = True ) -> None :
194- del reset
195- emit_warning (make_warning ())
151+ install_check_warning (monkeypatch )
196152
197153 def fake_compile (_def_ids , * , reset = True ):
198154 del reset
199155 emit_warning (make_warning ())
200156 return SimpleNamespace (package = SimpleNamespace (modules = []))
201157
202- monkeypatch .setattr (ENGINE , "check" , fake_check )
203158 monkeypatch .setattr (ENGINE , "compile" , fake_compile )
204159
205160 with warnings .catch_warnings (record = True ) as records :
206161 warnings .simplefilter ("always" )
207162 with rich_warnings ():
208163 library .compile ()
209164
210- assert len (records ) == 1
165+ guppy_records = guppy_warning_records (records )
166+ assert len (guppy_records ) == 1
211167 err = capsys .readouterr ().err
212168 assert err .count ("Warning: Public API warning" ) == 1
0 commit comments