Skip to content

Add thorough tests for git change detection and utils#67

Merged
DFanso merged 1 commit intoDFanso:mainfrom
Muneer320:main
Oct 6, 2025
Merged

Add thorough tests for git change detection and utils#67
DFanso merged 1 commit intoDFanso:mainfrom
Muneer320:main

Conversation

@Muneer320
Copy link
Contributor

@Muneer320 Muneer320 commented Oct 6, 2025

Description

Strengthens the test safety net by adding focused unit and integration coverage for git change detection, file heuristics, and shared prompt types.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring
  • Performance improvement
  • Other (please describe): Test coverage improvements

Related Issue

Fixes #2

Changes Made

  • Added unit tests for NormalizePath, IsTextFile, IsSmallFile, and FilterEmpty in internal/utils
  • Introduced repository detection and end-to-end GetChanges coverage in internal/git
  • Verified JSON tags and CommitPrompt integrity in pkg/types

Testing

  • Tested with Gemini API
  • Tested with Grok API
  • Tested on Windows
  • Tested on Linux
  • Tested on macOS
  • Added/updated tests (if applicable)

Checklist

  • My code follows the project's code style
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings or errors
  • I have tested this in a real Git repository
  • I have read the CONTRIBUTING.md guidelines

Screenshots (if applicable)

Not applicable

Additional Notes

Targeted packages now reach roughly 92% statement coverage (go tool cover -func coverprofile), giving confidence that git change harvesting and file heuristics behave as expected.


For Hacktoberfest Participants

  • This PR is submitted as part of Hacktoberfest 2025

Summary by CodeRabbit

  • Tests
    • Added comprehensive test coverage for Git repository detection, change summaries, and error handling outside repositories.
    • Expanded utility tests for path normalization, text/binary detection, small file classification, and filtering of empty values.
    • Added configuration and prompt content validation via JSON marshalling checks.
    • Improves reliability, correctness, and regression protection across core workflows.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 6, 2025

Walkthrough

Adds new Go test files for git operations, utility functions, and types. Tests include repository detection, change listing, error cases outside repos, path normalization, text/small file checks, empty filtering, JSON marshalling, and prompt content verification. Some tests run in parallel and use temporary directories; one integration-style test is skipped in short mode.

Changes

Cohort / File(s) Summary
Git operations tests
internal/git/operations_test.go
Introduces unit and integration-style tests for IsRepository and GetChanges, including repo setup, staging/untracked scenarios, error outside repo, and a git command helper. Skips long-running test in short mode.
Utilities tests
internal/utils/utils_test.go
Adds tests for NormalizePath, IsTextFile (case-insensitive), IsSmallFile (size thresholds, missing file), and FilterEmpty. Uses t.TempDir, parallel subtests where applicable.
Types tests
pkg/types/types_test.go
Adds tests verifying Config JSON contains expected keys and that CommitPrompt includes required substrings.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

Thump-thump go my paws on the keys,
New tests sprout up like clover in breeze.
Git hums, utils shine, types sing along—
Coverage hops higher, swift and strong.
In burrows of code, I nibble and grin—
Green ticks bloom; let reviews begin! 🐇✅

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly captures the primary focus of the changeset by highlighting the addition of comprehensive tests for git change detection and utility functions, and it is concise, clear, and free of extraneous details.
Linked Issues Check ✅ Passed The pull request adds comprehensive unit tests for all functions referenced in issue #2, including repository detection, file heuristics, JSON marshalling, and commit prompt content, employs the Go testing framework with both positive and negative cases, and achieves the targeted coverage threshold.
Out of Scope Changes Check ✅ Passed All changes consist solely of new test files for functionality that aligns with the linked issue objectives, and there are no additions or modifications unrelated to the specified scope of enhancing test coverage.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
pkg/types/types_test.go (1)

9-34: Consider unmarshalling for structural verification.

The test checks for literal key strings in the JSON output. While this provides basic coverage, unmarshalling the JSON back into a Config struct and verifying the values would be more robust and less brittle.

Example refactor:

-	jsonStr := string(data)
-	if !strings.Contains(jsonStr, "\"grok_api\"") {
-		t.Fatalf("expected grok_api key in JSON: %s", jsonStr)
-	}
-	if !strings.Contains(jsonStr, "\"repos\"") {
-		t.Fatalf("expected repos key in JSON: %s", jsonStr)
-	}
+	var decoded Config
+	if err := json.Unmarshal(data, &decoded); err != nil {
+		t.Fatalf("json.Unmarshal failed: %v", err)
+	}
+	
+	if decoded.GrokAPI != cfg.GrokAPI {
+		t.Fatalf("GrokAPI mismatch: got %q, want %q", decoded.GrokAPI, cfg.GrokAPI)
+	}
+	if len(decoded.Repos) != len(cfg.Repos) {
+		t.Fatalf("Repos count mismatch: got %d, want %d", len(decoded.Repos), len(cfg.Repos))
+	}
internal/git/operations_test.go (1)

55-111: Good integration test with one optional enhancement.

The test properly skips in short mode and creates a realistic repository state with unstaged, staged, and untracked changes. Section headers are verified, but you could strengthen the test by asserting specific diff content.

Example enhancement to verify unstaged diff content:

fragments := []string{
	"Unstaged changes:",
	"tracked.txt",              // file name in unstaged section
	"+updated version",         // diff content showing the change
	"Staged changes:",
	"Untracked files:",
	"Content of new file new.txt:",
	"Recent commits for context:",
	"brand new file",
}
internal/utils/utils_test.go (1)

10-33: LGTM!

Well-structured table-driven test with parallel execution. Covers Windows-style paths, already-normalized paths, and no trailing slash scenarios.

Optional: Add edge cases for more thorough coverage:

{name: "forward slash trailing", input: "foo/bar/", expected: "foo/bar"},
{name: "empty string", input: "", expected: ""},
{name: "root path", input: "/", expected: ""},
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 961cd50 and 4b3dc82.

📒 Files selected for processing (3)
  • internal/git/operations_test.go (1 hunks)
  • internal/utils/utils_test.go (1 hunks)
  • pkg/types/types_test.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
pkg/types/types_test.go (2)
pkg/types/types.go (2)
  • Config (4-7)
  • RepoConfig (10-13)
pkg/types/prompt.go (1)
  • CommitPrompt (3-19)
internal/utils/utils_test.go (1)
internal/utils/utils.go (4)
  • NormalizePath (10-16)
  • IsTextFile (19-35)
  • IsSmallFile (38-47)
  • FilterEmpty (50-58)
internal/git/operations_test.go (2)
internal/git/operations.go (2)
  • IsRepository (16-23)
  • GetChanges (26-130)
pkg/types/types.go (1)
  • RepoConfig (10-13)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Build Go Binary (windows-latest)
🔇 Additional comments (7)
pkg/types/types_test.go (1)

36-50: LGTM!

The test verifies that CommitPrompt contains essential instructional fragments. Note that checking for literal strings makes the test brittle to prompt wording changes, but this is acceptable for a basic content validation test.

internal/git/operations_test.go (3)

13-53: LGTM!

Comprehensive coverage of IsRepository with positive (initialized repo) and negative cases (non-repo directory, missing path). Proper use of t.Parallel(), t.TempDir(), and graceful skipping when git is unavailable.


113-121: LGTM!

Correctly verifies that GetChanges returns an error when called on a non-repository directory.


123-133: LGTM!

Well-structured helper with t.Helper() marker and GIT_TERMINAL_PROMPT=0 to prevent interactive prompts during tests.

internal/utils/utils_test.go (3)

35-58: LGTM!

Correctly tests text file detection by extension, including case-insensitivity ("README.MD"), binary extensions, and files without extensions.


60-95: LGTM!

Thorough test using actual temporary files to verify the 10KB size threshold. Correctly tests small files (1KB), large files (11KB), and missing files.


97-114: LGTM!

Correctly verifies that FilterEmpty removes only empty strings while preserving whitespace strings ( ), which aligns with the implementation.

Copy link
Owner

@DFanso DFanso left a comment

Choose a reason for hiding this comment

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

LGTM 🎉

@DFanso DFanso added enhancement New feature or request hacktoberfest Eligible for Hacktoberfest hacktoberfest-accepted Approved Hacktoberfest contribution go Pull requests that update go code labels Oct 6, 2025
@DFanso DFanso merged commit 3f17f8c into DFanso:main Oct 6, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request go Pull requests that update go code hacktoberfest Eligible for Hacktoberfest hacktoberfest-accepted Approved Hacktoberfest contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Test] Add Unit Tests for Core Functions

2 participants