Skip to content

Commit

Permalink
better spam suppression for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AriehSchneier committed Nov 18, 2024
1 parent 4164257 commit d3b5bcb
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 156 deletions.
12 changes: 6 additions & 6 deletions cmd/sysl/cmd_binaryinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func (p *infoCmd) Configure(app *kingpin.Application) *kingpin.CmdClause {
}

func (p *infoCmd) Execute(args cmdutils.ExecuteArgs) error {
fmt.Printf("Build:\n")
fmt.Printf(" Version : %s\n", Version)
fmt.Printf(" Git Commit : %s\n", GitFullCommit)
fmt.Printf(" Date : %s\n", BuildDate)
fmt.Printf(" Go Version : %s\n", GoVersion)
fmt.Printf(" OS : %s\n", BuildOS)
fmt.Fprintf(args.Stdout, "Build:\n")
fmt.Fprintf(args.Stdout, " Version : %s\n", Version)
fmt.Fprintf(args.Stdout, " Git Commit : %s\n", GitFullCommit)
fmt.Fprintf(args.Stdout, " Date : %s\n", BuildDate)
fmt.Fprintf(args.Stdout, " Go Version : %s\n", GoVersion)
fmt.Fprintf(args.Stdout, " OS : %s\n", BuildOS)

return nil
}
2 changes: 1 addition & 1 deletion cmd/sysl/cmd_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (c *envCmd) Configure(app *kingpin.Application) *kingpin.CmdClause {
func (c *envCmd) Execute(args cmdutils.ExecuteArgs) error {
for _, e := range env.Vars {
if !strings.HasPrefix(e.Name(), "SYSL_DEV_") || e.Value() != e.Default() {
fmt.Printf("%s=\"%s\"\n", e, e.Value())
fmt.Fprintf(args.Stdout, "%s=\"%s\"\n", e, e.Value())
}
}
return nil
Expand Down
8 changes: 5 additions & 3 deletions cmd/sysl/cmd_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"sort"
"strings"

"github.com/anz-bank/sysl/pkg/cmdutils"
"github.com/sirupsen/logrus"

"github.com/anz-bank/sysl/pkg/importer"
"github.com/anz-bank/sysl/pkg/cmdutils"

"github.com/spf13/afero"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/anz-bank/sysl/pkg/importer"
)

type importCmd struct {
Expand Down Expand Up @@ -96,7 +98,7 @@ func (p *importCmd) Execute(args cmdutils.ExecuteArgs) error {
if p.outFile != "" {
return afero.WriteFile(args.Filesystem, p.outFile, []byte(output), os.ModePerm)
}
_, err = fmt.Println(output)
_, err = fmt.Fprintln(args.Stdout, output)
return err
}

Expand Down
7 changes: 3 additions & 4 deletions cmd/sysl/cmd_protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"os"
"reflect"
"strings"

Expand Down Expand Up @@ -83,14 +82,14 @@ func (p *protobufCmd) Execute(args cmdutils.ExecuteArgs) error {
if p.splitappspath != "-" {
return pbutil.OutputSplitApplications(m, "json", opt, p.splitappspath, "data.json", args.Filesystem)
} else if p.output == "-" {
return pbutil.FJSONPBWithOpt(os.Stdout, m, opt)
return pbutil.FJSONPBWithOpt(args.Stdout, m, opt)
}
return pbutil.JSONPBWithOpt(m, p.output, args.Filesystem, opt)
}

if p.mode == "" || p.mode == "textpb" {
if p.output == "-" && p.splitappspath == "-" {
return pbutil.FTextPBWithOpt(os.Stdout, m, opt)
return pbutil.FTextPBWithOpt(args.Stdout, m, opt)
}
if p.splitappspath != "-" {
return pbutil.OutputSplitApplications(m, p.mode, opt, p.splitappspath, "data.textpb", args.Filesystem)
Expand All @@ -101,7 +100,7 @@ func (p *protobufCmd) Execute(args cmdutils.ExecuteArgs) error {

// output format is binary
if p.output == "-" && p.splitappspath == "-" {
return pbutil.GeneratePBBinaryMessage(os.Stdout, m)
return pbutil.GeneratePBBinaryMessage(args.Stdout, m)
}

if p.splitappspath != "-" {
Expand Down
7 changes: 3 additions & 4 deletions cmd/sysl/cmd_repl.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package main

import (
"os"

"github.com/anz-bank/sysl/pkg/cmdutils"

"github.com/anz-bank/sysl/pkg/eval"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/anz-bank/sysl/pkg/eval"
)

type replCmd struct{}
Expand All @@ -21,7 +20,7 @@ func (p *replCmd) Configure(app *kingpin.Application) *kingpin.CmdClause {

func (p *replCmd) Execute(args cmdutils.ExecuteArgs) error {
s := &eval.Scope{}
repl := eval.NewREPL(os.Stdin, os.Stdout)
repl := eval.NewREPL(args.Stdin, args.Stdout)
for {
if err := repl(s, nil, nil); err != nil {
return nil // means EOF
Expand Down
3 changes: 2 additions & 1 deletion cmd/sysl/cmd_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type cmdRunner struct {

// Run identifies the command to run, loads the Sysl modules from the input (if necessary), then
// executes the command with all of the accumulated context.
func (r *cmdRunner) Run(which string, fs afero.Fs, logger *logrus.Logger, stdin io.Reader) error {
func (r *cmdRunner) Run(which string, fs afero.Fs, logger *logrus.Logger, stdin io.Reader, stdout io.Writer) error {
// splitter to parse main command from subcommand
mainCommand := strings.Split(which, " ")[0]
if cmd, ok := r.commands[mainCommand]; ok {
Expand Down Expand Up @@ -88,6 +88,7 @@ func (r *cmdRunner) Run(which string, fs afero.Fs, logger *logrus.Logger, stdin
ModulePaths: r.modules,
Root: r.Root,
Stdin: stdin,
Stdout: stdout,
})
}
}
Expand Down
41 changes: 21 additions & 20 deletions cmd/sysl/cmd_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"strings"
"testing"

"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"

"github.com/anz-bank/sysl/pkg/cmdutils"
"github.com/anz-bank/sysl/pkg/parse"
"github.com/anz-bank/sysl/pkg/pbutil"
"github.com/anz-bank/sysl/pkg/sysl"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"
"gopkg.in/alecthomas/kingpin.v2"
Expand All @@ -34,15 +35,15 @@ Stdin::App:
...`

func TestLoadModule_path(t *testing.T) {
sysl := kingpin.New("sysl", "System Modelling Language Toolkit")
cmd := sysl.Command("foo", "")
syslKingpin := kingpin.New("sysl", "System Modelling Language Toolkit")
cmd := syslKingpin.Command("foo", "")
_ = cmd.Flag("bar", "").Default("foo").String()
_ = cmd.Flag("other", "").Default("foo").String()

EnsureFlagsNonEmpty(cmd, "bar")

args := []string{"foo", "--bar", ""}
selected, err := sysl.Parse(args)
selected, err := syslKingpin.Parse(args)
assert.Equal(t, "foo", selected)
assert.NoError(t, err)
}
Expand All @@ -56,7 +57,7 @@ func TestLoadModule_stdinBytes(t *testing.T) {
stdin := new(bytes.Buffer)
require.NoError(t, pbutil.GeneratePBBinaryMessage(stdin, m))

require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin))
require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin, io.Discard))

assert.Len(t, mods, 1)
assert.NotEmpty(t, mods[0].Apps["Stdin :: App"])
Expand All @@ -73,7 +74,7 @@ func TestLoadModule_stdinSplitBytes(t *testing.T) {
dummyFs := afero.NewMemMapFs()
require.NoError(t, pbutil.OutputSplitApplications(m, "binarypb", opt, "/", "test.pb", dummyFs))

require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin))
require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin, io.Discard))

fileExists, err := afero.Exists(dummyFs, "/Stdin/App/test.pb")
assert.NoError(t, err)
Expand All @@ -91,7 +92,7 @@ func TestLoadModule_stdinSplitJSON(t *testing.T) {
dummyFs := afero.NewMemMapFs()
require.NoError(t, pbutil.OutputSplitApplications(m, "json", opt, "/", "test.json", dummyFs))

require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin))
require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin, io.Discard))

fileExists, err := afero.Exists(dummyFs, "/Stdin/App/test.json")
assert.NoError(t, err)
Expand All @@ -109,7 +110,7 @@ func TestLoadModule_stdinSplitTextPB(t *testing.T) {
dummyFs := afero.NewMemMapFs()
require.NoError(t, pbutil.OutputSplitApplications(m, "textpb", opt, "/", "test.textpb", dummyFs))

require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin))
require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin, io.Discard))

fileExists, err := afero.Exists(dummyFs, "/Stdin/App/test.textpb")
assert.NoError(t, err)
Expand All @@ -121,7 +122,7 @@ func TestLoadModule_stdin(t *testing.T) {
r := buildRunner(t, modsCmd(&mods))

stdin := toStdin(t, stdinFile{Path: filepath.Join(cwd(), "test.sysl"), Content: emptyAppSrc})
require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin))
require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin, io.Discard))

assert.Len(t, mods, 1)
assert.NotEmpty(t, mods[0].Apps["Stdin :: App"])
Expand All @@ -132,7 +133,7 @@ func TestLoadModule_stdinImport(t *testing.T) {
r := buildRunner(t, modsCmd(&mods))

stdin := toStdin(t, stdinFile{Path: "test.sysl", Content: importSrc})
require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin))
require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin, io.Discard))

assert.Len(t, mods, 1)
assert.Len(t, mods[0].Apps, 3)
Expand All @@ -146,7 +147,7 @@ func TestLoadModule_stdinPathImport(t *testing.T) {
r := buildRunner(t, modsCmd(&mods))

stdin := toStdin(t, stdinFile{Path: filepath.Join("..", "..", "tests", "test.sysl"), Content: "import simple.sysl"})
require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin))
require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin, io.Discard))

assert.Len(t, mods, 1)
assert.Len(t, mods[0].Apps, 2)
Expand All @@ -164,32 +165,32 @@ func TestLoadModule_pathsAndStdin(t *testing.T) {
r := buildRunnerWithRoot(t, cmd, testDir, args...)

stdin := toStdin(t, stdinFile{Path: "test.sysl", Content: emptyAppSrc})
require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin))
require.NoError(t, r.Run(cmdName, afero.NewOsFs(), logrus.StandardLogger(), stdin, io.Discard))
}

func TestEnsureFlagsNonEmpty_AllowsExcludes(t *testing.T) {
sysl := kingpin.New("sysl", "System Modelling Language Toolkit")
cmd := sysl.Command("foo", "")
syslKingpin := kingpin.New("sysl", "System Modelling Language Toolkit")
cmd := syslKingpin.Command("foo", "")
_ = cmd.Flag("bar", "").Default("foo").String()
_ = cmd.Flag("other", "").Default("foo").String()

EnsureFlagsNonEmpty(cmd, "bar")

args := []string{"foo", "--bar", ""}
selected, err := sysl.Parse(args)
selected, err := syslKingpin.Parse(args)
assert.Equal(t, "foo", selected)
assert.NoError(t, err)
}

func TestEnsureFlagsNonEmpty(t *testing.T) {
sysl := kingpin.New("sysl", "System Modelling Language Toolkit")
cmd := sysl.Command("foo", "")
syslKingpin := kingpin.New("sysl", "System Modelling Language Toolkit")
cmd := syslKingpin.Command("foo", "")
cmd.Flag("bar", "").Default("foo")

EnsureFlagsNonEmpty(cmd)

args := []string{"foo", "--bar", ""}
_, err := sysl.ParseContext(args)
_, err := syslKingpin.ParseContext(args)
assert.Error(t, err)
}

Expand Down
18 changes: 10 additions & 8 deletions cmd/sysl/databasescript_mod_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"io"
"os"
"path/filepath"
"testing"
Expand All @@ -9,11 +10,12 @@ import (

"github.com/anz-bank/sysl/pkg/cmdutils"

"github.com/anz-bank/sysl/pkg/database"
"github.com/anz-bank/sysl/pkg/syslutil"
"github.com/sirupsen/logrus/hooks/test"
"github.com/spf13/afero"

"github.com/anz-bank/sysl/pkg/database"
"github.com/anz-bank/sysl/pkg/syslutil"

"github.com/stretchr/testify/assert"
"gopkg.in/alecthomas/kingpin.v2"
)
Expand Down Expand Up @@ -54,7 +56,7 @@ func TestModDBScriptValidSyslFile(t *testing.T) {
main2([]string{"sysl", "generatedbscriptsdelta", "-t", "PetStore", "-o", "", "-a", "RelModel",
filepath.Join(database.DBTestDir, "db_scripts/dataForSqlScriptOrg.sysl"),
filepath.Join(database.DBTestDir, "db_scripts/dataForSqlScriptModifiedTwoApps.sysl")},
fs, logger, os.Stdin, main3)
fs, logger, os.Stdin, io.Discard, main3)
syslutil.AssertFsHasExactly(t, memFs, "/RelModel.sql")
}

Expand All @@ -66,7 +68,7 @@ func TestModDBScriptInValidOrgSyslFile(t *testing.T) {
err := main2([]string{"sysl", "generatedbscriptsdelta", "-t", "PetStore", "-o", "", "-a", "RelModel",
filepath.Join(database.DBTestDir, "db_scripts/invalid.sysl"),
filepath.Join(database.DBTestDir, "db_scripts/dataForSqlScriptModified.sysl")},
fs, logger, os.Stdin, main3)
fs, logger, os.Stdin, io.Discard, main3)
assert.Equal(t, 2, err)
}

Expand All @@ -77,7 +79,7 @@ func TestModDBScriptOneModule(t *testing.T) {

err := main2([]string{"sysl", "generatedbscriptsdelta", "-t", "PetStore", "-o", "", "-a", "RelModel",
filepath.Join(database.DBTestDir, "db_scripts/dataForSqlScriptOrg.sysl")},
fs, logger, os.Stdin, main3)
fs, logger, os.Stdin, io.Discard, main3)
assert.Equal(t, 1, err)
}

Expand All @@ -90,7 +92,7 @@ func TestModDBScriptThreeModule(t *testing.T) {
filepath.Join(database.DBTestDir, "db_scripts/dataForSqlScriptOrg.sysl"),
filepath.Join(database.DBTestDir, "db_scripts/dataForSqlScriptModified.sysl"),
filepath.Join(database.DBTestDir, "db_scripts/dataForSqlScriptModified.sysl")},
fs, logger, os.Stdin, main3)
fs, logger, os.Stdin, io.Discard, main3)
assert.Equal(t, 1, err)
}

Expand All @@ -102,7 +104,7 @@ func TestModDBScriptInValidNewSyslFile(t *testing.T) {
err := main2([]string{"sysl", "generatedbscriptsdelta", "-t", "PetStore", "-o", "", "-a", "RelModel",
filepath.Join(database.DBTestDir, "db_scripts/dataForSqlScriptOrg.sysl"),
filepath.Join(database.DBTestDir, "db_scripts/invalid.sysl")},
fs, logger, os.Stdin, main3)
fs, logger, os.Stdin, io.Discard, main3)
assert.Equal(t, 2, err)
}

Expand All @@ -114,7 +116,7 @@ func TestModDBScriptNewAppSyslFile(t *testing.T) {
err := main2([]string{"sysl", "generatedbscriptsdelta", "-t", "PetStore", "-o", "", "-a", "RelModel,RelModelNew",
filepath.Join(database.DBTestDir, "db_scripts/dataForSqlScriptOrg.sysl"),
filepath.Join(database.DBTestDir, "db_scripts/dataForSqlScriptModifiedTwoApps.sysl")},
fs, logger, os.Stdin, main3)
fs, logger, os.Stdin, io.Discard, main3)
assert.Equal(t, 0, err)
syslutil.AssertFsHasExactly(t, memFs, "/RelModel.sql", "/RelModelNew.sql")
}
Expand Down
12 changes: 7 additions & 5 deletions cmd/sysl/databasescript_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -9,11 +10,12 @@ import (
"github.com/anz-bank/sysl/pkg/cmdutils"
"github.com/anz-bank/sysl/pkg/loader"

"github.com/anz-bank/sysl/pkg/database"
"github.com/anz-bank/sysl/pkg/syslutil"
"github.com/sirupsen/logrus/hooks/test"
"github.com/spf13/afero"

"github.com/anz-bank/sysl/pkg/database"
"github.com/anz-bank/sysl/pkg/syslutil"

"github.com/stretchr/testify/assert"
"gopkg.in/alecthomas/kingpin.v2"
)
Expand Down Expand Up @@ -53,7 +55,7 @@ func TestCreateDBScriptValidSyslFile(t *testing.T) {
"-o", "",
filepath.Join(database.DBTestDir, "db_scripts/dataForSqlScriptOrg.sysl"),
"-a", "RelModel"},
fs, logger, os.Stdin, main3)
fs, logger, os.Stdin, io.Discard, main3)
syslutil.AssertFsHasExactly(t, memFs, "/RelModel.sql")
}

Expand All @@ -64,7 +66,7 @@ func TestCreateDBScriptInValidSyslFile(t *testing.T) {

err := main2([]string{"sysl", "generatescript", "-t", "PetStore", "-o", "", "-a", "RelModel",
filepath.Join(database.DBTestDir, "db_scripts/invalid.sysl")},
fs, logger, os.Stdin, main3)
fs, logger, os.Stdin, io.Discard, main3)
assert.Equal(t, 1, err)
}

Expand All @@ -75,7 +77,7 @@ func TestCreateDBScriptNoAppSyslFile(t *testing.T) {

err := main2([]string{"sysl", "generatescript", "-t", "PetStore", "-o", "", "-a", "Proj123",
filepath.Join(database.DBTestDir, "db_scripts/dataForSqlScriptOrg.sysl")},
fs, logger, os.Stdin, main3)
fs, logger, os.Stdin, io.Discard, main3)
assert.Equal(t, 1, err)
}

Expand Down
Loading

0 comments on commit d3b5bcb

Please sign in to comment.