Skip to content

Commit b6ba0de

Browse files
committed
Generalize file ensuring infrastructure
1 parent 0f6f458 commit b6ba0de

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -859,12 +859,12 @@ fn manual(fields: &[(&'static str, &'static str, &[&str], &str)]) -> String {
859859
mod tests {
860860
use std::fs;
861861

862-
use test_utils::project_dir;
862+
use test_utils::{ensure_file_contents, project_dir};
863863

864864
use super::*;
865865

866866
#[test]
867-
fn schema_in_sync_with_package_json() {
867+
fn ensure_schema_in_package_json() {
868868
let s = Config::json_schema();
869869
let schema = format!("{:#}", s);
870870
let mut schema = schema
@@ -885,13 +885,12 @@ mod tests {
885885

886886
let start = package_json.find(start_marker).unwrap() + start_marker.len();
887887
let end = package_json.find(end_marker).unwrap();
888+
888889
let p = remove_ws(&package_json[start..end]);
889890
let s = remove_ws(&schema);
890-
891891
if !p.contains(&s) {
892892
package_json.replace_range(start..end, &schema);
893-
fs::write(&package_json_path, &mut package_json).unwrap();
894-
panic!("new config, updating package.json")
893+
ensure_file_contents(&package_json_path, &package_json)
895894
}
896895
}
897896

crates/test_utils/src/lib.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod fixture;
1414
use std::{
1515
convert::{TryFrom, TryInto},
1616
env, fs,
17-
path::PathBuf,
17+
path::{Path, PathBuf},
1818
};
1919

2020
use profile::StopWatch;
@@ -353,3 +353,36 @@ pub fn bench(label: &'static str) -> impl Drop {
353353

354354
Bencher { sw: StopWatch::start(), label }
355355
}
356+
357+
/// Checks that the `file` has the specified `contents`. If that is not the
358+
/// case, updates the file and then fails the test.
359+
pub fn ensure_file_contents(file: &Path, contents: &str) {
360+
if let Err(()) = try_ensure_file_contents(file, contents) {
361+
panic!("Some files were not up-to-date");
362+
}
363+
}
364+
365+
/// Checks that the `file` has the specified `contents`. If that is not the
366+
/// case, updates the file and return an Error.
367+
pub fn try_ensure_file_contents(file: &Path, contents: &str) -> Result<(), ()> {
368+
match std::fs::read_to_string(file) {
369+
Ok(old_contents) if normalize_newlines(&old_contents) == normalize_newlines(contents) => {
370+
return Ok(())
371+
}
372+
_ => (),
373+
}
374+
let display_path = file.strip_prefix(&project_dir()).unwrap_or(file);
375+
eprintln!(
376+
"\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
377+
display_path.display()
378+
);
379+
if let Some(parent) = file.parent() {
380+
let _ = std::fs::create_dir_all(parent);
381+
}
382+
std::fs::write(file, contents).unwrap();
383+
Err(())
384+
}
385+
386+
fn normalize_newlines(s: &str) -> String {
387+
s.replace("\r\n", "\n")
388+
}

0 commit comments

Comments
 (0)