This is an automated email from the ASF dual-hosted git repository.

nferraro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/master by this push:
     new aaeb457  fix(cli): check run sources exists
aaeb457 is described below

commit aaeb4573fb62881fae1f6fa0b8f20695237414d4
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Fri Jan 15 10:33:34 2021 +0100

    fix(cli): check run sources exists
    
    * 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
---
 pkg/cmd/run.go          |  4 ++--
 pkg/cmd/run_test.go     | 17 +++++++++++++++++
 pkg/cmd/util_content.go |  2 +-
 pkg/cmd/util_sources.go |  4 +++-
 4 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index 5904272..695f01f 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -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
@@ -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
diff --git a/pkg/cmd/run_test.go b/pkg/cmd/run_test.go
index 9db4193..ce9527f 100644
--- a/pkg/cmd/run_test.go
+++ b/pkg/cmd/run_test.go
@@ -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
diff --git a/pkg/cmd/util_content.go b/pkg/cmd/util_content.go
index 75d9c08..c210870 100644
--- a/pkg/cmd/util_content.go
+++ b/pkg/cmd/util_content.go
@@ -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)
diff --git a/pkg/cmd/util_sources.go b/pkg/cmd/util_sources.go
index 50a0838..f539705 100644
--- a/pkg/cmd/util_sources.go
+++ b/pkg/cmd/util_sources.go
@@ -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
@@ -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)
                        }
                }
        }

Reply via email to