Skip to content

Commit 209e36e

Browse files
mikolalysenkoclaude
andcommitted
Test that the berry stanza view and block scan agree
The hosted berry writers read the lock as blank-line stanzas and the vendored backend and field readers through scan_blocks. A test now asserts both name the same blocks in the same order across LF, CRLF, BOM, header-comment and no-trailing-newline locks, so the two reads cannot drift on any shape yarn writes. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 574a3e6 commit 209e36e

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

  • crates/socket-patch-core/src/formats/yarn

‎crates/socket-patch-core/src/formats/yarn/stanzas.rs‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,43 @@ mod tests {
173173
assert_eq!(stanza_key("# yarn lockfile"), None);
174174
assert_eq!(stanza_key(" version: 8"), None);
175175
}
176+
177+
/// The stanza view and [`super::super::blocks::scan_blocks`] are two
178+
/// reads of one berry lock (the hosted writers use the first, the
179+
/// vendored backend and the field readers the second): on every shape
180+
/// yarn writes — LF, CRLF, a BOM, a header comment, sub-maps, a final
181+
/// entry with or without a trailing newline — they must name the same
182+
/// blocks in the same order. (A blank line inside a block, which yarn
183+
/// never writes, is where they would part.)
184+
#[test]
185+
fn stanza_keys_match_the_block_scan_on_every_line_ending() {
186+
use super::super::blocks::scan_blocks;
187+
let lf = "# This file is generated by running \"yarn install\" inside your project.\n\
188+
# Manual changes might be lost - proceed with caution!\n\n\
189+
__metadata:\n version: 8\n cacheKey: 10c0\n\n\
190+
\"@scope/a@npm:^1.0.0, @scope/a@npm:^1.2.0\":\n version: 1.2.0\n \
191+
resolution: \"@scope/a@npm:1.2.0\"\n dependencies:\n b: \"npm:^2\"\n \
192+
checksum: 10c0/aa\n languageName: node\n linkType: hard\n\n\
193+
\"app@workspace:.\":\n version: 0.0.0-use.local\n resolution: \"app@workspace:.\"\n\n\
194+
\"b@npm:^2\":\n version: 2.0.0\n resolution: \"b@npm:2.0.0\"\n bin:\n b: cli.js\n";
195+
let crlf = lf.replace('\n', "\r\n");
196+
for text in [
197+
lf.to_string(),
198+
lf.trim_end().to_string(),
199+
crlf.clone(),
200+
format!("\u{feff}{crlf}"),
201+
format!("\u{feff}{lf}\n\n"),
202+
] {
203+
let stanzas = BerryStanzas::parse(&text);
204+
let from_stanzas: Vec<&str> = stanzas
205+
.stanzas
206+
.iter()
207+
.filter_map(|s| stanza_key(s))
208+
.collect();
209+
let blocks = scan_blocks(&text);
210+
let from_blocks: Vec<&str> = blocks.iter().map(|b| b.key.as_str()).collect();
211+
assert_eq!(from_stanzas, from_blocks, "{text:?}");
212+
assert_eq!(from_blocks.len(), 4, "{text:?}");
213+
}
214+
}
176215
}

0 commit comments

Comments
 (0)