Skip to content

Commit

Permalink
fix(cli): check run sources exists
Browse files Browse the repository at this point in the history
* Changed the name of run.isLocal func to run.isLocalAndExists as the function does both things
* Added a default switch case to report either the file doesn't exist or the URI scheme cannot be parsed
* Added some unit test to verify error messages expected

Fixes #1911
  • Loading branch information
squakez authored and nicolaferraro committed Jan 15, 2021
1 parent 754fa31 commit aaeb457
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (o *runCmdOptions) syncIntegration(cmd *cobra.Command, c client.Client, sou
files = append(files, o.OpenAPIs...)

for _, s := range files {
if isLocal(s) {
if isLocalAndFileExists(s) {
changes, err := sync.File(o.Context, s)
if err != nil {
return err
Expand Down Expand Up @@ -645,7 +645,7 @@ func (*runCmdOptions) configureTraits(integration *v1.Integration, options []str
return nil
}

func isLocal(fileName string) bool {
func isLocalAndFileExists(fileName string) bool {
info, err := os.Stat(fileName)
if os.IsNotExist(err) {
return false
Expand Down
17 changes: 17 additions & 0 deletions pkg/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,23 @@ func TestRunVolumeFlagWrongPVCFormat(t *testing.T) {
assert.NotNil(t, err)
}

func TestRunValidateArgs(t *testing.T) {
runCmdOptions, rootCmd, _ := initializeRunCmdOptions(t)
args := []string{}
err := runCmdOptions.validateArgs(rootCmd, args)
assert.NotNil(t, err)
assert.Equal(t, "run expects at least 1 argument, received 0", err.Error())

args = []string{"run_test.go"}
err = runCmdOptions.validateArgs(rootCmd, args)
assert.Nil(t, err)

args = []string{"missing_file"}
err = runCmdOptions.validateArgs(rootCmd, args)
assert.NotNil(t, err)
assert.Equal(t, "One of the provided sources is not reachable: Missing file or unsupported scheme in missing_file", err.Error())
}

//
// This test does work when running as single test but fails
// otherwise as we are using a global viper instance
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/util_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func loadContent(source string, compress bool, compressBinary bool) (string, boo
var content []byte
var err error

if isLocal(source) {
if isLocalAndFileExists(source) {
content, err = ioutil.ReadFile(source)
} else {
u, err := url.Parse(source)
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/util_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S
sources := make([]Source, 0, len(locations))

for _, location := range locations {
if isLocal(location) {
if isLocalAndFileExists(location) {
answer, err := ResolveLocalSource(location, compress)
if err != nil {
return sources, err
Expand Down Expand Up @@ -176,6 +176,8 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S
return sources, err
}
sources = append(sources, answer)
default:
return sources, fmt.Errorf("Missing file or unsupported scheme in %s", location)
}
}
}
Expand Down

0 comments on commit aaeb457

Please sign in to comment.