Skip to content

Commit

Permalink
fix for no outfile
Browse files Browse the repository at this point in the history
  • Loading branch information
AriehSchneier committed Nov 18, 2024
1 parent 80835dc commit 7ee171b
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions cmd/sysl/sysl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"regexp"
"strings"
"sync"
"testing"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -749,7 +750,7 @@ func TestMain2WithDataMultipleRelationships(t *testing.T) {

func TestMain2WithBinaryInfoCmd(t *testing.T) {
t.Parallel()
runSysl(t, 0, nil, "info")
runSyslSuppressStdOut(t, 0, nil, "info")
}

func TestSwaggerExportCurrentDir(t *testing.T) {
Expand Down Expand Up @@ -1096,7 +1097,7 @@ func TestProtobufImportWithPaths(t *testing.T) {

func TestPbCloneVersion(t *testing.T) {
t.Parallel()
runSyslWithOutput(t, "", nil,
runSyslSuppressStdOut(t, 0, nil,
"protobuf",
"--mode=json",
"--clone-version=298ef0551d1d2b30863d8cf898d974fbf9d009ad",
Expand All @@ -1123,7 +1124,6 @@ func runSyslWithOutput(t *testing.T, outFileExt string, stdin io.Reader, args ..
outFileExt = ".out"
}
tempDir := t.TempDir()
_ = os.Chmod(tempDir, 0777) //nolint:errcheck
outputPath := filepath.Join(tempDir, "output"+outFileExt)
args = append(args, "--output", outputPath)
runSysl(t, 0, stdin, args...)
Expand Down Expand Up @@ -1156,6 +1156,41 @@ func runSysl(t *testing.T, expectedRet int, stdin io.Reader, args ...string) {
require.Equal(t, expectedRet, ret, lastMessage)
}

// runSysl runs the Sysl command line tool with the specified arguments (without needing 'sysl' as the first argument)
// and then ensures it completed with the specified return code.
func runSyslSuppressStdOut(t *testing.T, expectedRet int, stdin io.Reader, args ...string) {
logger, hook := test.NewNullLogger()

if args[0] != "sysl" {
args = append([]string{"sysl"}, args...)
}

if stdin == nil {
stdin = os.Stdin
}

// dump stdout
null, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0) //nolint:errcheck
sOut := os.Stdout
os.Stdout = null
resetOut := sync.OnceFunc(func() {
os.Stdout = sOut
_ = null.Close() //nolint:errcheck
})
defer resetOut()

ret := main2(args, afero.NewOsFs(), logger, stdin, main3)

resetOut()

lastEntry := hook.LastEntry()
var lastMessage string
if lastEntry != nil {
lastMessage = lastEntry.Message
}
require.Equal(t, expectedRet, ret, lastMessage)
}

func TestMain3(t *testing.T) {
logger, _ := test.NewNullLogger()
fs := afero.NewOsFs()
Expand Down

0 comments on commit 7ee171b

Please sign in to comment.