-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
144 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package integration | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
// In these tests, there's a lot going on. Have a look at this article for a | ||
// longer explanation: | ||
// https://lucapette.me/writing/writing-integration-tests-for-a-go-cli-application | ||
|
||
var update = flag.Bool("update", false, "update golden files") | ||
|
||
const binaryName = "fakedata" | ||
|
||
var binaryPath string | ||
|
||
func TestMain(m *testing.M) { | ||
err := os.Chdir("..") | ||
if err != nil { | ||
fmt.Printf("could not change dir: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
abs, err := filepath.Abs(binaryName) | ||
if err != nil { | ||
fmt.Printf("could not get abs path for %s: %v", binaryName, err) | ||
os.Exit(1) | ||
} | ||
|
||
binaryPath = abs | ||
|
||
if err := exec.Command("make").Run(); err != nil { | ||
fmt.Printf("could not make binary for %s: %v", binaryName, err) | ||
os.Exit(1) | ||
} | ||
os.Exit(m.Run()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package integration | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/lucapette/fakedata/testutil" | ||
) | ||
|
||
var templateTests = []struct { | ||
tmpl string | ||
golden string | ||
wantErr bool | ||
}{ | ||
{"simple.tmpl", "simple-template.golden", false}, | ||
{"loop.tmpl", "loop.golden", false}, | ||
{"loop-with-index.tmpl", "loop-with-index.golden", false}, | ||
{"broken.tmpl", "broken-template.golden", true}, | ||
{"unknown-function.tmpl", "unknown-function.golden", true}, | ||
} | ||
|
||
func TestTemplatesWithCLIArgs(t *testing.T) { | ||
for _, tt := range templateTests { | ||
t.Run(tt.tmpl, func(t *testing.T) { | ||
cmd := exec.Command(binaryPath, "--template", fmt.Sprintf("testutil/fixtures/%s", tt.tmpl)) | ||
output, err := cmd.CombinedOutput() | ||
if (err != nil) != tt.wantErr { | ||
t.Fatalf("%s\nexpected (err != nil) to be %v, but got %v. err: %v", output, tt.wantErr, err != nil, err) | ||
} | ||
|
||
golden := testutil.NewGoldenFile(t, tt.golden) | ||
actual := string(output) | ||
if *update { | ||
golden.Write(actual) | ||
} | ||
|
||
expected := golden.Load() | ||
|
||
if !reflect.DeepEqual(actual, expected) { | ||
t.Fatalf("diff: %v", testutil.Diff(expected, actual)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestTemplatesWithPipe(t *testing.T) { | ||
for _, tt := range templateTests { | ||
t.Run(tt.tmpl, func(t *testing.T) { | ||
fixture := testutil.NewFixture(t, tt.tmpl) | ||
cmd := exec.Command(binaryPath) | ||
cmd.Stdin = fixture.AsFile() | ||
output, err := cmd.CombinedOutput() | ||
if (err != nil) != tt.wantErr { | ||
t.Fatalf("%s\nexpected (err != nil) to be %v, but got %v. err: %v", output, tt.wantErr, err != nil, err) | ||
} | ||
|
||
golden := testutil.NewGoldenFile(t, tt.golden) | ||
actual := string(output) | ||
if *update { | ||
golden.Write(actual) | ||
} | ||
|
||
expected := golden.Load() | ||
|
||
if !reflect.DeepEqual(actual, expected) { | ||
t.Fatalf("diff: %v", testutil.Diff(expected, actual)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{range $i := Loop 5}}{{$i}}{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
01234 | ||
01234 | ||
01234 | ||
01234 | ||
01234 | ||
01234 | ||
01234 | ||
01234 | ||
01234 | ||
01234 |