From 3b36a48b7ce5a53a9366f3b31f94311f178e2553 Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Sat, 12 Sep 2026 01:22:12 +0200 Subject: krino: matching — scan, ignore, conditions, extraction, duplicates, explain, dry run MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/engine/facts_test.go | 77 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 internal/engine/facts_test.go (limited to 'internal/engine/facts_test.go') diff --git a/internal/engine/facts_test.go b/internal/engine/facts_test.go new file mode 100644 index 0000000..c511804 --- /dev/null +++ b/internal/engine/facts_test.go @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package engine + +import ( + "context" + "os" + "path/filepath" + "testing" + "time" + + "krino/internal/cond" + "krino/internal/extract" + "krino/internal/scan" +) + +// contentFacts builds a *facts for a real text file, under a Dir whose +// ContentVariants is variants, for exercising B2's raw-release directly. +func contentFacts(t *testing.T, body string, variants []cond.Options) *facts { + t.Helper() + p := filepath.Join(t.TempDir(), "a.txt") + if err := os.WriteFile(p, []byte(body), 0o644); err != nil { + t.Fatal(err) + } + fi, err := os.Stat(p) + if err != nil { + t.Fatal(err) + } + e := &Engine{Extract: extract.New(), Now: time.Now} + d := &Dir{Name: "d", ContentVariants: variants} + file := scan.File{Path: p, Rel: "a.txt", Name: "a.txt", Size: fi.Size(), ModTime: fi.ModTime()} + run := newMatchRun(e, d, context.Background(), time.Now(), []scan.File{file}) + return newFacts(run, file) +} + +// TestContentReleasesRawWithOneVariant: B2. A directory whose rules use +// exactly one (ignoreCase, fold) variant releases the raw extracted text +// once that variant's normalised copy exists. +func TestContentReleasesRawWithOneVariant(t *testing.T) { + f := contentFacts(t, "Hello World", []cond.Options{{IgnoreCase: true, Fold: false}}) + const want = "hello world" + got, err := f.Content(true, false) + if err != nil { + t.Fatal(err) + } + if got != want { + t.Fatalf("got %q, want %q", got, want) + } + if f.content != "" { + t.Errorf("raw text not released with a single content variant: %q", f.content) + } + // A second call for the same (already cached) variant must still work + // from normCache, without needing the released raw text. + if got, err := f.Content(true, false); err != nil || got != want { + t.Errorf("second call for the cached variant: got %q, %v, want %q", got, err, want) + } +} + +// TestContentKeepsRawWithTwoVariants: B2. A directory whose rules use two +// distinct variants must not release the raw text after the first: the +// second variant still needs it, and normalising it correctly (not from an +// emptied string) is the proof the raw text was kept. +func TestContentKeepsRawWithTwoVariants(t *testing.T) { + f := contentFacts(t, "Hello World", []cond.Options{ + {IgnoreCase: true, Fold: false}, + {IgnoreCase: false, Fold: false}, + }) + if got, err := f.Content(true, false); err != nil || got != "hello world" { + t.Fatalf("first variant: got %q, %v", got, err) + } + if f.content == "" { + t.Fatal("raw text released after only the first of two variants") + } + if got, err := f.Content(false, false); err != nil || got != "Hello World" { + t.Fatalf("second variant: got %q, %v, want the unfolded original (raw text must still be available)", got, err) + } +} -- cgit v1.3