From 680560e85306a1995b03b73b5ce00e14e83dfd52 Mon Sep 17 00:00:00 2001 From: jakub-nt <175944085+jakub-nt@users.noreply.github.com> Date: Mon, 23 Jun 2025 19:46:10 +0200 Subject: [PATCH] Added more unit test coverage Signed-off-by: jakub-nt <175944085+jakub-nt@users.noreply.github.com> --- tests/sample/sample_dir/sample_file_1.txt | 2 + tests/sample/sample_dir/sample_file_2.txt | 0 .../sample_subdir_A/sample_file_3.txt | 0 .../sample_subdir_C/sample_file_4.txt | 0 tests/sample/sample_json.json | 7 + tests/test_utils.py | 127 ++++++++++++++++++ 6 files changed, 136 insertions(+) create mode 100644 tests/sample/sample_dir/sample_file_1.txt create mode 100644 tests/sample/sample_dir/sample_file_2.txt create mode 100644 tests/sample/sample_dir/sample_subdir_A/sample_file_3.txt create mode 100644 tests/sample/sample_dir/sample_subdir_B/sample_subdir_C/sample_file_4.txt create mode 100644 tests/sample/sample_json.json diff --git a/tests/sample/sample_dir/sample_file_1.txt b/tests/sample/sample_dir/sample_file_1.txt new file mode 100644 index 00000000..4c22e6e0 --- /dev/null +++ b/tests/sample/sample_dir/sample_file_1.txt @@ -0,0 +1,2 @@ +sample_string +123 \ No newline at end of file diff --git a/tests/sample/sample_dir/sample_file_2.txt b/tests/sample/sample_dir/sample_file_2.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/sample/sample_dir/sample_subdir_A/sample_file_3.txt b/tests/sample/sample_dir/sample_subdir_A/sample_file_3.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/sample/sample_dir/sample_subdir_B/sample_subdir_C/sample_file_4.txt b/tests/sample/sample_dir/sample_subdir_B/sample_subdir_C/sample_file_4.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/sample/sample_json.json b/tests/sample/sample_json.json new file mode 100644 index 00000000..56cd6a5c --- /dev/null +++ b/tests/sample/sample_json.json @@ -0,0 +1,7 @@ +{ + "a": 1, + "b": { + "c": "value", + "d": [2, "string"] + } +} diff --git a/tests/test_utils.py b/tests/test_utils.py index edba41a6..1a6ebe37 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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 = { @@ -142,6 +205,20 @@ 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"} @@ -149,6 +226,47 @@ def test_dict_diff(): 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" @@ -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"