-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
357 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
version: 2 | ||
jobs: | ||
build: | ||
docker: | ||
- image: circleci/golang:1.10.1-stretch | ||
working_directory: /go/src/github.com/timdp/lwc | ||
environment: | ||
TEST_RESULTS: /tmp/test-results | ||
steps: | ||
- checkout | ||
- run: mkdir -p $TEST_RESULTS | ||
- restore_cache: | ||
keys: | ||
- v1-pkg-cache | ||
- run: go get github.com/jstemmer/go-junit-report | ||
- run: | ||
command: | | ||
trap "go-junit-report < $TEST_RESULTS/go-test.out > $TEST_RESULTS/go-test-report.xml" EXIT | ||
go test -v $( go list ./... ) | tee $TEST_RESULTS/go-test.out | ||
- save_cache: | ||
key: v1-pkg-cache | ||
paths: | ||
- /go/pkg | ||
- store_test_results: | ||
path: /tmp/test-results | ||
when: always | ||
- store_artifacts: | ||
path: /tmp/test-results | ||
destination: test-results | ||
when: always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package lwc | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type configTest struct { | ||
args []string | ||
expected Config | ||
} | ||
|
||
var configTests = []configTest{ | ||
{ | ||
[]string{}, | ||
Config{ | ||
true, true, false, true, false, | ||
time.Duration(DEFAULT_INTERVAL) * time.Millisecond, | ||
false, false, | ||
[]string{}, | ||
nil, | ||
}, | ||
}, | ||
{ | ||
[]string{"-w", "--lines"}, | ||
Config{ | ||
true, true, false, false, false, | ||
time.Duration(DEFAULT_INTERVAL) * time.Millisecond, | ||
false, false, | ||
[]string{}, | ||
nil, | ||
}, | ||
}, | ||
{ | ||
[]string{"foo"}, | ||
Config{ | ||
true, true, false, true, false, | ||
time.Duration(DEFAULT_INTERVAL) * time.Millisecond, | ||
false, false, | ||
[]string{"foo"}, | ||
nil, | ||
}, | ||
}, | ||
{ | ||
[]string{"--", "/path/to/file"}, | ||
Config{ | ||
true, true, false, true, false, | ||
time.Duration(DEFAULT_INTERVAL) * time.Millisecond, | ||
false, false, | ||
[]string{"/path/to/file"}, | ||
nil, | ||
}, | ||
}, | ||
{ | ||
[]string{"--max-line-length", "--bytes", "/etc/passwd", "/etc/group"}, | ||
Config{ | ||
false, false, false, true, true, | ||
time.Duration(DEFAULT_INTERVAL) * time.Millisecond, | ||
false, false, | ||
[]string{"/etc/passwd", "/etc/group"}, | ||
nil, | ||
}, | ||
}, | ||
{ | ||
[]string{"-i", "5000"}, | ||
Config{ | ||
true, true, false, true, false, | ||
time.Duration(5000) * time.Millisecond, | ||
false, false, | ||
[]string{}, | ||
nil, | ||
}, | ||
}, | ||
{ | ||
[]string{"--interval=2000"}, | ||
Config{ | ||
true, true, false, true, false, | ||
time.Duration(2000) * time.Millisecond, | ||
false, false, | ||
[]string{}, | ||
nil, | ||
}, | ||
}, | ||
{ | ||
[]string{"--interval", "3000"}, | ||
Config{ | ||
true, true, false, true, false, | ||
time.Duration(3000) * time.Millisecond, | ||
false, false, | ||
[]string{}, | ||
nil, | ||
}, | ||
}, | ||
{ | ||
[]string{"-i", "0"}, | ||
Config{ | ||
true, true, false, true, false, | ||
time.Duration(0), | ||
false, false, | ||
[]string{}, | ||
nil, | ||
}, | ||
}, | ||
} | ||
|
||
func TestBuildConfig(t *testing.T) { | ||
for i, test := range configTests { | ||
actual := BuildConfig(append([]string{"lwc"}, test.args...)) | ||
// Clear getopt Set because we don't want to compare it | ||
actual.g = nil | ||
if !reflect.DeepEqual(test.expected, actual) { | ||
t.Errorf("Test #%d failed: expecting %#v, got %#v", i, test.expected, actual) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package lwc | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
type formatTest struct { | ||
counts []uint64 | ||
label string | ||
cr bool | ||
lf bool | ||
} | ||
|
||
func (t *formatTest) expected() []string { | ||
result := make([]string, len(t.counts)) | ||
for i, num := range t.counts { | ||
result[i] = fmt.Sprintf("%d", num) | ||
} | ||
if t.label != "" { | ||
result = append(result, t.label) | ||
} | ||
return result | ||
} | ||
|
||
func withWithout(b bool) string { | ||
if b { | ||
return "with" | ||
} else { | ||
return "without" | ||
} | ||
} | ||
|
||
func tokenize(str string) []string { | ||
tokens := make([]string, 100) | ||
count := 0 | ||
scanner := bufio.NewScanner(strings.NewReader(str)) | ||
scanner.Split(bufio.ScanWords) | ||
for scanner.Scan() { | ||
tokens[count] = scanner.Text() | ||
count++ | ||
} | ||
return tokens[0:count] | ||
} | ||
|
||
var formatTests = []formatTest{ | ||
{ | ||
[]uint64{42939}, | ||
"", | ||
false, | ||
false, | ||
}, | ||
{ | ||
[]uint64{42, 2993}, | ||
"bar", | ||
true, | ||
false, | ||
}, | ||
{ | ||
[]uint64{90210}, | ||
"baz-quux", | ||
false, | ||
true, | ||
}, | ||
{ | ||
[]uint64{123, 4567, 899999}, | ||
"/etc/passwd", | ||
true, | ||
true, | ||
}, | ||
} | ||
|
||
func TestFormatCounts(t *testing.T) { | ||
for i, test := range formatTests { | ||
result := FormatCounts(&test.counts, test.label, test.cr, test.lf) | ||
hasCr := strings.HasPrefix(result, "\r") | ||
if test.cr != hasCr { | ||
t.Errorf("Test #%d failed: expecting string %s LF", i, withWithout(test.lf)) | ||
} | ||
hasLf := strings.HasSuffix(result, "\n") | ||
if test.lf != hasLf { | ||
t.Errorf("Test #%d failed: expecting string %s LF", i, withWithout(test.lf)) | ||
} | ||
actual := tokenize(result) | ||
expected := test.expected() | ||
if !reflect.DeepEqual(expected, actual) { | ||
t.Errorf("Test #%d failed: expecting %#v, got %#v", i, expected, actual) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.