Skip to content

Prevent duplicate file processing #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions files_to_prompt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import click

global_index = 1
processed_paths = set()

EXT_TO_LANG = {
"py": "python",
Expand Down Expand Up @@ -155,6 +156,9 @@ def process_path(

for file in sorted(files):
file_path = os.path.join(root, file)
if file_path in processed_paths:
continue
processed_paths.add(file_path)
try:
with open(file_path, "r") as f:
print_path(
Expand Down Expand Up @@ -292,9 +296,10 @@ def cli(
Contents of file1.py
```
"""
# Reset global_index for pytest
global global_index
# Reset globals for pytest
global global_index, processed_paths
global_index = 1
processed_paths = set()

# Read paths from stdin if available
stdin_paths = read_paths_from_stdin(use_null_separator=null)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_files_to_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,23 @@ def test_markdown(tmpdir, option):
"`````\n"
)
assert expected.strip() == actual.strip()

def test_duplicate_paths(tmpdir):
runner = CliRunner()
with tmpdir.as_cwd():
os.makedirs("test_dir/subdir")
with open("test_dir/file1.txt", "w") as f:
f.write("File 1 contents")
with open("test_dir/subdir/file2.txt", "w") as f:
f.write("File 2 contents in subdir")

# Invoke with both "test_dir" and "test_dir/subdir"
result = runner.invoke(cli, ["test_dir", "test_dir/subdir"])
assert result.exit_code == 0
assert "test_dir/file1.txt" in result.output
assert "File 1 contents" in result.output
assert "test_dir/subdir/file2.txt" in result.output
assert "File 2 contents in subdir" in result.output

# Ensure file2.txt is included only once
assert result.output.count("test_dir/subdir/file2.txt") == 1