Skip to content

Commit dab297d

Browse files
Fix Path constructor, unit test default output file paths
1 parent 22b3d3a commit dab297d

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/geophires_x/Outputs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ def read_parameters(self, model: Model, default_output_path: Path = None) -> Non
716716
if not Path(ParameterReadIn.sValue).is_absolute() and default_output_path is not None:
717717
original_val = ParameterReadIn.sValue
718718
ParameterReadIn.sValue = str(
719-
default_output_path.joinpath(ParameterReadIn.sValue).absolute())
719+
default_output_path.joinpath(Path(ParameterReadIn.sValue)).absolute())
720720
model.logger.info(f'Adjusted {key} path to {ParameterReadIn.sValue} because original value '
721721
f'({original_val}) was not an absolute path.')
722722

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import os
2+
import sys
3+
from pathlib import Path
4+
5+
from geophires_x.Model import Model
6+
from geophires_x_client import GeophiresInputParameters
7+
from tests.base_test_case import BaseTestCase
8+
9+
10+
class OutputsTestCase(BaseTestCase):
11+
12+
def test_relative_output_file_path(self):
13+
input_file = GeophiresInputParameters({'HTML Output File': 'foo.html'}).as_file_path()
14+
m = self._new_model(input_file=input_file, original_cwd=Path('/tmp/')) # noqa: S108
15+
html_filepath = Path(m.outputs.html_output_file.value)
16+
self.assertTrue(html_filepath.is_absolute())
17+
self.assertEqual(str(html_filepath), '/tmp/foo.html') # noqa: S108
18+
19+
def test_absolute_output_file_path(self):
20+
input_file = GeophiresInputParameters(
21+
{'HTML Output File': '/home/user/my-geophires-project/foo.html'}
22+
).as_file_path()
23+
m = self._new_model(input_file=input_file, original_cwd=Path('/tmp/')) # noqa: S108
24+
html_filepath = Path(m.outputs.html_output_file.value)
25+
self.assertTrue(html_filepath.is_absolute())
26+
self.assertEqual(str(html_filepath), '/home/user/my-geophires-project/foo.html')
27+
28+
def _new_model(self, input_file=None, original_cwd=None) -> Model:
29+
stash_cwd = Path.cwd()
30+
stash_sys_argv = sys.argv
31+
32+
sys.argv = ['']
33+
34+
if input_file is not None:
35+
sys.argv.append(input_file)
36+
37+
m = Model(enable_geophires_logging_config=False)
38+
39+
if input_file is not None:
40+
m.read_parameters(default_output_path=original_cwd)
41+
42+
sys.argv = stash_sys_argv
43+
os.chdir(stash_cwd)
44+
45+
return m

0 commit comments

Comments
 (0)