Skip to content

Commit

Permalink
chore: split integration tests from engine_test
Browse files Browse the repository at this point in the history
  • Loading branch information
jvmakine committed Aug 27, 2024
1 parent 992d72c commit ef3554f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 39 deletions.
35 changes: 35 additions & 0 deletions internal/buildengine/engine_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//go:build integration

package buildengine_test

import (
"testing"

in "github.com/TBD54566975/ftl/internal/integration"
)

func TestCycleDetection(t *testing.T) {
in.Run(t,
in.WithTestDataDir("testdata"),
in.CopyModule("depcycle1"),
in.CopyModule("depcycle2"),

in.ExpectError(
in.Build("depcycle1", "depcycle2"),
`detected a module dependency cycle that impacts these modules:`,
),
)
}

func TestInt64BuildError(t *testing.T) {
in.Run(t,
in.WithTestDataDir("testdata"),
in.CopyModule("integer"),

in.ExpectError(
in.Build("integer"),
`unsupported type "int64" for field "Input"`,
`unsupported type "int64" for field "Output"`,
),
)
}
36 changes: 1 addition & 35 deletions internal/buildengine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import (
"github.com/TBD54566975/ftl/internal/log"
)

func TestEngine(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
func TestGraph(t *testing.T) {
ctx := log.ContextWithNewDefaultLogger(context.Background())
engine, err := buildengine.New(ctx, nil, t.TempDir(), []string{"testdata/alpha", "testdata/other", "testdata/another"})
assert.NoError(t, err)
Expand Down Expand Up @@ -58,34 +55,3 @@ func TestEngine(t *testing.T) {
err = engine.Build(ctx)
assert.NoError(t, err)
}

func TestCycleDetection(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
ctx := log.ContextWithNewDefaultLogger(context.Background())
engine, err := buildengine.New(ctx, nil, t.TempDir(), []string{"testdata/depcycle1", "testdata/depcycle2"})
assert.NoError(t, err)

defer engine.Close()

err = engine.Build(ctx)
assert.Error(t, err)
assert.Contains(t, err.Error(), "detected a module dependency cycle that impacts these modules:")
}

func TestInt64BuildError(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
ctx := log.ContextWithNewDefaultLogger(context.Background())
engine, err := buildengine.New(ctx, nil, t.TempDir(), []string{"testdata/integer"})
assert.NoError(t, err)

defer engine.Close()

err = engine.Build(ctx)
assert.Error(t, err)
assert.Contains(t, err.Error(), `unsupported type "int64" for field "Input"`)
assert.Contains(t, err.Error(), `unsupported type "int64" for field "Output"`)
}
15 changes: 15 additions & 0 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exec

import (
"context"
"errors"
"os"
"os/exec" //nolint:depguard
"syscall"
Expand Down Expand Up @@ -65,6 +66,20 @@ func (c *Cmd) RunBuffered(ctx context.Context) error {
return nil
}

// RunStderrError runs the command and captures the output. If the command fails, the stderr is returned as the error message.
func (c *Cmd) RunStderrError(ctx context.Context) error {
errorBuffer := NewCircularBuffer(100)

c.Cmd.Stdout = nil
c.Cmd.Stderr = errorBuffer.WriterAt(ctx, c.level)

if err := c.Run(); err != nil {
return errors.New(string(errorBuffer.Bytes()))
}

return nil
}

// Kill sends a signal to the process group of the command.
func (c *Cmd) Kill(signal syscall.Signal) error {
if c.Process == nil {
Expand Down
10 changes: 6 additions & 4 deletions internal/integration/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func DebugShell() Action {
func Exec(cmd string, args ...string) Action {
return func(t testing.TB, ic TestContext) {
Infof("Executing (in %s): %s %s", ic.workDir, cmd, shellquote.Join(args...))
err := ftlexec.Command(ic, log.Debug, ic.workDir, cmd, args...).RunBuffered(ic)
err := ftlexec.Command(ic, log.Debug, ic.workDir, cmd, args...).RunStderrError(ic)
assert.NoError(t, err)
}
}
Expand Down Expand Up @@ -199,13 +199,15 @@ func ExecWithOutput(cmd string, args []string, capture func(output string)) Acti
}
}

// ExpectError wraps an action and expects it to return an error with the given message.
func ExpectError(action Action, expectedErrorMsg string) Action {
// ExpectError wraps an action and expects it to return an error containing the given messages.
func ExpectError(action Action, expectedErrorMsg ...string) Action {
return func(t testing.TB, ic TestContext) {
defer func() {
if r := recover(); r != nil {
if e, ok := r.(TestingError); ok {
assert.Contains(t, string(e), expectedErrorMsg)
for _, msg := range expectedErrorMsg {
assert.Contains(t, string(e), msg)
}
} else {
panic(r)
}
Expand Down

0 comments on commit ef3554f

Please sign in to comment.