Skip to content

Document test side-effect prevention guidelines#1096

Merged
nathanjmcdougall merged 3 commits intomainfrom
copilot/document-test-side-effects
Nov 5, 2025
Merged

Document test side-effect prevention guidelines#1096
nathanjmcdougall merged 3 commits intomainfrom
copilot/document-test-side-effects

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 5, 2025

Tests were modifying repository files instead of operating in isolated temporary directories. Added documentation clarifying that tests must use tmp_path fixture and the change_cwd context manager.

Changes

CONTRIBUTING.md

  • Added "Test Side-Effects" subsection explaining:
    • Tests must not modify/create/delete files in the repository directory
    • Use tmp_path fixture for all file operations
    • Use usethis._test.change_cwd context manager for CWD-dependent operations
    • Included example demonstrating correct pattern

.github/copilot-instructions.md

  • Added "No side-effects" requirement to Test Requirements
  • Quick reference for tmp_path and change_cwd usage

Example

from usethis._test import change_cwd

def test_example(tmp_path: Path):
    # Arrange - create files in temporary directory
    (tmp_path / "example.txt").write_text("test")
    
    # Act - change to temp directory for operations
    with change_cwd(tmp_path):
        do_something()
    
    # Assert
    assert (tmp_path / "output.txt").exists()

If tests modify repo files, it indicates missing change_cwd context manager.

Original prompt

This section details on the original issue you should resolve

<issue_title>Document that tests should not have side-effects in the repo</issue_title>
<issue_description>e.g. shouldn't modify any files, create new files, etc. in the repo itself; only in temporary directories (usually via the pytest tmp_path fixture).

Mention that if this happens, it's a sign that the CWD is not properly set using the usethis._test.change_cwd context manager

This should be mentioned in CONTRIBUTING.md and copilot-instructions.md</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…instructions.md

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copilot AI changed the title [WIP] Document that tests should not have side-effects in the repo Document test side-effect prevention guidelines Nov 5, 2025
@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review November 5, 2025 12:31
@nathanjmcdougall nathanjmcdougall merged commit 02917af into main Nov 5, 2025
@nathanjmcdougall nathanjmcdougall deleted the copilot/document-test-side-effects branch November 5, 2025 12:31
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.

Document that tests should not have side-effects in the repo

2 participants