Skip to content

Commit deb84d5

Browse files
committed
Add assertion for different os line break inidicators
1 parent 0caa382 commit deb84d5

File tree

7 files changed

+24
-18
lines changed

7 files changed

+24
-18
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
before_install:
3333
- choco install make
3434
script:
35-
- make test
35+
- make test-windows
3636

3737
- name: Unit tests
3838
before_script:

command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ type Command struct {
2424
executed bool
2525
exitCode int
2626
// stderr and stdout retrieve the output after the command was executed
27-
stderr bytes.Buffer
28-
stdout bytes.Buffer
27+
stderr bytes.Buffer
28+
stdout bytes.Buffer
2929
}
3030

3131
// NewCommand creates a new command

command_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestCommand_WithStandardStreams(t *testing.T) {
6262
cmd := NewCommand("echo hey", WithStandardStreams)
6363
cmd.Execute()
6464

65-
r, err := ioutil.ReadFile(tmpFile.Name())
65+
r, err := ioutil.ReadFile(tmpFile.Name())
6666
assert.Nil(t, err)
6767
assert.Equal(t, "hey\n", string(r))
6868
}

command_test.go

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

2323
assert.Nil(t, err)
2424
assert.True(t, cmd.Executed())
25-
if runtime.GOOS == "windows" {
26-
assert.Equal(t, cmd.Stdout(), "hello\r\n")
27-
} else {
28-
assert.Equal(t, cmd.Stdout(), "hello\n")
29-
}
25+
assertEqualWithLineBreak(t, "hello", cmd.Stdout())
3026
}
3127

3228
func TestCommand_ExitCode(t *testing.T) {
@@ -48,7 +44,7 @@ func TestCommand_WithEnvVariables(t *testing.T) {
4844

4945
_ = cmd.Execute()
5046

51-
assert.Equal(t, cmd.Stdout(), "hey\n")
47+
assertEqualWithLineBreak(t, "hey", cmd.Stdout())
5248
}
5349

5450
func TestCommand_Executed(t *testing.T) {
@@ -81,7 +77,7 @@ func TestCommand_AddEnvWithShellVariable(t *testing.T) {
8177
err := c.Execute()
8278

8379
assert.Nil(t, err)
84-
assert.Equal(t, "test from shell\n", c.Stdout())
80+
assertEqualWithLineBreak(t, "test from shell", c.Stdout())
8581
}
8682

8783
func TestCommand_AddMultipleEnvWithShellVariable(t *testing.T) {
@@ -101,7 +97,7 @@ func TestCommand_AddMultipleEnvWithShellVariable(t *testing.T) {
10197
err := c.Execute()
10298

10399
assert.Nil(t, err)
104-
assert.Equal(t, "Hello world, I am Simon\n", c.Stdout())
100+
assertEqualWithLineBreak(t, "Hello world, I am Simon", c.Stdout())
105101
}
106102

107103
func getCommand() string {
@@ -153,5 +149,15 @@ func TestCommand_SetOptions(t *testing.T) {
153149

154150
assert.Nil(t, err)
155151
assert.Equal(t, time.Duration(1000000000), c.Timeout)
156-
assert.Equal(t, "test\n", writer.String())
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)
157163
}

command_windows_test.go

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

1414
assert.Nil(t, err)
15-
assert.Equal(t, "hello ", cmd.Stderr())
15+
assertEqualWithLineBreak(t, "hello ", cmd.Stderr())
1616
}
1717

1818
func TestCommand_WithTimeout(t *testing.T) {

examples/standard_streams.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package main
1+
package examples
22

33
import "github.com/SimonBaeumer/cmd"
44

5-
func main() {
5+
func CreateNewCommandWithStandardStream() {
66
c := cmd.NewCommand("echo hello; sleep 1; echo another;", cmd.WithStandardStreams)
77
c.Execute()
88
}

examples/working_dir.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package main
1+
package examples
22

33
import "github.com/SimonBaeumer/cmd"
44

5-
func main() {
5+
func CreateWithWorkingDir() {
66
setWorkingDir := func(c *cmd.Command) {
77
c.WorkingDir = "/tmp"
88
}

0 commit comments

Comments
 (0)