File size: 3,964 Bytes
b386992
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) 2025, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import tarfile
import tempfile
from pathlib import Path

import pytest

from nemo.export.tarutils import TarPath


@pytest.fixture
def sample_tar():
    # Create a temporary directory and tar file with sample content
    with tempfile.TemporaryDirectory() as temp_dir:
        # Create some test files
        test_dir = Path(temp_dir) / "test_dir"
        test_dir.mkdir()

        (test_dir / "file1.txt").write_text("content1")
        (test_dir / "file2.txt").write_text("content2")
        (test_dir / "subdir").mkdir()
        (test_dir / "subdir" / "file3.txt").write_text("content3")

        # Create tar file
        tar_path = Path(temp_dir) / "test.tar"
        with tarfile.open(tar_path, "w") as tar:
            tar.add(test_dir, arcname=".")

        yield str(tar_path)


def test_tar_path_initialization(sample_tar):
    # Test initialization with string path
    with TarPath(sample_tar) as path:
        assert isinstance(path, TarPath)
        assert path.exists()

    # Test initialization with tarfile object
    with tarfile.open(sample_tar, "r") as tar:
        path = TarPath(tar)
        assert isinstance(path, TarPath)
        assert path.exists()


def test_path_operations(sample_tar):
    with TarPath(sample_tar) as root:
        # Test path division
        file_path = root / "file1.txt"
        assert str(file_path) == f"{sample_tar}/file1.txt"

        # Test nested path division
        subdir_path = root / "subdir" / "file3.txt"
        assert str(subdir_path) == f"{sample_tar}/subdir/file3.txt"

        # Test name property
        assert file_path.name == "file1.txt"
        assert subdir_path.name == "file3.txt"

        # Test suffix property
        assert file_path.suffix == ".txt"
        assert (root / "subdir").suffix == ""


def test_file_operations(sample_tar):
    with TarPath(sample_tar) as root:
        # Test file existence
        assert (root / "file1.txt").exists()
        assert (root / "file1.txt").is_file()

        # Test directory existence
        assert (root / "subdir").exists()
        assert (root / "subdir").is_dir()

        # Test non-existent path
        assert not (root / "nonexistent.txt").exists()

        # Test file reading
        with (root / "file1.txt").open("r") as f:
            content = f.read()
            assert content == b"content1"


def test_directory_operations(sample_tar):
    with TarPath(sample_tar) as root:
        # Test iterdir
        entries = list(root.iterdir())
        assert len(entries) == 5  # file1.txt, file2.txt, subdir, ., file3.txt

        # Test glob
        txt_files = list(root.glob("*.txt"))
        assert len(txt_files) == 3
        assert all(f.suffix == ".txt" for f in txt_files)

        # Test rglob
        all_txt_files = list(root.rglob("*.txt"))
        assert len(all_txt_files) == 3
        assert all(f.suffix == ".txt" for f in all_txt_files)


def test_error_handling(sample_tar):
    with TarPath(sample_tar) as root:
        # Test opening non-existent file
        with pytest.raises(FileNotFoundError):
            (root / "nonexistent.txt").open("r")

        # Test invalid mode
        with pytest.raises(NotImplementedError):
            (root / "file1.txt").open("w")

        # Test invalid initialization
        with pytest.raises(ValueError):
            TarPath(123)  # Invalid type