1+ from collections import OrderedDict
12from cfbs .utils import (
23 canonify ,
34 deduplicate_def_json ,
5+ deduplicate_list ,
46 dict_diff ,
7+ dict_sorted_by_key ,
58 file_sha256 ,
9+ immediate_files ,
10+ immediate_subdirectories ,
11+ is_a_commit_hash ,
612 merge_json ,
713 loads_bundlenames ,
14+ pad_left ,
15+ pad_right ,
16+ path_append ,
17+ read_json ,
818 string_sha256 ,
919)
1020
1121
22+ def test_pad_left ():
23+ s = "module_name"
24+ n = 20
25+
26+ assert pad_left (s , n ) == " module_name"
27+
28+
29+ def test_pad_right ():
30+ s = "module_name"
31+ n = 20
32+
33+ assert pad_right (s , n ) == "module_name "
34+
35+
36+ def test_read_json ():
37+ json_path = "tests/sample/sample_json.json"
38+ expected_dict = OrderedDict (
39+ [("a" , 1 ), ("b" , OrderedDict ([("c" , "value" ), ("d" , [2 , "string" ])]))]
40+ )
41+
42+ assert read_json (json_path ) == expected_dict
43+
44+ assert read_json ("tests/thisfiledoesntexist.json" ) is None
45+ assert read_json ("tests/thisdirdoesntexist/file.json" ) is None
46+
47+
1248def test_merge_json ():
1349 original = {"classes" : {"services_autorun" : ["any" ]}}
1450 extras = {
@@ -142,13 +178,48 @@ def test_deduplicate_def_json():
142178 assert deduplicated == expected
143179
144180
181+ def test_deduplicate_list ():
182+ l = [1 , 2 , 3 , 3 , 1 , 4 ]
183+
184+ assert deduplicate_list (l ) == [1 , 2 , 3 , 4 ]
185+
186+
187+ def test_dict_sorted_by_key ():
188+ d = {"b" : 1 , "c" : 3 , "a" : 2 }
189+
190+ expected_dict = OrderedDict ([("a" , 2 ), ("b" , 1 ), ("c" , 3 )])
191+
192+ assert dict_sorted_by_key (d ) == expected_dict
193+
194+
145195def test_dict_diff ():
146196 A = {"A" : "a" , "B" : "b" , "C" : "c" }
147197 B = {"A" : "a" , "B" : "c" , "D" : "d" }
148198
149199 assert dict_diff (A , B ) == (["C" ], ["D" ], [("B" , "b" , "c" )])
150200
151201
202+ def test_immediate_subdirectories ():
203+ path = "tests/sample/sample_dir"
204+ expected = ["sample_subdir_A" , "sample_subdir_B" ]
205+
206+ assert immediate_subdirectories (path ) == expected
207+
208+
209+ def test_immediate_files ():
210+ path = "tests/sample/sample_dir"
211+ expected = ["sample_file_1.txt" , "sample_file_2.txt" ]
212+
213+ assert immediate_files (path ) == expected
214+
215+
216+ def test_path_append ():
217+ path = "tests/sample/sample_dir"
218+
219+ assert path_append (path , "abc" ) == "tests/sample/sample_dir/abc"
220+ assert path_append (path , None ) == path
221+
222+
152223def test_string_sha256 ():
153224 s = "cfbs/masterfiles/"
154225 checksum = "9e63d3266f80328fb6547b3462e81ab55b13f689d6b0944e242e2b3a0f3a32a3"
@@ -163,6 +234,15 @@ def test_file_sha256():
163234 assert file_sha256 (file_path ) == checksum
164235
165236
237+ def test_is_a_commit_hash ():
238+ assert is_a_commit_hash ("304d123ac7ff50714a1eb57077acf159f923c941" ) == True
239+ sha256_hash = "98142d6fa7e2e5f0942b0a215c1c4b976e7ae2ee5edb61cef974f1ba6756cbbc"
240+ assert is_a_commit_hash (sha256_hash ) == True
241+ # at least currently, commit cannot be a shortened hash
242+ assert is_a_commit_hash ("4738c43" ) == False
243+ assert is_a_commit_hash ("" ) == False
244+
245+
166246def test_canonify ():
167247 assert canonify ("Hello CFEngine!" ) == "Hello_CFEngine_"
168248 assert canonify ("/etc/os-release" ) == "_etc_os_release"
0 commit comments