Skip to content
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

fix(cli): check run sources exists #1913

Merged
merged 1 commit into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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