Skip to content

Commit

Permalink
Simplify sysl command line (#662)
Browse files Browse the repository at this point in the history
* #619 another approach to fix this issue, its command line will looks like 'sysl codegen @config_file' and config_file has the all of command flags. Actually this approach can simply all commands.

* #619 update test data and test case.
  • Loading branch information
ericzhang6222 authored Mar 2, 2020
1 parent ec4047b commit 849153c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/sysl/sysl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const syslRootMarker = ".sysl"
// main3 is the real main function. It takes its output streams and command-line
// arguments as parameters to support testability.
func main3(args []string, fs afero.Fs, logger *logrus.Logger) error {
flags, err := syslutil.PopulateCMDFlagsFromFile(args)
if err == nil && len(flags) > 0 {
// apply flags in file
args = flags
}

syslCmd := kingpin.New("sysl", "System Modelling Language Toolkit")
syslCmd.Version(Version)
syslCmd.UsageTemplate(kingpin.SeparateOptionalFlagsUsageTemplate)
Expand Down
16 changes: 16 additions & 0 deletions cmd/sysl/sysl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,3 +908,19 @@ func TestTemplating(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, string(expected), string(actual))
}

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

assert.Equal(t, nil, main3([]string{"sysl"}, fs, logger))

This comment has been minimized.

Copy link
@cuminandpaprika

cuminandpaprika Mar 19, 2020

Contributor

@ericzhang6222 I'm trying to chase down and fix #705 and I've found that commenting out the line above lets tests pass.

So, I've worked out that this test needs to be refactored in some way, so that gotestsum works
In order to do that I need to understand what this test is trying to validate.

Is it just trying to test that running main3 doesn't return an error?

This comment has been minimized.

Copy link
@ericzhang6222

ericzhang6222 Mar 19, 2020

Author Contributor

@cuminandpaprika, this test is used to test Sysl behaviors with special params.
For example, type sysl in a terminal it will print out Sysl help informations, so there is validation code assert.Equal(t, nil, main3([]string{"sysl"}, fs, logger)). And type sysl codgen in terminal, it returns an error, so it has validation code assert.Error(t, main3([]string{"sysl", "codegen"}, fs, logger)). As the change was to update params passed to Sysl, so I added this test case which is a little odd.

This comment has been minimized.

Copy link
@cuminandpaprika

cuminandpaprika Mar 19, 2020

Contributor

Ahh, I see! So it's to check that the sysl command by itself doesn't error!
Okay great, thanks


assert.Error(t, main3([]string{"sysl", "codegen"}, fs, logger))

assert.Error(t, main3([]string{"sysl", "codegen", "@tests/config.txt"}, fs, logger))

assert.Error(t, main3([]string{"sysl", "codegen", "@tests/config_new.txt"}, fs, logger))

assert.Error(t, main3([]string{"sysl", "codegen", "--grammar=go.gen.g", "--transform=go.gen.sysl", "model.sysl"},
fs, logger))
}
40 changes: 40 additions & 0 deletions pkg/syslutil/cmd_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package syslutil

import (
"fmt"
"io/ioutil"
"strings"
)

func ReadCMDFlags(configPath string) ([]string, error) {
var flags []string
data, ferr := ioutil.ReadFile(configPath)
if ferr != nil {
return flags, ferr
}
str := string(data)
flags = strings.Fields(strings.TrimSpace(str))

return flags, nil
}

func PopulateCMDFlagsFromFile(cmdArgs []string) ([]string, error) {
if len(cmdArgs) < 3 {
return nil, fmt.Errorf("command arguments are not enough")
}

var fileFlag string
for _, flag := range cmdArgs[2:] {
if strings.HasPrefix(flag, "@") {
fileFlag = flag
break
}
}

flags, err := ReadCMDFlags(strings.Replace(fileFlag, "@", "", 1))
if err != nil {
return nil, err
}

return append(cmdArgs[0:2], flags...), err
}
32 changes: 32 additions & 0 deletions pkg/syslutil/cmd_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package syslutil

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestReadCMDFlags(t *testing.T) {
t.Parallel()
flags, err := ReadCMDFlags("tests/config.txt")
assert.Equal(t, nil, err)
assert.Equal(t, 4, len(flags))
assert.Equal(t, "--grammar=go.gen.g", flags[0])
assert.Equal(t, "--transform=go.gen.sysl", flags[1])
assert.Equal(t, "model.sysl", flags[2])
assert.Equal(t, "--app-name=Test", flags[3])
}

func TestPopulateCMDFlagsFromFile(t *testing.T) {
t.Parallel()
cmdArgs := []string{"sysl", "codegen", "@tests/config.txt"}
flags, err := PopulateCMDFlagsFromFile(cmdArgs)
assert.Equal(t, nil, err)
assert.Equal(t, 6, len(flags))
assert.Equal(t, "sysl", flags[0])
assert.Equal(t, "codegen", flags[1])
assert.Equal(t, "--grammar=go.gen.g", flags[2])
assert.Equal(t, "--transform=go.gen.sysl", flags[3])
assert.Equal(t, "model.sysl", flags[4])
assert.Equal(t, "--app-name=Test", flags[5])
}
1 change: 1 addition & 0 deletions pkg/syslutil/tests/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--grammar=go.gen.g --transform=go.gen.sysl model.sysl --app-name=Test

0 comments on commit 849153c

Please sign in to comment.