Skip to content

Commit

Permalink
Fix Path constructor, unit test default output file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
softwareengineerprogrammer committed Oct 3, 2024
1 parent 22b3d3a commit dab297d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/geophires_x/Outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ def read_parameters(self, model: Model, default_output_path: Path = None) -> Non
if not Path(ParameterReadIn.sValue).is_absolute() and default_output_path is not None:
original_val = ParameterReadIn.sValue
ParameterReadIn.sValue = str(
default_output_path.joinpath(ParameterReadIn.sValue).absolute())
default_output_path.joinpath(Path(ParameterReadIn.sValue)).absolute())
model.logger.info(f'Adjusted {key} path to {ParameterReadIn.sValue} because original value '
f'({original_val}) was not an absolute path.')

Expand Down
45 changes: 45 additions & 0 deletions tests/geophires_x_tests/test_outputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import sys
from pathlib import Path

from geophires_x.Model import Model
from geophires_x_client import GeophiresInputParameters
from tests.base_test_case import BaseTestCase


class OutputsTestCase(BaseTestCase):

def test_relative_output_file_path(self):
input_file = GeophiresInputParameters({'HTML Output File': 'foo.html'}).as_file_path()
m = self._new_model(input_file=input_file, original_cwd=Path('/tmp/')) # noqa: S108
html_filepath = Path(m.outputs.html_output_file.value)
self.assertTrue(html_filepath.is_absolute())
self.assertEqual(str(html_filepath), '/tmp/foo.html') # noqa: S108

def test_absolute_output_file_path(self):
input_file = GeophiresInputParameters(
{'HTML Output File': '/home/user/my-geophires-project/foo.html'}
).as_file_path()
m = self._new_model(input_file=input_file, original_cwd=Path('/tmp/')) # noqa: S108
html_filepath = Path(m.outputs.html_output_file.value)
self.assertTrue(html_filepath.is_absolute())
self.assertEqual(str(html_filepath), '/home/user/my-geophires-project/foo.html')

def _new_model(self, input_file=None, original_cwd=None) -> Model:
stash_cwd = Path.cwd()
stash_sys_argv = sys.argv

sys.argv = ['']

if input_file is not None:
sys.argv.append(input_file)

m = Model(enable_geophires_logging_config=False)

if input_file is not None:
m.read_parameters(default_output_path=original_cwd)

sys.argv = stash_sys_argv
os.chdir(stash_cwd)

return m

0 comments on commit dab297d

Please sign in to comment.