1515
1616if TYPE_CHECKING :
1717 from ruamel .yaml .comments import CommentedMap , CommentedSeq
18+ from ruamel .yaml .compat import VersionType
1819 from ruamel .yaml .emitter import Emitter
1920
2021fixtures_dir = Path (__file__ ).parent / "fixtures"
@@ -202,8 +203,7 @@ def test_custom_ruamel_yaml_emitter(
202203 assert output == expected_output
203204
204205
205- @pytest .fixture (name = "yaml_formatting_fixtures" )
206- def fixture_yaml_formatting_fixtures (fixture_filename : str ) -> tuple [str , str , str ]:
206+ def load_yaml_formatting_fixtures (fixture_filename : str ) -> tuple [str , str , str ]:
207207 """Get the contents for the formatting fixture files.
208208
209209 To regenerate these fixtures, please run ``pytest --regenerate-formatting-fixtures``.
@@ -220,26 +220,61 @@ def fixture_yaml_formatting_fixtures(fixture_filename: str) -> tuple[str, str, s
220220
221221
222222@pytest .mark .parametrize (
223- "fixture_filename" ,
223+ ( "before" , "after" , "version" ) ,
224224 (
225- pytest .param ("fmt-1.yml" , id = "1" ),
226- pytest .param ("fmt-2.yml" , id = "2" ),
227- pytest .param ("fmt-3.yml" , id = "3" ),
228- pytest .param ("fmt-4.yml" , id = "4" ),
229- pytest .param ("fmt-5.yml" , id = "5" ),
225+ pytest .param ("---\n foo: bar\n " , "---\n foo: bar\n " , None , id = "1" ),
226+ # verify that 'on' is not translated to bool (1.2 behavior)
227+ pytest .param ("---\n foo: on\n " , "---\n foo: on\n " , None , id = "2" ),
228+ # When version is manually mentioned by us, we expect to output without version directive
229+ pytest .param ("---\n foo: on\n " , "---\n foo: on\n " , (1 , 2 ), id = "3" ),
230+ pytest .param ("---\n foo: on\n " , "---\n foo: true\n " , (1 , 1 ), id = "4" ),
231+ pytest .param ("%YAML 1.1\n ---\n foo: on\n " , "---\n foo: true\n " , (1 , 1 ), id = "5" ),
232+ # verify that in-line directive takes precedence but dumping strips if we mention a specific version
233+ pytest .param ("%YAML 1.1\n ---\n foo: on\n " , "---\n foo: true\n " , (1 , 2 ), id = "6" ),
234+ # verify that version directive are kept if present
235+ pytest .param ("%YAML 1.1\n ---\n foo: on\n " , "---\n foo: true\n " , None , id = "7" ),
236+ pytest .param (
237+ "%YAML 1.2\n ---\n foo: on\n " ,
238+ "%YAML 1.2\n ---\n foo: on\n " ,
239+ None ,
240+ id = "8" ,
241+ ),
242+ pytest .param ("---\n foo: YES\n " , "---\n foo: true\n " , (1 , 1 ), id = "9" ),
243+ pytest .param ("---\n foo: YES\n " , "---\n foo: YES\n " , (1 , 2 ), id = "10" ),
244+ pytest .param ("---\n foo: YES\n " , "---\n foo: YES\n " , None , id = "11" ),
245+ ),
246+ )
247+ def test_fmt (before : str , after : str , version : VersionType ) -> None :
248+ """Tests behavior of formatter in regards to different YAML versions, specified or not."""
249+ yaml = ansiblelint .yaml_utils .FormattedYAML (version = version )
250+ data = yaml .load (before )
251+ result = yaml .dumps (data )
252+ assert result == after
253+
254+
255+ @pytest .mark .parametrize (
256+ ("fixture_filename" , "version" ),
257+ (
258+ pytest .param ("fmt-1.yml" , (1 , 1 ), id = "1" ),
259+ pytest .param ("fmt-2.yml" , (1 , 1 ), id = "2" ),
260+ pytest .param ("fmt-3.yml" , (1 , 1 ), id = "3" ),
261+ pytest .param ("fmt-4.yml" , (1 , 1 ), id = "4" ),
262+ pytest .param ("fmt-5.yml" , (1 , 1 ), id = "5" ),
230263 ),
231264)
232265def test_formatted_yaml_loader_dumper (
233- yaml_formatting_fixtures : tuple [ str , str , str ] ,
234- fixture_filename : str , # noqa: ARG001
266+ fixture_filename : str ,
267+ version : tuple [ int , int ],
235268) -> None :
236269 """Ensure that FormattedYAML loads/dumps formatting fixtures consistently."""
237270 # pylint: disable=unused-argument
238- before_content , prettier_content , after_content = yaml_formatting_fixtures
271+ before_content , prettier_content , after_content = load_yaml_formatting_fixtures (
272+ fixture_filename ,
273+ )
239274 assert before_content != prettier_content
240275 assert before_content != after_content
241276
242- yaml = ansiblelint .yaml_utils .FormattedYAML ()
277+ yaml = ansiblelint .yaml_utils .FormattedYAML (version = version )
243278
244279 data_before = yaml .load (before_content )
245280 dump_from_before = yaml .dumps (data_before )
0 commit comments