Skip to content

Commit

Permalink
Merge pull request #1 from go-exec/tests
Browse files Browse the repository at this point in the history
Refactoring and tests
  • Loading branch information
radutopala authored May 28, 2020
2 parents a071783 + 9f0ff09 commit 6869466
Show file tree
Hide file tree
Showing 38 changed files with 1,853 additions and 281 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
on: [push]
name: tests
jobs:
test:
strategy:
matrix:
go-version: [1.13]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v1
- name: Lint
run: make lint
- name: Tests
run: make tests
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
examples/*/*
!examples/*/*.go
artifacts
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
all:

lint-local:
@echo "Running linters"
golangci-lint cache clean
golangci-lint run -v ./...

lint:
@echo "Running linters"
docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint:v1.21.0 golangci-lint run -v ./...

tests:
@echo "Running tests"
@mkdir -p artifacts
go test -race -count=1 -cover -coverprofile=artifacts/coverage.out -v ./...

coverage: tests
@echo "Running tests & coverage"
go tool cover -html=artifacts/coverage.out -o artifacts/coverage.html
8 changes: 6 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ func (c *config) Value() interface{} {
if v.Kind() == reflect.Func {
t := v.Type()

if t.NumIn() != 0 && t.NumOut() != 1 {
panic("Function type must have no input parameters and a single return value")
if t.NumIn() != 0 {
panic("Function type must have no input parameters")
}

if t.NumOut() != 1 {
panic("Function type must have a single return value")
}

if t.Out(0).Kind().String() != "interface" {
Expand Down
132 changes: 132 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package exec

import (
"github.com/stretchr/testify/require"
"reflect"
"testing"
"time"
)

func TestConfig_Value(t *testing.T) {
type testCase struct {
test string
cfg *config
expectedPanic interface{}
}

testCases := []testCase{
{
test: "valid simple value",
cfg: &config{
value: "",
},
},
{
test: "valid func value",
cfg: &config{
value: func() interface{} {
return "value"
},
},
},
{
test: "panics on invalid func with input param",
cfg: &config{
value: func(s string) interface{} {
return "value"
},
},
expectedPanic: "Function type must have no input parameters",
},
{
test: "panics on invalid func with more than one return param",
cfg: &config{
value: func() (interface{}, string) {
return "value", "one"
},
},
expectedPanic: "Function type must have a single return value",
},
{
test: "panics on invalid func with no input param",
cfg: &config{
value: func() string {
return "value"
},
},
expectedPanic: "Function return value must be an interface{}",
},
}

for _, testCase := range testCases {
t.Run(testCase.test, func(t *testing.T) {
if testCase.expectedPanic != nil {
require.PanicsWithValue(t, testCase.expectedPanic, func() {
testCase.cfg.Value()
})
} else {
require.Equal(t, testCase.cfg.value, testCase.cfg.Value())
}
})
}
}

func TestConfig_String(t *testing.T) {
cfg := &config{
Name: "name",
value: "string",
}

require.Equal(t, cfg.value, cfg.String())
require.IsType(t, reflect.TypeOf(cfg.value), reflect.TypeOf(cfg.String()))
}

func TestConfig_Int(t *testing.T) {
cfg := &config{
Name: "name",
value: 1,
}

require.Equal(t, cfg.value, cfg.Int())
require.IsType(t, reflect.TypeOf(cfg.value), reflect.TypeOf(cfg.Int()))
}

func TestConfig_Int64(t *testing.T) {
cfg := &config{
Name: "name",
value: int64(1),
}

require.Equal(t, cfg.value, cfg.Int64())
require.IsType(t, reflect.TypeOf(cfg.value), reflect.TypeOf(cfg.Int64()))
}

func TestConfig_Bool(t *testing.T) {
cfg := &config{
Name: "name",
value: true,
}

require.Equal(t, cfg.value, cfg.Bool())
require.IsType(t, reflect.TypeOf(cfg.value), reflect.TypeOf(cfg.Bool()))
}

func TestConfig_Slice(t *testing.T) {
cfg := &config{
Name: "name",
value: []string{"a", "b"},
}

require.Equal(t, cfg.value, cfg.Slice())
require.IsType(t, reflect.TypeOf(cfg.value), reflect.TypeOf(cfg.Slice()))
}

func TestConfig_Time(t *testing.T) {
cfg := &config{
Name: "name",
value: time.Now(),
}

require.Equal(t, cfg.value, cfg.Time())
require.IsType(t, reflect.TypeOf(cfg.value), reflect.TypeOf(cfg.Time()))
}
17 changes: 12 additions & 5 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ package main
import (
"fmt"
"github.com/fatih/color"
"github.com/go-exec/exec"
e "github.com/go-exec/exec"
"time"
)

/*
Example with general setup of tasks
*/
func main() {
exec := e.Instance
defer exec.Run()

exec.Task("onStart", func() {
exec.Set("startTime", time.Now())
}).Private()

exec.Task("onEnd", func() {
exec.Println(fmt.Sprintf("Finished in %s!`", time.Now().Sub(exec.Get("startTime").Time()).String()))
exec.Println(fmt.Sprintf("Finished in %s!", time.Since(exec.Get("startTime").Time()).String()))
}).Private()

type F struct {
Expand All @@ -25,7 +28,7 @@ func main() {

stage := exec.NewArgument("stage", "Provide the running stage")
stage.Default = "qa"
stage.Type = exec.String
stage.Type = e.String

exec.AddArgument(stage)

Expand Down Expand Up @@ -78,12 +81,18 @@ func main() {
Task("upload", func() {
exec.Remote("ls -la /")
exec.Upload("test.txt", "~/test.txt")
}).
OnServers(func() []string {
return []string{"prod1"}
})

exec.
Task("download", func() {
exec.Remote("ls -la /")
exec.Download("~/test.txt", "test.txt")
}).
OnServers(func() []string {
return []string{"prod1"}
})

exec.
Expand Down Expand Up @@ -266,6 +275,4 @@ func main() {
exec.After("local", "onservers:a")
exec.After("local", "get3")
exec.After("onservers:a", "local")

exec.Init()
}
5 changes: 3 additions & 2 deletions examples/symfony/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
Example of deploying a Symfony app using the deploy recipes
*/
func main() {
exec := exec.Instance
defer exec.Run()

exec.Set("repository", "[email protected]:namespace/app.git")
exec.Set("shared_files", []string{})
exec.Set("shared_dirs", []string{"var/logs", "vendor", "web/uploads", "web/media", "node_modules"})
Expand Down Expand Up @@ -38,6 +41,4 @@ func main() {
exec.OnServers(func() []string {
return []string{exec.GetArgument("stage").String()}
})

exec.Init()
}
Loading

0 comments on commit 6869466

Please sign in to comment.