|
1 | 1 | package changeloggenerator |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "strings" |
4 | 7 | "testing" |
5 | 8 | ) |
6 | 9 |
|
| 10 | +var fakeGitCommands = map[string]string{} |
| 11 | + |
| 12 | +func fakeExecCommand(command string, args ...string) *exec.Cmd { |
| 13 | + cmdStr := command + " " + strings.Join(args, " ") |
| 14 | + cmd := exec.Command(os.Args[0], "-test.run=TestHelperProcess", "--", cmdStr) //nolint:gosec // standard test re-exec pattern |
| 15 | + |
| 16 | + cmd.Env = append(os.Environ(), |
| 17 | + "GO_TEST_HELPER_PROCESS=1", |
| 18 | + "MOCK_KEY="+cmdStr, |
| 19 | + "MOCK_VAL="+fakeGitCommands[cmdStr], |
| 20 | + ) |
| 21 | + |
| 22 | + return cmd |
| 23 | +} |
| 24 | + |
| 25 | +func TestHelperProcess(t *testing.T) { |
| 26 | + if os.Getenv("GO_TEST_HELPER_PROCESS") != "1" { |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + val := os.Getenv("MOCK_VAL") |
| 31 | + |
| 32 | + if val == "ERROR" { |
| 33 | + _, _ = os.Stderr.WriteString("mock failure") |
| 34 | + os.Exit(1) |
| 35 | + } |
| 36 | + |
| 37 | + _, _ = os.Stdout.WriteString(val) |
| 38 | + os.Exit(0) |
| 39 | +} |
| 40 | + |
| 41 | +func TestGetCommitsWithMeta(t *testing.T) { |
| 42 | + tests := []struct { |
| 43 | + name string |
| 44 | + since string |
| 45 | + until string |
| 46 | + mockGitCommands map[string]string |
| 47 | + expectedCount int |
| 48 | + expectErr bool |
| 49 | + }{ |
| 50 | + { |
| 51 | + name: "with explicit since and until", |
| 52 | + since: "v1.0.0", |
| 53 | + until: "HEAD", |
| 54 | + mockGitCommands: map[string]string{ |
| 55 | + "git log --pretty=format:%H|%h|%s|%an|%ae v1.0.0..HEAD": "abc123|abc123|feat: login|Alice|alice@example.com\ndef456|def456|fix: bug|Bob|bob@example.com", |
| 56 | + }, |
| 57 | + expectedCount: 2, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "fallback to HEAD~10 when no tag and enough commits", |
| 61 | + since: "", |
| 62 | + until: "HEAD", |
| 63 | + mockGitCommands: map[string]string{ |
| 64 | + "git describe --tags --abbrev=0": "", // no tags |
| 65 | + "git rev-list --count HEAD": "25", |
| 66 | + "git log --pretty=format:%H|%h|%s|%an|%ae HEAD~10..HEAD": "abc123|abc123|feat: update|Alice|alice@example.com", |
| 67 | + }, |
| 68 | + expectedCount: 1, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "fallback to root commit when fewer than 10 commits", |
| 72 | + since: "", |
| 73 | + until: "HEAD", |
| 74 | + mockGitCommands: map[string]string{ |
| 75 | + "git describe --tags --abbrev=0": "", // no tags |
| 76 | + "git rev-list --count HEAD": "2", |
| 77 | + "git rev-list --max-parents=0 HEAD": "root123", |
| 78 | + "git log --pretty=format:%H|%h|%s|%an|%ae root123..HEAD": "abc123|abc123|feat: init|Alice|alice@example.com", |
| 79 | + }, |
| 80 | + expectedCount: 1, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "fallback to last tag when tag exists", |
| 84 | + since: "", |
| 85 | + until: "HEAD", |
| 86 | + mockGitCommands: map[string]string{ |
| 87 | + "git describe --tags --abbrev=0": "v2.0.0", |
| 88 | + "git log --pretty=format:%H|%h|%s|%an|%ae v2.0.0..HEAD": "abc123|abc123|feat: new|Alice|alice@example.com", |
| 89 | + }, |
| 90 | + expectedCount: 1, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "git log returns error", |
| 94 | + since: "v1.0.0", |
| 95 | + until: "HEAD", |
| 96 | + mockGitCommands: map[string]string{ |
| 97 | + "git log --pretty=format:%H|%h|%s|%an|%ae v1.0.0..HEAD": "ERROR", |
| 98 | + }, |
| 99 | + expectErr: true, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "empty commit log", |
| 103 | + since: "v1.0.0", |
| 104 | + until: "HEAD", |
| 105 | + mockGitCommands: map[string]string{ |
| 106 | + "git log --pretty=format:%H|%h|%s|%an|%ae v1.0.0..HEAD": "", |
| 107 | + }, |
| 108 | + expectedCount: 0, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + for _, tt := range tests { |
| 113 | + t.Run(tt.name, func(t *testing.T) { |
| 114 | + fakeGitCommands = tt.mockGitCommands |
| 115 | + |
| 116 | + g := &GitOps{ExecCommandFn: fakeExecCommand} |
| 117 | + commits, err := g.getCommitsWithMeta(tt.since, tt.until) |
| 118 | + |
| 119 | + if (err != nil) != tt.expectErr { |
| 120 | + t.Fatalf("unexpected error: %v", err) |
| 121 | + } |
| 122 | + if len(commits) != tt.expectedCount { |
| 123 | + t.Fatalf("expected %d commits, got %d", tt.expectedCount, len(commits)) |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
| 128 | + |
7 | 129 | func TestParseRemoteURL(t *testing.T) { |
8 | 130 |
|
9 | 131 | tests := []struct { |
|
0 commit comments