Skip to content

Commit 2a69d6e

Browse files
committed
Refactor package directory handling in SubfolderBuildConfig and tests
This commit updates the SubfolderBuildConfig class to ensure the source directory is treated as a package before creating a temporary package directory. It modifies the logic to copy the __init__.py file from the source directory to the temporary directory. Additionally, tests are adjusted to reference the correct package directory, which may now point to the temporary package directory, ensuring that the directory structure is preserved during builds and verifications.
1 parent 7228558 commit 2a69d6e

5 files changed

Lines changed: 35 additions & 21 deletions

File tree

src/python_package_folder/subfolder_build.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,21 +344,23 @@ def create_temp_pyproject(self) -> Path | None:
344344
if not self.version:
345345
raise ValueError("Version is required for subfolder builds")
346346

347-
# Create temporary package directory with correct import name
348-
self._create_temp_package_directory()
349-
350-
# Determine which directory to use (temp package dir or src_dir)
351-
package_dir = self._temp_package_dir if self._temp_package_dir and self._temp_package_dir.exists() else self.src_dir
352-
353-
# Ensure package_dir is a package (has __init__.py) for hatchling
354-
init_file = package_dir / "__init__.py"
347+
# Ensure src_dir is a package (has __init__.py) before creating temp directory
348+
# This way the __init__.py will be copied to the temp directory
349+
init_file = self.src_dir / "__init__.py"
355350
if not init_file.exists():
356351
# Create a temporary __init__.py to make it a package
357352
init_file.write_text("# Temporary __init__.py for build\n", encoding="utf-8")
358353
self._temp_init_created = True
359354
else:
360355
self._temp_init_created = False
361356

357+
# Create temporary package directory with correct import name
358+
# This will copy the __init__.py we just created (if any)
359+
self._create_temp_package_directory()
360+
361+
# Determine which directory to use (temp package dir or src_dir)
362+
package_dir = self._temp_package_dir if self._temp_package_dir and self._temp_package_dir.exists() else self.src_dir
363+
362364
# Check if pyproject.toml exists in subfolder
363365
subfolder_pyproject = self.src_dir / "pyproject.toml"
364366
if subfolder_pyproject.exists():

tests/test_build_with_external_deps.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,15 @@ def shared_function():
440440

441441
# Verify dependencies were copied
442442
assert len(external_deps) >= 2
443-
assert (subfolder / "_shared").exists()
444-
assert (subfolder / "_globals.py").exists()
443+
# Dependencies are copied to the temp package directory (if it exists) or original subfolder
444+
package_dir = manager.src_dir # This will be temp package dir if it was created
445+
assert (package_dir / "_shared").exists()
446+
assert (package_dir / "_globals.py").exists()
445447

446448
# Verify imports were converted to relative
447-
modified_content = test_file.read_text()
449+
# Read from package_dir which may be temp package directory
450+
test_file_in_package = package_dir / test_file.name
451+
modified_content = test_file_in_package.read_text()
448452
assert "from ._shared.image_utils import save_PIL_image" in modified_content
449453
assert "from ._shared.file_utils import get_filepaths_config" in modified_content
450454
assert "from ._globals import is_testing" in modified_content
@@ -455,12 +459,15 @@ def shared_function():
455459
assert "from pathlib import Path" in modified_content
456460

457461
# Verify import statement conversion
458-
modified_content2 = test_file2.read_text()
462+
test_file2_in_package = package_dir / test_file2.name
463+
modified_content2 = test_file2_in_package.read_text()
459464
assert "from . import _globals" in modified_content2
460465

461466
# Verify relative imports in copied files were fixed
462-
if (subfolder / "_shared" / "shared_utils.py").exists():
463-
shared_utils_content = (subfolder / "_shared" / "shared_utils.py").read_text()
467+
# Check in the package directory (which may be temp package dir)
468+
shared_utils_path = package_dir / "_shared" / "shared_utils.py"
469+
if shared_utils_path.exists():
470+
shared_utils_content = shared_utils_path.read_text()
464471
# The relative import should be converted to absolute
465472
assert (
466473
"from .file_utils import" not in shared_utils_content
@@ -470,6 +477,7 @@ def shared_function():
470477
# Cleanup should restore original imports
471478
manager.cleanup()
472479

480+
# After cleanup, check original files (not temp package dir)
473481
restored_content = test_file.read_text()
474482
assert restored_content == original_content
475483

tests/test_preserve_directory_structure.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,16 @@ def test_build_manager_preserves_structure(
161161
assert len(data_deps) > 0, "data dependencies should be found"
162162

163163
# Verify models structure was copied with full path
164-
models_path = src_dir / "models" / "Information_extraction" / "_shared_ie" / "ie_enums.py"
164+
# Use manager.src_dir which may point to temp package directory
165+
package_dir = manager.src_dir
166+
models_path = package_dir / "models" / "Information_extraction" / "_shared_ie" / "ie_enums.py"
165167
assert models_path.exists(), (
166168
"models/Information_extraction/_shared_ie/ie_enums.py should be copied with full structure"
167169
)
168170

169171
# Verify data structure was copied with full path
170172
data_path = (
171-
src_dir / "data" / "spreadsheet_creation" / "spreadsheet_formatting_dataclasses.py"
173+
package_dir / "data" / "spreadsheet_creation" / "spreadsheet_formatting_dataclasses.py"
172174
)
173175
assert data_path.exists(), (
174176
"data/spreadsheet_creation/spreadsheet_formatting_dataclasses.py should be copied with full structure"

tests/test_spreadsheet_creation_imports.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ def test_spreadsheet_modules_copied_not_added_as_dependencies(
139139

140140
# Verify spreadsheet_creation directory was copied with full structure preserved
141141
# Import is "data.spreadsheet_creation.spreadsheet_formatting_dataclasses", so structure should be preserved
142-
copied_dir = src_dir / "data" / "spreadsheet_creation"
142+
# Use manager.src_dir which may point to temp package directory
143+
package_dir = manager.src_dir
144+
copied_dir = package_dir / "data" / "spreadsheet_creation"
143145
assert copied_dir.exists(), (
144146
f"spreadsheet_creation directory should be copied with structure at {copied_dir}"
145147
)

tests/test_subfolder_build.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,8 @@ def test_temporary_pyproject_has_correct_structure(
683683
assert "[tool.hatch.version]" not in content
684684
assert "[tool.uv-dynamic-versioning]" not in content
685685

686-
# Verify packages path is set correctly
687-
assert 'packages = ["subfolder"]' in content or '"subfolder"' in content
686+
# Verify packages path is set correctly (should use temp package directory)
687+
assert '.temp_package_my_custom_package' in content
688688

689689
# Verify backup was created
690690
assert (project_root / "pyproject.toml.original").exists()
@@ -734,8 +734,8 @@ def test_file_exclusion_patterns_added(self, test_project_with_pyproject: Path)
734734
# Verify only-include is present
735735
assert "only-include = [" in content
736736

737-
# Verify the subfolder is included
738-
assert '"subfolder"' in content
737+
# Verify the temp package directory is included (not the original subfolder)
738+
assert '.temp_package_test_package' in content
739739

740740
# Verify necessary files are included
741741
assert '"pyproject.toml"' in content

0 commit comments

Comments
 (0)