From ae0527a26e8b78f44e7452f746a86df3e3aa7867 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Wed, 2 Sep 2026 19:09:19 +0200 Subject: [PATCH] Tests for the real data-path error, an empty data path, and a crypto/rand failure --- cmd/fejkdata/main_test.go | 7 +++++++ fejkdata_test.go | 24 ++++++++++++++++++++++-- template_test.go | 8 +++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/cmd/fejkdata/main_test.go b/cmd/fejkdata/main_test.go index afe2a62..cb08507 100644 --- a/cmd/fejkdata/main_test.go +++ b/cmd/fejkdata/main_test.go @@ -355,3 +355,10 @@ func TestRunUnknownFlagIsNamedByRune(t *testing.T) { t.Errorf("run(-รค) = %d, %q, want the flag named whole", code, errb) } } + +func TestRunEmptyDataPathIsNamed(t *testing.T) { + code, _, errb := runOut("--data-path=", "sv_SE.word") + if code != 1 || !strings.Contains(errb, "empty") { + t.Errorf("run(--data-path=) = %d, %q, want the empty path named", code, errb) + } +} diff --git a/fejkdata_test.go b/fejkdata_test.go index d85401f..51e8e24 100644 --- a/fejkdata_test.go +++ b/fejkdata_test.go @@ -1,8 +1,11 @@ package fejkdata import ( + "errors" + "io/fs" "strings" "testing" + "testing/fstest" ) // newGenerator creates a generator over a single data directory, failing on @@ -38,8 +41,25 @@ func fake(t *testing.T, f *Generator, path string) string { func TestNewMissingDirectory(t *testing.T) { _, err := New(WithoutShippedData(), WithDataPath("data/de_DE")) - if err == nil || !strings.Contains(err.Error(), "de_DE") { - t.Fatalf("New(missing) error = %v, want it to name the path", err) + if err == nil || !strings.Contains(err.Error(), "de_DE") || !errors.Is(err, fs.ErrNotExist) { + t.Fatalf("New(missing) error = %v, want the real error, naming the path", err) + } +} + +func TestNewRejectsAnEmptyDataPath(t *testing.T) { + _, err := New(WithoutShippedData(), WithDataPath("")) + if err == nil || !strings.Contains(err.Error(), "empty") { + t.Fatalf("New(WithDataPath(\"\")) = %v, want the empty path named", err) + } +} + +func TestNewReportsAnEntropyFailure(t *testing.T) { + saved := randomBytes + randomBytes = func([]byte) (int, error) { return 0, errors.New("no entropy") } + defer func() { randomBytes = saved }() + _, err := New(WithoutShippedData(), WithDataFS(fstest.MapFS{"w.json": {Data: []byte(`"x"`)}})) + if err == nil || !strings.Contains(err.Error(), "no entropy") { + t.Fatalf("New() without entropy = %v, want the failure reported", err) } } diff --git a/template_test.go b/template_test.go index 0ce3237..d5aa2ad 100644 --- a/template_test.go +++ b/template_test.go @@ -8,7 +8,13 @@ import ( ) // engine builds a seeded generator with no loaded categories, for rendering tests. -func engine(seed uint64) *Generator { return &Generator{rand: newRand(seed, true)} } +func engine(seed uint64) *Generator { + s, err := newRand(seed, true) + if err != nil { + panic(err) + } + return &Generator{rand: s} +} // parse unmarshals a JSON template fragment into its dynamic form. func parse(t *testing.T, s string) any {