Skip to content

Commit 4bf12d7

Browse files
authored
Merge pull request #515 from NVIDIA/am/check-input-files
Verify if input path exist on argparse level
2 parents 0e2d114 + 2eda80e commit 4bf12d7

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

src/cloudai/cli/cli.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
)
2727

2828

29+
def existing_path(filepath: str) -> Path:
30+
fpath = Path(filepath)
31+
if not fpath.exists():
32+
raise argparse.ArgumentTypeError(f"Path '{fpath}' does not exist.")
33+
return fpath
34+
35+
2936
class CloudAICLI:
3037
"""Command-line argument parser for CloudAI and derivatives."""
3138

@@ -68,18 +75,25 @@ def add_command(
6875
self.handlers[name] = handler
6976
if system_config is not None:
7077
p.add_argument(
71-
"--system-config", help="Path to the system configuration file.", required=system_config, type=Path
78+
"--system-config",
79+
help="Path to the system configuration file.",
80+
required=system_config,
81+
type=existing_path,
7282
)
7383
if tests_dir is not None:
7484
p.add_argument(
75-
"--tests-dir", help="Path to the test configuration directory.", required=tests_dir, type=Path
85+
"--tests-dir", help="Path to the test configuration directory.", required=tests_dir, type=existing_path
7686
)
7787
if test_scenario is not None:
78-
p.add_argument("--test-scenario", help="Path to the test scenario file.", required=test_scenario, type=Path)
88+
p.add_argument(
89+
"--test-scenario", help="Path to the test scenario file.", required=test_scenario, type=existing_path
90+
)
7991
if output_dir is not None:
8092
p.add_argument("--output-dir", help="Path to the output directory.", required=output_dir, type=Path)
8193
if result_dir is not None:
82-
p.add_argument("--result-dir", help="Path to the result directory.", required=result_dir, type=Path)
94+
p.add_argument(
95+
"--result-dir", help="Path to the result directory.", required=result_dir, type=existing_path
96+
)
8397

8498
return p
8599

tests/test_cli.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ def test_add_command_all_required():
139139
[
140140
"test",
141141
"--system-config",
142-
"system_config",
142+
f"{Path(__file__)}",
143143
"--tests-dir",
144-
"tests_dir",
144+
f"{Path.cwd()}",
145145
"--test-scenario",
146-
"test_scenario",
146+
f"{Path(__file__)}",
147147
"--output-dir",
148148
"output_dir",
149149
]
@@ -152,9 +152,9 @@ def test_add_command_all_required():
152152
log_file="debug.log",
153153
log_level="INFO",
154154
mode="test",
155-
system_config=Path("system_config"),
156-
tests_dir=Path("tests_dir"),
157-
test_scenario=Path("test_scenario"),
155+
system_config=Path(__file__),
156+
tests_dir=Path.cwd(),
157+
test_scenario=Path(__file__),
158158
output_dir=Path("output_dir"),
159159
)
160160

@@ -194,18 +194,18 @@ def test_install_uninstall_modes(self, cli: CloudAICLI):
194194
[
195195
mode,
196196
"--system-config",
197-
"system_config",
197+
f"{Path(__file__)}",
198198
"--tests-dir",
199-
"tests_dir",
199+
f"{Path.cwd()}",
200200
]
201201
)
202202

203203
assert args == argparse.Namespace(
204204
log_file="debug.log",
205205
log_level="INFO",
206206
mode=mode,
207-
system_config=Path("system_config"),
208-
tests_dir=Path("tests_dir"),
207+
system_config=Path(__file__),
208+
tests_dir=Path.cwd(),
209209
test_scenario=None,
210210
output_dir=None,
211211
)
@@ -214,12 +214,12 @@ def test_verify_all_configs_mode(self, cli: CloudAICLI):
214214
assert "verify-configs" in cli.handlers
215215
assert cli.handlers["verify-configs"] is handle_verify_all_configs
216216

217-
args = cli.parser.parse_args(["verify-configs", "--tests-dir", "tests_dir", "configs_dir"])
217+
args = cli.parser.parse_args(["verify-configs", "--tests-dir", f"{Path.cwd()}", "configs_dir"])
218218
assert args == argparse.Namespace(
219219
log_file="debug.log",
220220
log_level="INFO",
221221
mode="verify-configs",
222-
tests_dir=Path("tests_dir"),
222+
tests_dir=Path.cwd(),
223223
strict=False,
224224
**{"configs_dir": Path("configs_dir")},
225225
)
@@ -242,23 +242,23 @@ def test_report_generation_mode(self, cli: CloudAICLI):
242242
[
243243
"generate-report",
244244
"--system-config",
245-
"system_config",
245+
f"{Path(__file__)}",
246246
"--tests-dir",
247-
"tests_dir",
247+
f"{Path.cwd()}",
248248
"--test-scenario",
249-
"test_scenario",
249+
f"{Path(__file__)}",
250250
"--result-dir",
251-
"result_dir",
251+
f"{Path.cwd()}",
252252
]
253253
)
254254
assert args == argparse.Namespace(
255255
log_file="debug.log",
256256
log_level="INFO",
257257
mode="generate-report",
258-
test_scenario=Path("test_scenario"),
259-
result_dir=Path("result_dir"),
260-
system_config=Path("system_config"),
261-
tests_dir=Path("tests_dir"),
258+
test_scenario=Path(__file__),
259+
result_dir=Path.cwd(),
260+
system_config=Path(__file__),
261+
tests_dir=Path.cwd(),
262262
)
263263

264264
def test_run_dry_run_modes(self, cli: CloudAICLI):
@@ -270,21 +270,21 @@ def test_run_dry_run_modes(self, cli: CloudAICLI):
270270
[
271271
mode,
272272
"--system-config",
273-
"system_config",
273+
f"{Path(__file__)}",
274274
"--tests-dir",
275-
"tests_dir",
275+
f"{Path.cwd()}",
276276
"--test-scenario",
277-
"test_scenario",
277+
f"{Path(__file__)}",
278278
]
279279
)
280280

281281
assert args == argparse.Namespace(
282282
log_file="debug.log",
283283
log_level="INFO",
284284
mode=mode,
285-
system_config=Path("system_config"),
286-
tests_dir=Path("tests_dir"),
287-
test_scenario=Path("test_scenario"),
285+
system_config=Path(__file__),
286+
tests_dir=Path.cwd(),
287+
test_scenario=Path(__file__),
288288
output_dir=None,
289289
enable_cache_without_check=False,
290290
)
@@ -306,9 +306,9 @@ def test_required_args(
306306
self, mode_and_missing_options: tuple[str, list[str]], cli: CloudAICLI, capsys: pytest.CaptureFixture[str]
307307
):
308308
opts = {
309-
"--system-config": "system_config",
310-
"--tests-dir": "tests_dir",
311-
"--test-scenario": "test_scenario",
309+
"--system-config": f"{Path(__file__)}",
310+
"--tests-dir": f"{Path.cwd()}",
311+
"--test-scenario": f"{Path(__file__)}",
312312
"--output-dir": "output_dir",
313313
}
314314
mode, missing_options = mode_and_missing_options

0 commit comments

Comments
 (0)