Passed
Pull Request — rhel9-branch (#255)
by
unknown
02:29 queued 01:13
created

test_utils.test_set()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nop 0
1
#
2
# Copyright (C) 2013  Red Hat, Inc.
3
#
4
# This copyrighted material is made available to anyone wishing to use,
5
# modify, copy, or redistribute it subject to the terms and conditions of
6
# the GNU General Public License v.2, or (at your option) any later version.
7
# This program is distributed in the hope that it will be useful, but WITHOUT
8
# ANY WARRANTY expressed or implied, including the implied warranties of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
10
# Public License for more details.  You should have received a copy of the
11
# GNU General Public License along with this program; if not, write to the
12
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
13
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
14
# source code or documentation are not subject to the GNU General Public
15
# License and may only be used or replicated with the express permission of
16
# Red Hat, Inc.
17
#
18
# Red Hat Author(s): Vratislav Podzimek <[email protected]>
19
#
20
21
22
from unittest import mock
23
import os
24
from collections import namedtuple
25
26
import pytest
27
28
from org_fedora_oscap import utils
29
30
31
@pytest.fixture()
32
def mock_os():
33
    mock_os = mock.Mock()
34
    mock_os.makedirs = mock.Mock()
35
    mock_os.path = mock.Mock()
36
    mock_os.path.isdir = mock.Mock()
37
    return mock_os
38
39
40
def mock_utils_os(mock_os, monkeypatch):
41
    utils_module_symbols = utils.__dict__
42
43
    monkeypatch.setitem(utils_module_symbols, "os", mock_os)
44
45
46
def test_existing_dir(mock_os, monkeypatch):
47
    mock_utils_os(mock_os, monkeypatch)
48
    mock_os.path.isdir.return_value = True
49
50
    utils.ensure_dir_exists("/tmp/test_dir")
51
52
    mock_os.path.isdir.assert_called_with("/tmp/test_dir")
53
    assert not mock_os.makedirs.called
54
55
56
def test_nonexisting_dir(mock_os, monkeypatch):
57
    mock_utils_os(mock_os, monkeypatch)
58
    mock_os.path.isdir.return_value = False
59
60
    utils.ensure_dir_exists("/tmp/test_dir")
61
62
    mock_os.path.isdir.assert_called_with("/tmp/test_dir")
63
    mock_os.makedirs.assert_called_with("/tmp/test_dir")
64
65
66
def test_no_dir(mock_os, monkeypatch):
67
    mock_utils_os(mock_os, monkeypatch)
68
    # shouldn't raise an exception
69
    utils.ensure_dir_exists("")
70
71
72
def test_relative_relative():
73
    assert utils.join_paths("foo", "blah") == "foo/blah"
74
75
76
def test_relative_absolute():
77
    assert utils.join_paths("foo", "/blah") == "foo/blah"
78
79
80
def test_absolute_relative():
81
    assert utils.join_paths("/foo", "blah") == "/foo/blah"
82
83
84
def test_absolute_absolute():
85
    assert utils.join_paths("/foo", "/blah") == "/foo/blah"
86
87
88
def test_dict():
89
    dct = {"a": 1, "b": 2}
90
91
    mapped_dct = utils.keep_type_map(str.upper, dct)
92
    assert list(mapped_dct.keys()) == ["A", "B"]
93
    assert isinstance(mapped_dct, dict)
94
95
96
def test_list():
97
    lst = [1, 2, 4, 5]
98
99
    mapped_lst = utils.keep_type_map(lambda x: x ** 2, lst)
100
    assert mapped_lst == [1, 4, 16, 25]
101
    assert isinstance(mapped_lst, list)
102
103
104
def test_tuple():
105
    tpl = (1, 2, 4, 5)
106
107
    mapped_tpl = utils.keep_type_map(lambda x: x ** 2, tpl)
108
    assert mapped_tpl == (1, 4, 16, 25)
109
    assert isinstance(mapped_tpl, tuple)
110
111
112
def test_namedtuple():
113
    NT = namedtuple("TestingNT", ["a", "b"])
114
    ntpl = NT(2, 4)
115
116
    mapped_tpl = utils.keep_type_map(lambda x: x ** 2, ntpl)
117
    assert mapped_tpl == NT(4, 16)
118
    assert isinstance(mapped_tpl, tuple)
119
    assert isinstance(mapped_tpl, NT)
120
121
122
def test_set():
123
    st = {1, 2, 4, 5}
124
125
    mapped_st = utils.keep_type_map(lambda x: x ** 2, st)
126
    assert mapped_st == {1, 4, 16, 25}
127
    assert isinstance(mapped_st, set)
128
129
130
def test_str():
131
    stri = "abcd"
132
133
    mapped_stri = utils.keep_type_map(lambda c: chr((ord(c) + 2) % 256), stri)
134
    assert mapped_stri == "cdef"
135
    assert isinstance(mapped_stri, str)
136
137
138
def test_gen():
139
    generator = (el for el in (1, 2, 4, 5))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable el does not seem to be defined.
Loading history...
140
141
    mapped_generator = utils.keep_type_map(lambda x: x ** 2, generator)
142
    assert tuple(mapped_generator) == (1, 4, 16, 25)
143
144
    # any better test for this?
145
    assert "__next__" in dir(mapped_generator)
146
147
148
def test_hash():
149
    file_hashes = {
150
       'md5':    'ea38136ca349e139c59f09e09d2aa956',
151
       'sha1':   'f905458483be8ac21002ab2c6409d3a10b3813f1',
152
       'sha224': '2b1e795db6b7397f47a270fbb5059e76b94a8c972240b17c45db1f13',
153
       'sha256': '87fcda7d9e7a22412e95779e2f8e70f929106c7b27a94f5f8510553ebf4624a6',
154
       'sha384': 'b3ffdfad2bf33caf6e44a8b34386ad741bb80fb02306d3889b8a5645cde31e9d'
155
                 '31ec44e0b0e6ce84d83a57339b75b9bf',
156
       'sha512': '7b05940e8d69e804a90f5110d22ad3a1cd03adc5bf4d0a4779790c78118b3c61'
157
                 'b7f3a3cd39fcf2902ec92ac80df71b952a7aeb2d53c16f0e77436eeb91e33e1d'
158
    }
159
160
    for hash_id, file_hash in file_hashes.items():
161
        hash_obj = utils.get_hashing_algorithm(file_hash)
162
        assert hash_obj.name == hash_id
163
164
    filepath = os.path.join(os.path.dirname(__file__), 'data', 'file')
165
    computed_hash = utils.get_file_fingerprint(filepath, hash_obj)
0 ignored issues
show
introduced by
The variable hash_obj does not seem to be defined in case the for loop on line 160 is not entered. Are you sure this can never be the case?
Loading history...
166
167
    assert file_hash == computed_hash
0 ignored issues
show
introduced by
The variable file_hash does not seem to be defined in case the for loop on line 160 is not entered. Are you sure this can never be the case?
Loading history...
168
169
170
def test_hash_unknown():
171
    file_hash = 'XXXX'
172
173
    hash_obj = utils.get_hashing_algorithm(file_hash)
174
    assert hash_obj is None
175