Passed
Pull Request — rhel9-branch (#227)
by Matěj
01:16
created

test_content_discovery   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 50
dl 0
loc 70
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A labelled_files() 0 12 1
A test_path_presence_detection() 0 17 3
B test_reduce() 0 28 5
1
import os
2
3
import pytest
4
5
import org_fedora_oscap.content_discovery as tested_module
6
7
8
@pytest.fixture
9
def labelled_files():
10
    return {
11
        "dir/datastream": "D",
12
        "dir/datastream2": "D",
13
        "dir/dir/datastream3": "D",
14
        "dir/dir/datastream3": "D",
15
        "dir/XCCDF": "X",
16
        "XCCDF2": "X",
17
        "cpe": "C",
18
        "t1": "T",
19
        "dir3/t2": "T",
20
    }
21
22
23
def test_reduce(labelled_files):
24
    bringer = tested_module.ContentBringer(None)
25
26
    d_count = 0
27
    x_count = 0
28
    for l in labelled_files.values():
29
        if l == "D":
30
            d_count += 1
31
        elif l == "X":
32
            x_count += 1
33
34
    reduced = bringer.reduce_files(labelled_files, "dir/datastream", ["D"])
35
    assert len(reduced) == len(labelled_files) - d_count + 1
36
    assert "dir/datastream" in reduced
37
38
    reduced = bringer.reduce_files(labelled_files, "dir/datastream", ["D", "X"])
39
    assert len(reduced) == len(labelled_files) - d_count - x_count + 1
40
    assert "dir/datastream" in reduced
41
42
    reduced = bringer.reduce_files(labelled_files, "dir/XCCDF", ["D", "X"])
43
    assert len(reduced) == len(labelled_files) - d_count - x_count + 1
44
    assert "dir/XCCDF" in reduced
45
46
    with pytest.raises(RuntimeError, match="dir/datastream4"):
47
        bringer.reduce_files(labelled_files, "dir/datastream4", ["D"])
48
49
    reduced = bringer.reduce_files(labelled_files, "cpe", ["C"])
50
    assert reduced == labelled_files
51
52
53
def test_path_presence_detection():
54
    list_of_paths = ["file1", os.path.abspath("file2"), os.path.abspath("dir///file3")]
55
56
    list_of_paths_in_list = [
57
        "file1", os.path.abspath("file1"), "./file1",
58
        "file2", "dir/..//file2",
59
        "dir/../dir/file3", "dir/file3",
60
    ]
61
    list_of_paths_not_in_list = [
62
        "../file1", "file3"
63
    ]
64
65
    for path in list_of_paths_in_list:
66
        assert tested_module.path_is_present_among_paths(path, list_of_paths)
67
68
    for path in list_of_paths_not_in_list:
69
        assert not tested_module.path_is_present_among_paths(path, list_of_paths)
70