Skip to content

Commit

Permalink
Add configuration validation beforehand (#46)
Browse files Browse the repository at this point in the history
* Add configuration validation beforehand (#44)

Signed-off-by: Mehdy Khoshnoody <[email protected]>

* Add test for invalid issue tracker scenario (#44)

Signed-off-by: Mehdy Khoshnoody <[email protected]>
  • Loading branch information
mehdy authored Aug 27, 2020
1 parent 64c1fc5 commit b109a2b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
29 changes: 29 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const (
IssueTrackerRedmine = "REDMINE"
)

var validIssueTrackers = []IssueTracker{
IssueTrackerJira,
IssueTrackerGithub,
IssueTrackerGitlab,
IssueTrackerPivotal,
IssueTrackerRedmine,
}

var windowsAbsolutePathPattern = regexp.MustCompile("^[A-Z]{1}:")

// Local todocheck configuration struct definition
Expand Down Expand Up @@ -66,6 +74,27 @@ func NewLocal(cfgPath, basepath string) (*Local, error) {
return cfg, nil
}

// Validate validates the values of given configuration
func (l *Local) Validate() []error {
var errors []error

if err := l.validateIssueTracker(); err != nil {
errors = append(errors, err)
}

return errors
}

func (l *Local) validateIssueTracker() error {
for _, issueTracker := range validIssueTrackers {
if l.IssueTracker == issueTracker {
return nil
}
}
return fmt.Errorf("invalid issue tracker: %q is not supported. the valid issue trackers are: %v",
l.IssueTracker, validIssueTrackers)
}

func exists(filepath string) bool {
info, err := os.Stat(filepath)
if os.IsNotExist(err) {
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func main() {
log.Fatalf("couldn't open configuration file: %s\n", err)
}

if errors := localCfg.Validate(); len(errors) > 0 {
for _, err := range errors {
log.Println(err)
}

os.Exit(1)
}

err = authmanager.AcquireToken(localCfg)
if err != nil {
log.Fatalf("couldn't acquire token from config: %s\n", err)
Expand Down
2 changes: 2 additions & 0 deletions testing/test_configs/invalid_issue_tracker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
origin: "invalid_issue_tracker"
issue_tracker: INVALID
11 changes: 11 additions & 0 deletions testing/todocheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,17 @@ func TestIgnoredDirectoriesWithDotDot(t *testing.T) {
}
}

func TestInvalidIssueTracker(t *testing.T) {
err := scenariobuilder.NewScenario().
WithBinary("../todocheck").
WithConfig("./test_configs/invalid_issue_tracker.yaml").
ExpectExecutionError().
Run()
if err != nil {
t.Errorf("%s", err)
}
}

func TestTraversingNonExistentDirectory(t *testing.T) {
err := scenariobuilder.NewScenario().
WithBinary("../todocheck").
Expand Down

0 comments on commit b109a2b

Please sign in to comment.