88
99from __future__ import annotations
1010
11- import warnings
1211from pathlib import Path
1312
1413import pytest
@@ -44,19 +43,20 @@ def test_yaml_extensions_in_input_dir_return_grandparent(
4443 assert data_dir == tmp_path
4544 assert name == expected_stem
4645
46+ @pytest .mark .parametrize ("dirname" , ["input" , "INPUT" ])
4747 def test_yaml_in_nested_input_dir_returns_grandparent (
48- self , tmp_path : Path , story : Scenario
48+ self , tmp_path : Path , story : Scenario , dirname : str
4949 ) -> None :
50- story .given ("a YAML file inside a deeply nested input / subdirectory" )
51- nested = tmp_path / "projects" / "resumes" / "input"
50+ story .given (f "a YAML file inside a deeply nested { dirname } / subdirectory" )
51+ nested = tmp_path / "projects" / "resumes" / dirname
5252 nested .mkdir (parents = True )
5353 yaml_file = nested / "resume.yaml"
5454 yaml_file .touch ()
5555
5656 story .when ("inferring data_dir without an explicit override" )
5757 data_dir , name = _infer_data_dir_and_name (yaml_file , data_dir = None )
5858
59- story .then ("data_dir points to the grandparent of input/" )
59+ story .then ("data_dir points to the grandparent of the input/ directory " )
6060 assert data_dir == tmp_path / "projects" / "resumes"
6161 assert name == "resume"
6262
@@ -181,10 +181,10 @@ def test_explicit_data_dir_with_nonexistent_yaml(
181181
182182
183183class TestExplicitDataDirEndingInInput :
184- """Warn when explicit data_dir ends in ``input/`` (path-doubling risk )."""
184+ """Raise ValueError when data_dir ends in ``input/`` (path-doubling)."""
185185
186186 @pytest .mark .parametrize ("dirname" , ["input" , "Input" , "INPUT" , "iNpUt" ])
187- def test_warns_when_data_dir_named_input (
187+ def test_raises_when_data_dir_named_input (
188188 self , tmp_path : Path , story : Scenario , dirname : str
189189 ) -> None :
190190 story .given (f"an explicit data_dir whose basename is '{ dirname } '" )
@@ -194,17 +194,11 @@ def test_warns_when_data_dir_named_input(
194194 yaml_file .touch ()
195195
196196 story .when ("inferring with that data_dir" )
197- with warnings .catch_warnings (record = True ) as caught :
198- warnings .simplefilter ("always" )
199- data_dir , name = _infer_data_dir_and_name (yaml_file , data_dir = input_dir )
200-
201- story .then ("a UserWarning about path-doubling is emitted regardless of case" )
202- assert len (caught ) == 1
203- assert "path doubling" in str (caught [0 ].message ).lower ()
204- assert data_dir == input_dir
205- assert name == "sample"
197+ story .then ("a ValueError about path-doubling is raised regardless of case" )
198+ with pytest .raises (ValueError , match = "path doubling" ):
199+ _infer_data_dir_and_name (yaml_file , data_dir = input_dir )
206200
207- def test_no_warning_for_normal_data_dir (
201+ def test_no_error_for_normal_data_dir (
208202 self , tmp_path : Path , story : Scenario
209203 ) -> None :
210204 story .given ("an explicit data_dir with a normal name" )
@@ -214,12 +208,11 @@ def test_no_warning_for_normal_data_dir(
214208 yaml_file .touch ()
215209
216210 story .when ("inferring with that data_dir" )
217- with warnings .catch_warnings (record = True ) as caught :
218- warnings .simplefilter ("always" )
219- _infer_data_dir_and_name (yaml_file , data_dir = custom_dir )
211+ data_dir , name = _infer_data_dir_and_name (yaml_file , data_dir = custom_dir )
220212
221- story .then ("no warning is emitted" )
222- assert len (caught ) == 0
213+ story .then ("no error is raised and data_dir is used as-is" )
214+ assert data_dir == custom_dir
215+ assert name == "sample"
223216
224217
225218class TestRootLevelInputGuard :
@@ -251,6 +244,22 @@ def test_root_level_input_raises(self, story: Scenario) -> None:
251244 with pytest .raises (ValueError , match = "Unable to infer data_dir" ):
252245 _infer_data_dir_and_name ("/input/resume.yaml" , data_dir = None )
253246
247+ def test_relative_input_dir_guard (
248+ self , tmp_path : Path , story : Scenario , monkeypatch : pytest .MonkeyPatch
249+ ) -> None :
250+ story .given ("a YAML at relative input/resume.yaml (grandparent is '.')" )
251+ input_dir = tmp_path / "input"
252+ input_dir .mkdir ()
253+ yaml_file = input_dir / "resume.yaml"
254+ yaml_file .touch ()
255+
256+ monkeypatch .chdir (tmp_path )
257+
258+ story .when ("inferring data_dir via the relative path" )
259+ story .then ("ValueError is raised because grandparent resolves to '.'" )
260+ with pytest .raises (ValueError , match = "root-level" ):
261+ _infer_data_dir_and_name (Path ("input/resume.yaml" ), data_dir = None )
262+
254263
255264class TestInferDataDirErrorCases :
256265 """Invalid inputs raise ValueError."""
0 commit comments