Skip to content

Commit

Permalink
Merge pull request #62 from apoorvam/master
Browse files Browse the repository at this point in the history
Add command `validate` to check for parse errors
  • Loading branch information
agentmilindu authored May 23, 2019
2 parents 239ec60 + 9506a77 commit 8becd1b
Show file tree
Hide file tree
Showing 8 changed files with 483 additions and 23 deletions.
57 changes: 50 additions & 7 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "gopkg.in/go-playground/validator.v9"
version = "9.28.0"

[[constraint]]
name = "github.com/go-playground/universal-translator"
version = "0.16.0"
42 changes: 42 additions & 0 deletions cmd/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"fmt"
"os"

"github.com/leopardslab/dunner/pkg/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func init() {
rootCmd.AddCommand(validateCmd)
}

var validateCmd = &cobra.Command{
Use: "validate",
Short: "Validate the dunner task file `.dunner.yaml`",
Long: "You can validate task file `.dunner.yaml` with this command to see if there are any parse errors",
Run: Validate,
Args: cobra.MinimumNArgs(0),
}

// Validate command invoked from command line, validates the dunner task file. If there are errors, it fails with non-zero exit code.
func Validate(_ *cobra.Command, args []string) {
var dunnerFile = viper.GetString("DunnerTaskFile")

configs, err := config.GetConfigs(dunnerFile)
if err != nil {
log.Fatal(err)
}

errs := configs.Validate()
if len(errs) != 0 {
fmt.Println("Validation failed with following errors:")
for _, err := range errs {
fmt.Println(err.Error())
}
os.Exit(1)
}
fmt.Println("Validation successful!")
}
22 changes: 22 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package util

import (
"os"
"path"
"strings"
)

// HomeDir is the environment variable HOME
var HomeDir = os.Getenv("HOME")

// DirExists returns true if the given param is a valid existing directory
func DirExists(dir string) bool {
if strings.HasPrefix(dir, "~") {
dir = path.Join(HomeDir, strings.Trim(dir, "~"))
}
src, err := os.Stat(dir)
if err != nil {
return false
}
return src.IsDir()
}
51 changes: 51 additions & 0 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package util

import (
"io/ioutil"
"os"
"testing"
)

func TestDirExistsSuccess(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "TestDir")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)

exists := DirExists(tmpdir)

if !exists {
t.Fatalf("Directory exists; but got false")
}
}

func TestDirExistsFail(t *testing.T) {
exists := DirExists("this path is invalid")

if exists {
t.Fatalf("Directory invalid; but got as exists")
}
}

func TestDirExistsFailForFile(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestFileExists")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpfile.Name())

exists := DirExists(tmpfile.Name())

if exists {
t.Fatalf("Not a directory; but got as true")
}
}

func TestDirExistsIfNotAbsPath(t *testing.T) {
exists := DirExists("~/invalidpathfortesting")

if exists {
t.Fatalf("Not a directory; but got as true")
}
}
Loading

0 comments on commit 8becd1b

Please sign in to comment.