33# pylint: disable=redefined-outer-name
44# pylint: disable=unused-argument
55# pylint: disable=use-implicit-booleaness-not-comparison
6+ import configparser
67import json
78import re
89
@@ -492,6 +493,21 @@ def test_add_to_output_with_none(self):
492493 comparator = dir_content_diff .XmlComparator ()
493494 comparator .add_to_output (None , None )
494495
496+ class TestIniComparator :
497+ """Test the INI comparator."""
498+
499+ def test_initodict (self , ref_tree ):
500+ """Test conversion of INI files into dict."""
501+ data = configparser .ConfigParser ()
502+ data .read (ref_tree / "file.ini" )
503+
504+ comparator = dir_content_diff .IniComparator ()
505+ res = comparator .configparser_to_dict (data )
506+ assert res == {
507+ "section1" : {"attr1" : "val1" , "attr2" : 1 },
508+ "section2" : {"attr3" : [1 , 2 , "a" , "b" ], "attr4" : {"a" : 1 , "b" : [1 , 2 ]}},
509+ }
510+
495511
496512class TestRegistry :
497513 """Test the internal registry."""
@@ -500,6 +516,9 @@ def test_init_register(self, registry_reseter):
500516 """Test the initial registry with the get_comparators() function."""
501517 assert dir_content_diff .get_comparators () == {
502518 None : dir_content_diff .DefaultComparator (),
519+ ".cfg" : dir_content_diff .IniComparator (),
520+ ".conf" : dir_content_diff .IniComparator (),
521+ ".ini" : dir_content_diff .IniComparator (),
503522 ".json" : dir_content_diff .JsonComparator (),
504523 ".pdf" : dir_content_diff .PdfComparator (),
505524 ".yaml" : dir_content_diff .YamlComparator (),
@@ -512,6 +531,9 @@ def test_update_register(self, registry_reseter):
512531 dir_content_diff .register_comparator (".test_ext" , dir_content_diff .JsonComparator ())
513532 assert dir_content_diff .get_comparators () == {
514533 None : dir_content_diff .DefaultComparator (),
534+ ".cfg" : dir_content_diff .IniComparator (),
535+ ".conf" : dir_content_diff .IniComparator (),
536+ ".ini" : dir_content_diff .IniComparator (),
515537 ".test_ext" : dir_content_diff .JsonComparator (),
516538 ".json" : dir_content_diff .JsonComparator (),
517539 ".pdf" : dir_content_diff .PdfComparator (),
@@ -524,6 +546,9 @@ def test_update_register(self, registry_reseter):
524546 dir_content_diff .unregister_comparator ("json" ) # Test suffix without dot
525547 assert dir_content_diff .get_comparators () == {
526548 None : dir_content_diff .DefaultComparator (),
549+ ".cfg" : dir_content_diff .IniComparator (),
550+ ".conf" : dir_content_diff .IniComparator (),
551+ ".ini" : dir_content_diff .IniComparator (),
527552 ".test_ext" : dir_content_diff .JsonComparator (),
528553 ".pdf" : dir_content_diff .PdfComparator (),
529554 ".yml" : dir_content_diff .YamlComparator (),
@@ -533,6 +558,9 @@ def test_update_register(self, registry_reseter):
533558 dir_content_diff .reset_comparators ()
534559 assert dir_content_diff .get_comparators () == {
535560 None : dir_content_diff .DefaultComparator (),
561+ ".cfg" : dir_content_diff .IniComparator (),
562+ ".conf" : dir_content_diff .IniComparator (),
563+ ".ini" : dir_content_diff .IniComparator (),
536564 ".json" : dir_content_diff .JsonComparator (),
537565 ".pdf" : dir_content_diff .PdfComparator (),
538566 ".yaml" : dir_content_diff .YamlComparator (),
@@ -556,6 +584,9 @@ def test_update_register(self, registry_reseter):
556584 dir_content_diff .register_comparator (".new_ext" , dir_content_diff .JsonComparator ())
557585 assert dir_content_diff .get_comparators () == {
558586 None : dir_content_diff .DefaultComparator (),
587+ ".cfg" : dir_content_diff .IniComparator (),
588+ ".conf" : dir_content_diff .IniComparator (),
589+ ".ini" : dir_content_diff .IniComparator (),
559590 ".json" : dir_content_diff .JsonComparator (),
560591 ".pdf" : dir_content_diff .PdfComparator (),
561592 ".yaml" : dir_content_diff .YamlComparator (),
@@ -568,6 +599,9 @@ def test_update_register(self, registry_reseter):
568599 )
569600 assert dir_content_diff .get_comparators () == {
570601 None : dir_content_diff .DefaultComparator (),
602+ ".cfg" : dir_content_diff .IniComparator (),
603+ ".conf" : dir_content_diff .IniComparator (),
604+ ".ini" : dir_content_diff .IniComparator (),
571605 ".json" : dir_content_diff .JsonComparator (),
572606 ".pdf" : dir_content_diff .PdfComparator (),
573607 ".yaml" : dir_content_diff .YamlComparator (),
@@ -676,17 +710,18 @@ def test_specific_args(self, ref_tree, res_tree_equal):
676710class TestDiffTrees :
677711 """Tests that should return differences."""
678712
679- def test_diff_tree (self , ref_tree , res_tree_diff , pdf_diff , dict_diff , xml_diff ):
713+ def test_diff_tree (self , ref_tree , res_tree_diff , pdf_diff , dict_diff , xml_diff , ini_diff ):
680714 """Test that the returned differences are correct."""
681715 res = compare_trees (ref_tree , res_tree_diff )
682716
683- assert len (res ) == 4
717+ assert len (res ) == 5
684718 match_res_0 = re .match (pdf_diff , res ["file.pdf" ])
685719 match_res_1 = re .match (dict_diff , res ["file.json" ])
686720 match_res_2 = re .match (dict_diff , res ["file.yaml" ])
687721 match_res_3 = re .match (xml_diff , res ["file.xml" ])
722+ match_res_4 = re .match (ini_diff , res ["file.ini" ])
688723
689- for match_i in [match_res_0 , match_res_1 , match_res_2 , match_res_3 ]:
724+ for match_i in [match_res_0 , match_res_1 , match_res_2 , match_res_3 , match_res_4 ]:
690725 assert match_i is not None
691726
692727 def test_assert_equal_trees (self , ref_tree , res_tree_diff , pdf_diff , dict_diff , xml_diff ):
@@ -704,7 +739,7 @@ def test_diff_ref_not_empty_res_empty(self, ref_tree, empty_res_tree):
704739 """Test with empty compared tree."""
705740 res = compare_trees (ref_tree , empty_res_tree )
706741
707- assert len (res ) == 4
742+ assert len (res ) == 5
708743 match_res_0 = re .match (
709744 r"The file 'file.pdf' does not exist in '\S*/res'\." , res ["file.pdf" ]
710745 )
@@ -717,8 +752,11 @@ def test_diff_ref_not_empty_res_empty(self, ref_tree, empty_res_tree):
717752 match_res_3 = re .match (
718753 r"The file 'file.xml' does not exist in '\S*/res'\." , res ["file.xml" ]
719754 )
755+ match_res_4 = re .match (
756+ r"The file 'file.ini' does not exist in '\S*/res'\." , res ["file.ini" ]
757+ )
720758
721- for match_i in [match_res_0 , match_res_1 , match_res_2 , match_res_3 ]:
759+ for match_i in [match_res_0 , match_res_1 , match_res_2 , match_res_3 , match_res_4 ]:
722760 assert match_i is not None
723761
724762 def test_exception_in_comparator (self , ref_tree , res_tree_equal , registry_reseter ):
@@ -740,7 +778,7 @@ def bad_comparator(ref_path, test_path, *args, **kwargs):
740778 )
741779 assert match is not None
742780
743- def test_specific_args (self , ref_tree , res_tree_diff , dict_diff , xml_diff ):
781+ def test_specific_args (self , ref_tree , res_tree_diff , dict_diff , xml_diff , ini_diff ):
744782 """Test specific args."""
745783 specific_args = {
746784 "file.pdf" : {"threshold" : 50 },
@@ -749,7 +787,7 @@ def test_specific_args(self, ref_tree, res_tree_diff, dict_diff, xml_diff):
749787 res = compare_trees (ref_tree , res_tree_diff , specific_args = specific_args )
750788
751789 # This time the PDF files are considered as equal
752- assert len (res ) == 3
790+ assert len (res ) == 4
753791 match_res_0 = re .match (dict_diff , res ["file.yaml" ])
754792 match_res_1 = re .match (
755793 dict_diff .replace (
@@ -759,8 +797,9 @@ def test_specific_args(self, ref_tree, res_tree_diff, dict_diff, xml_diff):
759797 res ["file.json" ],
760798 )
761799 match_res_2 = re .match (xml_diff , res ["file.xml" ])
800+ match_res_3 = re .match (ini_diff , res ["file.ini" ])
762801
763- for match_i in [match_res_0 , match_res_1 , match_res_2 ]:
802+ for match_i in [match_res_0 , match_res_1 , match_res_2 , match_res_3 ]:
764803 assert match_i is not None
765804
766805 def test_unknown_comparator (self , ref_tree , res_tree_diff , registry_reseter ):
@@ -783,12 +822,14 @@ def test_nested_files(self, ref_with_nested_file, res_diff_with_nested_file):
783822 )
784823 assert match is not None
785824
786- def test_fix_dot_notation (self , ref_tree , res_tree_diff , pdf_diff , dict_diff , xml_diff ):
825+ def test_fix_dot_notation (
826+ self , ref_tree , res_tree_diff , pdf_diff , dict_diff , xml_diff , ini_diff
827+ ):
787828 """Test that the dot notation is properly fixed."""
788829 specific_args = {"file.yaml" : {"args" : [None , None , None , False , 0 , True ]}}
789830 res = compare_trees (ref_tree , res_tree_diff , specific_args = specific_args )
790831
791- assert len (res ) == 4
832+ assert len (res ) == 5
792833 match_res_0 = re .match (pdf_diff , res ["file.pdf" ])
793834 match_res_1 = re .match (
794835 dict_diff .replace (
@@ -800,8 +841,9 @@ def test_fix_dot_notation(self, ref_tree, res_tree_diff, pdf_diff, dict_diff, xm
800841 )
801842 match_res_2 = re .match (dict_diff , res ["file.json" ])
802843 match_res_3 = re .match (xml_diff , res ["file.xml" ])
844+ match_res_4 = re .match (ini_diff , res ["file.ini" ])
803845
804- for match_i in [match_res_0 , match_res_1 , match_res_2 , match_res_3 ]:
846+ for match_i in [match_res_0 , match_res_1 , match_res_2 , match_res_3 , match_res_4 ]:
805847 assert match_i is not None
806848
807849 def test_format_inside_diff (self , ref_tree , res_tree_diff , dict_diff ):
0 commit comments