Skip to content

Commit 422af6c

Browse files
Fix combining a base file and params when the base file lacks a trailing newline
1 parent 537702d commit 422af6c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/geophires_x_client/geophires_input_parameters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def as_file_path(self) -> Path:
157157

158158
file_path = Path(tempfile.gettempdir(), f'geophires-input-params_{self._instance_id!s}.txt')
159159

160-
with open(file_path, 'w', encoding='UTF-8') as f:
160+
with open(file_path, 'w+', encoding='UTF-8') as f:
161161
if self.from_file_path:
162162
with open(self.from_file_path, encoding='UTF-8') as base_file:
163163
f.write(base_file.read())

tests/geophires_x_client_tests/test_geophires_input_parameters.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,23 @@ def test_immutability_of_params(self):
9696
with self.assertRaises(TypeError):
9797
# This should fail because MappingProxyType is read-only
9898
p1.params['Reservoir Depth'] = 4
99+
100+
def test_combining_file_and_params_with_no_trailing_newline(self):
101+
"""Verify that combining a base file and params works correctly when the base file lacks a trailing newline."""
102+
# Arrange
103+
base_content = 'base_key,base_value' # Note: no trailing newline
104+
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False, newline='') as tmp_file:
105+
base_file_path = Path(tmp_file.name)
106+
tmp_file.write(base_content)
107+
108+
# Act
109+
params = ImmutableGeophiresInputParameters(from_file_path=base_file_path, params={'new_key': 'new_value'})
110+
combined_file_path = params.as_file_path()
111+
combined_content = combined_file_path.read_text()
112+
113+
# Assert
114+
expected_content = 'base_key,base_value\nnew_key, new_value\n'
115+
self.assertEqual(expected_content, combined_content)
116+
117+
# Clean up the temporary file
118+
base_file_path.unlink()

0 commit comments

Comments
 (0)