Skip to content

Commit

Permalink
Parse options once
Browse files Browse the repository at this point in the history
  • Loading branch information
lucapette committed Jun 4, 2017
1 parent 3eb1db3 commit 04355c7
Show file tree
Hide file tree
Showing 11 changed files with 475 additions and 452 deletions.
153 changes: 80 additions & 73 deletions integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,108 +54,115 @@ func loadFixture(t *testing.T, fixture string) string {
return string(content)
}

func TestCliArgs(t *testing.T) {
func TestCLI(t *testing.T) {
tests := []struct {
name string
args []string
fixture string
wantErr bool
}{
{
name: "no arguments",
args: []string{},
fixture: "help.golden",
"no arguments",
[]string{},
"help.golden",
false,
},
{
name: "list generators",
args: []string{"-g"},
fixture: "generators.golden",
"list generators",
[]string{"-g"},
"generators.golden",
false,
},
{
name: "default format",
args: []string{"int:42,42", "enum:foo,foo"},
fixture: "default-format.golden",
"default format",
[]string{"int:42,42", "enum:foo,foo"},
"default-format.golden",
false,
},
{
name: "unknown generators",
args: []string{"madeupgenerator", "anothermadeupgenerator"},
fixture: "unknown-generators.golden",
"unknown generators",
[]string{"madeupgenerator"},
"unknown-generators.golden",
true,
},
{
name: "default format with limit short",
args: []string{"-l=5", "int:42,42", "enum:foo,foo"},
fixture: "default-format-with-limit.golden",
"default format with limit short",
[]string{"-l=5", "int:42,42", "enum:foo,foo"},
"default-format-with-limit.golden",
false,
},
{
name: "default format with limit",
args: []string{"--limit=5", "int:42,42", "enum:foo,foo"},
fixture: "default-format-with-limit.golden",
"default format with limit",
[]string{"--limit=5", "int:42,42", "enum:foo,foo"},
"default-format-with-limit.golden",
false,
},
{
name: "csv format short",
args: []string{"-f=csv", "int:42,42", "enum:foo,foo"},
fixture: "csv-format.golden",
"csv format short",
[]string{"-f=csv", "int:42,42", "enum:foo,foo"},
"csv-format.golden",
false,
},
{
name: "csv format",
args: []string{"--format=csv", "int:42,42", "enum:foo,foo"},
fixture: "csv-format.golden",
"csv format",
[]string{"--format=csv", "int:42,42", "enum:foo,foo"},
"csv-format.golden",
false,
},
{
name: "tab format",
args: []string{"-f=tab", "int:42,42", "enum:foo,foo"},
fixture: "tab-format.golden",
"tab format",
[]string{"-f=tab", "int:42,42", "enum:foo,foo"},
"tab-format.golden",
false,
},
{
name: "sql format",
args: []string{"-f=sql", "int:42,42", "enum:foo,foo"},
fixture: "sql-format.golden",
"sql format",
[]string{"-f=sql", "int:42,42", "enum:foo,foo"},
"sql-format.golden",
false,
},
{
name: "sql format with keys",
args: []string{"-f=sql", "age=int:42,42", "name=enum:foo,foo"},
fixture: "sql-format-with-keys.golden",
"sql format with keys",
[]string{"-f=sql", "age=int:42,42", "name=enum:foo,foo"},
"sql-format-with-keys.golden",
false,
},
{
name: "sql format with table name",
args: []string{"-f=sql", "-t=USERS", "int:42,42", "enum:foo,foo"},
fixture: "sql-format-with-table-name.golden",
"sql format with table name",
[]string{"-f=sql", "-t=USERS", "int:42,42", "enum:foo,foo"},
"sql-format-with-table-name.golden",
false,
},
{
"no file",
[]string{"file"},
"path-empty.golden",
true,
},
{
"no file,",
[]string{"file:"},
"path-empty.golden",
true,
},
{
"file does not exist",
[]string{`file:'this file does not exist.txt'`},
"file-does-not-exist.golden",
true,
},
{
"file exists",
[]string{`file:integration/file.txt`},
"file-exist.golden",
false,
},
{
"file exists with quotes",
[]string{`file:'integration/file.txt'`},
"file-exist.golden",
false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := exec.Command(binaryPath, tt.args...)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("output: %s\nerr: %v", output, err)
}

if *update {
writeFixture(t, tt.fixture, output)
}

actual := string(output)
expected := loadFixture(t, tt.fixture)

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("diff: %v", diff(expected, actual))
}
})
}
}

func TestFileGenerator(t *testing.T) {
tests := []struct {
name string
args []string
fixture string
wantErr bool
}{
{"no file", []string{"file"}, "path-empty.golden", true},
{"no file,", []string{"file:"}, "path-empty.golden", true},
{"file does not exist", []string{`file:'this file does not exist.txt'`}, "file-empty.golden", true},
{"file exists", []string{`file:integration/file.txt`}, "file-exist.golden", false},
{"file exists with quotes", []string{`file:'integration/file.txt'`}, "file-exist.golden", false},
}

for _, tt := range tests {
Expand Down
9 changes: 9 additions & 0 deletions integration/file-does-not-exist.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
could not read file this file does not exist.txt: open this file does not exist.txt: no such file or directory

Usage: fakedata [option ...] field...

-f, --format string generators rows in f format. Available formats: csv|tab|sql
-g, --generators lists available generators
-l, --limit int limits rows up to n (default 10)
-t, --table string table name of the sql format (default "TABLE")
-v, --version shows version information
1 change: 0 additions & 1 deletion integration/file-empty.golden

This file was deleted.

10 changes: 9 additions & 1 deletion integration/path-empty.golden
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
file: no file path given
no file path given

Usage: fakedata [option ...] field...

-f, --format string generators rows in f format. Available formats: csv|tab|sql
-g, --generators lists available generators
-l, --limit int limits rows up to n (default 10)
-t, --table string table name of the sql format (default "TABLE")
-v, --version shows version information
10 changes: 7 additions & 3 deletions integration/unknown-generators.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Unknown generator: madeupgenerator.
Unknown generator: anothermadeupgenerator.
unknown generator: madeupgenerator

Usage: fakedata [option ...] field...

See fakedata --generators for a list of available generators.
-f, --format string generators rows in f format. Available formats: csv|tab|sql
-g, --generators lists available generators
-l, --limit int limits rows up to n (default 10)
-t, --table string table name of the sql format (default "TABLE")
-v, --version shows version information
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ func main() {

columns, err := fakedata.NewColumns(flag.Args())
if err != nil {
fmt.Printf("%v\n\nSee fakedata --generators for a list of available generators.\n", err)
os.Exit(0)
fmt.Printf("%v\n\n", err)
flag.Usage()
os.Exit(1)
}

formatter := getFormatter(*formatFlag)

for i := 0; i < *limitFlag; i++ {
fmt.Print(fakedata.GenerateRow(columns, formatter))
fmt.Println(columns.GenerateRow(formatter))
}
}

Expand Down
46 changes: 30 additions & 16 deletions pkg/fakedata/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (

// A Column represents one field of data to generate
type Column struct {
Name string
Key string
Options string
Name string
Key string
Gen Generator
}

func (c *Column) String() string {
return fmt.Sprintf("Column(%s=%s)", c.Name, c.Key)
func (column Column) generate() string {
return column.Gen.Func()
}

// Columns is an array of Column
Expand All @@ -24,17 +24,14 @@ type Columns []Column
// It returns an error with a line for each unknown key
func NewColumns(keys []string) (cols Columns, err error) {
cols = make(Columns, len(keys))
var errors bytes.Buffer

f := newFactory()

for i, k := range keys {
specs := strings.Split(k, ":")

if len(specs) > 1 {
cols[i].Options = specs[1]
}

values := strings.Split(specs[0], "=")
var name, key string
var name, key, options string

if len(values) == 2 {
name = values[0]
Expand All @@ -44,19 +41,36 @@ func NewColumns(keys []string) (cols Columns, err error) {
key = values[0]
}

if _, ok := generators[key]; !ok {
fmt.Fprintf(&errors, "Unknown generator: %s.\n", key)
if len(specs) > 1 {
options = specs[1]
}

gen, err := f.getGenerator(key, options)
if err != nil {
return cols, err
}

cols[i].Name = name
cols[i].Key = key
cols[i].Gen = gen
}

if errors.Len() > 0 {
err = fmt.Errorf(errors.String())
return cols, err
}

// GenerateRow generates a row of fake data using columns
// in the specified format
func (columns Columns) GenerateRow(formatter Formatter) string {
row := &bytes.Buffer{}

genValues := make([]string, len(columns))
for i, column := range columns {
genValues[i] = column.generate()
}

return cols, err
fmt.Fprintf(row, "%s", formatter.Format(columns, genValues))

return row.String()
}

func (columns Columns) names() (names []string) {
Expand Down
Loading

0 comments on commit 04355c7

Please sign in to comment.