Skip to content

feat: test creation and commit in /tests - #13

Merged
haddybhaiya merged 4 commits into
mainfrom
check-test
Mar 31, 2026
Merged

feat: test creation and commit in /tests#13
haddybhaiya merged 4 commits into
mainfrom
check-test

Conversation

@haddybhaiya

Copy link
Copy Markdown
Member

feat: test creation and commit in /tests

Apricot current release allows only test creation and commenting on PR, while this pull request allows apricot to generated tests and commit them onto /tests with test_generated.py file name.

- key changes made:

  • add committer.py
  • change in permissions of apricot ~github-agent from read to write
  • main.py calls commit_tests()
    -test_generator.py checks if tests starts with bash (```) or python keyword

@github-actions

Copy link
Copy Markdown

Automated Code Review Comment:

Bugs

  • The get_pr_diff function has an undefined variable ret.
  • The commit_tests function in the agent/github/committer.py file does not handle potential errors that might occur when running git commands.
  • In the agent/main.py file, the commit_tests function is called without error handling.

Improvements

  • The commit_tests function can be improved by adding error handling for git commands.
  • The code in agent/main.py can be improved by removing unnecessary imports and using more descriptive variable names.
  • The generate_tests function in agent/llm/test_generator.py can be improved by adding more error handling and logging.

Suggestions

  • Consider using a try-except block when calling the commit_tests function in agent/main.py to handle any potential errors.
  • The commit_tests function in agent/github/committer.py can be optimized by using a loop to configure git settings and adding tests instead of hardcoding the commands.
  • It's a good practice to log the output of the subprocess.run commands for debugging purposes.
  • Code looks good overall, but these suggestions can improve it further.

Suggested Tests

import pytest
from agent.github.committer import commit_tests
from agent.main import get_pr_diff
from agent.llm.test_generator import generate_tests

def test_commit_tests(tmpdir):
with pytest.raises(Exception):
commit_tests()

def test_get_pr_diff():
diff = get_pr_diff()
assert isinstance(diff, str)

def test_generate_tests():
diff = "example diff"
tests = generate_tests(diff)
assert isinstance(tests, str)

def test_generate_tests_empty_diff():
diff = ""
tests = generate_tests(diff)
assert tests == ""

def test_main(tmpdir, monkeypatch):
monkeypatch.setattr('agent.main.get_pr_diff', lambda: "example diff")
monkeypatch.setattr('agent.main.generate_tests', lambda x, y: "example tests")
monkeypatch.setattr('agent.main.post_comment', lambda x: None)
from agent.main import main
main()

Copilot AI left a comment

Copy link
Copy Markdown

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 extends the apricot agent so it can not only generate suggested tests, but also write them into /tests/test_generated.py and attempt to commit/push them back to the repository from the GitHub Actions workflow.

Changes:

  • Save LLM-generated tests to tests/test_generated.py and invoke a new commit/push helper from agent/main.py.
  • Add agent/github/committer.py to git-add/commit/push the generated test file.
  • Update the workflow to grant contents: write and adjust test generation output stripping for fenced code blocks.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.

File Description
agent/main.py Writes generated tests to disk and calls commit_tests() after commenting.
agent/llm/test_generator.py Attempts to strip markdown code fences from LLM output before returning tests.
agent/github/committer.py New helper to configure git, add/commit, and push generated tests.
.github/workflows/agent.yml Elevates workflow token permissions to allow writing repository contents.

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

Comment thread agent/main.py
from agent.llm.test_generator import generate_tests

import subprocess
from agent.github.committer import commit_tests

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

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

commit_tests is imported from agent.github.committer, but the new module added in this PR is agent/github/committer.py (double “t”). As-is, the agent will crash with ModuleNotFoundError at startup. Update the import to match the actual filename/module, or rename the module for consistency.

Suggested change
from agent.github.committer import commit_tests
from agent.github.commiter import commit_tests

Copilot uses AI. Check for mistakes.
Comment thread agent/main.py
Comment on lines 74 to +78
tests = generate_tests(diff, context=relevant_chunks)
os.makedirs("tests", exist_ok=True)
with open("tests/test_generated.py", "w", encoding="utf-8") as f:
f.write(tests) # Save generated tests to a file for potential commit
print("Generated tests saved to tests/test_generated.py")

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

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

The generated test content is written directly to tests/test_generated.py even when generate_tests() returns an error string (it returns "Error generating tests:..." on exceptions). This can lead to committing a non-test error message into the repository. Consider checking for the error sentinel (or otherwise validating that the output is Python test code) before writing/committing.

Copilot uses AI. Check for mistakes.
Comment thread agent/main.py
Comment on lines +90 to +92
# 11. Commit tests
print("Committing tests...")
commit_tests()

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

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

commit_tests() is called unconditionally in the PR workflow. Combined with the workflow trigger on pull_request and a constant commit message, this can create repeated commits / reruns, or attempts to commit when nothing changed. Add a guard (e.g., only commit when tests/test_generated.py differs, and/or skip when the actor is the actions bot / commit message matches).

Copilot uses AI. Check for mistakes.
Comment on lines +37 to +42
content =response.choices[0].message.content.strip()
if content.startswith("```"):
content = content.split("```")[1] # Extract code from markdown
if content.startswith("python"):
content = content[len("python"):] # Remove language specifier
return content.strip()

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

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

Markdown code-fence stripping is fragile here: content.split("```")[1] will raise/behave incorrectly when the response contains multiple fenced blocks, leading text before the fence, or an unclosed fence. Also, the PR description mentions handling bash, but this code only strips python and will leave a leading bash token that will make the saved file invalid Python. Consider robustly parsing a single fenced block (and rejecting/handling non-Python languages) before returning.

Copilot uses AI. Check for mistakes.
Comment thread agent/github/committer.py Outdated
Comment on lines +10 to +16
# add the generated test file
subprocess.run(["git","add","tests/test_generated.py"],check=True)
# commit the changes
subprocess.run(["git","commit","-m","Add AI-generated tests"],check=True)
# push
subprocess.run(["git","push"],check=True)
print("Tests committed and pushed successfully.")

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

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

This function will likely fail to push in the current workflow: on pull_request events, actions/checkout typically checks out a detached merge ref (e.g. refs/pull/*/merge), so git commit/git push will not update the PR branch. If the intent is to push to the PR’s head branch, the workflow needs to check out github.head_ref (and the push should target that branch explicitly).

Copilot uses AI. Check for mistakes.
Comment thread agent/github/committer.py Outdated
subprocess.run(["git","push"],check=True)
print("Tests committed and pushed successfully.")
except Exception as e:
print(f"Commit Failed {e}")

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

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

commit_tests() swallows all exceptions and only prints Commit Failed ..., which can make the workflow appear successful even though nothing was committed/pushed. Consider returning a status / re-raising after logging so CI accurately reflects whether the commit step worked.

Suggested change
print(f"Commit Failed {e}")
print(f"Commit Failed {e}")
raise

Copilot uses AI. Check for mistakes.
Comment thread agent/github/committer.py Outdated
import subprocess
def commit_tests():
try:
# configue git (required in github actions)

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

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

Typo in comment: “configue git” should be “configure git”.

Suggested change
# configue git (required in github actions)
# configure git (required in github actions)

Copilot uses AI. Check for mistakes.
Comment on lines 8 to +10
permissions:
pull-requests: write
contents : read
contents : write

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

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

Changing contents permission to write enables pushing commits from this workflow. On pull_request triggers this has two concrete risks: (1) the push can retrigger the workflow and create a commit loop unless you add explicit safeguards, and (2) for fork PRs the token won’t be able to push anyway, so the job will repeatedly attempt/fail. Consider adding if: guards (e.g., only run on non-fork PRs and skip when github.actor is the actions bot), and ensure checkout/push targets the PR head branch if committing is intended.

Copilot uses AI. Check for mistakes.
@deoxyforge

Copy link
Copy Markdown
Collaborator

all good @haddybhaiya

@github-actions

Copy link
Copy Markdown

Automated Code Review Comment:

Bugs

  • The get_pr_diff function is missing a return statement, which would cause a SyntaxError.
  • In the commit_tests function, code is not defined, which would cause a NameError.
  • In the generate_tests function, if the content does not start with "```python", it will still try to remove the language specifier, which could lead to incorrect results.

Improvements

  • Error handling in the commit_tests function could be more specific and provide more informative error messages.
  • The generate_tests function could benefit from more detailed documentation and type hints.
  • The main function is quite long and complex, and could be broken up into smaller functions for better readability and maintainability.

Suggestions

  • Use a linter and a code formatter to improve code consistency and readability.
  • Consider using a more robust way to extract code from markdown, such as using a dedicated markdown parsing library.
  • Add more tests to ensure the correctness of the code.

Code does not look good due to identified bugs and potential improvements.


Suggested Tests

import pytest
import os
from agent.github.committer import commit_tests
from agent.main import get_pr_diff

def test_commit_tests():
commit_tests()

def test_get_pr_diff_empty():
with pytest.raises(subprocess.CalledProcessError):
get_pr_diff()

def test_commit_tests_exception():
try:
commit_tests()
except Exception as e:
assert str(e)

def test_get_pr_diff_no_diff():
diff = get_pr_diff()
assert diff.strip() == ""

def test_commit_tests_push():
subprocess.run(["git", "config", "user.name", "github-actions"], check=True)
subprocess.run(["git", "config", "user.email", "actions@github.com"], check=True)
commit_tests()

@haddybhaiya
haddybhaiya merged commit 454e436 into main Mar 31, 2026

@haddybhaiya haddybhaiya left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

FINE NOW!

@haddybhaiya
haddybhaiya deleted the check-test branch April 1, 2026 10:41
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.

5 participants