|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/errata-ai/vale/v3/internal/glob" |
| 9 | +) |
| 10 | + |
| 11 | +// TestNewFileSectionOrderWins verifies that when multiple config sections |
| 12 | +// match the same file, NewFile resolves Lang and Transform deterministically |
| 13 | +// by taking the LAST matching section in the order it was written -- the |
| 14 | +// same "later one wins" semantics the rule/level resolution loop documents |
| 15 | +// for #965 -- rather than depending on Go's randomized map iteration order. |
| 16 | +func TestNewFileSectionOrderWins(t *testing.T) { |
| 17 | + sections := []struct { |
| 18 | + name string |
| 19 | + lang string |
| 20 | + xslt string |
| 21 | + }{ |
| 22 | + {"*.md", "en", "one.xsl"}, |
| 23 | + {"*.md", "fr", "two.xsl"}, |
| 24 | + {"*.md", "de", "three.xsl"}, |
| 25 | + {"*.md", "ja", "four.xsl"}, |
| 26 | + {"*.md", "es", "five.xsl"}, |
| 27 | + {"*.md", "it", "six.xsl"}, |
| 28 | + } |
| 29 | + |
| 30 | + cfg, err := NewConfig(&CLIFlags{}) |
| 31 | + if err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + |
| 35 | + for i, s := range sections { |
| 36 | + // Reuse the same glob pattern across sections, but track each |
| 37 | + // occurrence under its own key so every one can carry its own |
| 38 | + // Lang/Transform value -- mirroring how repeated real config |
| 39 | + // sections (e.g. multiple `[*.md]` blocks) are keyed internally. |
| 40 | + key := s.name |
| 41 | + if i > 0 { |
| 42 | + key = s.name + string(rune('a'+i)) |
| 43 | + } |
| 44 | + |
| 45 | + pat, cerr := glob.Compile(s.name) |
| 46 | + if cerr != nil { |
| 47 | + t.Fatal(cerr) |
| 48 | + } |
| 49 | + |
| 50 | + cfg.SecToPat[key] = pat |
| 51 | + cfg.RuleKeys = append(cfg.RuleKeys, key) |
| 52 | + cfg.FormatToLang[key] = s.lang |
| 53 | + cfg.Stylesheets[key] = s.xslt |
| 54 | + cfg.SChecks[key] = map[string]bool{} |
| 55 | + cfg.SLevels[key] = map[string]string{} |
| 56 | + } |
| 57 | + |
| 58 | + wantLang := sections[len(sections)-1].lang |
| 59 | + wantTransform := sections[len(sections)-1].xslt |
| 60 | + |
| 61 | + docPath := filepath.Join(t.TempDir(), "doc.md") |
| 62 | + if werr := os.WriteFile(docPath, []byte("# Title\n"), 0600); werr != nil { |
| 63 | + t.Fatal(werr) |
| 64 | + } |
| 65 | + |
| 66 | + const iterations = 50 |
| 67 | + for i := 0; i < iterations; i++ { |
| 68 | + f, ferr := NewFile(docPath, cfg) |
| 69 | + if ferr != nil { |
| 70 | + t.Fatal(ferr) |
| 71 | + } |
| 72 | + |
| 73 | + if f.NLP.Lang != wantLang { |
| 74 | + t.Fatalf("iteration %d: expected Lang %q (the last written matching section), got %q", |
| 75 | + i, wantLang, f.NLP.Lang) |
| 76 | + } |
| 77 | + |
| 78 | + if f.Transform != wantTransform { |
| 79 | + t.Fatalf("iteration %d: expected Transform %q (the last written matching section), got %q", |
| 80 | + i, wantTransform, f.Transform) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// TestNewFileGlobalLangFallback verifies that the `[*] Lang = ...` global |
| 86 | +// default still applies when no format-specific section matches the file. |
| 87 | +func TestNewFileGlobalLangFallback(t *testing.T) { |
| 88 | + cfg, err := NewConfig(&CLIFlags{}) |
| 89 | + if err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + |
| 93 | + cfg.FormatToLang["*"] = "ja" |
| 94 | + |
| 95 | + docPath := filepath.Join(t.TempDir(), "doc.txt") |
| 96 | + if werr := os.WriteFile(docPath, []byte("hello\n"), 0600); werr != nil { |
| 97 | + t.Fatal(werr) |
| 98 | + } |
| 99 | + |
| 100 | + f, err := NewFile(docPath, cfg) |
| 101 | + if err != nil { |
| 102 | + t.Fatal(err) |
| 103 | + } |
| 104 | + |
| 105 | + if f.NLP.Lang != "ja" { |
| 106 | + t.Fatalf("expected global Lang fallback %q, got %q", "ja", f.NLP.Lang) |
| 107 | + } |
| 108 | +} |
0 commit comments