Skip to content

Commit e88e936

Browse files
committed
Added more unit test coverage
Signed-off-by: jakub-nt <175944085+jakub-nt@users.noreply.github.com>
1 parent bc81301 commit e88e936

6 files changed

Lines changed: 100 additions & 0 deletions

File tree

tests/sample/sample_dir/sample_file_1.txt

Whitespace-only changes.

tests/sample/sample_dir/sample_file_2.txt

Whitespace-only changes.

tests/sample/sample_dir/sample_subdir_A/sample_file_3.txt

Whitespace-only changes.

tests/sample/sample_dir/sample_subdir_B/sample_subdir_C/sample_file_4.txt

Whitespace-only changes.

tests/sample/sample_json.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"a": 1,
3+
"b": {
4+
"c": "value",
5+
"d": [2, "string"]
6+
}
7+
}

tests/test_utils.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,50 @@
1+
from collections import OrderedDict
12
from 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+
1248
def test_merge_json():
1349
original = {"classes": {"services_autorun": ["any"]}}
1450
extras = {
@@ -142,13 +178,61 @@ 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+
145195
def 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+
actual = immediate_subdirectories(path)
207+
# `immediate_subdirectories` currently returns the entries in arbitrary order
208+
actual = sorted(actual)
209+
210+
assert actual == expected
211+
212+
213+
def test_immediate_files():
214+
path = "tests/sample/sample_dir"
215+
expected = ["sample_file_1.txt", "sample_file_2.txt"]
216+
217+
actual = immediate_files(path)
218+
# `immediate_files` currently returns the entries in arbitrary order
219+
actual = sorted(actual)
220+
221+
assert actual == expected
222+
223+
224+
def test_path_append():
225+
path = "tests/sample/sample_dir"
226+
227+
# `path_append` is currently coupled with the below code
228+
import os
229+
230+
path = os.path.abspath(os.path.expanduser(path))
231+
232+
assert path_append(path, "abc") == path + "/abc"
233+
assert path_append(path, None) == path
234+
235+
152236
def test_string_sha256():
153237
s = "cfbs/masterfiles/"
154238
checksum = "9e63d3266f80328fb6547b3462e81ab55b13f689d6b0944e242e2b3a0f3a32a3"
@@ -163,6 +247,15 @@ def test_file_sha256():
163247
assert file_sha256(file_path) == checksum
164248

165249

250+
def test_is_a_commit_hash():
251+
assert is_a_commit_hash("304d123ac7ff50714a1eb57077acf159f923c941") == True
252+
sha256_hash = "98142d6fa7e2e5f0942b0a215c1c4b976e7ae2ee5edb61cef974f1ba6756cbbc"
253+
assert is_a_commit_hash(sha256_hash) == True
254+
# at least currently, commit cannot be a shortened hash
255+
assert is_a_commit_hash("4738c43") == False
256+
assert is_a_commit_hash("") == False
257+
258+
166259
def test_canonify():
167260
assert canonify("Hello CFEngine!") == "Hello_CFEngine_"
168261
assert canonify("/etc/os-release") == "_etc_os_release"

0 commit comments

Comments
 (0)