Tests: a # glued to the closing quote is no shell comment

In nightly-lib.sh, MAC_NIGHTLY_ID='x'#y assigns x#y, so the helper now
treats a # right after the closing quote as a comment only for the
Python file. Both readings have a case in
assigned_reads_the_active_line_in_either_quote_style.
This commit is contained in:
dotCooCoo committed 2026-12-01 06:15:20 -08:00
1 parent 4c8e40c191
commit 7307781c1d
1 file changed
+19 -12
+19 -12
View File
@@ -124,14 +124,15 @@ mod tests {
assert_eq!(NIGHTLY_APP_ID.strip_suffix("-v1"), Some(NIGHTLY_PACKAGE_ID), "the window id is the package id plus -v1");
// The packaging publishes the same package id.
let plist = include_str!("../../../packaging/macos/nightly_plist.py");
assert_eq!(assigned(plist, "IDENTIFIER"), Some(NIGHTLY_PACKAGE_ID), "nightly_plist.py");
assert_eq!(assigned(plist, "IDENTIFIER", false), Some(NIGHTLY_PACKAGE_ID), "nightly_plist.py");
let lib = include_str!("../../../packaging/macos/nightly-lib.sh");
assert_eq!(assigned(lib, "MAC_NIGHTLY_ID"), Some(NIGHTLY_PACKAGE_ID), "nightly-lib.sh");
assert_eq!(assigned(lib, "MAC_NIGHTLY_ID", true), Some(NIGHTLY_PACKAGE_ID), "nightly-lib.sh");
}
/// The value of the last uncommented top-level `name = value` or `name=value` line, with one pair of
/// single or double quotes removed and a trailing `#` comment after the value ignored.
fn assigned<'a>(source: &'a str, name: &str) -> Option<&'a str> {
/// single or double quotes removed and a trailing `#` comment after the value ignored. With `shell`, a `#`
/// after a closing quote starts a comment only when whitespace comes before it; Python needs no whitespace.
fn assigned<'a>(source: &'a str, name: &str, shell: bool) -> Option<&'a str> {
source
.lines()
.filter_map(|line| {
@@ -139,8 +140,9 @@ mod tests {
let quoted = ['\'', '"'].iter().find_map(|q| {
let inner = rest.strip_prefix(*q)?;
let end = inner.find(*q)?;
let after = inner[end + 1..].trim();
(after.is_empty() || after.starts_with('#')).then(|| &inner[..end])
let after = &inner[end + 1..];
let comment = (!shell || after.starts_with(char::is_whitespace)) && after.trim_start().starts_with('#');
(after.trim().is_empty() || comment).then(|| &inner[..end])
});
let bare = rest.split_once(" #").map_or(rest, |(value, _)| value.trim_end());
Some(quoted.unwrap_or(bare))
@@ -150,12 +152,17 @@ mod tests {
#[test]
fn assigned_reads_the_active_line_in_either_quote_style() {
assert_eq!(assigned("# ID='old'\nID=\"new\"\n", "ID"), Some("new"));
assert_eq!(assigned("ID = 'a'\n", "ID"), Some("a"));
assert_eq!(assigned("IDX='a'\n", "ID"), None);
assert_eq!(assigned("ID = 'a' # bundle ID\n", "ID"), Some("a"));
assert_eq!(assigned("ID=\"a\" # bundle ID\n", "ID"), Some("a"));
assert_eq!(assigned("ID=a # bundle ID\n", "ID"), Some("a"));
for shell in [false, true] {
assert_eq!(assigned("# ID='old'\nID=\"new\"\n", "ID", shell), Some("new"));
assert_eq!(assigned("ID = 'a'\n", "ID", shell), Some("a"));
assert_eq!(assigned("IDX='a'\n", "ID", shell), None);
assert_eq!(assigned("ID = 'a' # bundle ID\n", "ID", shell), Some("a"));
assert_eq!(assigned("ID=\"a\" # bundle ID\n", "ID", shell), Some("a"));
assert_eq!(assigned("ID=a # bundle ID\n", "ID", shell), Some("a"));
}
// In Python a `#` right after the closing quote starts a comment; in a shell it is part of the word.
assert_eq!(assigned("ID='a'#b\n", "ID", false), Some("a"));
assert_eq!(assigned("ID='a'#b\n", "ID", true), Some("'a'#b"));
}
#[test]