Skip to content

Commit

Permalink
chore: test testdir finiding more thorughly and setup codecov config
Browse files Browse the repository at this point in the history
  • Loading branch information
areknoster committed Oct 11, 2024
1 parent d8cb35c commit ae99ad0
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 47 deletions.
23 changes: 0 additions & 23 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,6 @@ run:
# - src/external_libs
# - autogenerated_by_my_lib

# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: true

# which files to skip: they will be analyzed, but issues from them
# won't be reported. Default value is empty list, but there is
# no need to include all autogenerated files, we confidently recognize
# autogenerated files. If it's not please let us know.
# "/" will be replaced by current OS file path separator to properly work
# on Windows.
# skip-files:
# - ".*\\.my\\.go$"
# - lib/bad.go

# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
# If invoked with -mod=readonly, the go command is disallowed from the implicit
# automatic updating of go.mod described above. Instead, it fails when any changes
# to go.mod are needed. This setting is most useful to check that go.mod does
# not need updates, such as in a continuous integration and testing system.
# If invoked with -mod=vendor, the go command assumes that the vendor
# directory holds the correct copies of dependencies and ignores
# the dependency descriptions in go.mod.
# modules-download-mode: readonly|vendor|mod

# Allow multiple parallel golangci-lint instances running.
# If false (default) - golangci-lint acquires file lock on start.
Expand Down
7 changes: 7 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
coverage:
status:
project:
default:
target: auto
threshold: "2%"
paths: "!examples/"
63 changes: 63 additions & 0 deletions internal/testdata_dir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package internal

import (
"fmt"
"path/filepath"
"runtime"
"testing"

"github.com/areknoster/hypert"
)

type mockT struct {
errMsg string
hypert.T
}

func (m *mockT) Errorf(format string, args ...any) {
m.errMsg = fmt.Sprintf(format, args...)
}

func (m *mockT) Helper() {}

func (m *mockT) Name() string {
return "mockT"
}

func (m *mockT) Fatalf(format string, args ...any) {
m.errMsg = fmt.Sprintf(format, args...)
}

func TestDefaultTestDataDir(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
path := hypert.DefaultTestDataDir(t)
expected := filepath.Join(filepath.Dir(currentFilePath()), "testdata", t.Name())
if path != expected {
t.Fatalf("failed to get testdata path, got: %s, expected: %s", path, expected)
}
})

t.Run("when wrapped a few times it works fine", func(t *testing.T) {
mockT := &mockT{}
path := WrapTestDataDir(mockT, 1)
expected := filepath.Join(filepath.Dir(currentFilePath()), "testdata", mockT.Name())
if path != expected {
t.Fatalf("failed to get testdata path, got: %s, expected: %s", path, expected)
}
})
t.Run("when wrapped a lot it finally fails", func(t *testing.T) {
mockT := &mockT{}
_ = WrapTestDataDir(mockT, 21)
if mockT.errMsg == "" {
t.Fatalf("expected error, got none")
}
})
}

func currentFilePath() string {
_, file, _, ok := runtime.Caller(0)
if !ok {
panic("failed to get current file path")
}
return file
}
18 changes: 18 additions & 0 deletions internal/testdir_test_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package internal

import (
"github.com/areknoster/hypert"
)

func WrapTestDataDir(t hypert.T, wraps int) string {
wrap := func(prev func(t hypert.T) string) func(t hypert.T) string {
return func(t hypert.T) string {
return prev(t)
}
}
wrapped := hypert.DefaultTestDataDir
for i := 0; i < wraps; i++ {
wrapped = wrap(wrapped)
}
return wrapped(t)
}
2 changes: 1 addition & 1 deletion testdata_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// that the test data directory should be placed in.
func DefaultTestDataDir(t T) string {
t.Helper()
for i := 0; i < 8; i++ {
for i := 0; i < 20; i++ {
_, file, _, ok := runtime.Caller(i)
if !ok {
t.Fatalf("failed to get caller")
Expand Down
23 changes: 0 additions & 23 deletions testdata_dir_test.go

This file was deleted.

0 comments on commit ae99ad0

Please sign in to comment.