Answer architecture round 1: a custom CLI names itself by its file, --version names its module beside fejkdata's and follows a replace, the module path comes from the library's type, the end-to-end test runs main in a subprocess, the CLI tests find the shipped modules by their manifests, and Your own CLI sits under Library
This commit is contained in:
+18
-22
@@ -1,32 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/larvit/fejkdata/cli"
|
||||
"github.com/larvit/fejkdata/data"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if os.Getenv("FEJKDATA_RUN_MAIN") == "1" {
|
||||
main()
|
||||
}
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func TestMainRendersTheShippedData(t *testing.T) {
|
||||
stdin, feed, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
drain, stdout, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
args, oldIn, oldOut := os.Args, os.Stdin, os.Stdout
|
||||
t.Cleanup(func() { os.Args, os.Stdin, os.Stdout = args, oldIn, oldOut })
|
||||
os.Args, os.Stdin, os.Stdout = []string{"fejkdata", "--seed", "1"}, stdin, stdout
|
||||
io.WriteString(feed, "{/misc.territory[SE].alpha2} {/en_US.person.last} {/sv_SE.address.locality}")
|
||||
feed.Close()
|
||||
code := cli.Main(data.Modules()...)
|
||||
stdout.Close()
|
||||
out, _ := io.ReadAll(drain)
|
||||
if code != 0 || len(out) < len("SE x y") || string(out[:3]) != "SE " {
|
||||
t.Errorf("fejkdata = %d, %q, want SE followed by a last name and a locality", code, out)
|
||||
cmd := exec.Command(os.Args[0], "--seed", "1")
|
||||
cmd.Env = append(os.Environ(), "FEJKDATA_RUN_MAIN=1")
|
||||
cmd.Stdin = strings.NewReader("{/misc.territory[SE].alpha2} {/en_US.person.last} {/sv_SE.address.locality}")
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
out, err := cmd.Output()
|
||||
if fields := strings.Fields(string(out)); err != nil || len(fields) < 3 || fields[0] != "SE" {
|
||||
t.Errorf("fejkdata = %v, %q, %q, want SE followed by a last name and a locality", err, out, stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user