Skip to content

Conversation

@javoire
Copy link
Owner

@javoire javoire commented Nov 28, 2025

Overview

This PR adds comprehensive unit testing infrastructure to Stackinator, enabling fast, isolated tests by mocking all Git and GitHub CLI interactions.

Key Changes

Dependency Injection Architecture

  • New interfaces: GitClient and GitHubClient for all CLI operations
  • Refactored packages: git, github, and stack now use interface-based clients
  • No backward compatibility: Clean refactor - all functions accept client parameters explicitly

Test Infrastructure

  • Test utilities: Mock implementations using testify/mock for both Git and GitHub clients
  • Test fixtures: Helper functions for common test data (PR info, git config, etc.)
  • Test setup/teardown: Utilities to disable spinners and manage test environment

Test Coverage

  • cmd package: 32.5% coverage

    • new command: 4 test cases (explicit/implicit parent, error handling)
    • status command: 2 test cases (display stack, no branches)
    • sync command: 7 test cases (basic sync, merged parent handling, PR base updates, stash handling, error cases)
  • internal/stack package: 70.8% coverage

    • Stack branch operations, chain building, tree construction, topological sorting
  • internal/git and github packages: Interface validation tests

Benefits

  • Fast tests: No network calls, no disk I/O, no actual CLI execution
  • Isolated tests: Each test is independent with mocked dependencies
  • Maintainable: Clear separation of concerns with dependency injection
  • CI-ready: All tests pass and are ready for continuous integration

Files Changed

  • 25 files changed: 1,903 insertions(+), 253 deletions(-)
  • 11 new test files created
  • 14 files refactored for dependency injection

All tests passing

- Add GitClient and GitHubClient interfaces for dependency injection
- Refactor git, github, and stack packages to use interface-based clients
- Update all command functions to accept client parameters (no backward compatibility)
- Create comprehensive test utilities with mocks and fixtures using testify
- Add package-level tests for stack (70.8% coverage), git, and github packages
- Add command-level tests for new, sync, and status commands
- Add testify dependency to go.mod
- Update all cmd files (new, sync, status, parent, prune, rename, reparent, worktree, root) to use dependency injection

All git and github CLI calls are now mockable for fast, isolated unit tests.
- Use .Maybe() for GetDefaultBranch and GetConfig(stack.baseBranch) calls in sync tests
- Add missing GetWorktreeBranches and GetCurrentWorktreePath mocks
- Update GetAllStackParents to use .Maybe() to handle variable call counts
- Fix TestRunStatus to not expect GetAllPRs when noPR is true

All tests now passing with proper mock expectations.
Update all sync tests to use .Maybe() instead of .Times(4) for GetAllStackParents to handle variable call counts across different code paths.
@javoire javoire merged commit ff9954a into main Nov 28, 2025
1 check passed
@javoire javoire deleted the feat/add-unit-testing branch November 28, 2025 22:55
@javoire
Copy link
Owner Author

javoire commented Nov 28, 2025

🎉 This PR is included in version 1.1.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant