Debian Codegraph @

src/bin/update_fixtures.rs

raw symbols SCIP
1 //! Run the `update_fixtures` binary after updating any of the `benches` `.ftl` fixtures.
2 //! This will update the `.json` files used in reference tests.
3 //!
4 //! This file must be run from `{PROJECT_ROOT}/fluent-syntax`
5 //!
6 //! ```sh
7 //! cargo run --bin update_fixtures --features="json"
8 //! ```
9 use std::fs;
10 use std::io;
11
12 use fluent_syntax::parser::parse;
13
14 fn read_file(path: &str) -> Result<String, io::Error> {
15 fs::read_to_string(path)
16 }
17
18 fn write_file(path: &str, source: &str) -> Result<(), io::Error> {
19 fs::write(path, source)
20 }
21
22 fn main() {
23 let samples = &["menubar", "preferences", "simple"];
24 let contexts = &["browser", "preferences"];
25
26 for sample in samples {
27 let path = format!("./benches/{}.ftl", sample);
28 let source = read_file(&path).expect(
29 "Could not read the benches file. Are you running this from the correct directory? It must be run from `{PROJECT_ROOT}/fluent-syntax`",
30 );
31 let ast = parse(source).unwrap();
32 let target_json = serde_json::to_string_pretty(&ast).unwrap();
33 let new_path = format!("./tests/fixtures/benches/{}.json", sample);
34 write_file(&new_path, &target_json).unwrap();
35 }
36
37 for test in contexts {
38 let paths = fs::read_dir(format!("./benches/contexts/{}", test)).unwrap();
39 for path in paths.into_iter() {
40 let p = path.unwrap().path();
41 let file_name = p.file_name().unwrap().to_str().unwrap();
42 let path = p.to_str().unwrap();
43 let source = read_file(path).unwrap();
44 let ast = parse(source).unwrap();
45 let target_json = serde_json::to_string_pretty(&ast).unwrap();
46 let new_path = format!(
47 "./tests/fixtures/benches/contexts/{}/{}",
48 test,
49 file_name.replace(".ftl", ".json")
50 );
51 write_file(&new_path, &target_json).unwrap();
52 }
53 }
54 }