mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-30 23:27:24 +00:00
Generalize file ensuring infrastructure
This commit is contained in:
parent
0f6f458cc1
commit
b6ba0dec0c
2 changed files with 38 additions and 6 deletions
|
@ -859,12 +859,12 @@ fn manual(fields: &[(&'static str, &'static str, &[&str], &str)]) -> String {
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use test_utils::project_dir;
|
use test_utils::{ensure_file_contents, project_dir};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn schema_in_sync_with_package_json() {
|
fn ensure_schema_in_package_json() {
|
||||||
let s = Config::json_schema();
|
let s = Config::json_schema();
|
||||||
let schema = format!("{:#}", s);
|
let schema = format!("{:#}", s);
|
||||||
let mut schema = schema
|
let mut schema = schema
|
||||||
|
@ -885,13 +885,12 @@ mod tests {
|
||||||
|
|
||||||
let start = package_json.find(start_marker).unwrap() + start_marker.len();
|
let start = package_json.find(start_marker).unwrap() + start_marker.len();
|
||||||
let end = package_json.find(end_marker).unwrap();
|
let end = package_json.find(end_marker).unwrap();
|
||||||
|
|
||||||
let p = remove_ws(&package_json[start..end]);
|
let p = remove_ws(&package_json[start..end]);
|
||||||
let s = remove_ws(&schema);
|
let s = remove_ws(&schema);
|
||||||
|
|
||||||
if !p.contains(&s) {
|
if !p.contains(&s) {
|
||||||
package_json.replace_range(start..end, &schema);
|
package_json.replace_range(start..end, &schema);
|
||||||
fs::write(&package_json_path, &mut package_json).unwrap();
|
ensure_file_contents(&package_json_path, &package_json)
|
||||||
panic!("new config, updating package.json")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ mod fixture;
|
||||||
use std::{
|
use std::{
|
||||||
convert::{TryFrom, TryInto},
|
convert::{TryFrom, TryInto},
|
||||||
env, fs,
|
env, fs,
|
||||||
path::PathBuf,
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use profile::StopWatch;
|
use profile::StopWatch;
|
||||||
|
@ -353,3 +353,36 @@ pub fn bench(label: &'static str) -> impl Drop {
|
||||||
|
|
||||||
Bencher { sw: StopWatch::start(), label }
|
Bencher { sw: StopWatch::start(), label }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks that the `file` has the specified `contents`. If that is not the
|
||||||
|
/// case, updates the file and then fails the test.
|
||||||
|
pub fn ensure_file_contents(file: &Path, contents: &str) {
|
||||||
|
if let Err(()) = try_ensure_file_contents(file, contents) {
|
||||||
|
panic!("Some files were not up-to-date");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks that the `file` has the specified `contents`. If that is not the
|
||||||
|
/// case, updates the file and return an Error.
|
||||||
|
pub fn try_ensure_file_contents(file: &Path, contents: &str) -> Result<(), ()> {
|
||||||
|
match std::fs::read_to_string(file) {
|
||||||
|
Ok(old_contents) if normalize_newlines(&old_contents) == normalize_newlines(contents) => {
|
||||||
|
return Ok(())
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
let display_path = file.strip_prefix(&project_dir()).unwrap_or(file);
|
||||||
|
eprintln!(
|
||||||
|
"\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
|
||||||
|
display_path.display()
|
||||||
|
);
|
||||||
|
if let Some(parent) = file.parent() {
|
||||||
|
let _ = std::fs::create_dir_all(parent);
|
||||||
|
}
|
||||||
|
std::fs::write(file, contents).unwrap();
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn normalize_newlines(s: &str) -> String {
|
||||||
|
s.replace("\r\n", "\n")
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue