-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Evidence CLI - update fields names #18
Changes from all commits
b67f47b
d9c3eed
bf36c53
d903e1d
fb52301
dcfacf1
709f5be
6b71910
3249c71
afe2025
938356e
e347df1
e3e5049
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,9 @@ GOARCH = $(shell go env GOARCH) | |
# ---------------------------------------------------------------------------------------------------------------------- | ||
export PROJECT_DIR = $(CURDIR) | ||
|
||
prereq: | ||
$(GOCMD) install go.uber.org/mock/[email protected] | ||
|
||
clean-mock: | ||
@find . -name "*_mock.go" -delete | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,12 +26,10 @@ func GetCommands() []components.Command { | |
} | ||
} | ||
|
||
var execFunc = func(command commands.Command) error { | ||
return commands.Exec(command) | ||
} | ||
var execFunc = commands.Exec | ||
|
||
func createEvidence(c *components.Context) error { | ||
if err := validateCreateEvidenceContext(c); err != nil { | ||
if err := validateCreateEvidenceCommonContext(c); err != nil { | ||
return err | ||
} | ||
subject, err := getAndValidateSubject(c) | ||
|
@@ -45,20 +43,20 @@ func createEvidence(c *components.Context) error { | |
|
||
var command EvidenceCommands | ||
switch subject { | ||
case repoPath: | ||
case subjectRepoPath: | ||
command = NewEvidenceCustomCommand(c, execFunc) | ||
case releaseBundle: | ||
command = NewEvidenceReleaseBundleCommand(c, execFunc) | ||
case build: | ||
case buildName: | ||
command = NewEvidenceBuildCommand(c, execFunc) | ||
default: | ||
return errors.New("unsupported subject") | ||
} | ||
|
||
return command.CreateEvidence(serverDetails) | ||
return command.CreateEvidence(c, serverDetails) | ||
} | ||
|
||
func validateCreateEvidenceContext(c *components.Context) error { | ||
func validateCreateEvidenceCommonContext(c *components.Context) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here c -> ctx There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Multiple places in this file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
if show, err := pluginsCommon.ShowCmdHelpIfNeeded(c, c.Arguments); show || err != nil { | ||
return err | ||
} | ||
|
@@ -68,13 +66,13 @@ func validateCreateEvidenceContext(c *components.Context) error { | |
} | ||
|
||
if !c.IsFlagSet(predicate) || assertValueProvided(c, predicate) != nil { | ||
return errorutils.CheckErrorf("'predicate' is a mandatory field for creating a custom evidence: --%s", predicate) | ||
return errorutils.CheckErrorf("'predicate' is a mandatory field for creating evidence: --%s", predicate) | ||
} | ||
if !c.IsFlagSet(predicateType) || assertValueProvided(c, predicateType) != nil { | ||
return errorutils.CheckErrorf("'predicate-type' is a mandatory field for creating a custom evidence: --%s", predicateType) | ||
return errorutils.CheckErrorf("'predicate-type' is a mandatory field for creating evidence: --%s", predicateType) | ||
} | ||
if !c.IsFlagSet(key) || assertValueProvided(c, key) != nil { | ||
return errorutils.CheckErrorf("'key' is a mandatory field for creating a custom evidence: --%s", key) | ||
return errorutils.CheckErrorf("'key' is a mandatory field for creating evidence: --%s", key) | ||
} | ||
return nil | ||
} | ||
|
@@ -88,7 +86,7 @@ func getAndValidateSubject(c *components.Context) (string, error) { | |
} | ||
|
||
if len(foundSubjects) == 0 { | ||
return "", errorutils.CheckErrorf("Subject must be one of the fields: [%s]", strings.Join(subjectTypes, ", ")) | ||
return "", errorutils.CheckErrorf("subject must be one of the fields: [%s]", strings.Join(subjectTypes, ", ")) | ||
} | ||
if len(foundSubjects) > 1 { | ||
return "", errorutils.CheckErrorf("multiple subjects found: [%s]", strings.Join(foundSubjects, ", ")) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package cli | ||
|
||
import ( | ||
"flag" | ||
"github.com/golang/mock/gomock" | ||
"github.com/jfrog/jfrog-cli-core/v2/common/commands" | ||
"github.com/jfrog/jfrog-cli-core/v2/plugins/components" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/urfave/cli" | ||
"testing" | ||
) | ||
|
||
func TestCreateEvidence_Context(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
app := cli.NewApp() | ||
app.Commands = []cli.Command{ | ||
{ | ||
Name: "create", | ||
}, | ||
} | ||
set := flag.NewFlagSet(predicate, 0) | ||
ctx := cli.NewContext(app, set, nil) | ||
|
||
tests := []struct { | ||
name string | ||
flags []components.Flag | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "InvalidContext - Missing Subject", | ||
flags: []components.Flag{ | ||
setDefaultValue(predicate, predicate), | ||
setDefaultValue(predicateType, predicateType), | ||
setDefaultValue(key, key), | ||
}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "InvalidContext - Missing Predicate", | ||
flags: []components.Flag{ | ||
setDefaultValue("", ""), | ||
setDefaultValue(predicateType, "InToto"), | ||
setDefaultValue(key, "PGP"), | ||
}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "InvalidContext - Subject Duplication", | ||
flags: []components.Flag{ | ||
setDefaultValue(predicate, predicate), | ||
setDefaultValue(predicateType, "InToto"), | ||
setDefaultValue(key, "PGP"), | ||
setDefaultValue(subjectRepoPath, subjectRepoPath), | ||
setDefaultValue(releaseBundle, releaseBundle), | ||
setDefaultValue(releaseBundleVersion, releaseBundleVersion), | ||
}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "ValidContext - ReleaseBundle", | ||
flags: []components.Flag{ | ||
setDefaultValue(predicate, predicate), | ||
setDefaultValue(predicateType, "InToto"), | ||
setDefaultValue(key, "PGP"), | ||
setDefaultValue(releaseBundle, releaseBundle), | ||
setDefaultValue(releaseBundleVersion, releaseBundleVersion), | ||
setDefaultValue("url", "url"), | ||
}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "ValidContext - RepoPath", | ||
flags: []components.Flag{ | ||
setDefaultValue(predicate, predicate), | ||
setDefaultValue(predicateType, "InToto"), | ||
setDefaultValue(key, "PGP"), | ||
setDefaultValue(subjectRepoPath, subjectRepoPath), | ||
setDefaultValue("url", "url"), | ||
}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "ValidContext - Build", | ||
flags: []components.Flag{ | ||
setDefaultValue(predicate, predicate), | ||
setDefaultValue(predicateType, "InToto"), | ||
setDefaultValue(key, "PGP"), | ||
setDefaultValue(buildName, buildName), | ||
setDefaultValue(buildNumber, buildNumber), | ||
setDefaultValue("url", "url"), | ||
}, | ||
expectErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
context, err1 := components.ConvertContext(ctx, tt.flags...) | ||
if err1 != nil { | ||
return | ||
} | ||
|
||
execFunc = func(command commands.Command) error { | ||
return nil | ||
} | ||
// Replace execFunc with the mockExec function | ||
defer func() { execFunc = exec }() // Restore original execFunc after test | ||
|
||
err := createEvidence(context) | ||
if tt.expectErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func setDefaultValue(flag string, defaultValue string) components.Flag { | ||
f := components.NewStringFlag(flag, flag) | ||
f.DefaultValue = defaultValue | ||
return f | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c -> ctx (as agreed)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done