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

Commit 6bb1c196 authored by Martin Geisler's avatar Martin Geisler Committed by Cherrypicker Worker
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}
(cherry picked from https://android-review.googlesource.com/q/commit:7b828af494080e3680f8df7e00db017c2b592a4c)
Merged-In: Ia6f3947281d9f6d2c286d7dce68a75a2fa4ceebf
Change-Id: Ia6f3947281d9f6d2c286d7dce68a75a2fa4ceebf
parent f5c370be
Loading
Loading
Loading
Loading
+3 −2
Original line number Original line Diff line number Diff line
@@ -317,8 +317,9 @@ mod tests {
    /// are read from `"tests/generated/{name}_{endianness}_{id}.rs"`
    /// are read from `"tests/generated/{name}_{endianness}_{id}.rs"`
    /// where `is` taken from the declaration.
    /// where `is` taken from the declaration.
    ///
    ///
    /// When adding new tests, use `touch` to create the missing files
    /// When adding new tests or modifying existing ones, use
    /// and use `UPDATE_SNAPSHOTS=1 cargo test` to populate them.
    /// `UPDATE_SNAPSHOTS=1 cargo test` to automatically populate the
    /// snapshots with the expected output.
    ///
    ///
    /// The `code` cannot have an endianness declaration, instead you
    /// The `code` cannot have an endianness declaration, instead you
    /// must supply either `little_endian` or `big_endian` as
    /// must supply either `little_endian` or `big_endian` as
+7 −4
Original line number Original line Diff line number Diff line
@@ -146,14 +146,17 @@ pub fn assert_contains(haystack: &str, needle: &str) {
/// a panic is triggered if they differ.
/// a panic is triggered if they differ.
#[track_caller]
#[track_caller]
pub fn assert_snapshot_eq<P: AsRef<Path>>(snapshot_path: P, actual_content: &str) {
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 = snapshot_path.as_ref();
    let snapshot_content = fs::read(snapshot).unwrap_or_else(|err| {
    let snapshot_content = match fs::read(snapshot) {
        panic!("Could not read snapshot from {}: {}", snapshot.display(), err)
        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");
    let snapshot_content = String::from_utf8(snapshot_content).expect("Snapshot was not UTF-8");


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