|
1 | 1 | package exec
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "fmt"
|
| 6 | + "runtime" |
5 | 7 | "testing"
|
6 | 8 |
|
7 | 9 | "github.com/influxdata/telegraf"
|
@@ -46,6 +48,29 @@ cpu,cpu=cpu5,host=foo,datacenter=us-east usage_idle=99,usage_busy=1
|
46 | 48 | cpu,cpu=cpu6,host=foo,datacenter=us-east usage_idle=99,usage_busy=1
|
47 | 49 | `
|
48 | 50 |
|
| 51 | +type CarriageReturnTest struct { |
| 52 | + input []byte |
| 53 | + output []byte |
| 54 | +} |
| 55 | + |
| 56 | +var crTests = []CarriageReturnTest{ |
| 57 | + {[]byte{0x4c, 0x69, 0x6e, 0x65, 0x20, 0x31, 0x0d, 0x0a, 0x4c, 0x69, |
| 58 | + 0x6e, 0x65, 0x20, 0x32, 0x0d, 0x0a, 0x4c, 0x69, 0x6e, 0x65, |
| 59 | + 0x20, 0x33}, |
| 60 | + []byte{0x4c, 0x69, 0x6e, 0x65, 0x20, 0x31, 0x0a, 0x4c, 0x69, 0x6e, |
| 61 | + 0x65, 0x20, 0x32, 0x0a, 0x4c, 0x69, 0x6e, 0x65, 0x20, 0x33}}, |
| 62 | + {[]byte{0x4c, 0x69, 0x6e, 0x65, 0x20, 0x31, 0x0a, 0x4c, 0x69, 0x6e, |
| 63 | + 0x65, 0x20, 0x32, 0x0a, 0x4c, 0x69, 0x6e, 0x65, 0x20, 0x33}, |
| 64 | + []byte{0x4c, 0x69, 0x6e, 0x65, 0x20, 0x31, 0x0a, 0x4c, 0x69, 0x6e, |
| 65 | + 0x65, 0x20, 0x32, 0x0a, 0x4c, 0x69, 0x6e, 0x65, 0x20, 0x33}}, |
| 66 | + {[]byte{0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x6c, |
| 67 | + 0x6c, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x62, 0x69, 0x67, 0x20, |
| 68 | + 0x6c, 0x69, 0x6e, 0x65}, |
| 69 | + []byte{0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x6c, |
| 70 | + 0x6c, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x62, 0x69, 0x67, 0x20, |
| 71 | + 0x6c, 0x69, 0x6e, 0x65}}, |
| 72 | +} |
| 73 | + |
49 | 74 | type runnerMock struct {
|
50 | 75 | out []byte
|
51 | 76 | err error
|
@@ -217,3 +242,21 @@ func TestExecCommandWithoutGlobAndPath(t *testing.T) {
|
217 | 242 | }
|
218 | 243 | acc.AssertContainsFields(t, "metric", fields)
|
219 | 244 | }
|
| 245 | + |
| 246 | +func TestRemoveCarriageReturns(t *testing.T) { |
| 247 | + if runtime.GOOS == "windows" { |
| 248 | + // Test that all carriage returns are removed |
| 249 | + for _, test := range crTests { |
| 250 | + b := bytes.NewBuffer(test.input) |
| 251 | + out := removeCarriageReturns(*b) |
| 252 | + assert.True(t, bytes.Equal(test.output, out.Bytes())) |
| 253 | + } |
| 254 | + } else { |
| 255 | + // Test that the buffer is returned unaltered |
| 256 | + for _, test := range crTests { |
| 257 | + b := bytes.NewBuffer(test.input) |
| 258 | + out := removeCarriageReturns(*b) |
| 259 | + assert.True(t, bytes.Equal(test.input, out.Bytes())) |
| 260 | + } |
| 261 | + } |
| 262 | +} |
0 commit comments