Skip to content

Commit

Permalink
[#200] Make file_diff and log_item VCS-generic
Browse files Browse the repository at this point in the history
  • Loading branch information
mengdaming committed Jan 2, 2023
1 parent 24cee3e commit e53d869
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/engine/tcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@ func (tcr *TcrEngine) PrintStats(p params.Params) {
stats.Print(tcr.vcs.GetWorkingBranch(), tcrLogsToEvents(tcrLogs))
}

func tcrLogsToEvents(tcrLogs vcs.GitLogItems) (tcrEvents events.TcrEvents) {
func tcrLogsToEvents(tcrLogs vcs.LogItems) (tcrEvents events.TcrEvents) {
tcrEvents = *events.NewTcrEvents()
for _, log := range tcrLogs {
tcrEvents.Add(log.Timestamp, parseCommitMessage(log.Message))
}
return tcrEvents
}

func (tcr *TcrEngine) queryGitLogs(p params.Params) vcs.GitLogItems {
func (tcr *TcrEngine) queryGitLogs(p params.Params) vcs.LogItems {
tcr.initSourceTree(p)
tcr.initVcs()

Expand Down
16 changes: 8 additions & 8 deletions src/engine/tcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func Test_tcr_cycle_end_state(t *testing.T) {
}
}

func initTcrEngineWithFakes(p *params.Params, toolchainFailures toolchain.Operations, gitFailures git.Commands, gitLogItems vcs.GitLogItems) (TcrInterface, *git.Fake) {
func initTcrEngineWithFakes(p *params.Params, toolchainFailures toolchain.Operations, gitFailures git.Commands, gitLogItems vcs.LogItems) (TcrInterface, *git.Fake) {
tchn := registerFakeToolchain(toolchainFailures)
lang := registerFakeLanguage(tchn)

Expand Down Expand Up @@ -385,7 +385,7 @@ func registerFakeLanguage(toolchainName string) string {
return fake.GetName()
}

func replaceGitImplWithFake(tcr TcrInterface, failures git.Commands, gitLogItems vcs.GitLogItems) *git.Fake {
func replaceGitImplWithFake(tcr TcrInterface, failures git.Commands, gitLogItems vcs.LogItems) *git.Fake {
fakeSettings := git.FakeSettings{
FailingCommands: failures,
ChangedFiles: vcs.FileDiffs{vcs.NewFileDiff("fake-src", 1, 1)},
Expand Down Expand Up @@ -560,16 +560,16 @@ func Test_mob_timer_should_not_start_in_solo_mode(t *testing.T) {

func Test_tcr_print_log(t *testing.T) {
now := time.Now()
sampleItems := vcs.GitLogItems{
vcs.NewGitLogItem("1111", now, "✅ TCR - tests passing"),
vcs.NewGitLogItem("2222", now, "❌ TCR - tests failing"),
vcs.NewGitLogItem("3333", now, "⏪ TCR - revert changes"),
vcs.NewGitLogItem("4444", now, "other commit message"),
sampleItems := vcs.LogItems{
vcs.NewLogItem("1111", now, "✅ TCR - tests passing"),
vcs.NewLogItem("2222", now, "❌ TCR - tests failing"),
vcs.NewLogItem("3333", now, "⏪ TCR - revert changes"),
vcs.NewLogItem("4444", now, "other commit message"),
}
testFlags := []struct {
desc string
filter func(msg report.Message) bool
gitLogItems vcs.GitLogItems
gitLogItems vcs.LogItems
expectedMatches int
}{
{
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/vcs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type GitInterface interface {
Stash(message string) error
UnStash(keep bool) error
Diff() (diffs FileDiffs, err error)
Log(msgFilter func(msg string) bool) (logs GitLogItems, err error)
Log(msgFilter func(msg string) bool) (logs LogItems, err error)
EnablePush(flag bool)
IsPushEnabled() bool
IsRemoteEnabled() bool
Expand Down
4 changes: 2 additions & 2 deletions src/vcs/git/git_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (g *gitImpl) Diff() (diffs vcs.FileDiffs, err error) {
// Log returns the list of git log items compliant with the provided msgFilter.
// When no msgFilter is provided, returns all git log items unfiltered.
// Current implementation uses go-git's Log() function
func (g *gitImpl) Log(msgFilter func(msg string) bool) (logs vcs.GitLogItems, err error) {
func (g *gitImpl) Log(msgFilter func(msg string) bool) (logs vcs.LogItems, err error) {
plainOpenOptions := git.PlainOpenOptions{
DetectDotGit: true,
EnableDotGitCommonDir: false,
Expand All @@ -333,7 +333,7 @@ func (g *gitImpl) Log(msgFilter func(msg string) bool) (logs vcs.GitLogItems, er
}
_ = cIter.ForEach(func(c *object.Commit) error {
if msgFilter == nil || msgFilter(c.Message) {
logs.Add(vcs.NewGitLogItem(c.Hash.String(), c.Committer.When.UTC(), c.Message))
logs.Add(vcs.NewLogItem(c.Hash.String(), c.Committer.When.UTC(), c.Message))
}
return nil
})
Expand Down
10 changes: 5 additions & 5 deletions src/vcs/git/git_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ func Test_git_unstash_command(t *testing.T) {

func Test_git_log_command(t *testing.T) {
// Note: this test may break if for any reason the TCR repository initial commit is altered
tcrInitialCommit := vcs.GitLogItem{
tcrInitialCommit := vcs.LogItem{
Hash: "a823c098187455bade90ee44874d2b41c7ef96d9",
Timestamp: time.Date(2021, time.June, 16, 15, 29, 41, 0, time.UTC),
Message: "Initial commit",
Expand All @@ -609,26 +609,26 @@ func Test_git_log_command(t *testing.T) {
testFlags := []struct {
desc string
filter func(msg string) bool
asserter func(t *testing.T, items vcs.GitLogItems)
asserter func(t *testing.T, items vcs.LogItems)
}{
{
"filter matching no item",
func(_ string) bool { return false },
func(t *testing.T, items vcs.GitLogItems) {
func(t *testing.T, items vcs.LogItems) {
assert.Equal(t, 0, items.Len())
},
},
{
"filter matching all items",
nil,
func(t *testing.T, items vcs.GitLogItems) {
func(t *testing.T, items vcs.LogItems) {
assert.Greater(t, items.Len(), 100)
},
},
{
"filter matching one single item",
func(msg string) bool { return tcrInitialCommit.Message == msg },
func(t *testing.T, items vcs.GitLogItems) {
func(t *testing.T, items vcs.LogItems) {
assert.Equal(t, 1, items.Len())
assert.Equal(t, tcrInitialCommit, items[0])
},
Expand Down
4 changes: 2 additions & 2 deletions src/vcs/git/git_test_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type (
FakeSettings struct {
FailingCommands Commands
ChangedFiles vcs.FileDiffs
Logs vcs.GitLogItems
Logs vcs.LogItems
}

// Fake provides a fake implementation of the git interface
Expand Down Expand Up @@ -138,7 +138,7 @@ func (gf *Fake) Diff() (_ vcs.FileDiffs, err error) {
}

// Log returns the list of git logs configured at fake initialization
func (gf *Fake) Log(msgFilter func(msg string) bool) (logs vcs.GitLogItems, err error) {
func (gf *Fake) Log(msgFilter func(msg string) bool) (logs vcs.LogItems, err error) {
err = gf.fakeCommand(LogCommand)

if msgFilter == nil {
Expand Down
22 changes: 11 additions & 11 deletions src/vcs/git_log_item.go → src/vcs/vcs_log_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,34 @@ import (
)

type (
// GitLogItem contains git log information for a commit
GitLogItem struct {
// LogItem contains VCS log information for a commit
LogItem struct {
Hash string
Timestamp time.Time
Message string
}

// GitLogItems contains a set of git log items in a slice
GitLogItems []GitLogItem
// LogItems contains a set of VCS log items in a slice
LogItems []LogItem
)

// NewGitLogItem creates a new git log item instance
func NewGitLogItem(hash string, timestamp time.Time, message string) GitLogItem {
return GitLogItem{hash, timestamp, message}
// NewLogItem creates a new git log item instance
func NewLogItem(hash string, timestamp time.Time, message string) LogItem {
return LogItem{hash, timestamp, message}
}

func (items *GitLogItems) sortByDate() {
func (items *LogItems) sortByDate() {
sort.Slice(*items, func(i, j int) bool {
return (*items)[i].Timestamp.Before((*items)[j].Timestamp)
})
}

// Add adds a GitLogItem to the GitLogItems collection
func (items *GitLogItems) Add(d GitLogItem) {
// Add adds a LogItem to the LogItems collection
func (items *LogItems) Add(d LogItem) {
*items = append(*items, d)
}

// Len returns the length of the items array
func (items *GitLogItems) Len() int {
func (items *LogItems) Len() int {
return len(*items)
}
36 changes: 18 additions & 18 deletions src/vcs/git_log_item_test.go → src/vcs/vcs_log_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,41 @@ import (
"time"
)

func Test_add_regular_git_log_item(t *testing.T) {
var items GitLogItems
item := NewGitLogItem("xxx", time.Now(), "some message")
func Test_add_regular_log_item(t *testing.T) {
var items LogItems
item := NewLogItem("xxx", time.Now(), "some message")
items.Add(item)
assert.Len(t, items, 1)
assert.Contains(t, items, item)
}

func Test_add_empty_git_log_item(t *testing.T) {
var items GitLogItems
item := NewGitLogItem("", time.Time{}, "")
func Test_add_empty_log_item(t *testing.T) {
var items LogItems
item := NewLogItem("", time.Time{}, "")
items.Add(item)
assert.Len(t, items, 1)
}

func Test_sort_empty_git_log_items_does_not_panic(t *testing.T) {
var items GitLogItems
func Test_sort_empty_log_items_does_not_panic(t *testing.T) {
var items LogItems
assert.NotPanics(t, func() {
items.sortByDate()
})
assert.Len(t, items, 0)
}

func Test_sort_already_sorted_git_log_items(t *testing.T) {
item1 := NewGitLogItem("xxx1", time.Now(), "first commit")
item2 := NewGitLogItem("xxx2", time.Now().Add(1*time.Second), "second commit")
items := GitLogItems{item1, item2}
func Test_sort_already_sorted_log_items(t *testing.T) {
item1 := NewLogItem("xxx1", time.Now(), "first commit")
item2 := NewLogItem("xxx2", time.Now().Add(1*time.Second), "second commit")
items := LogItems{item1, item2}
items.sortByDate()
assert.Equal(t, GitLogItems{item1, item2}, items)
assert.Equal(t, LogItems{item1, item2}, items)
}

func Test_sort_unsorted_git_log_items(t *testing.T) {
item1 := NewGitLogItem("xxx1", time.Now(), "first commit")
item2 := NewGitLogItem("xxx2", time.Now().Add(1*time.Second), "second commit")
items := GitLogItems{item2, item1}
func Test_sort_unsorted_log_items(t *testing.T) {
item1 := NewLogItem("xxx1", time.Now(), "first commit")
item2 := NewLogItem("xxx2", time.Now().Add(1*time.Second), "second commit")
items := LogItems{item2, item1}
items.sortByDate()
assert.Equal(t, GitLogItems{item1, item2}, items)
assert.Equal(t, LogItems{item1, item2}, items)
}

0 comments on commit e53d869

Please sign in to comment.