Skip to content

Commit

Permalink
Fixed input file overwrite bug (#15)
Browse files Browse the repository at this point in the history
Reported-by: Gabriela Moreira <gabrielamoreiramafra@gmail.com>
Signed-off-by: Andrew Helwer <2n8rn1w1f@mozmail.com>
  • Loading branch information
ahelwer authored May 17, 2024
1 parent 36cb75b commit a57505b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ fn convert(input_path: &Path, output_path: &Path, mode: Mode, force: bool) -> Re
.context(format!("Failed to read input file [{:?}]", input_path))?;
}

let mut output_file = File::create(output_path)?;
match rewrite(&input, &mode, force) {
Ok(output) => {
let mut output_file = File::create(output_path)?;
output_file.write_all(output.as_bytes()).context(format!("Failed to write to output file [{:?}]", output_path))?;
Ok(())
},
Expand All @@ -83,3 +83,28 @@ fn convert(input_path: &Path, output_path: &Path, mode: Mode, force: bool) -> Re
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
// https://github.com/tlaplus-community/tlauc/issues/14
fn test_input_file_unchanged_on_parse_failure() {
let project_root = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let input_path = PathBuf::from(project_root)
.join("tests")
.join("InvalidSyntax.tla");
let expected = std::fs::read_to_string(&input_path).unwrap();
let output_path = input_path.clone();
let result: Result<()> = convert(
input_path.as_path(),
output_path.as_path(),
tlauc::Mode::AsciiToUnicode,
false,
);
assert!(result.is_err());
let actual = std::fs::read_to_string(&output_path).unwrap();
assert_eq!(expected, actual);
}
}
4 changes: 4 additions & 0 deletions tests/InvalidSyntax.tla
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---- MODULE test ----
foo == invalid,
====

0 comments on commit a57505b

Please sign in to comment.