diff --git a/cmd/console.go b/cmd/console.go index ecd2c03..9dc1c56 100644 --- a/cmd/console.go +++ b/cmd/console.go @@ -408,12 +408,14 @@ func runLeftRightCompare(left, right string, updateChan chan *model.ProgressUpda Message: fmt.Sprintf("Original: %s, Modified: %s", left, right), CommitDate: time.Now(), Data: rightBytes, + FilePath: right, }, { Hash: uuid.New().String()[:6], Message: fmt.Sprintf("Original file: %s", left), CommitDate: time.Now(), Data: leftBytes, + FilePath: left, }, } diff --git a/cmd/left_right_test.go b/cmd/left_right_test.go new file mode 100644 index 0000000..ea544c7 --- /dev/null +++ b/cmd/left_right_test.go @@ -0,0 +1,30 @@ +package cmd + +import ( + "testing" + + "github.com/pb33f/openapi-changes/model" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestRunLeftRightSummary_IdenticalSpecsReturnsNoError(t *testing.T) { + updateChan := make(chan *model.ProgressUpdate, 32) + errorChan := make(chan model.ProgressError, 32) + + errs := runLeftRightSummary("../sample-specs/petstorev3.json", "../sample-specs/petstorev3.json", + updateChan, errorChan, "", true, false, false, false, nil) + + require.Empty(t, errs) +} + +func TestRunLeftRightReport_IdenticalSpecsReturnsNilReport(t *testing.T) { + updateChan := make(chan *model.ProgressUpdate, 32) + errorChan := make(chan model.ProgressError, 32) + + report, errs := runLeftRightReport("../sample-specs/petstorev3.json", "../sample-specs/petstorev3.json", + updateChan, errorChan, "", true, false, nil) + + require.Empty(t, errs) + assert.Nil(t, report) +} diff --git a/cmd/report.go b/cmd/report.go index 9a64ece..92d3da2 100644 --- a/cmd/report.go +++ b/cmd/report.go @@ -371,12 +371,14 @@ func runLeftRightReport(left, right string, Message: fmt.Sprintf("Original: %s, Modified: %s, ", left, right), CommitDate: time.Now(), Data: rightBytes, + FilePath: right, }, { Hash: uuid.New().String()[:6], Message: fmt.Sprintf("Original file: %s", left), CommitDate: time.Now(), Data: leftBytes, + FilePath: left, }, } @@ -386,6 +388,9 @@ func runLeftRightReport(left, right string, if len(errs) > 0 { return nil, errs } + if len(commits) == 0 { + return nil, nil + } if commits[0].Changes == nil { return nil, nil } diff --git a/cmd/summary.go b/cmd/summary.go index 09b2e02..eb7b384 100644 --- a/cmd/summary.go +++ b/cmd/summary.go @@ -363,12 +363,14 @@ func runLeftRightSummary(left, right string, updateChan chan *model.ProgressUpda Message: fmt.Sprintf("Original: %s, Modified: %s, ", left, right), CommitDate: time.Now(), Data: rightBytes, + FilePath: right, }, { Hash: uuid.New().String()[:6], Message: fmt.Sprintf("Original file: %s", left), CommitDate: time.Now(), Data: leftBytes, + FilePath: left, }, } diff --git a/git/read_local.go b/git/read_local.go index 9c3d932..291e54e 100644 --- a/git/read_local.go +++ b/git/read_local.go @@ -287,9 +287,11 @@ func BuildCommitChangelog(commitHistory []*model.Commit, } } if len(oldBits) == 0 && len(newBits) > 0 { - model.SendProgressWarning("building models", - fmt.Sprintf("Commit %s is the first version of '%s' — no prior version to compare against, skipping", - commitHistory[c].Hash, commitHistory[c].FilePath), progressChan) + if commitHistory[c].RepoDirectory != "" { + model.SendProgressWarning("building models", + fmt.Sprintf("Commit %s is the first version of '%s' — no prior version to compare against, skipping", + commitHistory[c].Hash, commitHistory[c].FilePath), progressChan) + } newDoc, err = libopenapi.NewDocumentWithConfiguration(newBits, docConfig) if err != nil { 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, if oldDoc != nil { commitHistory[c].OldDocument = oldDoc } - if commitHistory[c].Changes != nil { + // Preserve the oldest entry as a sentinel when there is no prior version + // or no changes, so left/right comparisons can still report "no changes" + // instead of collapsing to an empty result set. + if c == len(commitHistory)-1 || commitHistory[c].Changes != nil { cleaned = append(cleaned, commitHistory[c]) } } diff --git a/git/read_local_test.go b/git/read_local_test.go index fa7f0dd..26ba162 100644 --- a/git/read_local_test.go +++ b/git/read_local_test.go @@ -4,7 +4,10 @@ package git import ( + "fmt" + "os" "testing" + "time" "github.com/pb33f/openapi-changes/model" "github.com/stretchr/testify/assert" @@ -72,3 +75,40 @@ func TestReadFile(t *testing.T) { assert.NoError(t, err) assert.NotEmpty(t, contentRaw) } + +func TestBuildCommitChangelog_IdenticalLeftRightPreservesSentinelCommit(t *testing.T) { + specBytes := mustReadTestFile(t, "../sample-specs/petstorev3.json") + progressChan := make(chan *model.ProgressUpdate, 32) + errorChan := make(chan model.ProgressError, 32) + + commits := []*model.Commit{ + { + Hash: "right1", + Message: "right", + CommitDate: time.Now(), + Data: specBytes, + FilePath: "../sample-specs/petstorev3.json", + }, + { + Hash: "left01", + Message: "left", + CommitDate: time.Now(), + Data: specBytes, + FilePath: "../sample-specs/petstorev3.json", + }, + } + + cleaned, errs := BuildCommitChangelog(commits, progressChan, errorChan, "", true, false, nil) + require.Empty(t, errs) + require.Len(t, cleaned, 1) + assert.Nil(t, cleaned[0].Changes) + assert.NotNil(t, cleaned[0].Document) +} + +func mustReadTestFile(t *testing.T, path string) []byte { + t.Helper() + + bits, err := os.ReadFile(path) + require.NoError(t, err, fmt.Sprintf("read test file %s", path)) + return bits +}