Skip to content

Commit 07055f9

Browse files
committed
internal: use be for tests
1 parent b7f79e2 commit 07055f9

29 files changed

Lines changed: 362 additions & 865 deletions

Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: build images
1+
.PHONY: build
22

33
# Development
44

@@ -12,14 +12,16 @@ setup:
1212
@go mod download
1313

1414
lint:
15-
@golangci-lint run --print-issued-lines=false --out-format=colored-line-number ./...
15+
@golangci-lint run ./...
16+
@echo "✓ lint"
1617

1718
vet:
1819
@go vet ./...
20+
@echo "✓ vet"
1921

2022
test:
21-
@go test ./... -v
22-
23+
@go test ./...
24+
@echo "✓ test"
2325

2426
build:
2527
@go build -ldflags "-X main.commit=$(build_rev) -X main.date=$(build_date)" -o build/codapi -v cmd/main.go

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
module github.com/nalgeon/codapi
22

3-
go 1.21
3+
go 1.23.0
4+
5+
toolchain go1.24.0
6+
7+
require github.com/nalgeon/be v0.1.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/nalgeon/be v0.1.0 h1:3h7GPMkzFaRIr2T7BRyUSc6K63cmmv82P6+h5mm9Wvg=
2+
github.com/nalgeon/be v0.1.0/go.mod h1:PMwMuBLopwKJkSHnr2qHyLcZYUTqNejN7A8RAqNWO3E=

internal/config/config_test.go

Lines changed: 24 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package config
22

33
import (
4-
"reflect"
54
"strings"
65
"testing"
6+
7+
"github.com/nalgeon/be"
78
)
89

910
func TestConfig_BoxNames(t *testing.T) {
@@ -16,9 +17,7 @@ func TestConfig_BoxNames(t *testing.T) {
1617

1718
want := []string{"go", "python"}
1819
got := cfg.BoxNames()
19-
if !reflect.DeepEqual(got, want) {
20-
t.Errorf("BoxNames: expected %v, got %v", want, got)
21-
}
20+
be.Equal(t, got, want)
2221
}
2322

2423
func TestConfig_CommandNames(t *testing.T) {
@@ -36,9 +35,7 @@ func TestConfig_CommandNames(t *testing.T) {
3635

3736
want := []string{"go", "python"}
3837
got := cfg.CommandNames()
39-
if !reflect.DeepEqual(got, want) {
40-
t.Errorf("CommandNames: expected %v, got %v", want, got)
41-
}
38+
be.Equal(t, got, want)
4239
}
4340

4441
func TestConfig_ToJSON(t *testing.T) {
@@ -60,9 +57,7 @@ func TestConfig_ToJSON(t *testing.T) {
6057
}
6158

6259
got := cfg.ToJSON()
63-
if !strings.Contains(got, `"pool_size": 8`) {
64-
t.Error("ToJSON: expected pool_size = 8")
65-
}
60+
be.True(t, strings.Contains(got, `"pool_size": 8`))
6661
}
6762

6863
func Test_setBoxDefaults(t *testing.T) {
@@ -83,45 +78,19 @@ func Test_setBoxDefaults(t *testing.T) {
8378
Files: []string{"config.py"},
8479
}
8580
setBoxDefaults(box, defs)
86-
if box.Image != "" {
87-
t.Error("Image: should not set default value")
88-
}
89-
if box.Runtime != defs.Runtime {
90-
t.Errorf("Runtime: expected %s, got %s", defs.Runtime, box.Runtime)
91-
}
92-
if box.CPU != defs.CPU {
93-
t.Errorf("CPU: expected %d, got %d", defs.CPU, box.CPU)
94-
}
95-
if box.Memory != defs.Memory {
96-
t.Errorf("Memory: expected %d, got %d", defs.Memory, box.Memory)
97-
}
98-
if box.Storage != defs.Storage {
99-
t.Errorf("Storage: expected %s, got %s", defs.Storage, box.Storage)
100-
}
101-
if box.Network != defs.Network {
102-
t.Errorf("Network: expected %s, got %s", defs.Network, box.Network)
103-
}
104-
if box.Volume != defs.Volume {
105-
t.Errorf("Volume: expected %s, got %s", defs.Volume, box.Volume)
106-
}
107-
if !reflect.DeepEqual(box.Tmpfs, defs.Tmpfs) {
108-
t.Errorf("Tmpfs: expected %v, got %v", defs.Tmpfs, box.Tmpfs)
109-
}
110-
if !reflect.DeepEqual(box.CapAdd, defs.CapAdd) {
111-
t.Errorf("CapAdd: expected %v, got %v", defs.CapAdd, box.CapAdd)
112-
}
113-
if !reflect.DeepEqual(box.CapDrop, defs.CapDrop) {
114-
t.Errorf("CapDrop: expected %v, got %v", defs.CapDrop, box.CapDrop)
115-
}
116-
if !reflect.DeepEqual(box.Ulimit, defs.Ulimit) {
117-
t.Errorf("Ulimit: expected %v, got %v", defs.Ulimit, box.Ulimit)
118-
}
119-
if box.NProc != defs.NProc {
120-
t.Errorf("NProc: expected %d, got %d", defs.NProc, box.NProc)
121-
}
122-
if len(box.Files) != 0 {
123-
t.Error("Files: should not set default value")
124-
}
81+
be.Equal(t, box.Image, "")
82+
be.Equal(t, box.Runtime, defs.Runtime)
83+
be.Equal(t, box.CPU, defs.CPU)
84+
be.Equal(t, box.Memory, defs.Memory)
85+
be.Equal(t, box.Storage, defs.Storage)
86+
be.Equal(t, box.Network, defs.Network)
87+
be.Equal(t, box.Volume, defs.Volume)
88+
be.Equal(t, box.Tmpfs, defs.Tmpfs)
89+
be.Equal(t, box.CapAdd, defs.CapAdd)
90+
be.Equal(t, box.CapDrop, defs.CapDrop)
91+
be.Equal(t, box.Ulimit, defs.Ulimit)
92+
be.Equal(t, box.NProc, defs.NProc)
93+
be.Equal(t, len(box.Files), 0)
12594
}
12695

12796
func Test_setStepDefaults(t *testing.T) {
@@ -136,22 +105,10 @@ func Test_setStepDefaults(t *testing.T) {
136105
}
137106

138107
setStepDefaults(step, defs)
139-
if step.Box != "" {
140-
t.Error("Box: should not set default value")
141-
}
142-
if step.User != defs.User {
143-
t.Errorf("User: expected %s, got %s", defs.User, step.User)
144-
}
145-
if step.Action != defs.Action {
146-
t.Errorf("Action: expected %s, got %s", defs.Action, step.Action)
147-
}
148-
if len(step.Command) != 0 {
149-
t.Error("Command: should not set default value")
150-
}
151-
if step.Timeout != defs.Timeout {
152-
t.Errorf("Timeout: expected %d, got %d", defs.Timeout, step.Timeout)
153-
}
154-
if step.NOutput != defs.NOutput {
155-
t.Errorf("NOutput: expected %d, got %d", defs.NOutput, step.NOutput)
156-
}
108+
be.Equal(t, step.Box, "")
109+
be.Equal(t, step.User, defs.User)
110+
be.Equal(t, step.Action, defs.Action)
111+
be.Equal(t, len(step.Command), 0)
112+
be.Equal(t, step.Timeout, defs.Timeout)
113+
be.Equal(t, step.NOutput, defs.NOutput)
157114
}

internal/config/load_test.go

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,24 @@ package config
22

33
import (
44
"testing"
5+
6+
"github.com/nalgeon/be"
57
)
68

79
func TestRead(t *testing.T) {
810
cfg, err := Read("testdata")
9-
if err != nil {
10-
t.Fatalf("unexpected error: %v", err)
11-
}
12-
13-
if cfg.PoolSize != 8 {
14-
t.Errorf("PoolSize: expected 8, got %d", cfg.PoolSize)
15-
}
16-
if !cfg.Verbose {
17-
t.Error("Verbose: expected true")
18-
}
19-
if cfg.Box.Memory != 64 {
20-
t.Errorf("Box.Memory: expected 64, got %d", cfg.Box.Memory)
21-
}
22-
if cfg.Step.User != "sandbox" {
23-
t.Errorf("Step.User: expected sandbox, got %s", cfg.Step.User)
24-
}
11+
be.Err(t, err, nil)
12+
be.Equal(t, cfg.PoolSize, 8)
13+
be.Equal(t, cfg.Verbose, true)
14+
be.Equal(t, cfg.Box.Memory, 64)
15+
be.Equal(t, cfg.Step.User, "sandbox")
2516

2617
// alpine box
27-
if _, ok := cfg.Boxes["custom-alpine"]; !ok {
28-
t.Error("Boxes: missing my/alpine box")
29-
}
30-
if cfg.Boxes["custom-alpine"].Image != "custom/alpine" {
31-
t.Errorf(
32-
"Boxes[custom-alpine]: expected custom/alpine image, got %s",
33-
cfg.Boxes["custom-alpine"].Image,
34-
)
35-
}
18+
be.True(t, cfg.Boxes["custom-alpine"] != nil)
19+
be.Equal(t, cfg.Boxes["custom-alpine"].Image, "custom/alpine")
3620

3721
// python box
38-
if _, ok := cfg.Boxes["python"]; !ok {
39-
t.Error("Boxes: missing python box")
40-
}
41-
if _, ok := cfg.Commands["python"]; !ok {
42-
t.Error("Commands: missing python sandbox")
43-
}
44-
if _, ok := cfg.Commands["python"]["run"]; !ok {
45-
t.Error("Commands[python]: missing run command")
46-
}
22+
be.True(t, cfg.Boxes["python"] != nil)
23+
be.True(t, cfg.Commands["python"] != nil)
24+
be.True(t, cfg.Commands["python"]["run"] != nil)
4725
}

internal/engine/docker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (e *Docker) Exec(req Request) Execution {
4747
err = NewExecutionError("create temp dir", err)
4848
return Fail(req.ID, err)
4949
}
50-
defer os.RemoveAll(dir)
50+
defer func() { _ = os.RemoveAll(dir) }()
5151

5252
// if the command entry point file is not defined,
5353
// there is no need to store request files in the temp directory
@@ -339,7 +339,7 @@ func expandVars(command []string, name string) []string {
339339
expanded := make([]string, len(command))
340340
copy(expanded, command)
341341
for i, cmd := range expanded {
342-
expanded[i] = strings.Replace(cmd, ":name", name, -1)
342+
expanded[i] = strings.ReplaceAll(cmd, ":name", name)
343343
}
344344
return expanded
345345
}

0 commit comments

Comments
 (0)