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

Add command validate to check for parse errors #62

Merged
merged 17 commits into from
May 23, 2019
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
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