aboutsummaryrefslogtreecommitdiff
path: root/tests/mlia/test_utils_filesystem.py
blob: 4d8d955f9f3de4f0f94894a540ddab939a409659 (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
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Tests for the filesystem module."""
import contextlib
import json
from pathlib import Path
from unittest.mock import MagicMock

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_profile
from mlia.utils.filesystem import get_profiles_data
from mlia.utils.filesystem import get_profiles_file
from mlia.utils.filesystem import get_supported_profile_names
from mlia.utils.filesystem import get_vela_config
from mlia.utils.filesystem import sha256
from mlia.utils.filesystem import temp_directory
from mlia.utils.filesystem import temp_file


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_profiles_file() -> None:
    """Test profiles file getter."""
    assert get_profiles_file().is_file()
    assert get_profiles_file().name == "profiles.json"


def test_profiles_data() -> None:
    """Test profiles data getter."""
    assert list(get_profiles_data().keys()) == [
        "ethos-u55-256",
        "ethos-u55-128",
        "ethos-u65-512",
    ]


def test_profiles_data_wrong_format(
    monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
    """Test if profile data has wrong format."""
    wrong_profile_data = tmp_path / "bad.json"
    with open(wrong_profile_data, "w", encoding="utf-8") as file:
        json.dump([], file)

    monkeypatch.setattr(
        "mlia.utils.filesystem.get_profiles_file",
        MagicMock(return_value=wrong_profile_data),
    )

    with pytest.raises(Exception, match="Profiles data format is not valid"):
        get_profiles_data()


def test_get_supported_profile_names() -> None:
    """Test profile names getter."""
    assert list(get_supported_profile_names()) == [
        "ethos-u55-256",
        "ethos-u55-128",
        "ethos-u65-512",
    ]


def test_get_profile() -> None:
    """Test getting profile data."""
    assert get_profile("ethos-u55-256") == {
        "target": "ethos-u55",
        "mac": 256,
        "system_config": "Ethos_U55_High_End_Embedded",
        "memory_mode": "Shared_Sram",
    }

    with pytest.raises(Exception, match="Unable to find target profile unknown"):
        get_profile("unknown")


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

            if raise_exception:
                raise Exception("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()