Updates from reviewers

This commit is contained in:
lilleman
2026-06-09 10:03:53 +02:00
parent cee2f35a2c
commit 5b6480a094
15 changed files with 845 additions and 498 deletions
+47 -6
View File
@@ -10,10 +10,12 @@
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"runtime/debug"
"strings"
"github.com/Timewave-AB/fakes"
@@ -25,7 +27,11 @@ const usage = `Usage: fakes -data-path D [-data-path D]... [-seed N] [-repeat N]
<path> a category, or a dotted path into one (person, person.last)
-seed N seed for reproducible output
-repeat N render the path N times (default 1)
-separator S string between repeated values (default newline)`
-separator S string between repeated values (default newline)
-list list the paths the data offers, then exit
-version print the version, then exit
Flags must come before <path>.`
// stringList collects a repeatable string flag, preserving order.
type stringList []string
@@ -45,19 +51,24 @@ func run(args []string, stdout, stderr io.Writer) int {
seed := fs.Uint64("seed", 0, "seed for reproducible output")
repeat := fs.Int("repeat", 1, "render the path this many times")
sep := fs.String("separator", "\n", "string between repeated values")
list := fs.Bool("list", false, "list the paths the data offers, then exit")
showVersion := fs.Bool("version", false, "print the version, then exit")
var dirs stringList
fs.Var(&dirs, "data-path", "a data directory to load (repeatable)")
if err := fs.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) { // -h/-help already printed usage
return 0
}
return 2
}
if len(dirs) == 0 || fs.NArg() != 1 {
if *showVersion {
fmt.Fprintln(stdout, "fakes "+buildVersion())
return 0
}
if len(dirs) == 0 {
fs.Usage()
return 2
}
if *repeat < 1 {
fmt.Fprintln(stderr, "repeat must be a positive integer")
return 2
}
var opts []fakes.Option
fs.Visit(func(fl *flag.Flag) {
@@ -66,6 +77,26 @@ func run(args []string, stdout, stderr io.Writer) int {
}
})
if *list {
f, err := fakes.New(dirs, opts...)
if err != nil {
fmt.Fprintln(stderr, err)
return 1
}
for _, p := range f.List() {
fmt.Fprintln(stdout, p)
}
return 0
}
if fs.NArg() != 1 {
fs.Usage()
return 2
}
if *repeat < 1 {
fmt.Fprintln(stderr, "repeat must be a positive integer")
return 2
}
path := fs.Arg(0)
f, err := fakes.New(dirs, opts...)
if err != nil {
@@ -82,3 +113,13 @@ func run(args []string, stdout, stderr io.Writer) int {
fmt.Fprintln(stdout, strings.Join(vals, *sep))
return 0
}
// buildVersion reports the module version stamped into the binary by `go install`
// (or "devel" for a local build), read from the build info — no version constant
// to bump, no extra dependency.
func buildVersion() string {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" {
return info.Main.Version
}
return "devel"
}
+36
View File
@@ -145,6 +145,42 @@ func TestRunUnknownCategoryFails(t *testing.T) {
}
}
func TestRunList(t *testing.T) {
// -list prints the discoverable paths and exits 0, no positional path needed.
code, out, errb := runOut("-data-path", svSE, "-list")
if code != 0 {
t.Fatalf("run = %d, stderr=%q", code, errb)
}
for _, want := range []string{"person", "person.last", "address"} {
if !strings.Contains(out, want) {
t.Errorf("list output missing %q:\n%s", want, out)
}
}
}
func TestRunHelpExitsZero(t *testing.T) {
// -h/-help is success (0), not misuse (2), so scripts under `set -e` survive.
for _, h := range []string{"-h", "-help"} {
code, _, errb := runOut(h)
if code != 0 {
t.Errorf("%s = %d, want 0", h, code)
}
if !strings.Contains(errb, "Usage") {
t.Errorf("%s: stderr = %q, want usage", h, errb)
}
}
}
func TestRunVersion(t *testing.T) {
code, out, errb := runOut("-version")
if code != 0 {
t.Fatalf("-version = %d, stderr=%q", code, errb)
}
if strings.TrimSpace(out) == "" {
t.Error("-version: want a version on stdout")
}
}
func TestRunMissingDirFails(t *testing.T) {
code, _, errb := runOut("-data-path", "../../data/nope", "person")
if code != 1 {