Skip to content

Commit 748692a

Browse files
committed
rimport: Use shared add_inputdata_root().
Changes behavior to hide inputdata_root help text.
1 parent 84d6d60 commit 748692a

5 files changed

Lines changed: 51 additions & 52 deletions

File tree

rimport

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,8 @@ def build_parser() -> argparse.ArgumentParser:
6969
),
7070
)
7171

72-
parser.add_argument(
73-
"--inputdata-root",
74-
"-inputdata-root",
75-
"--inputdata",
76-
"-inputdata",
77-
"-i",
78-
dest="inputdata_root",
79-
metavar="inputdata_dir",
80-
default=DEFAULT_INPUTDATA_ROOT,
81-
help=(
82-
"Change the default local top level inputdata directory."
83-
f" Default: '{DEFAULT_INPUTDATA_ROOT}'"
84-
),
85-
)
72+
# Add inputdata_root option flags
73+
shared.add_inputdata_root(parser)
8674

8775
parser.add_argument(
8876
"--check",

tests/conftest.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
"""
2-
Pytest configuration and shared fixtures for relink tests.
2+
Pytest configuration and shared fixtures for all tests.
33
"""
44

55
import os
6+
import tempfile
7+
import shutil
68

79
import pytest
10+
from unittest.mock import patch
811

912

1013
@pytest.fixture(scope="session")
1114
def workspace_root():
1215
"""Return the root directory of the workspace."""
1316
return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
@pytest.fixture(scope="function", name="temp_dirs")
20+
def fixture_temp_dirs():
21+
"""Create temporary source and target directories for testing."""
22+
source_dir = tempfile.mkdtemp(prefix="test_source_")
23+
target_dir = tempfile.mkdtemp(prefix="test_target_")
24+
25+
with patch("relink.DEFAULT_INPUTDATA_ROOT", source_dir):
26+
with patch("relink.DEFAULT_STAGING_ROOT", target_dir):
27+
with patch("shared.DEFAULT_INPUTDATA_ROOT", source_dir):
28+
with patch("shared.DEFAULT_STAGING_ROOT", target_dir):
29+
yield source_dir, target_dir
30+
31+
# Cleanup
32+
shutil.rmtree(source_dir, ignore_errors=True)
33+
shutil.rmtree(target_dir, ignore_errors=True)

tests/relink/conftest.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,8 @@
33
"""
44

55
import os
6-
import tempfile
7-
import shutil
86

97
import pytest
10-
from unittest.mock import patch
11-
12-
13-
@pytest.fixture(scope="function", name="temp_dirs")
14-
def fixture_temp_dirs():
15-
"""Create temporary source and target directories for testing."""
16-
source_dir = tempfile.mkdtemp(prefix="test_source_")
17-
target_dir = tempfile.mkdtemp(prefix="test_target_")
18-
19-
with patch("relink.DEFAULT_INPUTDATA_ROOT", source_dir):
20-
with patch("relink.DEFAULT_STAGING_ROOT", target_dir):
21-
with patch("shared.DEFAULT_INPUTDATA_ROOT", source_dir):
22-
with patch("shared.DEFAULT_STAGING_ROOT", target_dir):
23-
yield source_dir, target_dir
24-
25-
# Cleanup
26-
shutil.rmtree(source_dir, ignore_errors=True)
27-
shutil.rmtree(target_dir, ignore_errors=True)
288

299

3010
@pytest.fixture(name="current_user")

tests/rimport/test_build_parser.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import sys
77
import argparse
8+
from pathlib import Path
89
import importlib.util
910
from importlib.machinery import SourceFileLoader
1011

@@ -49,12 +50,12 @@ def test_list_arguments_accepted(self, list_flag):
4950
assert args.file is None
5051

5152
@pytest.mark.parametrize("inputdata_flag", ["-inputdata", "-i", "--inputdata", "--inputdata-root", "-inputdata-root"])
52-
def test_inputdata_arguments_accepted(self, inputdata_flag):
53+
def test_inputdata_arguments_accepted(self, temp_dirs, inputdata_flag):
5354
"""Test that all inputdata argument flags are accepted."""
55+
inputdata_root, _ = temp_dirs
5456
parser = rimport.build_parser()
55-
inputdata_dir = "/some/dir"
56-
args = parser.parse_args([inputdata_flag, inputdata_dir, "-f", "dummy_file.nc"])
57-
assert args.inputdata_root == inputdata_dir
57+
args = parser.parse_args([inputdata_flag, inputdata_root, "-f", "dummy_file.nc"])
58+
assert args.inputdata_root == inputdata_root
5859

5960
def test_file_and_list_mutually_exclusive(self, capsys):
6061
"""Test that -file and -list cannot be used together."""
@@ -82,7 +83,7 @@ def test_inputdata_default(self):
8283
"""Test that -inputdata has correct default value."""
8384
parser = rimport.build_parser()
8485
args = parser.parse_args(["-file", "test.txt"])
85-
assert args.inputdata_root == rimport.DEFAULT_INPUTDATA_ROOT
86+
assert args.inputdata_root == str(rimport.DEFAULT_INPUTDATA_ROOT)
8687

8788
def test_check_default(self):
8889
"""Test that --check has the correct default value."""
@@ -97,10 +98,12 @@ def test_check_arguments_accepted(self, check_flag):
9798
args = parser.parse_args(["-file", "test.txt", check_flag])
9899
assert args.check is True
99100

100-
def test_inputdata_custom(self):
101+
def test_inputdata_custom(self, temp_dirs):
101102
"""Test that -inputdata can be customized."""
102103
parser = rimport.build_parser()
103-
custom_path = "/custom/path"
104+
inputdata_root, _ = temp_dirs
105+
custom_path = os.path.join(inputdata_root, "custom", "path")
106+
os.makedirs(custom_path)
104107
args = parser.parse_args(["-file", "test.txt", "-inputdata", custom_path])
105108
assert args.inputdata_root == custom_path
106109

@@ -113,19 +116,25 @@ def test_help_flags_show_help(self, help_flag):
113116
# Help should exit with code 0
114117
assert exc_info.value.code == 0
115118

116-
def test_file_with_inputdata(self):
119+
def test_file_with_inputdata(self, temp_dirs):
117120
"""Test combining -file with -inputdata."""
118121
parser = rimport.build_parser()
119-
args = parser.parse_args(["-file", "data.nc", "-inputdata", "/my/data"])
122+
inputdata_root, _ = temp_dirs
123+
custom_path = os.path.join(inputdata_root, "custom", "path2")
124+
os.makedirs(custom_path)
125+
args = parser.parse_args(["-file", "data.nc", "-inputdata", custom_path])
120126
assert args.file == "data.nc"
121-
assert args.inputdata_root == "/my/data"
127+
assert args.inputdata_root == custom_path
122128

123-
def test_list_with_inputdata(self):
129+
def test_list_with_inputdata(self, temp_dirs):
124130
"""Test combining -list with -inputdata."""
125131
parser = rimport.build_parser()
126-
args = parser.parse_args(["-list", "files.txt", "-inputdata", "/my/data"])
132+
inputdata_root, _ = temp_dirs
133+
custom_path = os.path.join(inputdata_root, "custom", "path3")
134+
os.makedirs(custom_path)
135+
args = parser.parse_args(["-list", "files.txt", "-inputdata", custom_path])
127136
assert args.filelist == "files.txt"
128-
assert args.inputdata_root == "/my/data"
137+
assert args.inputdata_root == custom_path
129138

130139
def test_quiet_default(self):
131140
"""Test that quiet defaults to False."""

tests/rimport/test_main.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import importlib.util
1010
from importlib.machinery import SourceFileLoader
1111
from unittest.mock import patch, call
12+
import pytest
1213

1314
# pylint: disable=too-many-arguments,too-many-positional-arguments
1415

@@ -156,14 +157,15 @@ def stage_data_side_effect(src, *_args, **_kwargs):
156157
def test_nonexistent_inputdata_directory(
157158
self, _mock_ensure_running_as, tmp_path, capsys
158159
):
159-
"""Test that main() returns error code 2 for nonexistent inputdata directory."""
160+
"""Test that argument parser rejects nonexistent inputdata directory."""
160161
nonexistent = tmp_path / "nonexistent"
161162

162-
result = rimport.main(["-file", "test.nc", "-inputdata", str(nonexistent)])
163+
with pytest.raises(SystemExit) as exc_info:
164+
rimport.main(["-file", "test.nc", "-inputdata", str(nonexistent)])
163165

164-
assert result == 2
166+
assert exc_info.value.code == 2
165167
captured = capsys.readouterr()
166-
assert "inputdata directory does not exist" in captured.err
168+
assert "does not exist" in captured.err
167169

168170
@patch.object(rimport, "ensure_running_as")
169171
def test_nonexistent_filelist(self, _mock_ensure_running_as, tmp_path, capsys):

0 commit comments

Comments
 (0)