Skip to content

Commit a354933

Browse files
authored
Merge pull request #1 from SimonBaeumer/add-travis-build
Add travis build and badges
2 parents 9d78132 + deb84d5 commit a354933

13 files changed

+152
-41
lines changed

.travis.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
language: go
2+
3+
env:
4+
global:
5+
- GO111MODULE=on
6+
- CC_TEST_REPORTER_ID=1fee6b47ad638c3cb28b932d28413d643a06c90b277bc5839e306f40e932422e
7+
8+
stages:
9+
- test
10+
11+
go:
12+
- 1.13.x
13+
14+
sudo: required
15+
dist: trusty
16+
17+
before_install:
18+
- go get -u golang.org/x/lint/golint
19+
- make deps
20+
- curl -L https://github.com/SimonBaeumer/commander/releases/download/v0.3.0/commander-linux-amd64 -o ~/bin/commander
21+
- chmod +x ~/bin/commander
22+
23+
jobs:
24+
include:
25+
- name: macOS Unit
26+
os: osx
27+
script:
28+
- make test
29+
30+
- name: windows Unit
31+
os: windows
32+
before_install:
33+
- choco install make
34+
script:
35+
- make test-windows
36+
37+
- name: Unit tests
38+
before_script:
39+
- curl https://s3.amazonaws.com/codeclimate/test-reporter/test-reporter-0.6.3-linux-amd64 --output test-reporter
40+
- chmod +x test-reporter
41+
- ./test-reporter before-build
42+
script:
43+
- make test-coverage
44+
after_script:
45+
- ./test-reporter after-build -t gocov --exit-code $TRAVIS_TEST_RESULT

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Svett Ralchev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ lint:
2424

2525
test:
2626
$(info INFO: Starting build $@)
27-
go test ./...
27+
go test `go list ./... | grep -v examples`
28+
29+
test-windows:
30+
$(info INFO: Starting build $@)
31+
go test .
2832

2933
test-coverage:
3034
$(info INFO: Starting build $@)
31-
go test -coverprofile c.out ./...
35+
go test -coverprofile c.out `go list ./... | grep -v examples`

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
[![Build Status](https://travis-ci.org/SimonBaeumer/cmd.svg?branch=master)](https://travis-ci.org/SimonBaeumer/cmd)
2+
[![GoDoc](https://godoc.org/github.com/SimonBaeumer/cmd?status.svg)](https://godoc.org/github.com/SimonBaeumer/cmd)
3+
[![Test Coverage](https://api.codeclimate.com/v1/badges/af3487439a313d580619/test_coverage)](https://codeclimate.com/github/SimonBaeumer/cmd/test_coverage)
4+
[![Maintainability](https://api.codeclimate.com/v1/badges/af3487439a313d580619/maintainability)](https://codeclimate.com/github/SimonBaeumer/cmd/maintainability)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/SimonBaeumer/cmd)](https://goreportcard.com/report/github.com/SimonBaeumer/cmd)
6+
17
# cmd package
28

3-
A simple package to execute shell commands on windows, darwin and windows.
9+
A simple package to execute shell commands on linux, darwin and windows.
10+
11+
## Installation
12+
13+
`$ go get -u github.com/SimonBaeumer/[email protected]`
414

515
## Usage
616

@@ -19,7 +29,7 @@ fmt.Println(cmd.Stderr())
1929
### Stream output to stderr and stdout
2030

2131
```go
22-
cmd := NewCommand("echo hello", WithStandardStreams)
32+
cmd := NewCommand("echo hello", cmd.WithStandardStreams)
2333
cmd.Execute()
2434
```
2535

@@ -48,10 +58,4 @@ make test
4858

4959
### ToDo
5060

51-
- Reports
52-
- Coverage reports
53-
- Go report
54-
- Codeclimate
55-
- Travis-Pipeline
56-
- Documentation
5761
- os.Stdout and os.Stderr output access after execution

command.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,32 @@ type Command struct {
2222
StdoutWriter io.Writer
2323
WorkingDir string
2424
executed bool
25-
stderr bytes.Buffer
26-
stdout bytes.Buffer
2725
exitCode int
26+
// stderr and stdout retrieve the output after the command was executed
27+
stderr bytes.Buffer
28+
stdout bytes.Buffer
2829
}
2930

30-
//NewCommand creates a new command
31+
// NewCommand creates a new command
32+
// You can add option with variadic option argument
33+
//
34+
// Example:
35+
// c := cmd.NewCommand("echo hello", function (c *Command) {
36+
// c.WorkingDir = "/tmp"
37+
// })
38+
// c.Execute()
39+
//
40+
// or you can use existing options functions
41+
//
42+
// c := cmd.NewCommand("echo hello", cmd.WithStandardStreams)
43+
// c.Execute()
44+
//
3145
func NewCommand(cmd string, options ...func(*Command)) *Command {
3246
c := &Command{
33-
Command: cmd,
34-
Timeout: 1 * time.Minute,
35-
executed: false,
36-
Env: []string{},
47+
Command: cmd,
48+
Timeout: 1 * time.Minute,
49+
executed: false,
50+
Env: []string{},
3751
}
3852

3953
c.StdoutWriter = &c.stdout

command_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func createBaseCommand(c *Command) *exec.Cmd {
9-
cmd := exec.Command("/bin/sh", "-Command", c.Command)
9+
cmd := exec.Command("/bin/sh", "-c", c.Command)
1010
return cmd
1111
}
1212

command_darwin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestCommand_ExecuteStderr(t *testing.T) {
1111
err := cmd.Execute()
1212

1313
assert.Nil(t, err)
14-
assert.Equal(t, "hello", cmd.Stderr())
14+
assert.Equal(t, "hello\n", cmd.Stderr())
1515
}
1616

1717
func TestCommand_WithTimeout(t *testing.T) {

command_linux_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cmd
22

33
import (
44
"github.com/stretchr/testify/assert"
5+
"io/ioutil"
6+
"os"
57
"strings"
68
"testing"
79
)
@@ -37,7 +39,7 @@ func TestCommand_WithValidTimeout(t *testing.T) {
3739
}
3840

3941
func TestCommand_WithWorkingDir(t *testing.T) {
40-
setWorkingDir := func (c *Command) {
42+
setWorkingDir := func(c *Command) {
4143
c.WorkingDir = "/tmp"
4244
}
4345

@@ -48,8 +50,19 @@ func TestCommand_WithWorkingDir(t *testing.T) {
4850
}
4951

5052
func TestCommand_WithStandardStreams(t *testing.T) {
53+
tmpFile, _ := ioutil.TempFile("/tmp", "stdout_")
54+
originalStdout := os.Stdout
55+
os.Stdout = tmpFile
56+
57+
// Reset os.Stdout to its original value
58+
defer func() {
59+
os.Stdout = originalStdout
60+
}()
61+
5162
cmd := NewCommand("echo hey", WithStandardStreams)
5263
cmd.Execute()
5364

54-
assert.Equal(t, "/tmp\n", cmd.Stdout())
55-
}
65+
r, err := ioutil.ReadFile(tmpFile.Name())
66+
assert.Nil(t, err)
67+
assert.Equal(t, "hey\n", string(r))
68+
}

command_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestCommand_Execute(t *testing.T) {
2222

2323
assert.Nil(t, err)
2424
assert.True(t, cmd.Executed())
25-
assert.Equal(t, cmd.Stdout(), "hello\n")
25+
assertEqualWithLineBreak(t, "hello", cmd.Stdout())
2626
}
2727

2828
func TestCommand_ExitCode(t *testing.T) {
@@ -44,7 +44,7 @@ func TestCommand_WithEnvVariables(t *testing.T) {
4444

4545
_ = cmd.Execute()
4646

47-
assert.Equal(t, cmd.Stdout(), "hey\n")
47+
assertEqualWithLineBreak(t, "hey", cmd.Stdout())
4848
}
4949

5050
func TestCommand_Executed(t *testing.T) {
@@ -77,7 +77,7 @@ func TestCommand_AddEnvWithShellVariable(t *testing.T) {
7777
err := c.Execute()
7878

7979
assert.Nil(t, err)
80-
assert.Equal(t, "test from shell\n", c.Stdout())
80+
assertEqualWithLineBreak(t, "test from shell", c.Stdout())
8181
}
8282

8383
func TestCommand_AddMultipleEnvWithShellVariable(t *testing.T) {
@@ -97,7 +97,7 @@ func TestCommand_AddMultipleEnvWithShellVariable(t *testing.T) {
9797
err := c.Execute()
9898

9999
assert.Nil(t, err)
100-
assert.Equal(t, "Hello world, I am Simon\n", c.Stdout())
100+
assertEqualWithLineBreak(t, "Hello world, I am Simon", c.Stdout())
101101
}
102102

103103
func getCommand() string {
@@ -149,5 +149,15 @@ func TestCommand_SetOptions(t *testing.T) {
149149

150150
assert.Nil(t, err)
151151
assert.Equal(t, time.Duration(1000000000), c.Timeout)
152-
assert.Equal(t, "test\n", writer.String())
153-
}
152+
assertEqualWithLineBreak(t, "test", writer.String())
153+
}
154+
155+
func assertEqualWithLineBreak(t *testing.T, expected string, actual string) {
156+
if runtime.GOOS == "windows" {
157+
expected = expected + "\r\n"
158+
} else {
159+
expected = expected + "\n"
160+
}
161+
162+
assert.Equal(t, expected, actual)
163+
}

command_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func createBaseCommand(c *Command) *exec.Cmd {
9-
cmd := exec.Command(`Command:\windows\system32\cmd.exe`, "/C", c.Command)
9+
cmd := exec.Command(`C:\windows\system32\cmd.exe`, "/C", c.Command)
1010
return cmd
1111
}
1212

0 commit comments

Comments
 (0)