2626import unittest
2727
2828import mock
29+ import saneyaml
2930
3031from attributecode import CRITICAL
3132from attributecode import ERROR
4041from testing_utils import extract_test_loc
4142from testing_utils import get_temp_file
4243from testing_utils import get_test_loc
43- import saneyaml
44+
45+ try :
46+ # Python 2
47+ unicode # NOQA
48+ except NameError : # pragma: nocover
49+ # Python 3
50+ unicode = str # NOQA
4451
4552
46- def check_csv (expected , result , regen = False ):
53+ def check_csv (expected , result , regen = False , fix_cell_linesep = False ):
4754 """
4855 Assert that the contents of two CSV files locations `expected` and
4956 `result` are equal.
5057 """
5158 if regen :
5259 shutil .copyfile (result , expected )
5360 expected = sorted ([sorted (d .items ()) for d in load_csv (expected )])
54- result = sorted ([sorted (d .items ()) for d in load_csv (result )])
61+ result = [d .items () for d in load_csv (result )]
62+ if fix_cell_linesep :
63+ result = [list (fix_crlf (items )) for items in result ]
64+ result = sorted (sorted (items ) for items in result )
65+
5566 assert expected == result
5667
5768
69+ def fix_crlf (items ):
70+ """
71+ Hackish... somehow the CVS returned on Windows is sometimes using a backward
72+ linesep convention:
73+ instead of LF inside cells and CRLF at EOL,
74+ they use CRLF everywhere.
75+ This is fixing this until we find can why
76+ """
77+ for key , value in items :
78+ if isinstance (value , unicode ) and '\r \n ' in value :
79+ value = value .replace ('\r \n ' , '\n ' )
80+ yield key , value
81+
82+
5883def check_json (expected , result ):
5984 """
6085 Assert that the contents of two JSON files are equal.
@@ -102,7 +127,7 @@ def test_empty_Field_has_default_value(self):
102127 def test_PathField_check_location (self ):
103128 test_file = 'license.LICENSE'
104129 field = model .PathField (name = 'f' , value = test_file , present = True )
105- base_dir = get_test_loc ('test_model/fields ' )
130+ base_dir = get_test_loc ('test_model/base_dir ' )
106131
107132 errors = field .validate (base_dir = base_dir )
108133 expected_errrors = []
@@ -115,7 +140,7 @@ def test_PathField_check_location(self):
115140 def test_PathField_check_missing_location (self ):
116141 test_file = 'does.not.exist'
117142 field = model .PathField (name = 'f' , value = test_file , present = True )
118- base_dir = get_test_loc ('test_model/fields ' )
143+ base_dir = get_test_loc ('test_model/base_dir ' )
119144 errors = field .validate (base_dir = base_dir )
120145
121146 file_path = posixpath .join (base_dir , test_file )
@@ -132,7 +157,7 @@ def test_TextField_loads_file(self):
132157 field = model .FileTextField (
133158 name = 'f' , value = 'license.LICENSE' , present = True )
134159
135- base_dir = get_test_loc ('test_model/fields ' )
160+ base_dir = get_test_loc ('test_model/base_dir ' )
136161 errors = field .validate (base_dir = base_dir )
137162 assert [] == errors
138163
@@ -230,6 +255,18 @@ def test_saneyaml_load_can_parse_simple_fields(self):
230255
231256 assert expected == list (result .items ())
232257
258+ def test_saneyaml_load_does_not_convert_to_crlf (self ):
259+ test = get_test_content ('test_model/crlf/about.ABOUT' )
260+ result = saneyaml .load (test )
261+
262+ expected = [
263+ (u'about_resource' , u'.' ),
264+ (u'name' , u'pytest' ),
265+ (u'description' , u'first line\n second line\n third line\n ' ),
266+ (u'copyright' , u'copyright' )
267+ ]
268+ assert expected == list (result .items ())
269+
233270 def test_saneyaml_load_can_parse_continuations (self ):
234271 test = get_test_content ('test_model/parse/continuation.about' )
235272 result = saneyaml .load (test )
@@ -937,14 +974,14 @@ def test_collect_inventory_does_not_raise_error_and_maintains_order_on_custom_fi
937974 expected = [u'about_resource: .\n name: test\n resource: .\n custom_mapping: test\n ' ]
938975 assert expected == [a .dumps () for a in abouts ]
939976
940- def test_saneyaml_load_license_expression (self ):
977+ def test_parse_license_expression (self ):
941978 spec_char , returned_lic = model .parse_license_expression ('mit or apache-2.0' )
942979 expected_lic = ['mit' , 'apache-2.0' ]
943980 expected_spec_char = []
944981 assert expected_lic == returned_lic
945982 assert expected_spec_char == spec_char
946983
947- def test_saneyaml_load_license_expression_with_special_chara (self ):
984+ def test_parse_license_expression_with_special_chara (self ):
948985 spec_char , returned_lic = model .parse_license_expression ('mit, apache-2.0' )
949986 expected_lic = []
950987 expected_spec_char = [',' ]
@@ -1019,7 +1056,18 @@ def test_collect_inventory_complex_from_directory(self):
10191056 assert all (e .severity == INFO for e in errors )
10201057
10211058 expected = get_test_loc ('test_model/inventory/complex/expected.csv' )
1022- check_csv (expected , result )
1059+ check_csv (expected , result , fix_cell_linesep = True , regen = False )
1060+
1061+ def test_collect_inventory_does_not_convert_lf_to_crlf_from_directory (self ):
1062+ location = get_test_loc ('test_model/crlf/about.ABOUT' )
1063+ result = get_temp_file ()
1064+ errors , abouts = model .collect_inventory (location )
1065+ errors2 = model .write_output (abouts , result , format = 'csv' )
1066+ errors .extend (errors2 )
1067+ assert all (e .severity == INFO for e in errors )
1068+
1069+ expected = get_test_loc ('test_model/crlf/expected.csv' )
1070+ check_csv (expected , result , fix_cell_linesep = True , regen = False )
10231071
10241072
10251073class FetchLicenseTest (unittest .TestCase ):
0 commit comments