-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathtest_path_tools.py
More file actions
176 lines (127 loc) · 5.9 KB
/
Copy pathtest_path_tools.py
File metadata and controls
176 lines (127 loc) · 5.9 KB
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
176
from pathlib import Path
import pytest
from kaleido._utils import path_tools
def test_next_filename_no_existing_files(tmp_path):
"""Test _next_filename when no files exist."""
result = path_tools._next_filename(tmp_path, "test", "png") # noqa: SLF001
assert result == "test.png"
def test_next_filename_base_file_exists(tmp_path):
"""Test _next_filename when base file exists."""
# Create the base file
(tmp_path / "test.png").touch()
result = path_tools._next_filename(tmp_path, "test", "png") # noqa: SLF001
assert result == "test-2.png"
def test_next_filename_numbered_files_exist(tmp_path):
"""Test _next_filename when numbered files exist."""
# Create various numbered files
(tmp_path / "test.png").touch()
(tmp_path / "test-2.png").touch()
(tmp_path / "test-3.png").touch()
(tmp_path / "test-5.png").touch() # Gap in numbering
result = path_tools._next_filename(tmp_path, "test", "png") # noqa: SLF001
assert result == "test-6.png" # Should be max + 1
def test_next_filename_similar_names_ignored(tmp_path):
"""Test _next_filename ignores files with similar but different names."""
# Create files that shouldn't match the pattern
(tmp_path / "test.png").touch()
(tmp_path / "test-2.png").touch()
(tmp_path / "testing-3.png").touch() # Different prefix
(tmp_path / "test-2.jpg").touch() # Different extension
(tmp_path / "test-abc.png").touch() # Non-numeric suffix
result = path_tools._next_filename(tmp_path, "test", "png") # noqa: SLF001
assert result == "test-3.png" # Should only count test.png and test-2.png
def test_next_filename_special_characters(tmp_path):
"""Test _next_filename with special characters in prefix and extension."""
prefix = "test-f$ile_name"
ext = "s$v&g" # set up to be parameterized but not
# Create some files
(tmp_path / f"{prefix}.{ext}").touch()
(tmp_path / f"{prefix}-2.{ext}").touch()
result = path_tools._next_filename(tmp_path, prefix, ext) # noqa: SLF001
assert result == f"{prefix}-3.{ext}"
def test_next_filename_only_numbered_files(tmp_path):
"""Test _next_filename when only numbered files exist (no base file)."""
# Create only numbered files, no base file
(tmp_path / "test-2.png").touch()
(tmp_path / "test-3.png").touch()
(tmp_path / "test-10.png").touch()
result = path_tools._next_filename(tmp_path, "test", "png") # noqa: SLF001
assert result == "test-11.png" # Should be max + 1
def test_next_filename_caps_long_prefix_before_filesystem_lookup(tmp_path):
"""Test _next_filename avoids probing overlong generated filenames."""
prefix = "a" * 500
result = path_tools._next_filename(tmp_path, prefix, "png") # noqa: SLF001
assert result == f"{'a' * 80}.png"
# Fixtures for determine_path tests - testing various title scenarios
@pytest.fixture(
params=[
(
{
"layout": {
"title": {"text": "My-Test!@#$%^&()Chart_with[lots]of{symbols}"},
},
},
"My_TestChart_withlotsofsymbols",
), # Complex title
(
{"layout": {"title": {"text": "Simple Title"}}},
"Simple_Title",
), # Simple title
({"layout": {}}, "fig"), # No title
],
)
def fig_fixture(request):
"""Parameterized fixture for fig with various title scenarios."""
return request.param
def test_determine_path_no_path_input(fig_fixture):
"""Test determine_path with no path input uses current path."""
fig_dict, expected_prefix = fig_fixture
result = path_tools.determine_path(None, fig_dict, "ext")
# Should use current directory
assert result.parent.resolve() == Path().cwd().resolve()
assert result.parent.is_dir()
assert result.name == f"{expected_prefix}.ext"
def test_determine_path_no_suffix_directory(tmp_path, fig_fixture):
"""Test determine_path with path to directory having no suffix."""
fig_dict, expected_prefix = fig_fixture
# Test directory no suffix
test_dir = tmp_path
result = path_tools.determine_path(test_dir, fig_dict, "ext")
# Should use provided directory
assert result.parent == test_dir
assert result.name == f"{expected_prefix}.ext"
# Test error
nonexistent_dir = Path("/nonexistent/directory")
with pytest.raises(ValueError, match=r"Directory .* not found. Please create it."):
path_tools.determine_path(nonexistent_dir, fig_dict, "ext")
def test_determine_path_directory_with_suffix(tmp_path, fig_fixture):
"""Test determine_path with path that is directory even with suffix."""
fig_dict, expected_prefix = fig_fixture
# Create a directory with a suffix-like name
dir_with_suffix = tmp_path / "mydir.png"
dir_with_suffix.mkdir()
result = path_tools.determine_path(dir_with_suffix, fig_dict, "ext")
# Should treat as directory
assert result.parent == dir_with_suffix
assert result.name == f"{expected_prefix}.ext"
def test_determine_path_caps_long_title_prefix(tmp_path):
"""Test determine_path keeps generated filenames within path length limits."""
fig_dict = {"layout": {"title": {"text": "a" * 500}}}
result = path_tools.determine_path(tmp_path, fig_dict, "png")
assert result.parent == tmp_path
assert result.name == f"{'a' * 80}.png"
def test_determine_path_file_with_suffix(tmp_path, fig_fixture):
"""Test determine_path with file path having suffix."""
fig_dict, _expected_prefix = fig_fixture
# Exists
file_path = tmp_path / "output.png"
result = path_tools.determine_path(file_path, fig_dict, "ext")
# Should return the exact path provided
assert result == file_path
# Doesn't exist
file_path = Path("/nonexistent/directory/output.png")
with pytest.raises(
RuntimeError,
match=r"Cannot reach path .* Are all directories created?",
):
path_tools.determine_path(file_path, fig_dict, "ext")