diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-12 01:22:12 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-12 01:22:12 +0200 |
| commit | 3b36a48b7ce5a53a9366f3b31f94311f178e2553 (patch) | |
| tree | ecbb277ff916b719f2ee45fba017792b85d5faf9 /internal/engine/facts_test.go | |
| parent | 42b02c47be9b285099203e44a2570636d4ca6f03 (diff) | |
| download | krino-3b36a48b7ce5a53a9366f3b31f94311f178e2553.tar.gz krino-3b36a48b7ce5a53a9366f3b31f94311f178e2553.zip | |
krino: matching — scan, ignore, conditions, extraction, duplicates, explain, dry run
Diffstat (limited to 'internal/engine/facts_test.go')
| -rw-r--r-- | internal/engine/facts_test.go | 77 |
1 files changed, 77 insertions, 0 deletions
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) + } +} |
