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 checking for invalid names used in unset #180

Merged
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
12 changes: 7 additions & 5 deletions cmd/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ This will update the current project configuration file.`,
utils.ExitError()
}

commandName := args[0]
matched, _ := regexp.MatchString(`^[a-zA-Z0-9\-_]+$`, commandName)
validNameRegex := regexp.MustCompile(`^[a-zA-Z0-9\-_]+$`)
for _, commandName := range args {
matched := validNameRegex.MatchString(commandName)

if !matched {
fmt.Println("1build unset: '" + commandName + "' is not a valid command name. See '1build unset --help'.")
utils.ExitError()
if !matched {
fmt.Println("1build unset: '" + commandName + "' is not a valid command name. See '1build unset --help'.")
utils.ExitError()
}
}
},
Run: func(cmd *cobra.Command, args []string) {
Expand Down
51 changes: 49 additions & 2 deletions testing/fixtures/command_unset_fixtures.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package fixtures

import (
"io/ioutil"
"testing"

"github.com/gopinath-langote/1build/testing/def"
"github.com/gopinath-langote/1build/testing/utils"
"github.com/stretchr/testify/assert"
"io/ioutil"
"testing"
)

func featureUnsetTestsData() []Test {
Expand All @@ -24,6 +25,8 @@ func featureUnsetTestsData() []Test {
shouldUnsetTheAfterCommand(feature),
unsetBeforeShouldFailWhenBeforeIsNotFound(feature),
unsetAfterShouldFailWhenAfterIsNotFound(feature),
unsetSingleCommandShouldFailWhenInvalidCommandNameIsEntered(feature),
unsetMultipleCommandShouldFailWhenInvalidCommandNameIsEntered(feature),
}
}

Expand Down Expand Up @@ -329,3 +332,47 @@ commands:
},
}
}

func unsetSingleCommandShouldFailWhenInvalidCommandNameIsEntered(feature string) Test {
defaultFileContent := `
project: Sample Project
commands:
- build: go build
`
invalidName := "!nv@lid-name"
expectedOutput := "1build unset: '" + invalidName + "' is not a valid command name. See '1build unset --help'."

return Test{
Feature: feature,
Name: "unsetSingleCommandShouldFailWhenInvalidCommandNameIsEntered",
CmdArgs: []string{"unset", invalidName},
Setup: func(dir string) error {
return utils.CreateConfigFile(dir, defaultFileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return assert.Contains(t, actualOutput, expectedOutput)
},
}
}

func unsetMultipleCommandShouldFailWhenInvalidCommandNameIsEntered(feature string) Test {
defaultFileContent := `
project: Sample Project
commands:
- build: go build
`
invalidName := "!nv@lid-name"
expectedOutput := "1build unset: '" + invalidName + "' is not a valid command name. See '1build unset --help'."

return Test{
Feature: feature,
Name: "unsetSingleCommandShouldFailWhenInvalidCommandNameIsEntered",
CmdArgs: []string{"unset", "build", invalidName},
Setup: func(dir string) error {
return utils.CreateConfigFile(dir, defaultFileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return assert.Contains(t, actualOutput, expectedOutput)
},
}
}