Skip to content

Commit

Permalink
[#674] Continue working on test case for introspective variant
Browse files Browse the repository at this point in the history
- add a list of received commands in VCS fake so that we can test that we got "commit && revert"
- add vcsfake.GetLastCommands(n) to get the tail of the list of last commands
- add vcsfake.VerifyLastCommandsAre() as an alternative approach
  • Loading branch information
mengdaming committed Oct 1, 2024
1 parent d65a768 commit d7516c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/engine/tcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,10 @@ func Test_introspective_variant(t *testing.T) {
})

tcr.revert(*events.ATcrEvent())
//sniffer.Stop()
assert.Equal(t, fake.RestoreCommand, vcsFake.GetLastCommand())
//assert.Equal(t, 1, sniffer.GetMatchCount())
assert.Equal(t, []fake.Command{fake.CommitCommand, fake.RevertCommand}, vcsFake.GetLastCommands(2))

//assert.True(t, vcsFake.VerifyLastCommandsAre(fake.CommitCommand, fake.RevertCommand),
// "got %v", vcsFake.GetLastCommands())

}

Expand Down
24 changes: 18 additions & 6 deletions src/vcs/fake/vcs_test_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package fake

import (
"errors"
"github.com/google/go-cmp/cmp"
"github.com/murex/tcr/vcs"
)

Expand Down Expand Up @@ -75,14 +76,14 @@ type (

// VCSFake provides a fake implementation of the VCS interface
VCSFake struct {
settings Settings
pushEnabled bool
lastCommand Command
settings Settings
pushEnabled bool
lastCommands []Command
}
)

func (vf *VCSFake) fakeCommand(cmd Command) (err error) {
vf.lastCommand = cmd
vf.lastCommands = append(vf.lastCommands, cmd)
if vf.settings.FailingCommands.contains(cmd) {
err = errors.New(vf.Name() + " " + string(cmd) + " error")
}
Expand All @@ -92,7 +93,7 @@ func (vf *VCSFake) fakeCommand(cmd Command) (err error) {
// NewVCSFake initializes a fake VCS implementation which does nothing
// apart from emulating errors on VCS operations
func NewVCSFake(settings Settings) *VCSFake {
return &VCSFake{settings: settings}
return &VCSFake{settings: settings, lastCommands: make([]Command, 0)}
}

// Name returns VCS name
Expand All @@ -107,7 +108,18 @@ func (vf *VCSFake) SessionSummary() string {

// GetLastCommand returns the last command called
func (vf *VCSFake) GetLastCommand() Command {
return vf.lastCommand
return vf.GetLastCommands(1)[0]
}

// GetLastCommands returns the last commands called
func (vf *VCSFake) GetLastCommands(count int) []Command {
return vf.lastCommands[len(vf.lastCommands)-count:]
}

// VerifyLastCommandsAre checks if last executed commands are the provided ones
func (vf *VCSFake) VerifyLastCommandsAre(expectedCommands ...Command) bool {
actual := vf.lastCommands[len(vf.lastCommands)-len(expectedCommands):]
return cmp.Equal(actual, expectedCommands)
}

// Add does nothing. Returns an error if in the list of failing commands
Expand Down

0 comments on commit d7516c0

Please sign in to comment.