Skip to content

Commit

Permalink
[#674] Add tests to assert source files are being reverted
Browse files Browse the repository at this point in the history
- Check whether a file contains a 'test' to know if it is a test file
- Added tests for different revert configuration of the relaxed TCR
  • Loading branch information
aatwi committed Sep 11, 2024
1 parent 5f7f35f commit c8cd131
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
62 changes: 60 additions & 2 deletions src/engine/tcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,48 @@ func Test_tcr_revert_end_state_with_commit_on_fail_enabled(t *testing.T) {
}
}

func Test_relaxed_source_revert(t *testing.T) {
tcr, vcsFake := initTCREngineWithFakes(nil, nil, nil, nil)
tcr.revert(*events.ATcrEvent())
assert.Equal(t, fake.RestoreCommand, vcsFake.GetLastCommand())
}

func Test_relaxed_source_reverts_only_source_files(t *testing.T) {
sniffer := report.NewSniffer(
func(msg report.Message) bool {
return msg.Type.Category == report.Warning &&
msg.Payload.ToString() == "1 file(s) reverted"
},
)
tcr, vcsFake := initTCREngineWithFakesWithFileDiffs(nil, nil, nil, nil,
vcs.FileDiffs{
vcs.NewFileDiff("fake-src", 1, 1),
vcs.NewFileDiff("fake-test", 1, 1),
})
tcr.revert(*events.ATcrEvent())
sniffer.Stop()
assert.Equal(t, fake.RestoreCommand, vcsFake.GetLastCommand())
assert.Equal(t, 1, sniffer.GetMatchCount())
}

func Test_relaxed_source_reverts_many_source_files(t *testing.T) {
sniffer := report.NewSniffer(
func(msg report.Message) bool {
return msg.Type.Category == report.Warning &&
msg.Payload.ToString() == "2 file(s) reverted"
},
)
tcr, vcsFake := initTCREngineWithFakesWithFileDiffs(nil, nil, nil, nil,
vcs.FileDiffs{
vcs.NewFileDiff("fake-src", 1, 1),
vcs.NewFileDiff("fake2-src", 1, 1),
})
tcr.revert(*events.ATcrEvent())
sniffer.Stop()
assert.Equal(t, fake.RestoreCommand, vcsFake.GetLastCommand())
assert.Equal(t, 1, sniffer.GetMatchCount())
}

func Test_tcr_cycle_end_state(t *testing.T) {
testFlags := []struct {
desc string
Expand Down Expand Up @@ -342,11 +384,12 @@ func Test_tcr_cycle_end_state(t *testing.T) {
}
}

func initTCREngineWithFakes(
func initTCREngineWithFakesWithFileDiffs(
p *params.Params,
toolchainFailures toolchain.Operations,
vcsFailures fake.Commands,
logItems vcs.LogItems,
fileDiffs vcs.FileDiffs,
) (*TCREngine, *fake.VCSFake) {
tchn := registerFakeToolchain(toolchainFailures)
lang := registerFakeLanguage(tchn)
Expand Down Expand Up @@ -381,7 +424,7 @@ func initTCREngineWithFakes(
factory.InitVCS = func(_ string, _ string) (vcs.Interface, error) {
fakeSettings := fake.Settings{
FailingCommands: vcsFailures,
ChangedFiles: vcs.FileDiffs{vcs.NewFileDiff("fake-src", 1, 1)},
ChangedFiles: fileDiffs,
Logs: logItems,
}
vcsFake = fake.NewVCSFake(fakeSettings)
Expand All @@ -393,6 +436,21 @@ func initTCREngineWithFakes(
tcr.fsWatchRearmDelay = 0
tcr.traceReporterWaitingTime = 0
return tcr, vcsFake

}

func initTCREngineWithFakes(
p *params.Params,
toolchainFailures toolchain.Operations,
vcsFailures fake.Commands,
logItems vcs.LogItems,
) (*TCREngine, *fake.VCSFake) {
return initTCREngineWithFakesWithFileDiffs(
p,
toolchainFailures,
vcsFailures,
logItems,
vcs.FileDiffs{vcs.NewFileDiff("fake-src", 1, 1)})
}

func registerFakeToolchain(failures toolchain.Operations) string {
Expand Down
9 changes: 6 additions & 3 deletions src/language/language_test_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ SOFTWARE.

package language

import "github.com/murex/tcr/toolchain"
import (
"github.com/murex/tcr/toolchain"
"strings"
)

type allFilesFunc func() ([]string, error)

Expand Down Expand Up @@ -84,8 +87,8 @@ func (fl *FakeLanguage) AllTestFiles() (result []string, err error) {
}

// IsSrcFile returns true if the provided filePath is recognized as a source file for this language
func (fl *FakeLanguage) IsSrcFile(_ string) bool {
return true
func (fl *FakeLanguage) IsSrcFile(name string) bool {
return !strings.Contains(name, "test")
}

// GetName uses real Language behaviour
Expand Down

0 comments on commit c8cd131

Please sign in to comment.