Skip to content

Commit

Permalink
[#791] Add function to undo a changelist from Perforce
Browse files Browse the repository at this point in the history
To support Introspective variant on perforce
- add 3 tests with error cases
- implement using p4 undo
- prepare test case on p4 RollbackLastCommit() (wip)
  • Loading branch information
mengdaming committed Oct 4, 2024
1 parent 2a2cffe commit edefb5a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 32 deletions.
7 changes: 6 additions & 1 deletion src/vcs/p4/p4_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (p *p4Impl) toP4ClientPath(dir string) (string, error) {
return "//" + p.clientName + slashedPath + "...", nil
}

func (p *p4Impl) getLatestChangelistId() (*changeList, error) {
func (p *p4Impl) getLatestChangelist() (*changeList, error) {
p4Output, err := p.runP4("changes", "-m1", "@"+p.clientName, "-s", "submitted")
if err != nil {
return nil, err
Expand All @@ -364,3 +364,8 @@ func (p *p4Impl) getLatestChangelistId() (*changeList, error) {

return &changeList{fields[1]}, nil
}

func (p *p4Impl) undoChangelist(changelist changeList) error {
err := p.traceP4("undo", fmt.Sprintf("@%v,@%v", changelist.number, changelist.number))
return err
}
102 changes: 71 additions & 31 deletions src/vcs/p4/p4_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,52 +560,46 @@ func Test_p4_rollback_last_commit(t *testing.T) {
t.Skip("Work in progress")

testFlags := []struct {
desc string
p4Error error
expectError bool
expectedArgs []string
desc string
p4ChangesOutput []byte
p4ChangesError error
p4UndoError error
expectedError error
}{
{
"p4 undo command call succeeds",
"p4 changes and p4 undo happy path",
[]byte("Change 7297330"),
nil,
nil,
nil,
false,
[]string{"undo", "--no-gpg-sign", "--no-edit", "--no-commit", "HEAD"},
},
{
"git revert command call fails",
errors.New("git revert error"),
true,
[]string{"revert", "--no-gpg-sign", "--no-edit", "--no-commit", "HEAD"},
},
}
for _, tt := range testFlags {
t.Run(tt.desc, func(t *testing.T) {
var actualArgs []string
p, _ := newP4Impl(inMemoryDepotInit, "", true)
// Stubs the p4 changes command call
p.runP4Function = func(args ...string) (out []byte, err error) {
return tt.p4ChangesOutput, tt.p4ChangesError
}
// Stubs the p4 undo command
p.traceP4Function = func(args ...string) (err error) {
actualArgs = args[2:]
return tt.p4Error
return tt.p4UndoError
}

err := p.RollbackLastCommit()
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.expectedArgs, actualArgs)
assert.Equal(t, tt.expectedError, err)
})
}
}

func Test_p4_get_latest_changelist_id(t *testing.T) {
func Test_p4_get_latest_changelist(t *testing.T) {
testFlags := []struct {
desc string
p4ChangesOutput string
p4ChangesError error
expectError bool
expectedArgs []string
expectedChangelistId *changeList
desc string
p4ChangesOutput string
p4ChangesError error
expectError bool
expectedArgs []string
expectedChangelist *changeList
}{
{"p4 changes working case",
"Change 7297330 on 2024/10/01 by pbourgau@pbourgau_LPBOU05_8775 '✅ TCR - tests passing cha'",
Expand Down Expand Up @@ -639,7 +633,54 @@ func Test_p4_get_latest_changelist_id(t *testing.T) {
actualArgs = args[4:]
return []byte(tt.p4ChangesOutput), tt.p4ChangesError
}
actualChangelistId, err := p.getLatestChangelistId()
actualChangelist, err := p.getLatestChangelist()
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
if tt.expectedArgs != nil {
assert.Equal(t, tt.expectedArgs, actualArgs)
}
assert.Equal(t, tt.expectedChangelist, actualChangelist)
})
}
}

func Test_p4_undo_changelist(t *testing.T) {
testFlags := []struct {
desc string
changeListToUndo *changeList
p4UndoError error
expectError bool
expectedArgs []string
}{
{
"p4 changes working case",
&changeList{number: "12345"},
nil,
false,
[]string{"undo", "@12345,@12345"},
},
{
"p4 undo returns an error",
&changeList{number: "12345"},
errors.New("p4 undo failed"),
true,
nil,
},
}

for _, tt := range testFlags {
t.Run(tt.desc, func(t *testing.T) {
var actualArgs []string
p, _ := newP4Impl(inMemoryDepotInit, "", true)
p.rootDir = ""
p.traceP4Function = func(args ...string) (err error) {
actualArgs = args[4:]
return tt.p4UndoError
}
err := p.undoChangelist(*tt.changeListToUndo)
if tt.expectError {
assert.Error(t, err)
} else {
Expand All @@ -648,7 +689,6 @@ func Test_p4_get_latest_changelist_id(t *testing.T) {
if tt.expectedArgs != nil {
assert.Equal(t, tt.expectedArgs, actualArgs)
}
assert.Equal(t, tt.expectedChangelistId, actualChangelistId)
})
}
}
Expand Down

0 comments on commit edefb5a

Please sign in to comment.