Skip to content

Commit 7b96fac

Browse files
authored
test: fix tests locally on windows, additional dev setup directions (#129)
* chore: try running tests on windows runner too * chore: turn off running tests on windows runners (fails, seems like it tries to run other containers as windows) * fix: multi target upload true in sample config so tests pass out the gates * fix: make logging test platform-agnostic * chore: additional setup directions, improved local testing experience
1 parent 4448907 commit 7b96fac

File tree

8 files changed

+87
-53
lines changed

8 files changed

+87
-53
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ vendor/
6565
/.direnv
6666

6767
/config*.json
68-
!config.sample.json
68+
!config.sample.json
69+
70+
coverage.txt

.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"ethan-reesor.exp-vscode-go"
4+
]
5+
}

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exp-vscode-go.testExplorer.enable": true
3+
}

README.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,16 @@ The Satisfactory Mod Repository backend API - a Go-based service providing REST
3838
mise run api
3939
```
4040

41-
If running `api` produces errors about the database failing to apply migrations,
42-
you may have switched branches without cleaning up after database changes you were working on.
43-
The easiest way to get back from this state
44-
is to delete the `postgres` Docker container
45-
and delete all not-in-use volumes so it creates a fresh one.
41+
If running `api` produces errors about the database failing to apply migrations,
42+
you may have switched branches without cleaning up after database changes you were working on.
43+
The easiest way to get back from this state
44+
is to delete the `postgres` Docker container
45+
and delete all not-in-use volumes so it creates a fresh one.
46+
47+
3. Set up the Configuration File
48+
49+
Create a copy of `config.sample.json` as `config.json` and fill in the required fields.
50+
See the [Configuration](#configuration) section for details.
4651

4752
## Development Commands
4853

@@ -54,7 +59,7 @@ mise run generate
5459
mise run api
5560

5661
# Testing
57-
mise run test
62+
mise run localtest
5863
mise run coverage
5964

6065
# Linting
@@ -68,6 +73,17 @@ mise run teardown # Stop services
6873
mise activate pwsh | Out-String | Invoke-Expression
6974
```
7075

76+
### Local Testing
77+
78+
To run specific tests:
79+
80+
- In VSCode, the [Go Companion](https://marketplace.visualstudio.com/items?itemName=ethan-reesor.exp-vscode-go) suggested extension adds Go test support to VSCode's testing integration.
81+
Note you may need to reload VSCode (`Developer: Reload Window`) after adding or removing tests.
82+
- From the command line, use `go test -v run TestNameHere`
83+
84+
To run all tests on your local machine, use `mise run localtest`, which excludes the verbose flag for easier human parsing of test output.
85+
It is expected for the command to output `[no test files]` for packages with no tests.
86+
7187
### Database Access
7288

7389
The development docker compose also includes a pgadmin container for testing database related stuff
@@ -101,16 +117,17 @@ You can view tables via `Servers >(name)> Databases > postgres > Schemas > publi
101117

102118
## Configuration
103119

104-
Create `config.json` or use environment variables with `REPO_` prefix.
120+
Create `config.json`, or use environment variables with `REPO_` prefix.
121+
A sample is provided: `config.sample.json`.
105122

106123
**Required services:**
107124

108125
1. **PostgreSQL** - Database (dev: port 5432)
109126
2. **Redis** - Cache/sessions (dev: port 6379)
110127
3. **MinIO** - Object storage (dev: ports 9000/9001)
111-
4. **OAuth providers** - GitHub, Google, Facebook
112-
5. **PASETO keys** - Generate with `go run cmd/paseto/main.go`
113-
6. **VirusTotal API key** - For mod scanning
128+
4. **OAuth providers** - GitHub, Google, Facebook. For testing purposes, [setting up just GitHub](https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/creating-an-oauth-app) is the easiest.
129+
5. **PASETO keys** - Generate these with `go run cmd/paseto/main.go`
130+
6. **VirusTotal API key** - For mod scanning. Optional for testing.
114131

115132
**Development services** are started automatically with `mise run setup`. MinIO configuration is included in the setup task.
116133

config.sample.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
},
5757

5858
"feature_flags": {
59-
"allow_multi_target_upload": false
59+
"allow_multi_target_upload": true
6060
},
6161

6262
"loki_endpoint": "http://localhost:3100/loki/api/v1/push",

logging/log_test.go

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ package logging
22

33
import (
44
"bytes"
5-
"context"
6-
"log/slog"
5+
"path/filepath"
76
"regexp"
8-
"sync"
97
"testing"
108

119
"github.com/MarvinJWendt/testza"
12-
"github.com/Vilsol/slox"
1310
"github.com/spf13/viper"
1411
)
1512

@@ -18,29 +15,18 @@ func TestLoggingDev(t *testing.T) {
1815

1916
testza.AssertNoError(t, SetupLogger(buffer))
2017

21-
slog.Info("A: 1")
22-
slox.Info(context.Background(), "B: 2")
23-
24-
func() {
25-
slox.Info(context.Background(), "C: 3")
26-
}()
27-
28-
var wg sync.WaitGroup
29-
wg.Add(1)
30-
go func() {
31-
defer wg.Done()
32-
slox.Info(context.Background(), "D: 4")
33-
}()
34-
wg.Wait()
18+
PrintLoggingDummies()
3519

3620
out := regexp.MustCompile(`\x1b\[\d+m`).ReplaceAll(buffer.Bytes(), []byte{})
3721

3822
lines := bytes.Split(out, []byte("\n"))
3923

40-
testza.AssertContains(t, string(lines[0]), "INF logging/log_test.go:21 A: 1 service=api")
41-
testza.AssertContains(t, string(lines[1]), "INF logging/log_test.go:22 B: 2 service=api")
42-
testza.AssertContains(t, string(lines[2]), "INF logging/log_test.go:25 C: 3 service=api")
43-
testza.AssertContains(t, string(lines[3]), "INF logging/log_test.go:32 D: 4 service=api")
24+
path := filepath.Join("logging", "logging_dummies.go")
25+
26+
testza.AssertContains(t, string(lines[0]), "INF "+path+":16 A: 1 service=api")
27+
testza.AssertContains(t, string(lines[1]), "INF "+path+":17 B: 2 service=api")
28+
testza.AssertContains(t, string(lines[2]), "INF "+path+":20 C: 3 service=api")
29+
testza.AssertContains(t, string(lines[3]), "INF "+path+":27 D: 4 service=api")
4430

4531
if t.Failed() {
4632
println(buffer.String())
@@ -53,27 +39,14 @@ func TestLoggingProd(t *testing.T) {
5339

5440
testza.AssertNoError(t, SetupLogger(buffer))
5541

56-
slog.Info("A: 1")
57-
slox.Info(context.Background(), "B: 2")
58-
59-
func() {
60-
slox.Info(context.Background(), "C: 3")
61-
}()
62-
63-
var wg sync.WaitGroup
64-
wg.Add(1)
65-
go func() {
66-
defer wg.Done()
67-
slox.Info(context.Background(), "D: 4")
68-
}()
69-
wg.Wait()
42+
PrintLoggingDummies()
7043

7144
lines := bytes.Split(buffer.Bytes(), []byte("\n"))
7245

73-
testza.AssertTrue(t, regexp.MustCompile(`\{"time":".+?","level":"INFO","source":\{"function":"github.com/satisfactorymodding/smr-api/logging.TestLoggingProd","file":".*?/logging/log_test.go","line":56},"msg":"A: 1"}`).Match(lines[0]))
74-
testza.AssertTrue(t, regexp.MustCompile(`\{"time":".+?","level":"INFO","source":\{"function":"github.com/satisfactorymodding/smr-api/logging.TestLoggingProd","file":".*?/logging/log_test.go","line":57},"msg":"B: 2"}`).Match(lines[1]))
75-
testza.AssertTrue(t, regexp.MustCompile(`\{"time":".+?","level":"INFO","source":\{"function":"github.com/satisfactorymodding/smr-api/logging.TestLoggingProd.func1","file":".*?/logging/log_test.go","line":60},"msg":"C: 3"}`).Match(lines[2]))
76-
testza.AssertTrue(t, regexp.MustCompile(`\{"time":".+?","level":"INFO","source":\{"function":"github.com/satisfactorymodding/smr-api/logging.TestLoggingProd.func2","file":".*?/logging/log_test.go","line":67},"msg":"D: 4"}`).Match(lines[3]))
46+
testza.AssertTrue(t, regexp.MustCompile(`\{"time":".+?","level":"INFO","source":\{"function":"github.com/satisfactorymodding/smr-api/logging.PrintLoggingDummies","file":".*?/logging/logging_dummies.go","line":16},"msg":"A: 1"}`).Match(lines[0]))
47+
testza.AssertTrue(t, regexp.MustCompile(`\{"time":".+?","level":"INFO","source":\{"function":"github.com/satisfactorymodding/smr-api/logging.PrintLoggingDummies","file":".*?/logging/logging_dummies.go","line":17},"msg":"B: 2"}`).Match(lines[1]))
48+
testza.AssertTrue(t, regexp.MustCompile(`\{"time":".+?","level":"INFO","source":\{"function":"github.com/satisfactorymodding/smr-api/logging.PrintLoggingDummies.func1","file":".*?/logging/logging_dummies.go","line":20},"msg":"C: 3"}`).Match(lines[2]))
49+
testza.AssertTrue(t, regexp.MustCompile(`\{"time":".+?","level":"INFO","source":\{"function":"github.com/satisfactorymodding/smr-api/logging.PrintLoggingDummies.func2","file":".*?/logging/logging_dummies.go","line":27},"msg":"D: 4"}`).Match(lines[3]))
7750

7851
if t.Failed() {
7952
println(buffer.String())

logging/logging_dummies.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package logging
2+
3+
import (
4+
"context"
5+
"log/slog"
6+
"sync"
7+
8+
"github.com/Vilsol/slox"
9+
)
10+
11+
// PrintLoggingDummies logs messages for testing purposes.
12+
// It's in a separate file from the actual logging tests because the tests care
13+
// about what file line numbers the calls are from. Having them in a separate file
14+
// makes editing the tests easier without breaking the line numbers.
15+
func PrintLoggingDummies() {
16+
slog.Info("A: 1")
17+
slox.Info(context.Background(), "B: 2")
18+
19+
func() {
20+
slox.Info(context.Background(), "C: 3")
21+
}()
22+
23+
var wg sync.WaitGroup
24+
wg.Add(1)
25+
go func() {
26+
defer wg.Done()
27+
slox.Info(context.Background(), "D: 4")
28+
}()
29+
wg.Wait()
30+
}

mise.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ protoc-gen-go-grpc = "1.5.1"
1212
run = "go run cmd/api/serve.go"
1313

1414
[tasks.test]
15-
run = "go test -p 1 -v -timeout 1h ./..."
15+
run = "go test -parallel 1 -v -timeout 1h ./..."
16+
17+
# Run the tests without the verbose flag so it is easier to parse the output for local development
18+
[tasks.localtest]
19+
run = "go test -parallel 1 -timeout 1h ./..."
1620

1721
[tasks.coverage]
1822
run = "go test -v -coverprofile=coverage.txt -covermode=atomic -coverpkg=./... -p 1 -timeout 1h ./..."

0 commit comments

Comments
 (0)