Skip to content

Commit e30784f

Browse files
committed
Update tests to use tmp_path to avoid writing over actual files
Also update test functionality
1 parent fb6651d commit e30784f

3 files changed

Lines changed: 46 additions & 40 deletions

File tree

tests/conftest.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import shutil
12
from pathlib import Path
23
from unittest.mock import MagicMock, Mock, patch
34

@@ -118,15 +119,18 @@ def components(builder_with_test_files: Builder):
118119

119120

120121
@pytest.fixture
121-
def json_map_generator():
122+
def json_map_generator(tmp_path):
122123
return JsonMapGenerator(
123-
Path(__file__).parent.joinpath(Path("t01-services/synoptic/index.bob"))
124+
Path(__file__).parent.joinpath(Path("t01-services/synoptic/index.bob")),
125+
output=tmp_path,
124126
)
125127

126128

127129
@pytest.fixture
128-
def status_gen():
129-
return GenerateStatusPvs(Path("tests/t01-services/synoptic/techui.yaml").absolute())
130+
def status_gen(tmp_path):
131+
return GenerateStatusPvs(
132+
Path("tests/t01-services/synoptic/techui.yaml").absolute(), output=tmp_path
133+
)
130134

131135

132136
@pytest.fixture
@@ -145,12 +149,15 @@ def example_json_map_root():
145149

146150

147151
@pytest.fixture
148-
def json_map_generator_with_test_files():
152+
def json_map_generator_with_test_files(tmp_path):
153+
# Copy test files to tmp_path so they are visible to the tests
154+
shutil.copytree(Path("tests/test_files/"), tmp_path / "test_files")
155+
shutil.copytree(Path(__file__).parent / "t01-services", tmp_path / "t01-services")
156+
149157
return JsonMapGenerator(
150-
bob_path=Path("tests/test_files/test_bob.bob").absolute(),
151-
techui=Path(__file__).parent.joinpath(
152-
Path("t01-services/synoptic/techui.yaml")
153-
),
158+
bob_path=tmp_path / "test_files/test_bob.bob",
159+
techui=tmp_path / "t01-services/synoptic/techui.yaml",
160+
output=tmp_path / "test_files",
154161
)
155162

156163

tests/test_files/test_bob_embedded.bob

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ $(pv_value)
2121
$(actions)</tooltip>
2222
</widget>
2323
<widget type="embedded" version="2.0.0">
24-
<name>Embedded Display</name>
24+
<name>Z</name>
25+
<width>220</width>
26+
<height>120</height>
2527
<file>motor_embed.bob</file>
28+
<macros>
29+
<P>BL01T-MO-MOTOR-01</P>
30+
<M>:EMBED</M>
31+
<label>EMBED</label>
32+
</macros>
2633
<x>150</x>
2734
<y>120</y>
28-
<width>160</width>
29-
<height>80</height>
3035
</widget>
3136
</display>

tests/test_generate_jsonmap.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ def test_write_json_map_no_synoptic(json_map_generator):
3131
json_map_generator.write_json_map()
3232

3333

34-
def test_app():
35-
result = runner.invoke(app, ["tests/t01-services/synoptic/techui.yaml"])
36-
if Path.exists(Path("tests/t01-services/synoptic/JsonMap.json")):
37-
os.remove("tests/t01-services/synoptic/JsonMap.json")
34+
def test_app(tmp_path):
35+
result = runner.invoke(
36+
app, ["tests/t01-services/synoptic/index.bob", "--output", tmp_path]
37+
)
38+
if Path.exists(Path(tmp_path / "JsonMap.json")):
39+
os.remove(tmp_path / "JsonMap.json")
3840
assert result.exit_code == 0
3941

4042

@@ -48,18 +50,14 @@ def test_json_map_generator_techui_exception(
4850
assert "No such file or directory" in str(excinfo.value)
4951

5052

51-
def test_write_json_map(json_map_generator):
53+
def test_write_json_map(json_map_generator, tmp_path):
5254
test_map = JsonMap(
5355
str(Path(__file__).parent.joinpath("test_files/test_bob.bob")), None
5456
)
5557

5658
# We don't want cover _generate_json_map in this test
5759
json_map_generator.generate_json_map = Mock(return_value=test_map)
5860

59-
# Make sure opis/ dir exists
60-
if not Path.exists(json_map_generator._write_directory):
61-
os.mkdir(json_map_generator._write_directory)
62-
6361
# We don't want to access the _serialise_json_map function in this test
6462
with patch(
6563
"techui_builder.generate_jsonmap._serialise_json_map"
@@ -71,20 +69,16 @@ def test_write_json_map(json_map_generator):
7169
dest_path = json_map_generator._write_directory.joinpath("JsonMap.json")
7270
assert Path.exists(dest_path)
7371

74-
if Path.exists(dest_path):
75-
os.remove(dest_path)
76-
7772

7873
# We don't want to access the _get_action_group function in this test
7974
@patch("techui_builder.generate_jsonmap._get_action_group")
8075
def test_generate_json_map(
8176
mock_get_action_group: MagicMock,
8277
json_map_generator_with_test_files,
8378
example_json_map,
79+
tmp_path,
8480
):
85-
json_map_generator_with_test_files.bob_path = Path(
86-
"tests/test_files/test_bob.bob"
87-
).absolute()
81+
json_map_generator_with_test_files.bob_path = tmp_path / "test_files/test_bob.bob"
8882

8983
mock_xml = objectify.Element("action")
9084
mock_xml["file"] = "test_child_bob.bob"
@@ -105,7 +99,7 @@ def test_generate_json_map(
10599

106100

107101
def test_generate_json_map_embedded_screen(
108-
json_map_generator_with_test_files, example_json_map
102+
json_map_generator_with_test_files, example_json_map, tmp_path
109103
):
110104
list_names = [
111105
"Display",
@@ -121,17 +115,17 @@ def test_generate_json_map_embedded_screen(
121115
side_effect=list_names
122116
)
123117

124-
json_map_generator_with_test_files.bob_path = Path(
125-
"tests/test_files/test_bob_embedded.bob"
126-
).absolute()
118+
json_map_generator_with_test_files.bob_path = (
119+
tmp_path / "test_files/test_bob_embedded.bob"
120+
)
127121

128122
example_json_map.file = "test_bob_embedded.bob"
129123
example_json_map.children.append(
130124
JsonMap(
131125
"$(IOC)/pmacAxis.pvi.bob",
132126
display_name="Embedded Display",
133127
exists=False,
134-
macros={"M": "$(M)", "P": "$(P)"},
128+
macros={"M": ":EMBED", "P": "BL01T-MO-MOTOR-01", "label": "EMBED"},
135129
)
136130
)
137131

@@ -143,7 +137,7 @@ def test_generate_json_map_embedded_screen(
143137

144138

145139
def test_generate_json_map_nav_tabs(
146-
json_map_generator_with_test_files, example_json_map_root
140+
json_map_generator_with_test_files, example_json_map_root, tmp_path
147141
):
148142
json_map_generator_with_test_files._parse_display_name = Mock(
149143
side_effect=["Display", "Tab1", "Tab2"]
@@ -152,9 +146,9 @@ def test_generate_json_map_nav_tabs(
152146
side_effect=["Display", "Tab1", "Tab2"]
153147
)
154148

155-
json_map_generator_with_test_files.bob_path = Path(
156-
"tests/test_files/test_bob_navtabs.bob"
157-
).absolute()
149+
json_map_generator_with_test_files.bob_path = (
150+
tmp_path / "test_files/test_bob_navtabs.bob"
151+
)
158152

159153
example_json_map_root.file = "test_bob_navtabs.bob"
160154
example_json_map_root.children.extend(
@@ -265,11 +259,11 @@ def test_generate_json_map_get_macros(
265259

266260

267261
def test_generate_json_map_xml_parse_error(
268-
json_map_generator_with_test_files,
262+
json_map_generator_with_test_files, tmp_path
269263
):
270-
json_map_generator_with_test_files.bob_path = Path(
271-
"tests/test_files/test_bob_bad.bob"
272-
).absolute()
264+
json_map_generator_with_test_files.bob_path = (
265+
tmp_path / "test_files/test_bob_bad.bob"
266+
)
273267

274268
test_json_map = json_map_generator_with_test_files.generate_json_map(
275269
json_map_generator_with_test_files.bob_path,

0 commit comments

Comments
 (0)