Skip to content

Phase 3: Add comprehensive tests for gitignore functionality - #295

Open
GargiGupta-io wants to merge 3 commits into
keras-team:mainfrom
GargiGupta-io:phase-3-add-tests
Open

Phase 3: Add comprehensive tests for gitignore functionality#295
GargiGupta-io wants to merge 3 commits into
keras-team:mainfrom
GargiGupta-io:phase-3-add-tests

Conversation

@GargiGupta-io

Copy link
Copy Markdown

Adds tests for git ls-files integration:

  • test_list_git_files_respects_gitignore: Verifies .gitignore is respected
  • test_write_git_files_excludes_paths: Verifies exclude_paths work correctly

Part of #288 (Phase 3 of 4). Merge after #294.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a git-based file packaging mechanism to kinetic/utils/packager.py to package only tracked and non-ignored files when zipping the working directory, falling back to os.walk if git is not available. It also adds corresponding unit tests. The review feedback highlights several robustness improvements: explicitly excluding __pycache__ and .git directories in the git-based path to match the fallback behavior, verifying that a directory is a git repository before running git ls-files recursively, and gracefully skipping the new git-dependent tests if the git command is not available in the test environment.

Comment thread kinetic/utils/packager.py
Comment on lines +66 to +67
for relative_path in git_files:
file_path = os.path.join(base_dir, relative_path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The zip_working_dir function is documented to always exclude __pycache__ and .git directories. However, when zipping via the git-based path (_write_git_files), untracked __pycache__ files or .git files that are not ignored by .gitignore will be included in the ZIP archive. We should explicitly filter them out to ensure consistent behavior with the fallback os.walk path and prevent bloated/stale Python bytecode from being packaged.

  for relative_path in git_files:
    if ".git" in relative_path.split("/") or "__pycache__" in relative_path.split("/"):
      continue
    file_path = os.path.join(base_dir, relative_path)
References
  1. Demand Robustness: Do not accept fragile code. If the proposed code is not robust enough or lacks proper error handling, explicitly tell the author why the current approach is brittle and what must be done to reinforce it. (link)

Comment thread kinetic/utils/packager.py
Comment on lines +74 to +75
if os.path.isdir(file_path) and not os.path.islink(file_path):
nested_files = _list_git_files(file_path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent running git ls-files on normal directories (which can happen due to type changes or other edge cases, causing git to find the parent repository and duplicate files in the archive), we should explicitly verify that the directory is a git repository or submodule by checking for the presence of a .git file or directory before recursing.

Suggested change
if os.path.isdir(file_path) and not os.path.islink(file_path):
nested_files = _list_git_files(file_path)
if os.path.isdir(file_path) and not os.path.islink(file_path) and os.path.exists(os.path.join(file_path, ".git")):
nested_files = _list_git_files(file_path)
References
  1. Demand Robustness: Do not accept fragile code. If the proposed code is not robust enough or lacks proper error handling, explicitly tell the author why the current approach is brittle and what must be done to reinforce it. (link)

Comment thread kinetic/utils/packager_test.py Outdated
Comment on lines +133 to +135
def test_list_git_files_respects_gitignore(self):
"""Test that _list_git_files respects .gitignore."""
tmp_path = _make_temp_path(self)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If git is not installed or available in the test environment (e.g., in a minimal CI container), running git init and git add via subprocess.run will raise a FileNotFoundError and fail the test suite. We should gracefully skip these tests if the git command is not available.

Suggested change
def test_list_git_files_respects_gitignore(self):
"""Test that _list_git_files respects .gitignore."""
tmp_path = _make_temp_path(self)
def test_list_git_files_respects_gitignore(self):
"""Test that _list_git_files respects .gitignore."""
import shutil
if not shutil.which("git"):
self.skipTest("git command not available")
tmp_path = _make_temp_path(self)
References
  1. Demand Robustness: Do not accept fragile code. If the proposed code is not robust enough or lacks proper error handling, explicitly tell the author why the current approach is brittle and what must be done to reinforce it. (link)

Comment thread kinetic/utils/packager_test.py Outdated
Comment on lines +154 to +156
def test_write_git_files_excludes_paths(self):
"""Test that _write_git_files respects exclude_paths."""
tmp_path = _make_temp_path(self)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If git is not installed or available in the test environment, this test will fail with a FileNotFoundError. We should gracefully skip it if the git command is not available.

Suggested change
def test_write_git_files_excludes_paths(self):
"""Test that _write_git_files respects exclude_paths."""
tmp_path = _make_temp_path(self)
def test_write_git_files_excludes_paths(self):
"""Test that _write_git_files respects exclude_paths."""
import shutil
if not shutil.which("git"):
self.skipTest("git command not available")
tmp_path = _make_temp_path(self)
References
  1. Demand Robustness: Do not accept fragile code. If the proposed code is not robust enough or lacks proper error handling, explicitly tell the author why the current approach is brittle and what must be done to reinforce it. (link)

@GargiGupta-io

GargiGupta-io commented Aug 11, 2026

Copy link
Copy Markdown
Author

Phase 3 of 4. It Depends on #294 being merged first and #293 consists of the merging strategy. This PR adds tests for the git ls-files integration.

Adds helper functions to support git-aware packaging:
- _list_git_files(): Lists tracked and non-ignored untracked files
- _path_is_excluded(): Checks if a path should be excluded
- _write_git_files(): Recursively writes files to ZIP respecting exclusions

These functions enable respecting .gitignore when zipping working directories.
- Uses git ls-files when in a git repository to respect .gitignore
- Falls back to directory traversal with os.walk when not in a git repo
- Maintains all existing features: empty directory preservation, secret detection, path exclusion
- Updated docstring to document git integration
Adds tests for:
- _list_git_files in a git repository
- _list_git_files when not in a git repository (returns None)
- _path_is_excluded functionality

Tests ensure git integration works correctly and falls back gracefully.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant