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:
2026-10-11 20:53:00 +02:00
committed by Lillem4n
parent deb42df0e2
commit 9528ecd8b0
6 changed files with 172 additions and 123 deletions
+18 -22
View File
@@ -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())
}
}