Skip to content

Commit

Permalink
[#791] Add function to get latest submitted 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 changes
  • Loading branch information
philou authored and mengdaming committed Oct 11, 2024
1 parent 425781f commit 9421bd2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/vcs/p4/p4_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (

// Name provides the name for this VCS implementation
const Name = "p4"
const p4TestClientName = "test"

// p4Impl provides the implementation of the Perforce interface
type p4Impl struct {
Expand Down Expand Up @@ -71,7 +72,7 @@ func newP4Impl(initDepotFs func() afero.Fs, dir string, testFlag bool) (*p4Impl,

if testFlag {
// For test purpose only: tests should run and pass without having p4 installed and with no p4 server available
p.clientName = "test"
p.clientName = p4TestClientName
p.rootDir = dir
} else {
p.clientName = GetP4ClientName()
Expand Down Expand Up @@ -349,3 +350,17 @@ func (p *p4Impl) toP4ClientPath(dir string) (string, error) {
}
return "//" + p.clientName + slashedPath + "...", nil
}

func (p *p4Impl) getLatestChangelistId() (*changeList, error) {
p4Output, err := p.runP4("changes", "-m1", p.clientName, "-s", "submitted")
if err != nil {
return nil, err
}

fields := strings.Split(string(p4Output), " ")
if len(fields) < 2 {
return nil, errors.New("unexpected output from `p4 changes`: " + string(p4Output))
}

return &changeList{fields[1]}, nil
}
57 changes: 56 additions & 1 deletion src/vcs/p4/p4_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Test_get_vcs_name(t *testing.T) {

func Test_get_vcs_session_summary(t *testing.T) {
p, _ := newP4Impl(inMemoryDepotInit, "", true)
assert.Equal(t, "p4 client \"test\"", p.SessionSummary())
assert.Equal(t, "p4 client \""+p4TestClientName+"\"", p.SessionSummary())
}

func Test_p4_auto_push_is_always_enabled(t *testing.T) {
Expand Down Expand Up @@ -598,6 +598,61 @@ func Test_p4_rollback_last_commit(t *testing.T) {
}
}

func Test_p4_get_latest_changelist_id(t *testing.T) {
testFlags := []struct {
desc string
p4ChangesOutput string
p4ChangesError error
expectError bool
expectedArgs []string
expectedChangelistId *changeList
}{
{"p4 changes working case",
"Change 7297330 on 2024/10/01 by pbourgau@pbourgau_LPBOU05_8775 '✅ TCR - tests passing cha'",
nil,
false,
[]string{"changes", "-m1", p4TestClientName, "-s", "submitted"},
&changeList{"7297330"},
},
{"p4 changes return an error",
"Change 7297330 on 2024/10/01 by pbourgau@pbourgau_LPBOU05_8775 '✅ TCR - tests passing cha'",
errors.New("p4 changes error"),
true,
nil,
nil,
},
{"p4 changes return an unexpectedly formed output",
"Single_word_output",
nil,
true,
nil,
nil,
},
}

for _, tt := range testFlags {
t.Run(tt.desc, func(t *testing.T) {
var actualArgs []string
p, _ := newP4Impl(inMemoryDepotInit, "", true)
p.rootDir = ""
p.runP4Function = func(args ...string) (output []byte, err error) {
actualArgs = args[4:]
return []byte(tt.p4ChangesOutput), tt.p4ChangesError
}
actualChangelistId, err := p.getLatestChangelistId()
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.expectedChangelistId, actualChangelistId)
})
}
}

func Test_convert_to_p4_client_path(t *testing.T) {
testFlags := []struct {
desc string
Expand Down

0 comments on commit 9421bd2

Please sign in to comment.