Skip to content

Commit

Permalink
fix creation of mock binaries during unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Jun 28, 2024
1 parent 854f6c9 commit 25e3d84
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 115 deletions.
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)
}

func TestGetProfile(t *testing.T) {
baseDir, _ := filepath.Abs(t.TempDir())
baseDir, err := filepath.EvalSymlinks(baseDir)
Expand Down
Loading

0 comments on commit 25e3d84

Please sign in to comment.