-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr_review_test.go
82 lines (68 loc) · 2.58 KB
/
pr_review_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cmd
import (
"github.com/google/go-github/v49/github"
"github.com/gsoares85/code-guardian/internal/github_internal"
"github.com/gsoares85/code-guardian/internal/openai"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
)
// ✅ Test Markdown Generation
func TestGenerateMarkdownReport(t *testing.T) {
prTitle := "Fix memory leak"
prOwner := "testUser"
prNumber := 23
pr, err := github_internal.MockGetPullRequest(prOwner, prTitle, prNumber)
files, err := github_internal.MockGetPullRequestFiles(prOwner, prTitle, prNumber)
prDiff, err := github_internal.MockGetPullRequestDiff(prOwner, prTitle, prNumber)
aiFeedback, err := openai.MockAnalyzePRWithAI(prDiff)
report := generateMarkdownReport(pr, files, prDiff, aiFeedback)
assert.Contains(t, report, "# Pull Request Analysis Report")
assert.Contains(t, report, prTitle)
assert.Contains(t, report, "src/main.c")
assert.Contains(t, report, "free(ptr);")
assert.Nil(t, err)
}
// ✅ Test File Saving
func TestSaveAnalysisToFile(t *testing.T) {
fileName := filepath.Join("tests", "reports", "test_report.md")
content := "# Sample Report\nThis is a test file."
err := saveAnalysisToFile(fileName, content)
assert.Nil(t, err)
// Verify file exists
_, err = os.Stat(fileName)
assert.Nil(t, err)
// Clean up
os.Remove(fileName)
}
// ✅ Test PR Analysis (Integration)
func TestAnalyzePullRequest(t *testing.T) {
// Use local variables in the test function to "mock" external functionality.
mockGetPullRequest := func(owner, title string, number int) (*github.PullRequest, error) {
return github_internal.MockGetPullRequest(owner, title, number)
}
mockGetPullRequestFiles := func(owner, title string, number int) ([]string, error) {
return github_internal.MockGetPullRequestFiles(owner, title, number)
}
mockGetPullRequestDiff := func(owner, title string, number int) (string, error) {
return github_internal.MockGetPullRequestDiff(owner, title, number)
}
mockAnalyzePRWithAI := func(prDiff string) (string, error) {
return openai.MockAnalyzePRWithAI(prDiff)
}
// Use the mocks for testing by calling them explicitly.
pr, err := mockGetPullRequest("example", "repo", 42)
assert.Nil(t, err)
files, err := mockGetPullRequestFiles("example", "repo", 42)
assert.Nil(t, err)
prDiff, err := mockGetPullRequestDiff("example", "repo", 42)
assert.Nil(t, err)
aiFeedback, err := mockAnalyzePRWithAI(prDiff)
assert.Nil(t, err)
// Add assertions to validate everything is working correctly
assert.NotNil(t, pr)
assert.Greater(t, len(files), 0)
assert.Greater(t, len(prDiff), 0)
assert.Greater(t, len(aiFeedback), 0)
}