Skip to content

Commit b79f1d9

Browse files
authored
Merge pull request #49 from forcedotcom/jo_file_handle
use environment variable to find a possible files directory
2 parents 2c00d4a + 0a24d98 commit b79f1d9

File tree

2 files changed

+94
-12
lines changed

2 files changed

+94
-12
lines changed

src/datacustomcode/file/path/default.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class DefaultFindFilePath(BaseDataAccessLayer):
3737
"""
3838

3939
# Default configuration values
40+
DEFAULT_ENV_VAR = "LIBRARY_PATH"
4041
DEFAULT_CODE_PACKAGE = "payload"
4142
DEFAULT_FILE_FOLDER = "files"
4243
DEFAULT_CONFIG_FILE = "config.json"
@@ -91,6 +92,13 @@ def _resolve_file_path(self, file_name: str) -> Path:
9192
Returns:
9293
The full path to the file
9394
"""
95+
# First check if environment variable is set
96+
env_path = os.getenv(self.DEFAULT_ENV_VAR)
97+
if env_path:
98+
file_path = Path(env_path) / file_name
99+
if file_path.exists():
100+
return file_path
101+
94102
# First try the default code package location
95103
if self._code_package_exists():
96104
file_path = self._get_code_package_file_path(file_name)

tests/file/test_path_default.py

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515
from __future__ import annotations
1616

17+
import os
1718
from pathlib import Path
1819
import tempfile
1920
from unittest.mock import MagicMock, patch
@@ -89,23 +90,96 @@ def test_find_file_path_success(self):
8990
assert result == mock_path
9091
mock_resolve.assert_called_once_with("test.txt")
9192

92-
def test_resolve_file_path_code_package_exists(self):
93-
"""Test _resolve_file_path when code package exists and file is found."""
93+
def test_resolve_file_path_env_var_set_file_exists(self):
94+
"""Test _resolve_file_path when environment variable is set and file exists."""
9495
finder = DefaultFindFilePath()
9596

96-
with patch.object(
97-
finder, "_code_package_exists", return_value=True
98-
) as mock_exists:
99-
with patch.object(finder, "_get_code_package_file_path") as mock_get_path:
100-
mock_path = MagicMock()
101-
mock_path.exists.return_value = True
102-
mock_get_path.return_value = mock_path
97+
with tempfile.TemporaryDirectory() as temp_dir:
98+
test_file = Path(temp_dir) / "test.txt"
99+
test_file.write_text("test content")
103100

101+
with patch.dict(os.environ, {finder.DEFAULT_ENV_VAR: str(temp_dir)}):
104102
result = finder._resolve_file_path("test.txt")
105103

106-
assert result == mock_path
107-
mock_exists.assert_called_once()
108-
mock_get_path.assert_called_once_with("test.txt")
104+
assert result == test_file
105+
assert result.exists()
106+
107+
def test_resolve_file_path_env_var_set_file_not_found(self):
108+
"""Test _resolve_file_path when environment variable is set but file not found,
109+
falls back to code package."""
110+
finder = DefaultFindFilePath()
111+
112+
with tempfile.TemporaryDirectory() as temp_dir:
113+
# Set env var to a directory that doesn't contain the file
114+
with patch.dict(os.environ, {finder.DEFAULT_ENV_VAR: str(temp_dir)}):
115+
with patch.object(
116+
finder, "_code_package_exists", return_value=True
117+
) as mock_exists:
118+
with patch.object(
119+
finder, "_get_code_package_file_path"
120+
) as mock_get_path:
121+
mock_path = MagicMock()
122+
mock_path.exists.return_value = True
123+
mock_get_path.return_value = mock_path
124+
125+
result = finder._resolve_file_path("test.txt")
126+
127+
assert result == mock_path
128+
mock_exists.assert_called_once()
129+
mock_get_path.assert_called_once_with("test.txt")
130+
131+
def test_resolve_file_path_env_var_not_set(self):
132+
"""Test _resolve_file_path when environment variable is not set,
133+
uses normal flow."""
134+
finder = DefaultFindFilePath()
135+
136+
# Ensure env var is not set
137+
env_backup = os.environ.pop(finder.DEFAULT_ENV_VAR, None)
138+
try:
139+
with patch.object(
140+
finder, "_code_package_exists", return_value=True
141+
) as mock_exists:
142+
with patch.object(
143+
finder, "_get_code_package_file_path"
144+
) as mock_get_path:
145+
mock_path = MagicMock()
146+
mock_path.exists.return_value = True
147+
mock_get_path.return_value = mock_path
148+
149+
result = finder._resolve_file_path("test.txt")
150+
151+
assert result == mock_path
152+
mock_exists.assert_called_once()
153+
mock_get_path.assert_called_once_with("test.txt")
154+
finally:
155+
if env_backup is not None:
156+
os.environ[finder.DEFAULT_ENV_VAR] = env_backup
157+
158+
def test_resolve_file_path_code_package_exists(self):
159+
"""Test _resolve_file_path when code package exists and file is found."""
160+
finder = DefaultFindFilePath()
161+
162+
# Ensure env var is not set to test normal flow
163+
env_backup = os.environ.pop(finder.DEFAULT_ENV_VAR, None)
164+
try:
165+
with patch.object(
166+
finder, "_code_package_exists", return_value=True
167+
) as mock_exists:
168+
with patch.object(
169+
finder, "_get_code_package_file_path"
170+
) as mock_get_path:
171+
mock_path = MagicMock()
172+
mock_path.exists.return_value = True
173+
mock_get_path.return_value = mock_path
174+
175+
result = finder._resolve_file_path("test.txt")
176+
177+
assert result == mock_path
178+
mock_exists.assert_called_once()
179+
mock_get_path.assert_called_once_with("test.txt")
180+
finally:
181+
if env_backup is not None:
182+
os.environ[finder.DEFAULT_ENV_VAR] = env_backup
109183

110184
def test_resolve_file_path_code_package_exists_file_not_found(self):
111185
"""Test _resolve_file_path when code package exists but file not found,

0 commit comments

Comments
 (0)