Skip to content

Refactor command handler tests to use IGitClient substitutes instead of TestGitRepositoryBuilder#334

Merged
geofflamrock merged 5 commits intomainfrom
copilot/fix-99485d52-4838-4f74-acf3-4b15dcb3bbdb
Aug 21, 2025
Merged

Refactor command handler tests to use IGitClient substitutes instead of TestGitRepositoryBuilder#334
geofflamrock merged 5 commits intomainfrom
copilot/fix-99485d52-4838-4f74-acf3-4b15dcb3bbdb

Conversation

Copy link
Contributor

Copilot AI commented Aug 20, 2025

This PR refactors command handler unit tests to use IGitClient substitutes instead of TestGitRepositoryBuilder for better test isolation and performance. The changes eliminate dependencies on actual git repositories in unit tests, making them faster and more reliable.

Changes Made

Converted Test Classes

  • RemoveBranchCommandHandlerTests (12 tests) - Complete conversion from TestGitRepositoryBuilder to IGitClient substitute
  • OpenPullRequestsCommandHandlerTests (5 tests) - Complete conversion from TestGitRepositoryBuilder to IGitClient substitute
  • CleanupStackCommandHandlerTests (9 tests) - Complete conversion from TestGitRepositoryBuilder to IGitClient substitute

Technical Implementation

The conversion follows a consistent pattern:

Before:

using var repo = new TestGitRepositoryBuilder()
    .WithBranch(sourceBranch)
    .WithBranch(branchToRemove)
    .Build();

var gitClient = new GitClient(logger, repo.GitClientSettings);
var stackConfig = new TestStackConfigBuilder()
    .WithStack(stack => stack
        .WithRemoteUri(repo.RemoteUri)
        // ...

After:

var remoteUri = Some.HttpsUri().ToString();

var gitClient = Substitute.For<IGitClient>();
var stackConfig = new TestStackConfigBuilder()
    .WithStack(stack => stack
        .WithRemoteUri(remoteUri)
        // ...

gitClient.GetRemoteUri().Returns(remoteUri);
gitClient.GetCurrentBranch().Returns(sourceBranch);

Test Verification

Tests now verify behavior through substitute method calls rather than checking repository state:

// Assert - branch without remote tracking should not be deleted
gitClient.DidNotReceive().DeleteLocalBranch(branchWithoutRemoteTracking);

Excluded Test Classes

The following test classes were excluded as they have in-progress PRs:

  • NewStackCommandHandlerTests
  • StackSwitchCommandHandlerTests
  • DeleteStackCommandHandlerTests

Test Classes Already Using Substitutes

These classes were already properly converted and required no changes:

  • UpdateStackCommandHandlerTests
  • PullStackCommandHandlerTests
  • PushStackCommandHandlerTests
  • SyncStackCommandHandlerTests

Benefits

  • Performance: Tests run faster without creating actual git repositories
  • Isolation: Tests are independent of git state and file system
  • Reliability: No dependency on git binary or file system permissions
  • Maintainability: Clearer test intent through explicit method call verification

All converted tests pass successfully and maintain the same test coverage as the original implementations.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Refactor all unit tests for **command handlers** to use a substitute instead of the test Git repo builder, except for the following test classes which already have in-progress PRs: NewStackCommandHandlerTests, StackSwitchCommandHandlerTests, DeleteStac... Refactor command handler tests to use IGitClient substitutes instead of TestGitRepositoryBuilder Aug 20, 2025
Copilot AI requested a review from geofflamrock August 20, 2025 08:10
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot convert the rest of the tests in the file to use the substitute

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Completed the conversion of all remaining tests in CleanupStackCommandHandlerTests.cs to use IGitClient substitutes instead of TestGitRepositoryBuilder. All 9 tests have been successfully converted and are passing. The changes follow the same pattern as the other converted test files, using GitBranchStatus objects to mock branch states and verifying behavior through substitute method calls rather than actual repository state. (870f5b4)

Copilot AI requested a review from geofflamrock August 20, 2025 23:15
@geofflamrock geofflamrock added the tests Add or improve existing tests label Aug 21, 2025
@geofflamrock geofflamrock marked this pull request as ready for review August 21, 2025 22:29
Copilot AI and others added 5 commits August 22, 2025 08:30
Co-authored-by: geofflamrock <2915931+geofflamrock@users.noreply.github.com>
Co-authored-by: geofflamrock <2915931+geofflamrock@users.noreply.github.com>
Co-authored-by: geofflamrock <2915931+geofflamrock@users.noreply.github.com>
…ubstitutes

Co-authored-by: geofflamrock <2915931+geofflamrock@users.noreply.github.com>
@geofflamrock geofflamrock force-pushed the copilot/fix-99485d52-4838-4f74-acf3-4b15dcb3bbdb branch from 870f5b4 to abd7a37 Compare August 21, 2025 22:30
@geofflamrock geofflamrock enabled auto-merge (squash) August 21, 2025 22:30
@geofflamrock geofflamrock merged commit c877763 into main Aug 21, 2025
15 checks passed
@geofflamrock geofflamrock deleted the copilot/fix-99485d52-4838-4f74-acf3-4b15dcb3bbdb branch August 21, 2025 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests Add or improve existing tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants