Skip to content

Commit 2ea1760

Browse files
committed
Address #251
1 parent e32cd84 commit 2ea1760

6 files changed

Lines changed: 88 additions & 4 deletions

File tree

cmd/console.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,14 @@ func runLeftRightCompare(left, right string, updateChan chan *model.ProgressUpda
408408
Message: fmt.Sprintf("Original: %s, Modified: %s", left, right),
409409
CommitDate: time.Now(),
410410
Data: rightBytes,
411+
FilePath: right,
411412
},
412413
{
413414
Hash: uuid.New().String()[:6],
414415
Message: fmt.Sprintf("Original file: %s", left),
415416
CommitDate: time.Now(),
416417
Data: leftBytes,
418+
FilePath: left,
417419
},
418420
}
419421

cmd/left_right_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pb33f/openapi-changes/model"
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestRunLeftRightSummary_IdenticalSpecsReturnsNoError(t *testing.T) {
12+
updateChan := make(chan *model.ProgressUpdate, 32)
13+
errorChan := make(chan model.ProgressError, 32)
14+
15+
errs := runLeftRightSummary("../sample-specs/petstorev3.json", "../sample-specs/petstorev3.json",
16+
updateChan, errorChan, "", true, false, false, false, nil)
17+
18+
require.Empty(t, errs)
19+
}
20+
21+
func TestRunLeftRightReport_IdenticalSpecsReturnsNilReport(t *testing.T) {
22+
updateChan := make(chan *model.ProgressUpdate, 32)
23+
errorChan := make(chan model.ProgressError, 32)
24+
25+
report, errs := runLeftRightReport("../sample-specs/petstorev3.json", "../sample-specs/petstorev3.json",
26+
updateChan, errorChan, "", true, false, nil)
27+
28+
require.Empty(t, errs)
29+
assert.Nil(t, report)
30+
}

cmd/report.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,12 +371,14 @@ func runLeftRightReport(left, right string,
371371
Message: fmt.Sprintf("Original: %s, Modified: %s, ", left, right),
372372
CommitDate: time.Now(),
373373
Data: rightBytes,
374+
FilePath: right,
374375
},
375376
{
376377
Hash: uuid.New().String()[:6],
377378
Message: fmt.Sprintf("Original file: %s", left),
378379
CommitDate: time.Now(),
379380
Data: leftBytes,
381+
FilePath: left,
380382
},
381383
}
382384

@@ -386,6 +388,9 @@ func runLeftRightReport(left, right string,
386388
if len(errs) > 0 {
387389
return nil, errs
388390
}
391+
if len(commits) == 0 {
392+
return nil, nil
393+
}
389394
if commits[0].Changes == nil {
390395
return nil, nil
391396
}

cmd/summary.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,14 @@ func runLeftRightSummary(left, right string, updateChan chan *model.ProgressUpda
363363
Message: fmt.Sprintf("Original: %s, Modified: %s, ", left, right),
364364
CommitDate: time.Now(),
365365
Data: rightBytes,
366+
FilePath: right,
366367
},
367368
{
368369
Hash: uuid.New().String()[:6],
369370
Message: fmt.Sprintf("Original file: %s", left),
370371
CommitDate: time.Now(),
371372
Data: leftBytes,
373+
FilePath: left,
372374
},
373375
}
374376

git/read_local.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,11 @@ func BuildCommitChangelog(commitHistory []*model.Commit,
287287
}
288288
}
289289
if len(oldBits) == 0 && len(newBits) > 0 {
290-
model.SendProgressWarning("building models",
291-
fmt.Sprintf("Commit %s is the first version of '%s' — no prior version to compare against, skipping",
292-
commitHistory[c].Hash, commitHistory[c].FilePath), progressChan)
290+
if commitHistory[c].RepoDirectory != "" {
291+
model.SendProgressWarning("building models",
292+
fmt.Sprintf("Commit %s is the first version of '%s' — no prior version to compare against, skipping",
293+
commitHistory[c].Hash, commitHistory[c].FilePath), progressChan)
294+
}
293295
newDoc, err = libopenapi.NewDocumentWithConfiguration(newBits, docConfig)
294296
if err != nil {
295297
model.SendFatalError("building models", fmt.Sprintf("unable to create OpenAPI modified document: %s", err.Error()), errorChan)
@@ -303,7 +305,10 @@ func BuildCommitChangelog(commitHistory []*model.Commit,
303305
if oldDoc != nil {
304306
commitHistory[c].OldDocument = oldDoc
305307
}
306-
if commitHistory[c].Changes != nil {
308+
// Preserve the oldest entry as a sentinel when there is no prior version
309+
// or no changes, so left/right comparisons can still report "no changes"
310+
// instead of collapsing to an empty result set.
311+
if c == len(commitHistory)-1 || commitHistory[c].Changes != nil {
307312
cleaned = append(cleaned, commitHistory[c])
308313
}
309314
}

git/read_local_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
package git
55

66
import (
7+
"fmt"
8+
"os"
79
"testing"
10+
"time"
811

912
"github.com/pb33f/openapi-changes/model"
1013
"github.com/stretchr/testify/assert"
@@ -72,3 +75,40 @@ func TestReadFile(t *testing.T) {
7275
assert.NoError(t, err)
7376
assert.NotEmpty(t, contentRaw)
7477
}
78+
79+
func TestBuildCommitChangelog_IdenticalLeftRightPreservesSentinelCommit(t *testing.T) {
80+
specBytes := mustReadTestFile(t, "../sample-specs/petstorev3.json")
81+
progressChan := make(chan *model.ProgressUpdate, 32)
82+
errorChan := make(chan model.ProgressError, 32)
83+
84+
commits := []*model.Commit{
85+
{
86+
Hash: "right1",
87+
Message: "right",
88+
CommitDate: time.Now(),
89+
Data: specBytes,
90+
FilePath: "../sample-specs/petstorev3.json",
91+
},
92+
{
93+
Hash: "left01",
94+
Message: "left",
95+
CommitDate: time.Now(),
96+
Data: specBytes,
97+
FilePath: "../sample-specs/petstorev3.json",
98+
},
99+
}
100+
101+
cleaned, errs := BuildCommitChangelog(commits, progressChan, errorChan, "", true, false, nil)
102+
require.Empty(t, errs)
103+
require.Len(t, cleaned, 1)
104+
assert.Nil(t, cleaned[0].Changes)
105+
assert.NotNil(t, cleaned[0].Document)
106+
}
107+
108+
func mustReadTestFile(t *testing.T, path string) []byte {
109+
t.Helper()
110+
111+
bits, err := os.ReadFile(path)
112+
require.NoError(t, err, fmt.Sprintf("read test file %s", path))
113+
return bits
114+
}

0 commit comments

Comments
 (0)