Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/datacustomcode/file/path/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class DefaultFindFilePath(BaseDataAccessLayer):
"""

# Default configuration values
DEFAULT_ENV_VAR = "LIBRARY_PATH"
DEFAULT_CODE_PACKAGE = "payload"
DEFAULT_FILE_FOLDER = "files"
DEFAULT_CONFIG_FILE = "config.json"
Expand Down Expand Up @@ -91,6 +92,13 @@ def _resolve_file_path(self, file_name: str) -> Path:
Returns:
The full path to the file
"""
# First check if environment variable is set
env_path = os.getenv(self.DEFAULT_ENV_VAR)
if env_path:
file_path = Path(env_path) / file_name
if file_path.exists():
return file_path

# First try the default code package location
if self._code_package_exists():
file_path = self._get_code_package_file_path(file_name)
Expand Down
98 changes: 86 additions & 12 deletions tests/file/test_path_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
from __future__ import annotations

import os
from pathlib import Path
import tempfile
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -89,23 +90,96 @@ def test_find_file_path_success(self):
assert result == mock_path
mock_resolve.assert_called_once_with("test.txt")

def test_resolve_file_path_code_package_exists(self):
"""Test _resolve_file_path when code package exists and file is found."""
def test_resolve_file_path_env_var_set_file_exists(self):
"""Test _resolve_file_path when environment variable is set and file exists."""
finder = DefaultFindFilePath()

with patch.object(
finder, "_code_package_exists", return_value=True
) as mock_exists:
with patch.object(finder, "_get_code_package_file_path") as mock_get_path:
mock_path = MagicMock()
mock_path.exists.return_value = True
mock_get_path.return_value = mock_path
with tempfile.TemporaryDirectory() as temp_dir:
test_file = Path(temp_dir) / "test.txt"
test_file.write_text("test content")

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

assert result == mock_path
mock_exists.assert_called_once()
mock_get_path.assert_called_once_with("test.txt")
assert result == test_file
assert result.exists()

def test_resolve_file_path_env_var_set_file_not_found(self):
"""Test _resolve_file_path when environment variable is set but file not found,
falls back to code package."""
finder = DefaultFindFilePath()

with tempfile.TemporaryDirectory() as temp_dir:
# Set env var to a directory that doesn't contain the file
with patch.dict(os.environ, {finder.DEFAULT_ENV_VAR: str(temp_dir)}):
with patch.object(
finder, "_code_package_exists", return_value=True
) as mock_exists:
with patch.object(
finder, "_get_code_package_file_path"
) as mock_get_path:
mock_path = MagicMock()
mock_path.exists.return_value = True
mock_get_path.return_value = mock_path

result = finder._resolve_file_path("test.txt")

assert result == mock_path
mock_exists.assert_called_once()
mock_get_path.assert_called_once_with("test.txt")

def test_resolve_file_path_env_var_not_set(self):
"""Test _resolve_file_path when environment variable is not set,
uses normal flow."""
finder = DefaultFindFilePath()

# Ensure env var is not set
env_backup = os.environ.pop(finder.DEFAULT_ENV_VAR, None)
try:
with patch.object(
finder, "_code_package_exists", return_value=True
) as mock_exists:
with patch.object(
finder, "_get_code_package_file_path"
) as mock_get_path:
mock_path = MagicMock()
mock_path.exists.return_value = True
mock_get_path.return_value = mock_path

result = finder._resolve_file_path("test.txt")

assert result == mock_path
mock_exists.assert_called_once()
mock_get_path.assert_called_once_with("test.txt")
finally:
if env_backup is not None:
os.environ[finder.DEFAULT_ENV_VAR] = env_backup

def test_resolve_file_path_code_package_exists(self):
"""Test _resolve_file_path when code package exists and file is found."""
finder = DefaultFindFilePath()

# Ensure env var is not set to test normal flow
env_backup = os.environ.pop(finder.DEFAULT_ENV_VAR, None)
try:
with patch.object(
finder, "_code_package_exists", return_value=True
) as mock_exists:
with patch.object(
finder, "_get_code_package_file_path"
) as mock_get_path:
mock_path = MagicMock()
mock_path.exists.return_value = True
mock_get_path.return_value = mock_path

result = finder._resolve_file_path("test.txt")

assert result == mock_path
mock_exists.assert_called_once()
mock_get_path.assert_called_once_with("test.txt")
finally:
if env_backup is not None:
os.environ[finder.DEFAULT_ENV_VAR] = env_backup

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