|
| 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