Skip to content

Commit

Permalink
[#674] Implement revert for BTCR variant
Browse files Browse the repository at this point in the history
- Add tests
- Inline revertSrc() file into revert()
- Extract shouldRevertFile()
- Extract noFilesRevertedMessage()
  • Loading branch information
mengdaming committed Sep 11, 2024
1 parent 42a6521 commit c1aac0a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
56 changes: 32 additions & 24 deletions src/engine/tcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,38 @@ func (tcr *TCREngine) revert(event events.TCREvent) {
}
tcr.handleError(tcr.vcsPushAuto(), false, status.VCSError)
}
tcr.revertSrcFiles()

diffs, err := tcr.vcs.Diff()
tcr.handleError(err, false, status.VCSError)
if err != nil {
return
}
var reverted int
for _, diff := range diffs {
if tcr.shouldRevertFile(diff.Path) {
err := tcr.revertFile(diff.Path)
tcr.handleError(err, false, status.VCSError)
if err == nil {
reverted++
}
}
}
if reverted > 0 {
report.PostWarning(reverted, " file(s) reverted")
} else {
report.PostInfo(tcr.noFilesRevertedMessage())
}
}

func (tcr *TCREngine) noFilesRevertedMessage() string {
if tcr.variant == variant.Relaxed {
return "No file reverted (only test files were updated since last commit)"
}
return "No file reverted"
}

func (tcr *TCREngine) shouldRevertFile(path string) bool {
return tcr.variant == variant.BTCR || tcr.language.IsSrcFile(path)
}

func (tcr *TCREngine) commitTestBreakingChanges(event events.TCREvent) (err error) {
Expand Down Expand Up @@ -647,29 +678,6 @@ func (tcr *TCREngine) commitTestBreakingChanges(event events.TCREvent) (err erro
return err
}

func (tcr *TCREngine) revertSrcFiles() {
diffs, err := tcr.vcs.Diff()
tcr.handleError(err, false, status.VCSError)
if err != nil {
return
}
var reverted int
for _, diff := range diffs {
if tcr.language.IsSrcFile(diff.Path) {
err := tcr.revertFile(diff.Path)
tcr.handleError(err, false, status.VCSError)
if err == nil {
reverted++
}
}
}
if reverted > 0 {
report.PostWarning(reverted, " file(s) reverted")
} else {
report.PostInfo("No file reverted (only test files were updated since last commit)")
}
}

func (tcr *TCREngine) revertFile(file string) error {
return tcr.vcs.Restore(file)
}
Expand Down
30 changes: 27 additions & 3 deletions src/engine/tcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,40 @@ func Test_relaxed_doesnt_revert_test_files(t *testing.T) {
func Test_relaxed_reverts(t *testing.T) {
testFlags := []struct {
description string
variant variant.Variant
fileDiffs vcs.FileDiffs
expectedRevertCount int
}{
{
description: "One source file and one test file",
variant: variant.Relaxed,
fileDiffs: vcs.FileDiffs{
vcs.NewFileDiff("fake-src", 1, 1),
vcs.NewFileDiff("fake-test", 1, 1),
},
expectedRevertCount: 1,
}, {
},
{
description: "Two source files and no test files",
variant: variant.Relaxed,
fileDiffs: vcs.FileDiffs{
vcs.NewFileDiff("fake-src", 1, 1),
vcs.NewFileDiff("fake2-src", 1, 1),
},
expectedRevertCount: 2,
},
{
description: "One source file and one test file",
variant: variant.BTCR,
fileDiffs: vcs.FileDiffs{
vcs.NewFileDiff("fake-src", 1, 1),
vcs.NewFileDiff("fake-test", 1, 1),
},
expectedRevertCount: 2,
},
{
description: "Two source files and no test files",
variant: variant.BTCR,
fileDiffs: vcs.FileDiffs{
vcs.NewFileDiff("fake-src", 1, 1),
vcs.NewFileDiff("fake2-src", 1, 1),
Expand All @@ -320,14 +342,16 @@ func Test_relaxed_reverts(t *testing.T) {
}

for _, tt := range testFlags {
t.Run(tt.description, func(t *testing.T) {
t.Run(tt.description+" in variant "+string(tt.variant), func(t *testing.T) {
sniffer := report.NewSniffer(
func(msg report.Message) bool {
return msg.Type.Category == report.Warning &&
msg.Payload.ToString() == fmt.Sprintf("%d file(s) reverted", tt.expectedRevertCount)
},
)
tcr, vcsFake := initTCREngineWithFakesWithFileDiffs(nil, nil, nil, nil, tt.fileDiffs)
tcr, vcsFake := initTCREngineWithFakesWithFileDiffs(
params.AParamSet(params.WithVariant(tt.variant)),
nil, nil, nil, tt.fileDiffs)
tcr.revert(*events.ATcrEvent())
sniffer.Stop()
assert.Equal(t, fake.RestoreCommand, vcsFake.GetLastCommand())
Expand Down

0 comments on commit c1aac0a

Please sign in to comment.