Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/sample/sample_dir/sample_file_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sample_string
123
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions tests/sample/sample_json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"a": 1,
"b": {
"c": "value",
"d": [2, "string"]
}
}
127 changes: 127 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,77 @@
from collections import OrderedDict
from cfbs.utils import (
are_paths_equal,
canonify,
deduplicate_def_json,
deduplicate_list,
dict_diff,
dict_sorted_by_key,
file_sha256,
immediate_files,
immediate_subdirectories,
is_a_commit_hash,
merge_json,
loads_bundlenames,
pad_left,
pad_right,
path_append,
read_file,
read_json,
string_sha256,
strip_left,
strip_right,
)


def test_pad_left():
s = "module_name"
n = 20

assert pad_left(s, n) == " module_name"


def test_pad_right():
s = "module_name"
n = 20

assert pad_right(s, n) == "module_name "


def test_strip_right():
s = "abab"

assert strip_right(s, "ab") == "ab"
assert strip_right(s, "a") == "abab"


def test_strip_left():
s = "abab"

assert strip_left(s, "ab") == "ab"
assert strip_left(s, "b") == "abab"


def test_read_file():
file_path = "tests/sample/sample_dir/sample_file_1.txt"
expected_str = "sample_string\n123"
nonpath = "tests/sample/sample_dir/sample_file_doesnt_exist.txt"

assert read_file(file_path) == expected_str
assert read_file(nonpath) is None


def test_read_json():
json_path = "tests/sample/sample_json.json"
expected_dict = OrderedDict(
[("a", 1), ("b", OrderedDict([("c", "value"), ("d", [2, "string"])]))]
)

assert read_json(json_path) == expected_dict

assert read_json("tests/thisfiledoesntexist.json") is None
assert read_json("tests/thisdirdoesntexist/file.json") is None


def test_merge_json():
original = {"classes": {"services_autorun": ["any"]}}
extras = {
Expand Down Expand Up @@ -142,13 +205,68 @@ def test_deduplicate_def_json():
assert deduplicated == expected


def test_deduplicate_list():
l = [1, 2, 3, 3, 1, 4]

assert deduplicate_list(l) == [1, 2, 3, 4]


def test_dict_sorted_by_key():
d = {"b": 1, "c": 3, "a": 2}

expected_dict = OrderedDict([("a", 2), ("b", 1), ("c", 3)])

assert dict_sorted_by_key(d) == expected_dict


def test_dict_diff():
A = {"A": "a", "B": "b", "C": "c"}
B = {"A": "a", "B": "c", "D": "d"}

assert dict_diff(A, B) == (["C"], ["D"], [("B", "b", "c")])


def test_immediate_subdirectories():
path = "tests/sample/sample_dir"
expected = ["sample_subdir_A", "sample_subdir_B"]

actual = immediate_subdirectories(path)
# `immediate_subdirectories` currently returns the entries in arbitrary order
actual = sorted(actual)

assert actual == expected


def test_immediate_files():
path = "tests/sample/sample_dir"
expected = ["sample_file_1.txt", "sample_file_2.txt"]

actual = immediate_files(path)
# `immediate_files` currently returns the entries in arbitrary order
actual = sorted(actual)

assert actual == expected


def test_path_append():
path = "tests/sample/sample_dir"

# `path_append` is currently coupled with the below code
import os

path = os.path.abspath(os.path.expanduser(path))

assert path_append(path, "abc") == path + "/abc"
assert path_append(path, None) == path


def test_are_paths_equal():
path_a = "abc"
path_b = "abc/..//abc/"

assert are_paths_equal(path_a, path_b)


def test_string_sha256():
s = "cfbs/masterfiles/"
checksum = "9e63d3266f80328fb6547b3462e81ab55b13f689d6b0944e242e2b3a0f3a32a3"
Expand All @@ -163,6 +281,15 @@ def test_file_sha256():
assert file_sha256(file_path) == checksum


def test_is_a_commit_hash():
assert is_a_commit_hash("304d123ac7ff50714a1eb57077acf159f923c941") == True
sha256_hash = "98142d6fa7e2e5f0942b0a215c1c4b976e7ae2ee5edb61cef974f1ba6756cbbc"
assert is_a_commit_hash(sha256_hash) == True
# at least currently, commit cannot be a shortened hash
assert is_a_commit_hash("4738c43") == False
assert is_a_commit_hash("") == False


def test_canonify():
assert canonify("Hello CFEngine!") == "Hello_CFEngine_"
assert canonify("/etc/os-release") == "_etc_os_release"
Expand Down