aboutsummaryrefslogtreecommitdiff
path: root/tests/test_utils_filesystem.py
blob: c1c9876ba54274f66b646bdb5281eeec52f76df6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Tests for the filesystem module."""
import contextlib
from pathlib import Path

import pytest

from mlia.utils.filesystem import all_files_exist
from mlia.utils.filesystem import all_paths_valid
from mlia.utils.filesystem import copy_all
from mlia.utils.filesystem import get_mlia_resources
from mlia.utils.filesystem import get_mlia_target_profiles_dir
from mlia.utils.filesystem import get_vela_config
from mlia.utils.filesystem import recreate_directory
from mlia.utils.filesystem import sha256
from mlia.utils.filesystem import temp_directory
from mlia.utils.filesystem import temp_file
from mlia.utils.filesystem import USER_ONLY_PERM_MASK
from mlia.utils.filesystem import working_directory
from tests.utils.common import check_expected_permissions


def test_get_mlia_resources() -> None:
    """Test resources getter."""
    assert get_mlia_resources().is_dir()


def test_get_vela_config() -> None:
    """Test Vela config files getter."""
    assert get_vela_config().is_file()
    assert get_vela_config().name == "vela.ini"


def test_get_mlia_target_profiles() -> None:
    """Test target profiles getter."""
    assert get_mlia_target_profiles_dir().is_dir()


@pytest.mark.parametrize("raise_exception", [True, False])
def test_temp_file(raise_exception: bool) -> None:
    """Test temp_file context manager."""
    with contextlib.suppress(RuntimeError):
        with temp_file() as tmp_path:
            assert tmp_path.is_file()

            if raise_exception:
                raise RuntimeError("Error!")

    assert not tmp_path.exists()


def test_sha256(tmp_path: Path) -> None:
    """Test getting sha256 hash."""
    sample = tmp_path / "sample.txt"

    with open(sample, "w", encoding="utf-8") as file:
        file.write("123")

    expected_hash = "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3"
    assert sha256(sample) == expected_hash


def test_temp_dir_context_manager() -> None:
    """Test context manager for temporary directories."""
    with temp_directory() as tmpdir:
        assert isinstance(tmpdir, Path)
        assert tmpdir.is_dir()

    assert not tmpdir.exists()


def test_all_files_exist(tmp_path: Path) -> None:
    """Test function all_files_exist."""
    sample1 = tmp_path / "sample1.txt"
    sample1.touch()

    sample2 = tmp_path / "sample2.txt"
    sample2.touch()

    sample3 = tmp_path / "sample3.txt"

    assert all_files_exist([sample1, sample2]) is True
    assert all_files_exist([sample1, sample2, sample3]) is False


def test_all_paths_valid(tmp_path: Path) -> None:
    """Test function all_paths_valid."""
    sample = tmp_path / "sample.txt"
    sample.touch()

    sample_dir = tmp_path / "sample_dir"
    sample_dir.mkdir()

    unknown = tmp_path / "unknown.txt"

    assert all_paths_valid([sample, sample_dir]) is True
    assert all_paths_valid([sample, sample_dir, unknown]) is False


def test_copy_all(tmp_path: Path) -> None:
    """Test function copy_all."""
    sample = tmp_path / "sample1.txt"
    sample.touch()

    sample_dir = tmp_path / "sample_dir"
    sample_dir.mkdir()

    sample_nested_file = sample_dir / "sample_nested.txt"
    sample_nested_file.touch()

    dest_dir = tmp_path / "dest"
    copy_all(sample, sample_dir, dest=dest_dir)

    assert (dest_dir / sample.name).is_file()
    assert (dest_dir / sample_nested_file.name).is_file()


@pytest.mark.parametrize(
    "should_exist, create_dir",
    [
        [True, False],
        [False, True],
    ],
)
def test_working_directory_context_manager(
    tmp_path: Path, should_exist: bool, create_dir: bool
) -> None:
    """Test working_directory context manager."""
    prev_wd = Path.cwd()

    working_dir = tmp_path / "work_dir"
    if should_exist:
        working_dir.mkdir()

    with working_directory(working_dir, create_dir=create_dir) as current_working_dir:
        assert current_working_dir.is_dir()
        assert Path.cwd() == current_working_dir

    assert Path.cwd() == prev_wd


def test_recreate_directory(tmp_path: Path) -> None:
    """Test function recreate_directory."""
    sample_dir = tmp_path / "sample"
    sample_dir.mkdir()

    test_dir1 = sample_dir / "test_dir1"
    test_dir1.mkdir()

    test_file1 = test_dir1 / "sample_file1.txt"
    test_file1.touch()

    test_dir2 = sample_dir / "test_dir2"
    recreate_directory(test_dir2)

    assert test_dir1.is_dir()
    assert test_file1.is_file()
    assert test_dir2.is_dir()
    check_expected_permissions(test_dir2, USER_ONLY_PERM_MASK)

    recreate_directory(test_dir1)
    assert test_dir2.is_dir()
    assert not test_file1.exists()
    assert test_dir1.is_dir()
    check_expected_permissions(test_dir1, USER_ONLY_PERM_MASK)


def test_recreate_directory_wrong_path(tmp_path: Path) -> None:
    """Test that function should fail if provided path is not a directory."""
    sample_file = tmp_path / "sample_file.txt"
    sample_file.touch()

    with pytest.raises(ValueError, match=rf"Path {sample_file} is not a directory."):
        recreate_directory(sample_file)