-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_execute_path_collection.py
More file actions
204 lines (152 loc) · 6.85 KB
/
test_execute_path_collection.py
File metadata and controls
204 lines (152 loc) · 6.85 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
"""
Tests for execution output path collection and deduplication.
This module tests the path collection logic that prevents duplicate
file paths in execution results.
"""
import sys
from pathlib import Path
import pytest
# Add src to path
sys.path.insert(0, str(Path(__file__).parent.parent))
try:
from execute.processor import (
_normalize_and_deduplicate_paths,
collect_execution_outputs,
)
PATH_COLLECTION_AVAILABLE = True
except ImportError as e:
PATH_COLLECTION_AVAILABLE = False
IMPORT_ERROR = str(e)
@pytest.mark.skipif(not PATH_COLLECTION_AVAILABLE, reason="Path collection not available")
class TestPathDeduplication:
"""Test path normalization and deduplication."""
def test_normalize_and_deduplicate_paths_empty(self) -> None:
"""Test deduplication with empty list."""
import logging
logger = logging.getLogger(__name__)
result = _normalize_and_deduplicate_paths([], logger)
assert result == []
def test_normalize_and_deduplicate_paths_single(self, tmp_path: Path) -> None:
"""Test deduplication with single file."""
import logging
logger = logging.getLogger(__name__)
test_file = tmp_path / "test.png"
test_file.write_text("test")
result = _normalize_and_deduplicate_paths([test_file], logger)
assert len(result) == 1
assert result[0] == test_file
def test_normalize_and_deduplicate_paths_duplicates(self, tmp_path: Path) -> None:
"""Test deduplication removes duplicate paths."""
import logging
logger = logging.getLogger(__name__)
test_file = tmp_path / "test.png"
test_file.write_text("test")
# Same file multiple times
result = _normalize_and_deduplicate_paths([test_file, test_file, test_file], logger)
assert len(result) == 1
assert result[0] == test_file
def test_normalize_and_deduplicate_paths_nested(self, tmp_path: Path) -> None:
"""Test deduplication removes nested duplicates."""
import logging
logger = logging.getLogger(__name__)
# Create nested structure
parent_dir = tmp_path / "visualizations"
parent_dir.mkdir()
nested_dir = parent_dir / "visualizations"
nested_dir.mkdir()
parent_file = parent_dir / "plot.png"
nested_file = nested_dir / "plot.png"
parent_file.write_text("parent")
nested_file.write_text("nested")
# Should prefer parent over nested
result = _normalize_and_deduplicate_paths([parent_file, nested_file], logger)
# Should keep at least one, prefer parent
assert len(result) >= 1
# Parent should be in result (processed first due to sorting)
assert parent_file in result
@pytest.mark.skipif(not PATH_COLLECTION_AVAILABLE, reason="Path collection not available")
class TestPathCollection:
"""Test execution output collection."""
def test_collect_execution_outputs_empty(self, tmp_path: Path) -> None:
"""Test collection with no files."""
import logging
logger = logging.getLogger(__name__)
script_path = tmp_path / "script.py"
script_path.write_text("# test script")
output_dir = tmp_path / "output"
output_dir.mkdir()
result = collect_execution_outputs(script_path, output_dir, "jax", logger)
assert isinstance(result, dict)
assert "visualizations" in result
assert "simulation_data" in result
assert "traces" in result
assert "other" in result
assert len(result["visualizations"]) == 0
def test_collect_execution_outputs_structure(self, tmp_path: Path) -> None:
"""Test that collection returns expected structure."""
import logging
logger = logging.getLogger(__name__)
script_path = tmp_path / "script.py"
script_path.write_text("# test script")
output_dir = tmp_path / "output"
output_dir.mkdir()
result = collect_execution_outputs(script_path, output_dir, "discopy", logger)
assert isinstance(result, dict)
assert all(isinstance(result[key], list) for key in result.keys())
@pytest.mark.skipif(not PATH_COLLECTION_AVAILABLE, reason="Path collection not available")
class TestActiveInferencePathCollection:
"""Test ActiveInference.jl specific path collection."""
def test_activeinference_jl_path_collection(self, tmp_path: Path) -> None:
"""Test ActiveInference.jl path collection avoids nested directories."""
import logging
logger = logging.getLogger(__name__)
# Create ActiveInference.jl output structure
script_dir = tmp_path
output_dir_name = "activeinference_outputs_2026-01-07_09-12-22"
output_dir = script_dir / output_dir_name
output_dir.mkdir()
# Create visualizations directory
viz_dir = output_dir / "visualizations"
viz_dir.mkdir()
# Create nested visualizations directory (should be avoided)
nested_viz_dir = viz_dir / "visualizations"
nested_viz_dir.mkdir()
# Create files
main_file = viz_dir / "plot.png"
nested_file = nested_viz_dir / "plot.png"
main_file.write_bytes(b"main")
nested_file.write_bytes(b"nested")
script_path = script_dir / "script.jl"
script_path.write_text("# test")
output_collection_dir = tmp_path / "collection"
output_collection_dir.mkdir()
result = collect_execution_outputs(script_path, output_collection_dir, "activeinference_jl", logger)
# Should collect files but avoid nested duplicates
assert isinstance(result, dict)
# Should have collected at least the main file
assert len(result["visualizations"]) >= 0 # May be 0 if pattern doesn't match
@pytest.mark.skipif(not PATH_COLLECTION_AVAILABLE, reason="Path collection not available")
class TestPathCollectionDeduplication:
"""Test that path collection properly deduplicates files."""
def test_no_duplicate_copies(self, tmp_path: Path) -> None:
"""Test that same file isn't copied multiple times."""
import logging
logger = logging.getLogger(__name__)
# Create source structure
source_dir = tmp_path / "source"
source_dir.mkdir()
test_file = source_dir / "data.json"
test_file.write_text('{"test": "data"}')
# Create script
script_path = source_dir / "script.py"
script_path.write_text("# test")
# Create destination
dest_dir = tmp_path / "dest"
dest_dir.mkdir()
# Collect (will search recursively)
collect_execution_outputs(script_path, dest_dir, "jax", logger)
# Check that file was copied at most once
copied_files = list(dest_dir.rglob("data.json"))
# Should have at most one copy
assert len(copied_files) <= 1