Skip to content

Commit a615444

Browse files
fix creation of mock binaries during unit tests (#375)
* fix creation of mock binaries during unit tests * run schedule jobs in parallel
1 parent 854f6c9 commit a615444

File tree

8 files changed

+175
-115
lines changed

8 files changed

+175
-115
lines changed

integration_test.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,17 @@ package main
33
import (
44
"bytes"
55
"os"
6-
"os/exec"
76
"path/filepath"
8-
"runtime"
97
"strings"
108
"testing"
119

1210
"github.com/creativeprojects/resticprofile/config"
11+
"github.com/creativeprojects/resticprofile/platform"
1312
"github.com/creativeprojects/resticprofile/term"
1413
"github.com/stretchr/testify/assert"
1514
"github.com/stretchr/testify/require"
1615
)
1716

18-
var (
19-
echoBinary string
20-
)
21-
22-
func init() {
23-
// build restic mock
24-
cmd := exec.Command("go", "build", "./shell/echo")
25-
cmd.Run()
26-
if runtime.GOOS == "windows" {
27-
echoBinary = "echo.exe"
28-
} else {
29-
echoBinary = "./echo"
30-
}
31-
}
32-
3317
// TestFromConfigFileToCommandLine loads all examples/integration_test.* configuration files
3418
// and run some commands to display all the arguments that were sent
3519
func TestFromConfigFileToCommandLine(t *testing.T) {
@@ -157,7 +141,7 @@ func TestFromConfigFileToCommandLine(t *testing.T) {
157141
require.NoError(t, err)
158142

159143
expected := fixture.expected
160-
if runtime.GOOS == "windows" {
144+
if platform.IsWindows() {
161145
if fixture.expectedOnWindows != "" {
162146
expected = fixture.expectedOnWindows
163147
} else {
@@ -167,7 +151,7 @@ func TestFromConfigFileToCommandLine(t *testing.T) {
167151
assert.Equal(t, expected, strings.TrimSpace(buffer.String()))
168152
})
169153

170-
if runtime.GOOS == "windows" {
154+
if platform.IsWindows() {
171155
continue
172156
}
173157

lock/lock_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestMain(m *testing.M) {
2727
if platform.IsWindows() {
2828
helperBinary = `.\locktest.exe`
2929
}
30-
cmd := exec.Command("go", "build", "-o", helperBinary, "./test")
30+
cmd := exec.Command("go", "build", "-buildvcs=false", "-o", helperBinary, "./test")
3131
if err := cmd.Run(); err != nil {
3232
fmt.Fprintf(os.Stderr, "Error building helper binary: %s\n", err)
3333
}
@@ -267,7 +267,7 @@ func TestLockIsRemovedAfterInterruptSignal(t *testing.T) {
267267
err = cmd.Start()
268268
require.NoError(t, err)
269269

270-
time.Sleep(200 * time.Millisecond)
270+
time.Sleep(300 * time.Millisecond)
271271
err = cmd.Process.Signal(syscall.SIGINT)
272272
require.NoError(t, err)
273273

@@ -293,7 +293,7 @@ func TestLockIsRemovedAfterInterruptSignalInsideShell(t *testing.T) {
293293
err = cmd.Start()
294294
require.NoError(t, err)
295295

296-
time.Sleep(200 * time.Millisecond)
296+
time.Sleep(300 * time.Millisecond)
297297
err = cmd.Process.Signal(syscall.SIGINT)
298298
require.NoError(t, err)
299299

main_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,49 @@
11
package main
22

33
import (
4+
"fmt"
45
"os"
6+
"os/exec"
57
"path/filepath"
68
"strings"
79
"testing"
810

911
"github.com/creativeprojects/resticprofile/config"
1012
"github.com/creativeprojects/resticprofile/constants"
13+
"github.com/creativeprojects/resticprofile/platform"
1114
"github.com/stretchr/testify/assert"
1215
"github.com/stretchr/testify/require"
1316
)
1417

18+
var (
19+
mockBinary string
20+
echoBinary string
21+
)
22+
23+
func TestMain(m *testing.M) {
24+
mockBinary = "./shellmock"
25+
if platform.IsWindows() {
26+
mockBinary = `.\\shellmock.exe`
27+
}
28+
cmdMock := exec.Command("go", "build", "-buildvcs=false", "-o", mockBinary, "./shell/mock")
29+
if output, err := cmdMock.CombinedOutput(); err != nil {
30+
fmt.Fprintf(os.Stderr, "Error building shell/mock binary: %s\nCommand output: %s\n", err, string(output))
31+
}
32+
33+
echoBinary = "./shellecho"
34+
if platform.IsWindows() {
35+
echoBinary = `.\\shellecho.exe`
36+
}
37+
cmdEcho := exec.Command("go", "build", "-buildvcs=false", "-o", echoBinary, "./shell/echo")
38+
if output, err := cmdEcho.CombinedOutput(); err != nil {
39+
fmt.Fprintf(os.Stderr, "Error building shell/echo binary: %s\nCommand output: %s\n", err, string(output))
40+
}
41+
42+
m.Run()
43+
_ = os.Remove(mockBinary)
44+
_ = os.Remove(echoBinary)
45+
}
46+
1547
func TestGetProfile(t *testing.T) {
1648
baseDir, _ := filepath.Abs(t.TempDir())
1749
baseDir, err := filepath.EvalSymlinks(baseDir)

schedule_jobs_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ func configForJob(command string, at ...string) *config.Schedule {
1818
}
1919

2020
func TestScheduleNilJobs(t *testing.T) {
21+
t.Parallel()
22+
2123
handler := mocks.NewHandler(t)
2224
handler.EXPECT().Init().Return(nil)
2325
handler.EXPECT().Close()
@@ -27,6 +29,8 @@ func TestScheduleNilJobs(t *testing.T) {
2729
}
2830

2931
func TestSimpleScheduleJob(t *testing.T) {
32+
t.Parallel()
33+
3034
handler := mocks.NewHandler(t)
3135
handler.EXPECT().Init().Return(nil)
3236
handler.EXPECT().Close()
@@ -47,6 +51,8 @@ func TestSimpleScheduleJob(t *testing.T) {
4751
}
4852

4953
func TestFailScheduleJob(t *testing.T) {
54+
t.Parallel()
55+
5056
handler := mocks.NewHandler(t)
5157
handler.EXPECT().Init().Return(nil)
5258
handler.EXPECT().Close()
@@ -64,6 +70,8 @@ func TestFailScheduleJob(t *testing.T) {
6470
}
6571

6672
func TestRemoveNilJobs(t *testing.T) {
73+
t.Parallel()
74+
6775
handler := mocks.NewHandler(t)
6876
handler.EXPECT().Init().Return(nil)
6977
handler.EXPECT().Close()
@@ -73,6 +81,8 @@ func TestRemoveNilJobs(t *testing.T) {
7381
}
7482

7583
func TestRemoveJob(t *testing.T) {
84+
t.Parallel()
85+
7686
handler := mocks.NewHandler(t)
7787
handler.EXPECT().Init().Return(nil)
7888
handler.EXPECT().Close()
@@ -89,6 +99,8 @@ func TestRemoveJob(t *testing.T) {
8999
}
90100

91101
func TestRemoveJobNoConfig(t *testing.T) {
102+
t.Parallel()
103+
92104
handler := mocks.NewHandler(t)
93105
handler.EXPECT().Init().Return(nil)
94106
handler.EXPECT().Close()
@@ -105,6 +117,8 @@ func TestRemoveJobNoConfig(t *testing.T) {
105117
}
106118

107119
func TestFailRemoveJob(t *testing.T) {
120+
t.Parallel()
121+
108122
handler := mocks.NewHandler(t)
109123
handler.EXPECT().Init().Return(nil)
110124
handler.EXPECT().Close()
@@ -117,6 +131,8 @@ func TestFailRemoveJob(t *testing.T) {
117131
}
118132

119133
func TestNoFailRemoveUnknownJob(t *testing.T) {
134+
t.Parallel()
135+
120136
handler := mocks.NewHandler(t)
121137
handler.EXPECT().Init().Return(nil)
122138
handler.EXPECT().Close()
@@ -129,6 +145,8 @@ func TestNoFailRemoveUnknownJob(t *testing.T) {
129145
}
130146

131147
func TestNoFailRemoveUnknownRemoveOnlyJob(t *testing.T) {
148+
t.Parallel()
149+
132150
handler := mocks.NewHandler(t)
133151
handler.EXPECT().Init().Return(nil)
134152
handler.EXPECT().Close()
@@ -141,6 +159,8 @@ func TestNoFailRemoveUnknownRemoveOnlyJob(t *testing.T) {
141159
}
142160

143161
func TestStatusNilJobs(t *testing.T) {
162+
t.Parallel()
163+
144164
handler := mocks.NewHandler(t)
145165
handler.EXPECT().Init().Return(nil)
146166
handler.EXPECT().Close()
@@ -151,6 +171,8 @@ func TestStatusNilJobs(t *testing.T) {
151171
}
152172

153173
func TestStatusJob(t *testing.T) {
174+
t.Parallel()
175+
154176
handler := mocks.NewHandler(t)
155177
handler.EXPECT().Init().Return(nil)
156178
handler.EXPECT().Close()
@@ -165,6 +187,8 @@ func TestStatusJob(t *testing.T) {
165187
}
166188

167189
func TestStatusRemoveOnlyJob(t *testing.T) {
190+
t.Parallel()
191+
168192
handler := mocks.NewHandler(t)
169193
handler.EXPECT().Init().Return(nil)
170194
handler.EXPECT().Close()

0 commit comments

Comments
 (0)