Skip to content

Commit

Permalink
Merge pull request #23 from chonla/feature/preload-variables
Browse files Browse the repository at this point in the history
Add variables file support
  • Loading branch information
chonla authored Jul 12, 2021
2 parents facc41f + 4307a1a commit 35f6c48
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ testcases/
pkg/
homebrew/
*.swp
bin/
30 changes: 27 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/chonla/cotton/cotton"
"github.com/chonla/cotton/parser"
"github.com/chonla/cotton/testsuite"
"github.com/chonla/cotton/types"
"github.com/fatih/color"
"github.com/fsnotify/fsnotify"
)

// VERSION of cotton
const VERSION = "0.4.0"
const VERSION = "0.5.0"

// Vars are injected variables from command line
type Vars []string
Expand All @@ -42,11 +44,13 @@ func main() {
var detail bool
var watch bool
var stopWhenFailed bool
var varsFile string
var vars Vars

flag.Usage = usage

flag.StringVar(&url, "u", "http://localhost:8080", "set base url")
flag.StringVar(&varsFile, "f", "", "set variables file")
flag.BoolVar(&detail, "d", false, "detail mode -- to dump test detail")
flag.BoolVar(&insecure, "i", false, "insecure mode -- to disable certificate verification")
flag.BoolVar(&watch, "w", false, "watch mode -- to auto-rerun when files are changed")
Expand All @@ -67,11 +71,20 @@ func main() {
os.Exit(1)
}

varsFromFiles := readVariablesFromFile(varsFile, vars)
mergedVars := []string{}
for _, v := range varsFromFiles {
mergedVars = append(mergedVars, v)
}
for _, v := range vars {
mergedVars = append(mergedVars, v)
}

c, e := cotton.NewCotton(testpath, cotton.Config{
BaseURL: url,
Insecure: insecure,
Verbose: detail,
Variables: vars,
Variables: mergedVars,
StopWhenFailed: stopWhenFailed,
})
if e != nil {
Expand Down Expand Up @@ -120,7 +133,7 @@ func main() {
func usage() {
fmt.Println("Usage of cotton:")
fmt.Println()
fmt.Println(" cotton [-u <base-url>] [-i] [-d] [-s] [-p name1=value1] [-p name2=value2] ... <test-cases>")
fmt.Println(" cotton [-u <base-url>] [-f <variables-filename>] [-i] [-d] [-s] [-p name1=value1] [-p name2=value2] ... <test-cases>")
fmt.Println()
fmt.Println(" test-cases can be a markdown file or a directory contain markdowns.")
fmt.Println()
Expand Down Expand Up @@ -164,3 +177,14 @@ func watchDir(path string, fi os.FileInfo, err error) error {

return nil
}

// read variables from given file and return lines content
func readVariablesFromFile(varsFile string, vars Vars) []string {
b, err := ioutil.ReadFile(varsFile)
if err != nil {
yellow := color.New(color.FgYellow).SprintFunc()
fmt.Printf("%s Unable to load variables file...\n", yellow("WARNING"))
return vars
}
return types.StringFromBytes(b).LineBreak()
}
19 changes: 19 additions & 0 deletions types/keyvalue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package types

import (
"errors"
"strings"
)

type KeyValue struct {
Key string
Value string
}

func ParseKeyValue(kv string) (*KeyValue, error) {
s := strings.SplitN(kv, "=", 2)
if len(s) == 2 {
return &KeyValue{s[0], s[1]}, nil
}
return nil, errors.New("unable to parse key-value variable")
}
30 changes: 30 additions & 0 deletions types/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package types

import "strings"

type String string

func StringFromBytes(b []byte) String {
return String(b)
}

func (o String) String() string {
return string(o)
}

func (o String) LineBreak() []string {
linebreak := "\n"
if strings.Contains(o.String(), "\r\n") {
linebreak = "\r\n"
}

lines := strings.Split(o.String(), linebreak)
output := []string{}
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" {
output = append(output, line)
}
}
return output
}

0 comments on commit 35f6c48

Please sign in to comment.