Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7b828af4 authored by Martin Geisler's avatar Martin Geisler
Browse files

pdl: Let UPDATE_SNAPSHOTS create new snapshots

Before, we had to create the files one-by-one, now we can regenerate
all of them automatically.

Test: atest pdl_tests pdl_rust_generator_tests_{le,be}
Change-Id: Ia6f3947281d9f6d2c286d7dce68a75a2fa4ceebf
parent 745b08d7
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -317,8 +317,9 @@ mod tests {
    /// are read from `"tests/generated/{name}_{endianness}_{id}.rs"`
    /// where `is` taken from the declaration.
    ///
    /// When adding new tests, use `touch` to create the missing files
    /// and use `UPDATE_SNAPSHOTS=1 cargo test` to populate them.
    /// When adding new tests or modifying existing ones, use
    /// `UPDATE_SNAPSHOTS=1 cargo test` to automatically populate the
    /// snapshots with the expected output.
    ///
    /// The `code` cannot have an endianness declaration, instead you
    /// must supply either `little_endian` or `big_endian` as
+7 −4
Original line number Diff line number Diff line
@@ -146,14 +146,17 @@ pub fn assert_contains(haystack: &str, needle: &str) {
/// a panic is triggered if they differ.
#[track_caller]
pub fn assert_snapshot_eq<P: AsRef<Path>>(snapshot_path: P, actual_content: &str) {
    let update_snapshots = std::env::var("UPDATE_SNAPSHOTS").is_ok();
    let snapshot = snapshot_path.as_ref();
    let snapshot_content = fs::read(snapshot).unwrap_or_else(|err| {
        panic!("Could not read snapshot from {}: {}", snapshot.display(), err)
    });
    let snapshot_content = match fs::read(snapshot) {
        Ok(content) => content,
        Err(_) if update_snapshots => Vec::new(),
        Err(err) => panic!("Could not read snapshot from {}: {}", snapshot.display(), err),
    };
    let snapshot_content = String::from_utf8(snapshot_content).expect("Snapshot was not UTF-8");

    // Normal comparison if UPDATE_SNAPSHOTS is unset.
    if std::env::var("UPDATE_SNAPSHOTS").is_err() {
    if !update_snapshots {
        return assert_eq_with_diff(
            snapshot.to_str().unwrap(),
            &snapshot_content,