Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/controllers/variable_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ pub fn write_edit_document(

fn escape_dotenv_value(value: &str) -> String {
let needs_quotes = value.is_empty()
|| value.contains([' ', '\t', '\n', '\r', '#', '"', '\'', '\\'])
|| value.chars().any(char::is_whitespace)
|| value.contains(['#', '"', '\'', '\\'])
|| value.starts_with(['=', '#']);

if !needs_quotes {
Expand Down Expand Up @@ -635,6 +636,48 @@ mod tests {
);
}

#[test]
fn dotenv_round_trip_preserves_unicode_whitespace() {
// Rust's trim recognizes more whitespace than the dotenv writer used to quote.
let whitespace = [
'\u{00a0}', '\u{2003}', '\u{2009}', '\u{202f}', '\u{3000}', '\u{0085}', '\u{000b}',
'\u{000c}',
];
let mut failures = Vec::new();
for ch in whitespace {
for value in [
format!("{ch}value"),
format!("value{ch}"),
ch.to_string(),
format!("left{ch}right"),
] {
let snapshot = EditSnapshot {
editable: BTreeMap::from([(
"VALUE".into(),
EditVariableEntry {
value: value.clone(),
is_sealed: false,
},
)]),
read_only: BTreeMap::new(),
};
let path = temp_edit_path("whitespace");
write_edit_document(&path, &snapshot, &[]).unwrap();
let parsed = parse_edit_document(&fs::read_to_string(&path).unwrap()).unwrap();
let _ = fs::remove_file(&path);
if parsed["VALUE"].value != value {
failures.push(format!("{value:?} became {:?}", parsed["VALUE"].value));
}
if !diff_edit_snapshot(&snapshot, &parsed).is_empty()
&& parsed["VALUE"].value == value
{
failures.push(format!("unchanged {value:?} produced a diff"));
}
}
}
assert!(failures.is_empty(), "{}", failures.join("\n"));
}

#[test]
fn read_only_railway_vars_are_comments_only() {
let snapshot = demo_snapshot();
Expand Down
Loading