Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down
30 changes: 30 additions & 0 deletions cmd/left_right_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 5 additions & 0 deletions cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down
13 changes: 9 additions & 4 deletions git/read_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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])
}
}
Expand Down
40 changes: 40 additions & 0 deletions git/read_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package git

import (
"fmt"
"os"
"testing"
"time"

"github.com/pb33f/openapi-changes/model"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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
}
Loading