Skip to content

Commit 0863f63

Browse files
feat: add workflow; fix dbtest
1 parent 7516b94 commit 0863f63

File tree

3 files changed

+60
-30
lines changed

3 files changed

+60
-30
lines changed

.github/workflows/test.yaml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
go-version: [ '1.22.x' ]
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
- name: Setup Go ${{ matrix.go-version }}
18+
uses: actions/setup-go@v5
19+
with:
20+
go-version: ${{ matrix.go-version }}
21+
22+
- name: Restore Cache
23+
uses: actions/cache@v4
24+
with:
25+
path: |
26+
~/.cache/go-build
27+
~/go/pkg/mod
28+
key: ${{ runner.os }}-v1-go-${{ hashFiles('**/go.sum') }}
29+
restore-keys: |
30+
${{ runner.os }}-go-
31+
32+
- name: Run Tests
33+
run: go test -v ./...
34+
35+
lint:
36+
name: lint
37+
runs-on: ubuntu-latest
38+
steps:
39+
- uses: actions/checkout@v4
40+
- uses: actions/setup-go@v5
41+
with:
42+
go-version: stable
43+
- name: golangci-lint
44+
uses: golangci/golangci-lint-action@v6
45+
with:
46+
version: v1.58

authentication/password/handler_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,16 @@ func TestUserRegistration(t *testing.T) {
6969
)
7070
})
7171

72-
// t.Run("user already exists", func(t *testing.T) {
73-
// must.Must1(db.Reset(ctx))
74-
//
75-
// // TODO: implement
76-
// })
72+
t.Run("user already exists", func(t *testing.T) {
73+
must.Must1(db.Reset(ctx))
74+
})
7775
}
7876

7977
func TestUserLogin(t *testing.T) {
8078
ctx := context.Background()
8179
db := dbtest.Must(ctx, t)
8280

8381
t.Run("user not found", func(t *testing.T) {
84-
defer db.TruncateAllTables(ctx)
82+
must.Must1(db.Reset(ctx))
8583
})
8684
}

sql/db/dbtest/dbtest.go

+10-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
// Package dbtesting providers utilities for testing database related code.
2+
//
3+
// It provides a set of utility functions to initialize a database pool,
4+
// load DLL schema, clean it up, etc.
5+
//
6+
// Note the package is intended to be used only within testing files.
27
package dbtest
38

49
import (
@@ -8,7 +13,6 @@ import (
813
"io"
914
"os"
1015
"path/filepath"
11-
"runtime"
1216
"strings"
1317
"sync"
1418
"testing"
@@ -22,10 +26,6 @@ import (
2226
"go.inout.gg/common/must"
2327
)
2428

25-
var (
26-
MaxConfigLookupCallerDepth = 10
27-
)
28-
2929
const (
3030
queryFetchAllTables = `
3131
SELECT table_name
@@ -58,18 +58,18 @@ type Config struct {
5858

5959
// MustLoadConfig loads the configuration from the environment.
6060
//
61-
// If no paths are provided, it defaults to ".test.env" in the current module path
61+
// If no paths are provided, it defaults to ".test.env" in the current
62+
// working directory (which for tests is the directory in which they are located at),
6263
// and in the root of the project.
6364
//
6465
// It panics if there is an error loading the configuration.
6566
func MustLoadConfig(paths ...string) *Config {
6667
if len(paths) == 0 {
67-
callingModulePath := lookupLastCallingModulePath(4)
68-
69-
rootPath := findModuleRoot(callingModulePath)
68+
currentModulePath := must.Must(os.Getwd())
69+
rootPath := findModuleRoot(currentModulePath)
7070
paths = []string{
7171
filepath.Join(rootPath, ".test.env"),
72-
filepath.Join(callingModulePath, ".test.env"),
72+
filepath.Join(currentModulePath, ".test.env"),
7373
}
7474
}
7575

@@ -78,20 +78,6 @@ func MustLoadConfig(paths ...string) *Config {
7878
return config
7979
}
8080

81-
func lookupLastCallingModulePath(m int) string {
82-
var p string
83-
for i := 0; i < m; i++ {
84-
_, basepath, _, ok := runtime.Caller(i)
85-
if !ok {
86-
break
87-
}
88-
89-
p = filepath.Dir(basepath)
90-
}
91-
92-
return p
93-
}
94-
9581
// DB is a wrapper around pgxpool.Pool with useful utilities for DB management
9682
// in tests.
9783
type DB struct {

0 commit comments

Comments
 (0)