|
17 | 17 | teardown_tempdir |
18 | 18 | end |
19 | 19 |
|
| 20 | + # `include_characters` is the list of characters Pagefind counts as part of a |
| 21 | + # word. It reaches past ASCII, which is what makes the entry a UTF-8 file. |
20 | 22 | def write_entry(languages) |
21 | | - File.write(@entry_path, JSON.generate({ "version" => "1.5.2", "languages" => languages })) |
| 23 | + entry = { |
| 24 | + "version" => "1.5.2", |
| 25 | + "languages" => languages, |
| 26 | + "include_characters" => ["_", "‿", "⁀", "_"], |
| 27 | + } |
| 28 | + File.write(@entry_path, JSON.generate(entry)) |
22 | 29 | end |
23 | 30 |
|
24 | 31 | def read_entry |
25 | | - JSON.parse(File.read(@entry_path)) |
| 32 | + JSON.parse(File.read(@entry_path, encoding: Encoding::UTF_8)) |
| 33 | + end |
| 34 | + |
| 35 | + # Stands in for a build host that leaves the locale at POSIX, which is what |
| 36 | + # Cloudflare Pages does. |
| 37 | + def with_default_external(encoding) |
| 38 | + original = Encoding.default_external |
| 39 | + verbose, $VERBOSE = $VERBOSE, nil |
| 40 | + Encoding.default_external = encoding |
| 41 | + yield |
| 42 | + ensure |
| 43 | + Encoding.default_external = original |
| 44 | + $VERBOSE = verbose |
26 | 45 | end |
27 | 46 |
|
28 | 47 | def search_index(pagefind: SearchIndex::PAGEFIND) |
@@ -148,6 +167,14 @@ def stub_pagefind(status) |
148 | 167 | _(error.message).must_match(/missing index "en"/) |
149 | 168 | end |
150 | 169 |
|
| 170 | + it "reads the entry as UTF-8 whatever locale the build runs under" do |
| 171 | + write_entry({ "en" => { "hash" => "en_abc", "wasm" => "en", "page_count" => 560 } }) |
| 172 | + |
| 173 | + with_default_external(Encoding::US_ASCII) { search_index.apply_fallbacks } |
| 174 | + |
| 175 | + _(read_entry["languages"]["bg"]["hash"]).must_equal "en_abc" |
| 176 | + end |
| 177 | + |
151 | 178 | it "raises when the bundle has no languages" do |
152 | 179 | write_entry({}) |
153 | 180 |
|
|
0 commit comments