Skip to content

Commit

Permalink
Merge pull request #34 from ktr0731/tweak-ci
Browse files Browse the repository at this point in the history
tweak code related to CI (linting and coverage)
  • Loading branch information
ktr0731 authored Dec 25, 2020
2 parents 2a033e9 + c18bf41 commit 8ba8c46
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 38 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-18.04, windows-2019, macOS-10.14]
go: ['1.13']
os: [ubuntu-latest, windows-latest, macOS-latest]
go: ['1.14']
steps:
- name: Set up Go ${{ matrix.go }}
uses: actions/setup-go@v1
Expand All @@ -32,6 +32,12 @@ jobs:
- name: Test
run: go test -v -coverpkg ./... -covermode atomic -coverprofile coverage.txt -tags fuzz -numCases 3000 -numEvents 10 ./...

- name: Lint
uses: golangci/golangci-lint-action@v2
if: matrix.os == 'ubuntu-latest'
with:
version: v1.33.0

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
Expand Down
56 changes: 56 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
linters-settings:
gofmt:
simplify: false

linters:
enable:
- bodyclose
- deadcode
- dogsled
- dupl
- errcheck
- gocritic
- gofmt
- goimports
- golint
- gosec
- govet
- ineffassign
- misspell
- nakedret
- prealloc
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- varcheck
- whitespace
- interfacer
- asciicheck
- depguard
- godot
- gomodguard
- goprintffuncname
- maligned
- nolintlint
- rowserrcheck
- errorlint
- exhaustivestruct
- exportloopref
- gci
disable:
- gosimple
- unused
- scopelint

issues:
exclude:
- G307 # gosec
exclude-rules:
- path: _test\.go
linters:
- golint
- maligned
- misspell
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ comment:

ignore:
- tcell.go
- mock.go
7 changes: 4 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func ExampleFind_previewWindow() {
{"id2", "bar"},
{"id3", "baz"},
}
fuzzyfinder.Find(
idx, _ := fuzzyfinder.Find(
slice,
func(i int) string {
return fmt.Sprintf("[%s] %s", slice[i].id, slice[i].name)
Expand All @@ -49,6 +49,7 @@ func ExampleFind_previewWindow() {
}
return s
}))
fmt.Println(slice[idx]) // The selected item.
}

func ExampleFindMulti() {
Expand Down Expand Up @@ -78,12 +79,12 @@ func ExampleTerminalMock() {
term.InjectKey(tcell.KeyEsc, rune(tcell.KeyEsc), tcell.ModNone)

slice := []string{"foo", "bar", "baz"}
fuzzyfinder.Find(slice, func(i int) string { return slice[i] })
_, _ = fuzzyfinder.Find(slice, func(i int) string { return slice[i] })

// Write out the execution result to a temp file.
// We can test it by the golden files testing pattern.
//
// See https://speakerdeck.com/mitchellh/advanced-testing-with-go?slide=19
result := term.GetResult()
ioutil.WriteFile("ui.out", []byte(result), 0644)
_ = ioutil.WriteFile("ui.out", []byte(result), 0600)
}
22 changes: 12 additions & 10 deletions fuzzyfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (f *finder) _draw() {
// prompt line
var promptLinePad int

//nolint:staticcheck
for _, r := range []rune(f.opt.promptString) {
style := tcell.StyleDefault.
Foreground(tcell.ColorBlue).
Expand Down Expand Up @@ -228,7 +229,6 @@ func (f *finder) _draw() {
if w+rw+2 > maxWidth {
f.term.SetContent(w, height-3-i, '.', nil, style)
f.term.SetContent(w+1, height-3-i, '.', nil, style)
w += 2
break
} else {
f.term.SetContent(w, height-3-i, r, nil, style)
Expand Down Expand Up @@ -260,11 +260,12 @@ func (f *finder) _drawPreview() {
// top line
for i := width / 2; i < width; i++ {
var r rune
if i == width/2 {
switch {
case i == width/2:
r = '┌'
} else if i == width-1 {
case i == width-1:
r = '┐'
} else {
default:
r = '─'
}

Expand All @@ -277,11 +278,12 @@ func (f *finder) _drawPreview() {
// bottom line
for i := width / 2; i < width; i++ {
var r rune
if i == width/2 {
switch {
case i == width/2:
r = '└'
} else if i == width-1 {
case i == width-1:
r = '┘'
} else {
default:
r = '─'
}

Expand Down Expand Up @@ -575,7 +577,7 @@ func (f *finder) find(slice interface{}, itemFunc func(i int) string, opts []Opt
matched := make([]matching.Matched, sliceLen)
for i := 0; i < sliceLen; i++ {
items[i] = itemFunc(i)
matched[i] = matching.Matched{Idx: i}
matched[i] = matching.Matched{Idx: i} //nolint:exhaustivestruct
}
return items, matched
}
Expand Down Expand Up @@ -646,9 +648,9 @@ func (f *finder) find(slice interface{}, itemFunc func(i int) string, opts []Opt
time.Sleep(50 * time.Millisecond)
}
switch {
case err == ErrAbort:
case errors.Is(err, ErrAbort):
return nil, ErrAbort
case err == errEntered:
case errors.Is(err, errEntered):
f.stateMu.RLock()
defer f.stateMu.RUnlock()

Expand Down
42 changes: 29 additions & 13 deletions fuzzyfinder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fuzzyfinder_test
import (
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/gdamore/tcell/v2"
"github.com/google/go-cmp/cmp"
fuzzyfinder "github.com/ktr0731/go-fuzzyfinder"
"github.com/pkg/errors"
)

var (
Expand All @@ -25,8 +27,12 @@ func init() {
testing.Init()
flag.Parse()
if *update {
os.RemoveAll(filepath.Join("testdata", "fixtures"))
os.MkdirAll(filepath.Join("testdata", "fixtures"), 0755)
if err := os.RemoveAll(filepath.Join("testdata", "fixtures")); err != nil {
log.Fatalf("RemoveAll should not return an error, but got '%s'", err)
}
if err := os.MkdirAll(filepath.Join("testdata", "fixtures"), 0755); err != nil {
log.Fatalf("MkdirAll should not return an error, but got '%s'", err)
}
}
}

Expand All @@ -50,7 +56,7 @@ func assertWithGolden(t *testing.T, f func(t *testing.T) string) {
fname := normalizeFilename(name)

if *update {
if err := ioutil.WriteFile(fname, []byte(actual), 0644); err != nil {
if err := ioutil.WriteFile(fname, []byte(actual), 0600); err != nil {
t.Fatalf("failed to update the golden file: %s", err)
}
return
Expand All @@ -63,7 +69,7 @@ func assertWithGolden(t *testing.T, f func(t *testing.T) string) {
}
expected := string(b)
if runtime.GOOS == "windows" {
expected = strings.Replace(expected, "\r\n", "\n", -1)
expected = strings.ReplaceAll(expected, "\r\n", "\n")
}

if diff := cmp.Diff(expected, actual); diff != "" {
Expand Down Expand Up @@ -180,6 +186,8 @@ func TestFind(t *testing.T) {
}

for name, c := range cases {
c := c

t.Run(name, func(t *testing.T) {
events := c.events

Expand All @@ -201,7 +209,7 @@ func TestFind(t *testing.T) {
}),
fuzzyfinder.WithMode(fuzzyfinder.ModeCaseSensitive),
)
if err != fuzzyfinder.ErrAbort {
if !errors.Is(err, fuzzyfinder.ErrAbort) {
t.Fatalf("Find must return ErrAbort, but got '%s'", err)
}

Expand Down Expand Up @@ -239,7 +247,7 @@ func TestFind_hotReload(t *testing.T) {
fuzzyfinder.WithMode(fuzzyfinder.ModeCaseSensitive),
fuzzyfinder.WithHotReload(),
)
if err != fuzzyfinder.ErrAbort {
if !errors.Is(err, fuzzyfinder.ErrAbort) {
t.Fatalf("Find must return ErrAbort, but got '%s'", err)
}

Expand All @@ -254,12 +262,13 @@ func TestFind_enter(t *testing.T) {
expected int
}{
"initial": {events: keys(input{tcell.KeyTab, rune(tcell.KeyTab), tcell.ModNone}), expected: 0},
"mode smart to case-sensitive": {events: append(runes("JI")), expected: 7},
"mode smart to case-sensitive": {events: runes("JI"), expected: 7},
}

for name, c := range cases {
c := c

t.Run(name, func(t *testing.T) {
c := c
events := c.events

f, term := fuzzyfinder.NewWithMockedTerminal()
Expand Down Expand Up @@ -322,14 +331,15 @@ func TestFindMulti(t *testing.T) {
{tcell.KeyTab, rune(tcell.KeyTab), tcell.ModNone},
{tcell.KeyTab, rune(tcell.KeyTab), tcell.ModNone},
}...), expected: []int{0}},
"empty result": {events: append(runes("fffffff")), abort: true},
"empty result": {events: runes("fffffff"), abort: true},
"resize window": {events: []tcell.Event{
tcell.NewEventResize(10, 10),
}, expected: []int{0}},
}
for name, c := range cases {
c := c

t.Run(name, func(t *testing.T) {
c := c
events := c.events

f, term := fuzzyfinder.NewWithMockedTerminal()
Expand All @@ -349,7 +359,7 @@ func TestFindMulti(t *testing.T) {
}),
)
if c.abort {
if err != fuzzyfinder.ErrAbort {
if !errors.Is(err, fuzzyfinder.ErrAbort) {
t.Fatalf("Find must return ErrAbort, but got '%s'", err)
}
return
Expand All @@ -374,7 +384,7 @@ func BenchmarkFind(b *testing.B) {
events := append(runes("adrele!!"), key(input{tcell.KeyEsc, rune(tcell.KeyEsc), tcell.ModNone}))
term.SetEventsV2(events...)

f.Find(
_, err := f.Find(
tracks,
func(i int) string {
return tracks[i].Name
Expand All @@ -386,6 +396,9 @@ func BenchmarkFind(b *testing.B) {
return "Name: " + tracks[i].Name + "\nArtist: " + tracks[i].Artist
}),
)
if err != nil {
b.Fatalf("should not return an error, but got '%s'", err)
}
}
})

Expand All @@ -397,7 +410,7 @@ func BenchmarkFind(b *testing.B) {
events := append(runes("adrele!!"), key(input{tcell.KeyEsc, rune(tcell.KeyEsc), tcell.ModNone}))
term.SetEventsV2(events...)

f.Find(
_, err := f.Find(
&tracks,
func(i int) string {
return tracks[i].Name
Expand All @@ -410,6 +423,9 @@ func BenchmarkFind(b *testing.B) {
}),
fuzzyfinder.WithHotReload(),
)
if err != nil {
b.Fatalf("should not return an error, but got '%s'", err)
}
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions matching/matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func match(input string, slice []string, opt opt) (res []Matched) {
s = strings.ToLower(s)
}
LINE_MATCHING:
for _, r := range []rune(s) {
for _, r := range []rune(s) { //nolint:staticcheck
if r == in[idx] {
idx++
if idx == len(in) {
Expand All @@ -95,5 +95,5 @@ func match(input string, slice []string, opt opt) (res []Matched) {
}
}
}
return
return res
}
Loading

0 comments on commit 8ba8c46

Please sign in to comment.