Skip to content

Commit 83d32dd

Browse files
committed
[#200] Add a few edge test case in vcs/p4 package
1 parent 4f5104e commit 83d32dd

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

src/vcs/p4/p4_impl.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,12 @@ func newP4Impl(initDepotFs func() afero.Fs, dir string, testFlag bool) (*p4Impl,
6666
tracePipedP4Function: tracePipedP4Command,
6767
}
6868

69-
// For test purpose only: tests should run and pass without p4 installed and no p4 server available
7069
if testFlag {
70+
// For test purpose only: tests should run and pass without having p4 installed and with no p4 server available
7171
p.clientName = ""
7272
p.rootDir = dir
7373
} else {
7474
p.clientName = GetP4ClientName()
75-
7675
var err error
7776
p.rootDir, err = GetP4RootDir()
7877
if err != nil {
@@ -317,7 +316,7 @@ func convertLine(charMap *charmap.Charmap, message string) string {
317316
func (p *p4Impl) submitChangeList(cl *changeList) error {
318317
if cl == nil {
319318
report.PostWarning("Empty changelist!")
320-
return nil
319+
return errors.New("empty p4 changelist")
321320
}
322321
return p.traceP4("submit", "-c", cl.number)
323322
}

src/vcs/p4/p4_impl_test.go

+58-1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,15 @@ func Test_p4_pull(t *testing.T) {
284284
true,
285285
[]string{"sync", "//test_client/base_dir/..."},
286286
},
287+
{
288+
"base directory is empty",
289+
filepath.FromSlash("/p4root"),
290+
"",
291+
"test_client",
292+
nil,
293+
true,
294+
nil,
295+
},
287296
}
288297
for _, tt := range testFlags {
289298
t.Run(tt.desc, func(t *testing.T) {
@@ -443,6 +452,54 @@ func Test_p4_commit(t *testing.T) {
443452
}
444453
}
445454

455+
func Test_p4_submit(t *testing.T) {
456+
testFlags := []struct {
457+
desc string
458+
p4Changelist *changeList
459+
p4SubmitError error
460+
p4SubmitExpectedArgs []string
461+
expectError bool
462+
}{
463+
{
464+
"p4 submit command call succeeds",
465+
&changeList{number: "1234567"},
466+
nil, []string{"submit", "-c", "1234567"},
467+
false,
468+
},
469+
{
470+
"p4 submit command call fails",
471+
&changeList{number: "1234567"},
472+
errors.New("p4 submit error"), []string{"submit", "-c", "1234567"},
473+
true,
474+
},
475+
{
476+
"empty changelist",
477+
nil,
478+
errors.New("p4 submit error"), nil,
479+
true,
480+
},
481+
}
482+
for _, tt := range testFlags {
483+
t.Run(tt.desc, func(t *testing.T) {
484+
var p4SubmitActualArgs []string
485+
p, _ := newP4Impl(inMemoryDepotInit, "", true)
486+
p.traceP4Function = func(args ...string) (err error) {
487+
// Stub for the call to "p4 submit -c <cl_number>"
488+
p4SubmitActualArgs = args[4:]
489+
return tt.p4SubmitError
490+
}
491+
492+
err := p.submitChangeList(tt.p4Changelist)
493+
if tt.expectError {
494+
assert.Error(t, err)
495+
} else {
496+
assert.NoError(t, err)
497+
}
498+
assert.Equal(t, tt.p4SubmitExpectedArgs, p4SubmitActualArgs)
499+
})
500+
}
501+
}
502+
446503
func Test_convert_to_p4_client_path(t *testing.T) {
447504
testFlags := []struct {
448505
desc string
@@ -496,7 +553,7 @@ func Test_convert_to_p4_client_path(t *testing.T) {
496553
"Dir outside the root directory",
497554
"D:\\p4root",
498555
"test_client",
499-
"D:\\somewhereelse\\sub_dir",
556+
"D:\\somewhere_else\\sub_dir",
500557
errors.New("path is outside p4 root directory"),
501558
"",
502559
},

0 commit comments

Comments
 (0)