Skip to content

Commit

Permalink
improved coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
wagoodman committed Feb 17, 2018
1 parent a64fbce commit f69ad05
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 45 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
debug
debug.test
runner
.vscode
/.bashful
Expand All @@ -8,4 +10,4 @@ archive
/downloads
/.idea
/vendor
/dist
/dist
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ $(TARGETS):

run:
go run main.go task.go config.go screen.go download.go log.go \
run example/11-tags.yml --tags some-app1
run example/06-share-variables.yml

examples: clean build
./dist/bashful run example/00-demo.yml
Expand Down
8 changes: 2 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/gob"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
Expand Down Expand Up @@ -469,18 +468,15 @@ func CreateTasks() (finalTasks []*Task) {
return finalTasks
}

// ReadConfig is the entrypoint for all config fetching and parsing. This returns a list of Task runtime objects to execute.
func ReadConfig(userYamlPath string) {
// ParseConfig is the entrypoint for all config fetching and parsing. This returns a list of Task runtime objects to execute.
func ParseConfig(yamlString []byte) {
config.Cli.RunTagSet = mapset.NewSet()
for _, tag := range config.Cli.RunTags {
config.Cli.RunTagSet.Add(tag)
}

readTimeCache()

yamlString, err := ioutil.ReadFile(userYamlPath)
checkError(err, "Unable to read yaml config.")

parseRunYaml(yamlString)

if config.Options.LogPath != "" {
Expand Down
2 changes: 2 additions & 0 deletions example/12-share-variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ tasks:

- cmd: eval "export VAR3=isnowreallyset"

- cmd: eval "export VAR3=$VAR3:sweeeeet"

- cmd: echo 1=${VAR1} 2=${VAR2} 3=${VAR3} ; sleep 10


59 changes: 35 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"os/signal"
Expand Down Expand Up @@ -33,8 +34,8 @@ var (
buildTime = "No build timestamp provided"
allTasks []*Task
ticker *time.Ticker
exitSignaled = false
startTime = time.Now()
exitSignaled bool
startTime time.Time
purple = color.ColorFunc("magenta+h")
red = color.ColorFunc("red+h")
blue = color.ColorFunc("blue+h")
Expand Down Expand Up @@ -134,10 +135,13 @@ func doesFileExist(name string) bool {
func bundle(userYamlPath, outputPath string) {
archivePath := "bundle.tar.gz"

ReadConfig(userYamlPath)
AllTasks := CreateTasks()
yamlString, err := ioutil.ReadFile(userYamlPath)
checkError(err, "Unable to read yaml config.")

DownloadAssets(AllTasks)
ParseConfig(yamlString)
allTasks := CreateTasks()

DownloadAssets(allTasks)

fmt.Println(bold("Bundling " + userYamlPath + " to " + outputPath))

Expand Down Expand Up @@ -196,14 +200,16 @@ __BASHFUL_ARCHIVE__

}

func run(userYamlPath string) {
func run(yamlString []byte, environment map[string]string) []*Task {
var err error
fmt.Print("\033[?25l") // hide cursor

ReadConfig(userYamlPath)
AllTasks := CreateTasks()
exitSignaled = false
startTime = time.Now()

ParseConfig(yamlString)
allTasks = CreateTasks()

DownloadAssets(AllTasks)
DownloadAssets(allTasks)

rand.Seed(time.Now().UnixNano())

Expand All @@ -218,29 +224,25 @@ func run(userYamlPath string) {
tagInfo := ""
if len(config.Cli.RunTags) > 0 {
if config.Cli.ExecuteOnlyMatchedTags {
tagInfo = " with only matching tags: "
tagInfo = " only matching tags: "
} else {
tagInfo = " with non-tagged and matching tags: "
tagInfo = " non-tagged and matching tags: "
}
tagInfo += strings.Join(config.Cli.RunTags, ", ")
}

fmt.Println(bold("Running " + userYamlPath + tagInfo))
logToMain("Running "+userYamlPath+tagInfo, majorFormat)
fmt.Println(bold("Running " + tagInfo))
logToMain("Running "+tagInfo, majorFormat)

// Since this is an empty map, no env vars will be loaded explicitly into the first exec.Command
// which will cause the current processes env vars to be loaded instead
environment := map[string]string{}

for _, task := range AllTasks {
for _, task := range allTasks {
task.Run(environment)
failedTasks = append(failedTasks, task.failedTasks...)

if exitSignaled {
break
}
}
logToMain("Finished "+userYamlPath, majorFormat)
logToMain("Complete", majorFormat)

err = Save(config.etaCachePath, &config.commandTimeCache)
checkError(err, "Unable to save command eta cache.")
Expand Down Expand Up @@ -280,9 +282,7 @@ func run(userYamlPath string) {

}

logToMain("Exiting", "")

exit(len(failedTasks))
return failedTasks
}

func exitWithErrorMessage(msg string) {
Expand Down Expand Up @@ -398,8 +398,19 @@ func main() {
}
}

run(userYamlPath)
// Since this is an empty map, no env vars will be loaded explicitly into the first exec.Command
// which will cause the current processes env vars to be loaded instead
environment := map[string]string{}

yamlString, err := ioutil.ReadFile(userYamlPath)
checkError(err, "Unable to read yaml config.")

fmt.Print("\033[?25l") // hide cursor
failedTasks := run(yamlString, environment)

logToMain("Exiting", "")

exit(len(failedTasks))
return nil
},
},
Expand Down
54 changes: 54 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
package main

import (
"strconv"
"testing"
)

func TestTaskErrorPolicy(t *testing.T) {
var simpleYamlStr string
var failedTasks []*Task

simpleYamlStr = `
tasks:
- cmd: false
ignore-failure: true
`
failedTasks = run([]byte(simpleYamlStr), map[string]string{})
if len(failedTasks) > 0 {
t.Error("TestTaskErrorPolicy: ignore-failure: Expected no tasks to fail, got " + strconv.Itoa(len(failedTasks)))
}

simpleYamlStr = `
tasks:
- cmd: false
`
failedTasks = run([]byte(simpleYamlStr), map[string]string{})
if len(failedTasks) != 1 {
t.Error("TestTaskErrorPolicy: ack failure: Expected exactly 1 task to fail, got " + strconv.Itoa(len(failedTasks)))
}

simpleYamlStr = `
config:
stop-on-failure: true
tasks:
- cmd: false
- cmd: false
`
failedTasks = run([]byte(simpleYamlStr), map[string]string{})
if len(failedTasks) != 1 {
t.Error("TestTaskErrorPolicy: stop on failure: Expected exactly 1 task to fail, got " + strconv.Itoa(len(failedTasks)))
}

simpleYamlStr = `
config:
stop-on-failure: false
tasks:
- cmd: false
- cmd: false
`
failedTasks = run([]byte(simpleYamlStr), map[string]string{})
if len(failedTasks) != 2 {
t.Error("TestTaskErrorPolicy: do not stop on failure: Expected exactly 2 task to fail, got " + strconv.Itoa(len(failedTasks)))
}

}
5 changes: 4 additions & 1 deletion screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ func (scr *screen) Display(message string, index int) {

// trim message length if it won't fit on the screen
width, err := terminalWidth()
checkError(err, "Unable to determine screen width.")
if err != nil {
logToMain("Unable to determine screen width", errorFormat)
width = 80
}
for visualLength(message) > int(width) {
message = trimToVisualLength(message, int(width)-3) + "..."
}
Expand Down
3 changes: 2 additions & 1 deletion scripts/test
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ cd $(dirname $0)/..
PACKAGES=". $(find . -name '*.go' | xargs -I{} dirname {} | cut -f2 -d/ | sort -u | grep -Ev '(^\.$|.git|.trash-cache|vendor|bin)' | sed -e 's!^!./!' -e 's!$!/...!')"

[ $(arch) == "x86_64" ] && RACE=-race
go test ${RACE} -cover -tags=test ${PACKAGES}
#go test ${RACE} -cover -tags=test ${PACKAGES}
go test -cover -tags=test ${PACKAGES}
21 changes: 10 additions & 11 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"testing"
"time"

"github.com/alecthomas/repr"
)
Expand Down Expand Up @@ -35,14 +34,16 @@ func TestTaskString(t *testing.T) {

func TestSerialTaskEnvPersistence(t *testing.T) {
var expStr, actStr string
var failedTasks []*Task
simpleYamlStr := `
tasks:
- cmd: export SOMEVAR=this
- name: start
cmd: export SOMEVAR=this
- name: append 'is'
cmd: export SOMEVAR=$SOMEVAR:is
- name: append 'is'
- name: append 'DONTDOIT'
parallel-tasks:
- cmd: export SOMEVAR=$SOMEVAR:DONTDOIT
Expand All @@ -51,23 +52,21 @@ tasks:
for-each:
- working
- just
- name: append 'is'
cmd: eval 'export SOMEVAR=$SOMEVAR:fine'
`

ticker = time.NewTicker(150 * time.Millisecond)
parseRunYaml([]byte(simpleYamlStr))
tasks := CreateTasks()
environment := map[string]string{}
for _, task := range tasks {
task.Run(environment)
if len(task.failedTasks) > 0 {
t.Error("TestSerialTaskEnvPersistence: Expected no tasks to fail")
}
config.Options.StopOnFailure = false
failedTasks = run([]byte(simpleYamlStr), environment)
if len(failedTasks) > 0 {
t.Error("TestSerialTaskEnvPersistence: Expected no tasks to fail")
}

expStr, actStr = "this:is:working:just:fine", environment["SOMEVAR"]
if expStr != actStr {
t.Error("Expected", expStr, "got", actStr)
}

}

0 comments on commit f69ad05

Please sign in to comment.