Skip to content

Conversation

@rakshanda-cmu
Copy link

@rakshanda-cmu rakshanda-cmu commented Nov 3, 2025

Feat: auto detect and include unit test files during pdd generate

This pull request introduces a new feature to pdd generate, which auto detects and includes unit test files to for code generation. This helps generate code that passes unit tests without the user manually having to include the test files.

Changes Implemented
New --exclude-tests Flag:

A --exclude-tests flag has been added to the pdd generate command in pdd/cli.py.
When this flag is used, the auto-include unit test feature is disabled.

Integrated Repository Logic in pdd/code_generate_main.py:

The core logic is now consolidated within code_generate_main.py:

  • If --exclude-tests flag is set then does nothing (behaviour unmodified)
  • The function auto detects unit test case files in the same directory as the .prompt file. --exclude-tests flag and triggers the new workflow.
  • If unit test files are found then a include test file statement is added to the .prompt file with the additional prompt instruction to generate code that passes these unit test
  • The new .prompt file is save with prefix "ut_"

How to Test

Navigate to any Git repository within your project with unit tests.
Run the command:
pdd generate

Observe the output:
The tool will first scan for unit test files and update .prompt file to include the unit test file and add prompt instructions to generate code that passes these tests. The updated .prompt file is saved as "ut_<original_filename>.prompt"

To disable the feature:
pdd --exclude-tests generate

The method now does not auto include test files during generation and runs as usuall

@rakshanda-cmu rakshanda-cmu changed the title Changes for issue #97 Resolves #97: Include in tests into the context window when generating Nov 3, 2025
@gltanaka
Copy link
Contributor

gltanaka commented Nov 3, 2025

close #97

@gltanaka gltanaka requested a review from Copilot November 3, 2025 18:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces automatic unit test file detection and inclusion during code generation to help generate code that passes existing unit tests. The feature is enabled by default and can be disabled using the new --exclude-tests flag.

Key Changes:

  • Adds --exclude-tests CLI flag to disable the auto-include unit test feature
  • Implements automatic test file detection logic that scans for test files in the project's test directory
  • Creates companion ut_<basename>.prompt files that include test file references and instructions to generate code passing those tests

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.

File Description
pdd/code_generator_main.py Implements core test file detection, prompt augmentation, and companion file creation logic
pdd/cli.py Adds the --exclude-tests flag to the CLI interface
README.md Documents the new --exclude-tests flag in the global options section

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

try:
if _pdd_unit_include_line and _pdd_unit_include_line not in prompt_content:
prompt_content = prompt_content + _pdd_unit_include_line
#prompt_content = prompt_content + _pdd_unit_include_line
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

Remove commented-out duplicate code. The active line 396 already performs this operation.

Suggested change
#prompt_content = prompt_content + _pdd_unit_include_line

Copilot uses AI. Check for mistakes.
)
# Append include directive if we created one earlier
if '_pdd_unit_include_line' in locals():
#ut_prompt_text = ut_prompt_text + _pdd_unit_include_line
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

Remove commented-out code. Line 450 provides the correct implementation that prepends the include line instead.

Suggested change
#ut_prompt_text = ut_prompt_text + _pdd_unit_include_line

Copilot uses AI. Check for mistakes.
Comment on lines +454 to +456
##Uncomment below
#ut_prompt_path = pathlib.Path(prompt_file).with_name(f"{pathlib.Path(prompt_file).stem}_ut.prompt")
#ut_prompt_path = pathlib.Path(prompt_file).with_name(f"{pathlib.Path(prompt_file).stem}_ut.prompt")
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

Remove commented-out code and the '##Uncomment below' comment. Line 457 provides the final implementation using the correct 'ut_' prefix pattern.

Suggested change
##Uncomment below
#ut_prompt_path = pathlib.Path(prompt_file).with_name(f"{pathlib.Path(prompt_file).stem}_ut.prompt")
#ut_prompt_path = pathlib.Path(prompt_file).with_name(f"{pathlib.Path(prompt_file).stem}_ut.prompt")

Copilot uses AI. Check for mistakes.
)
prompt_content = input_strings["prompt_file"]


Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

Remove extra blank lines. One blank line is sufficient for code separation.

Suggested change

Copilot uses AI. Check for mistakes.
"exclude_tests",
is_flag=True,
default=False,
help="Exclude test scenarios from code coverage generation. (WIP)",
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

The help text is misleading. Based on the implementation and PR description, this flag excludes test files from being auto-included during code generation, not from 'code coverage generation'. Consider: 'Disable automatic inclusion of unit test files during code generation.'

Suggested change
help="Exclude test scenarios from code coverage generation. (WIP)",
help="Disable automatic inclusion of unit test files during code generation.",

Copilot uses AI. Check for mistakes.
- `--local`: Run commands locally instead of in the cloud.
- `--context CONTEXT_NAME`: Override automatic context detection and use the specified context from `.pddrc`.
- `--list-contexts`: List all available contexts defined in `.pddrc` and exit.
- `--exclude-tests`: Exclude test scenarios from code coverage generation. (WIP)
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

The documentation description doesn't match the actual feature. This flag disables automatic inclusion of unit test files during code generation, not code coverage generation. Update to match the actual functionality.

Suggested change
- `--exclude-tests`: Exclude test scenarios from code coverage generation. (WIP)
- `--exclude-tests`: Disable automatic inclusion of unit test files during code generation. (WIP)

Copilot uses AI. Check for mistakes.
# Only create companion UT prompt and test files when tests are not
# excluded via the --exclude-tests flag. When excluded, we will use the
# original prompt file as authored by the user.
if not exclude_tests_flag:
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

[nitpick] This creates a deeply nested block (280+ lines inside a conditional). The entire unit test handling logic (lines 246-526) should be extracted into a separate function to improve readability and maintainability.

Copilot uses AI. Check for mistakes.
@gltanaka
Copy link
Contributor

gltanaka commented Nov 3, 2025

  1. Carefully follow Contributing guide
  2. make sure all unit tests pass
  3. append new tests to tests/test_*

target 11/5

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.

2 participants