Skip to content

Commit

Permalink
Enhance error message when user runs from a non-component directory (r…
Browse files Browse the repository at this point in the history
…edhat-developer#6006)

* Enhance error message when user runs from a non-component directory

* Dharmit's review

* Use different messages

Signed-off-by: Parthvi Vala <pvala@redhat.com>
  • Loading branch information
valaparthvi authored Aug 9, 2022
1 parent f4e96a4 commit 076f2e2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
17 changes: 15 additions & 2 deletions pkg/odo/genericclioptions/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,28 @@ func TestNew(t *testing.T) {
},
},
{
name: "no env file",
name: "no env file; non-empty directory",
input: input{
needDevfile: false,
isOffline: true,
workingDir: filepath.Join(prefixDir, "myapp"),
populateWorkingDir: func(fs filesystem.Filesystem) {
_ = fs.WriteFile(filepath.Join(prefixDir, "myapp", "main.go"), []byte{}, 0644)
},
},
expectedErr: "The current directory does not represent an odo component",
expectedErr: "Use \"odo dev\" to initialize an odo component for this folder and deploy it on cluster",
},
{
name: "no env file; empty directory",
input: input{
needDevfile: false,
isOffline: true,
workingDir: filepath.Join(prefixDir, "myapp"),
populateWorkingDir: func(fs filesystem.Filesystem) {
_ = fs.MkdirAll(filepath.Join(prefixDir, "myapp"), 0755)
},
},
expectedErr: "Use \"odo init\" to initialize an odo component in the folder.",
},
}

Expand Down
24 changes: 20 additions & 4 deletions pkg/odo/genericclioptions/localprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package genericclioptions

import (
"errors"
"fmt"

"github.com/redhat-developer/odo/pkg/devfile/location"
"github.com/redhat-developer/odo/pkg/envinfo"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
)

// GetValidEnvInfo accesses the environment file
Expand Down Expand Up @@ -33,11 +36,24 @@ func GetValidEnvInfo(cmdline cmdline.Cmdline) (*envinfo.EnvSpecificInfo, error)

// Check to see if the environment file exists
if !envInfo.Exists() {
exitMessage := `The current directory does not represent an odo component.
To get started,%s
* Open this folder in your favorite IDE and start editing, your changes will be reflected directly on the cluster.
Visit https://odo.dev for more information.`

if isEmpty, _ := location.DirIsEmpty(filesystem.DefaultFs{}, componentContext); isEmpty {
exitMessage = fmt.Sprintf(exitMessage, `
* Create and move to a new directory
* Use "odo init" to initialize an odo component in the folder.
* Use "odo dev" to deploy it on cluster.`)
} else {
exitMessage = fmt.Sprintf(exitMessage, `
* Use "odo dev" to initialize an odo component for this folder and deploy it on cluster.`)
}
//revive:disable:error-strings This is a top-level error message displayed as is to the end user
return nil, errors.New(`The current directory does not represent an odo component.
To start editing your component, use "odo dev" and open this folder in your favorite IDE. Changes will be directly reflected on the cluster.
To deploy your component to a cluster use "odo deploy".
Or switch to directory with a component.`)
return nil, errors.New(exitMessage)
//revive:enable:error-strings
}

Expand Down
32 changes: 26 additions & 6 deletions tests/integration/cmd_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,32 @@ var _ = Describe("odo delete command tests", func() {
helper.CommonAfterEach(commonVar)
})

When("running odo delete from a directory that does not contain a .odo/env/env.yaml file", func() {
var files []string
BeforeEach(func() {
files = helper.ListFilesInDir(commonVar.Context)
Expect(files).ToNot(ContainElement(".odo"))
})
When("the directory is empty", func() {
BeforeEach(func() {
Expect(len(files)).To(BeZero())
})
It("should fail", func() {
errOut := helper.Cmd("odo", "delete", "component", "-f").ShouldFail().Err()
helper.MatchAllInOutput(errOut, []string{"The current directory does not represent an odo component", "Use \"odo init\" to initialize an odo component in the folder."})
})
})
When("the directory is not empty", func() {
BeforeEach(func() {
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context)
})
It("should fail", func() {
errOut := helper.Cmd("odo", "delete", "component", "-f").ShouldFail().Err()
helper.MatchAllInOutput(errOut, []string{"The current directory does not represent an odo component", "Use \"odo dev\" to initialize an odo component for this folder and deploy it on cluster."})
})
})
})

for _, ctx := range []struct {
title string
devfileName string
Expand Down Expand Up @@ -64,12 +90,6 @@ var _ = Describe("odo delete command tests", func() {
ctx.setupFunc()
}
})
It("should fail when the directory does not contain a .odo/env.yaml file", func() {
files := helper.ListFilesInDir(commonVar.Context)
Expect(files).ToNot(ContainElement(".odo"))
errOut := helper.Cmd("odo", "delete", "component", "-f").ShouldFail().Err()
Expect(errOut).To(ContainSubstring("The current directory does not represent an odo component"))
})
When("the components are not deployed", func() {
var stdOut string
BeforeEach(func() {
Expand Down

0 comments on commit 076f2e2

Please sign in to comment.