Skip to content

Commit

Permalink
Add tests for commands (#1)
Browse files Browse the repository at this point in the history
* Add tests for command

* Update CI config

* go mod tidy
  • Loading branch information
cszatmary authored Feb 7, 2020
1 parent d25ad6d commit fe9d962
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 34 deletions.
9 changes: 3 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2.1

jobs:
lint-build:
lint-test:
docker:
- image: circleci/golang:1.12
working_directory: ~/goutils
Expand All @@ -22,14 +22,11 @@ jobs:
- run:
name: Run linter
command: make lint
- run:
name: Build cannon
command: make build
- run:
name: Run tests
command: make test

workflows:
lint-build:
lint-test:
jobs:
- lint-build
- lint-test
5 changes: 5 additions & 0 deletions _fixtures/text_tests/hype.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# HYPE ZONE
This file is ***hype***.

## Hype Section
This section is pretty hype.
31 changes: 8 additions & 23 deletions command/command.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package command

import (
"bytes"
"os/exec"
"strings"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand All @@ -17,8 +17,8 @@ func IsCommandAvailable(command string) bool {
return true
}

func Exec(id string, name string, arg ...string) error {
cmd := exec.Command(name, arg...)
func Exec(cmdName string, args []string, id string, opts ...func(*exec.Cmd)) error {
cmd := exec.Command(cmdName, args...)

stdout := log.WithFields(log.Fields{
"id": id,
Expand All @@ -33,30 +33,15 @@ func Exec(id string, name string, arg ...string) error {
cmd.Stdout = stdout
cmd.Stderr = stderr

err := cmd.Run()
if err != nil {
return errors.Wrapf(err, "Exec failed to run %s %s", name, arg)
for _, opt := range opts {
opt(cmd)
}

return nil
}

func ExecResult(id string, name string, args ...string) (*bytes.Buffer, error) {
cmd := exec.Command(name, args...)

stdoutBuf := &bytes.Buffer{}
stderr := log.WithFields(log.Fields{
"id": id,
}).WriterLevel(log.DebugLevel)
defer stderr.Close()

cmd.Stdout = stdoutBuf
cmd.Stderr = stderr

err := cmd.Run()
if err != nil {
return nil, errors.Wrapf(err, "Exec failed to run %s %s", name, args)
argsStr := strings.Join(args, " ")
return errors.Wrapf(err, "Exec failed to run %s %s", cmdName, argsStr)
}

return stdoutBuf, nil
return nil
}
44 changes: 44 additions & 0 deletions command/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package command

import (
"bytes"
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsCommandAvailable(t *testing.T) {
exists := IsCommandAvailable("echo")

assert.True(t, exists)
}

func TestNotIsCommandAvailable(t *testing.T) {
exists := IsCommandAvailable("asljhasld")

assert.False(t, exists)
}

func TestExec(t *testing.T) {
err := Exec("echo", []string{"Hello world"}, "test-exec")

assert.NoError(t, err)
}

func TestExecOpts(t *testing.T) {
assert := assert.New(t)
buf := &bytes.Buffer{}
err := Exec("echo", []string{"Hello world"}, "test-exec-opts", func(cmd *exec.Cmd) {
cmd.Stdout = buf
})

assert.NoError(err)
assert.Equal("Hello world\n", buf.String())
}

func TestExecError(t *testing.T) {
err := Exec("notacmd", []string{"Hello World"}, "test-exec-error")

assert.Error(t, err)
}
24 changes: 24 additions & 0 deletions file/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package file

import (
"github.com/stretchr/testify/assert"
"testing"
)

const fixtures = "../_fixtures"
const textTests = fixtures + "/text_tests"

func TestDirExists(t *testing.T) {
path := textTests
assert.True(t, FileOrDirExists(path))
}

func TestFileExists(t *testing.T) {
path := textTests + "/hype.md"
assert.True(t, FileOrDirExists(path))
}

func TestFileNotExists(t *testing.T) {
path := fixtures + "/notafile.txt"
assert.False(t, FileOrDirExists(path))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ go 1.13
require (
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.4.2
gopkg.in/yaml.v2 v2.2.8
github.com/stretchr/testify v1.2.2
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 comments on commit fe9d962

Please sign in to comment.