Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix creation of mock binaries during unit tests #375

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,17 @@ package main
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/creativeprojects/resticprofile/config"
"github.com/creativeprojects/resticprofile/platform"
"github.com/creativeprojects/resticprofile/term"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
echoBinary string
)

func init() {
// build restic mock
cmd := exec.Command("go", "build", "./shell/echo")
cmd.Run()
if runtime.GOOS == "windows" {
echoBinary = "echo.exe"
} else {
echoBinary = "./echo"
}
}

// TestFromConfigFileToCommandLine loads all examples/integration_test.* configuration files
// and run some commands to display all the arguments that were sent
func TestFromConfigFileToCommandLine(t *testing.T) {
Expand Down Expand Up @@ -157,7 +141,7 @@ func TestFromConfigFileToCommandLine(t *testing.T) {
require.NoError(t, err)

expected := fixture.expected
if runtime.GOOS == "windows" {
if platform.IsWindows() {
if fixture.expectedOnWindows != "" {
expected = fixture.expectedOnWindows
} else {
Expand All @@ -167,7 +151,7 @@ func TestFromConfigFileToCommandLine(t *testing.T) {
assert.Equal(t, expected, strings.TrimSpace(buffer.String()))
})

if runtime.GOOS == "windows" {
if platform.IsWindows() {
continue
}

Expand Down
6 changes: 3 additions & 3 deletions lock/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestMain(m *testing.M) {
if platform.IsWindows() {
helperBinary = `.\locktest.exe`
}
cmd := exec.Command("go", "build", "-o", helperBinary, "./test")
cmd := exec.Command("go", "build", "-buildvcs=false", "-o", helperBinary, "./test")
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error building helper binary: %s\n", err)
}
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestLockIsRemovedAfterInterruptSignal(t *testing.T) {
err = cmd.Start()
require.NoError(t, err)

time.Sleep(200 * time.Millisecond)
time.Sleep(300 * time.Millisecond)
err = cmd.Process.Signal(syscall.SIGINT)
require.NoError(t, err)

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

time.Sleep(200 * time.Millisecond)
time.Sleep(300 * time.Millisecond)
err = cmd.Process.Signal(syscall.SIGINT)
require.NoError(t, err)

Expand Down
32 changes: 32 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
package main

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/creativeprojects/resticprofile/config"
"github.com/creativeprojects/resticprofile/constants"
"github.com/creativeprojects/resticprofile/platform"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
mockBinary string
echoBinary string
)

func TestMain(m *testing.M) {
mockBinary = "./shellmock"
if platform.IsWindows() {
mockBinary = `.\\shellmock.exe`
}
cmdMock := exec.Command("go", "build", "-buildvcs=false", "-o", mockBinary, "./shell/mock")
if output, err := cmdMock.CombinedOutput(); err != nil {
fmt.Fprintf(os.Stderr, "Error building shell/mock binary: %s\nCommand output: %s\n", err, string(output))
}

echoBinary = "./shellecho"
if platform.IsWindows() {
echoBinary = `.\\shellecho.exe`
}
cmdEcho := exec.Command("go", "build", "-buildvcs=false", "-o", echoBinary, "./shell/echo")
if output, err := cmdEcho.CombinedOutput(); err != nil {
fmt.Fprintf(os.Stderr, "Error building shell/echo binary: %s\nCommand output: %s\n", err, string(output))
}

m.Run()
_ = os.Remove(mockBinary)
_ = os.Remove(echoBinary)
}

creativeprojects marked this conversation as resolved.
Show resolved Hide resolved
func TestGetProfile(t *testing.T) {
baseDir, _ := filepath.Abs(t.TempDir())
baseDir, err := filepath.EvalSymlinks(baseDir)
Expand Down
24 changes: 24 additions & 0 deletions schedule_jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func configForJob(command string, at ...string) *config.Schedule {
}

func TestScheduleNilJobs(t *testing.T) {
t.Parallel()

handler := mocks.NewHandler(t)
handler.EXPECT().Init().Return(nil)
handler.EXPECT().Close()
Expand All @@ -27,6 +29,8 @@ func TestScheduleNilJobs(t *testing.T) {
}

func TestSimpleScheduleJob(t *testing.T) {
t.Parallel()

handler := mocks.NewHandler(t)
handler.EXPECT().Init().Return(nil)
handler.EXPECT().Close()
Expand All @@ -47,6 +51,8 @@ func TestSimpleScheduleJob(t *testing.T) {
}

func TestFailScheduleJob(t *testing.T) {
t.Parallel()

handler := mocks.NewHandler(t)
handler.EXPECT().Init().Return(nil)
handler.EXPECT().Close()
Expand All @@ -64,6 +70,8 @@ func TestFailScheduleJob(t *testing.T) {
}

func TestRemoveNilJobs(t *testing.T) {
t.Parallel()

handler := mocks.NewHandler(t)
handler.EXPECT().Init().Return(nil)
handler.EXPECT().Close()
Expand All @@ -73,6 +81,8 @@ func TestRemoveNilJobs(t *testing.T) {
}

func TestRemoveJob(t *testing.T) {
t.Parallel()

handler := mocks.NewHandler(t)
handler.EXPECT().Init().Return(nil)
handler.EXPECT().Close()
Expand All @@ -89,6 +99,8 @@ func TestRemoveJob(t *testing.T) {
}

func TestRemoveJobNoConfig(t *testing.T) {
t.Parallel()

handler := mocks.NewHandler(t)
handler.EXPECT().Init().Return(nil)
handler.EXPECT().Close()
Expand All @@ -105,6 +117,8 @@ func TestRemoveJobNoConfig(t *testing.T) {
}

func TestFailRemoveJob(t *testing.T) {
t.Parallel()

handler := mocks.NewHandler(t)
handler.EXPECT().Init().Return(nil)
handler.EXPECT().Close()
Expand All @@ -117,6 +131,8 @@ func TestFailRemoveJob(t *testing.T) {
}

func TestNoFailRemoveUnknownJob(t *testing.T) {
t.Parallel()

handler := mocks.NewHandler(t)
handler.EXPECT().Init().Return(nil)
handler.EXPECT().Close()
Expand All @@ -129,6 +145,8 @@ func TestNoFailRemoveUnknownJob(t *testing.T) {
}

func TestNoFailRemoveUnknownRemoveOnlyJob(t *testing.T) {
t.Parallel()

handler := mocks.NewHandler(t)
handler.EXPECT().Init().Return(nil)
handler.EXPECT().Close()
Expand All @@ -141,6 +159,8 @@ func TestNoFailRemoveUnknownRemoveOnlyJob(t *testing.T) {
}

func TestStatusNilJobs(t *testing.T) {
t.Parallel()

handler := mocks.NewHandler(t)
handler.EXPECT().Init().Return(nil)
handler.EXPECT().Close()
Expand All @@ -151,6 +171,8 @@ func TestStatusNilJobs(t *testing.T) {
}

func TestStatusJob(t *testing.T) {
t.Parallel()

handler := mocks.NewHandler(t)
handler.EXPECT().Init().Return(nil)
handler.EXPECT().Close()
Expand All @@ -165,6 +187,8 @@ func TestStatusJob(t *testing.T) {
}

func TestStatusRemoveOnlyJob(t *testing.T) {
t.Parallel()

handler := mocks.NewHandler(t)
handler.EXPECT().Init().Return(nil)
handler.EXPECT().Close()
Expand Down
Loading