Skip to content

Commit

Permalink
fix: DirPackageBuilder clears dist directory before building
Browse files Browse the repository at this point in the history
Fixes #107
  • Loading branch information
tumidi committed Jun 25, 2024
1 parent af68006 commit 82c04ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions questionpy_sdk/package/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def write_package(self) -> None:
Raises:
PackageBuildError: If the package failed to build.
"""
self._prepare()
self._run_build_hooks("pre")
self._install_questionpy()
self._install_requirements()
Expand All @@ -53,6 +54,9 @@ def write_package(self) -> None:
self._copy_source_files()
self._run_build_hooks("post")

def _prepare(self) -> None:
pass

def _run_build_hooks(self, hook_name: BuildHookName) -> None:
commands = self._source.config.build_hooks.get(hook_name, [])

Expand Down Expand Up @@ -182,6 +186,12 @@ def __init__(self, source: PackageSource):
"""
super().__init__(source, copy_sources=False)

def _prepare(self) -> None:
# clear dist dir before building
dist_path = self._source.path / DIST_DIR
if dist_path.is_dir():
shutil.rmtree(dist_path)

def _write_file(self, source_path: Path, dest_path: Path) -> None:
if not source_path.is_dir():
abs_dest_path = self._source.path / dest_path
Expand Down
12 changes: 12 additions & 0 deletions tests/package/test_package_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,15 @@ def test_dir_package_builder(tmp_path: Path, source_path: Path) -> None:
assert (dist_dir / MANIFEST_FILENAME).is_file()
assert (dist_dir / "python" / "local" / "minimal_example" / "__init__.py").is_file()
assert (dist_dir / "dependencies" / "site-packages" / "questionpy" / "__init__.py").is_file()


def test_dir_package_builder_clears_dist(tmp_path: Path, source_path: Path) -> None:
dist_dir = source_path / DIST_DIR
(dist_dir / "static").mkdir(parents=True)
some_file_path = dist_dir / "static" / "some_file.txt"
some_file_path.touch()

with DirPackageBuilder(PackageSource(source_path)) as builder:
builder.write_package()

assert not some_file_path.exists()

0 comments on commit 82c04ff

Please sign in to comment.