Skip to content

Commit 31e1536

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

7 files changed

Lines changed: 98 additions & 8 deletions

File tree

cfbs/utils.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import copy
66
import subprocess
77
import hashlib
8-
import logging as log
98
from typing import List, Tuple
109
import urllib
1110
import urllib.request # needed on some platforms
@@ -79,12 +78,12 @@ def cp(src, dst):
7978
sh("rsync -r %s/ %s" % (src, dst))
8079

8180

82-
def pad_left(s, n) -> int:
83-
return s if len(s) >= n else " " * (n - len(s)) + s
81+
def pad_left(s, n):
82+
return s.rjust(n)
8483

8584

86-
def pad_right(s, n) -> int:
87-
return s if len(s) >= n else s + " " * (n - len(s))
85+
def pad_right(s, n):
86+
return s.ljust(n)
8887

8988

9089
def split_command(command) -> Tuple[str, List[str]]:
@@ -137,12 +136,14 @@ def item_index(iterable, item, extra_at_end=True):
137136

138137

139138
def strip_right(string, ending):
139+
# can be replaced with str.removesuffix from Python 3.9 onwards
140140
if not string.endswith(ending):
141141
return string
142142
return string[0 : -len(ending)]
143143

144144

145145
def strip_left(string, beginning):
146+
# can be replaced with str.removeprefix from Python 3.9 onwards
146147
if not string.startswith(beginning):
147148
return string
148149
return string[len(beginning) :]
@@ -163,7 +164,7 @@ def save_file(path, data):
163164
f.write(data)
164165

165166

166-
def read_json(path):
167+
def read_json(path) -> OrderedDict:
167168
try:
168169
with open(path, "r") as f:
169170
return json.loads(f.read(), object_pairs_hook=OrderedDict)
@@ -280,7 +281,6 @@ def immediate_files(path):
280281

281282

282283
def path_append(dir, subdir):
283-
dir = os.path.abspath(os.path.expanduser(dir))
284284
return dir if not subdir else os.path.join(dir, subdir)
285285

286286

@@ -296,7 +296,10 @@ def are_paths_equal(path_a, path_b) -> bool:
296296

297297

298298
def cfengine_dir(subdir=None):
299-
return path_append("~/.cfengine/", subdir)
299+
CFENGINE_DIR = "~/.cfengine/"
300+
cfengine_dir_abspath = os.path.abspath(os.path.expanduser(CFENGINE_DIR))
301+
302+
return path_append(cfengine_dir_abspath, subdir)
300303

301304

302305
def cfbs_dir(append=None) -> str:

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: 80 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,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+
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+
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+
152223
def 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+
166246
def test_canonify():
167247
assert canonify("Hello CFEngine!") == "Hello_CFEngine_"
168248
assert canonify("/etc/os-release") == "_etc_os_release"

0 commit comments

Comments
 (0)