glean-sym 71.0.0

A Rust API on top of the Glean UniFFI C FFI
Documentation
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::fs;

use xshell::{Shell, cmd};

/// Test that the generated metrics type file for glean-sym is correct.
///
/// The `glean-sym/src/metrics.rs` file is generated by tools/glean-sym-parser.
/// This can be done manually with:
/// `cargo run -p glean-sym-parser -- glean-core/src/glean.udl  > glean-core/glean-sym/src/metrics.rs`
///
/// This tests does the same and checks that the file on disk is unmodified.
/// Otherwise the test will fail.
#[test]
fn generated_metrics_code_up_to_date() {
    // Relative to `glean-sym`
    let udl_file = "../src/glean.udl";
    let udl_src = fs::read_to_string(udl_file).expect("unable to read UDL file");

    let output = glean_sym_parser::generate(&udl_src);
    let dst_file = "src/metrics.rs";

    fs::write(dst_file, output.as_bytes()).unwrap();

    // Last but not least check if we modified the document.
    let sh = Shell::new().unwrap();
    cmd!(sh, "git --no-pager diff --exit-code {dst_file}")
        .run()
        .unwrap();
}

/// Test that the embedded contract version of glean-sym matches what glean-core sets.
#[test]
fn contract_version_matches() {
    let glean_core_version = glean_core::ffi_glean_core_uniffi_contract_version();

    // Relative to `glean-sym`
    let embedded_version = "src/contract_version.txt";
    let version_str =
        fs::read_to_string(embedded_version).expect("unable to read contract_version.txt");
    let version = version_str
        .trim_end()
        .parse::<u32>()
        .expect("can't parse content of contract_version.txt");

    if glean_core_version != version {
        let version = format!("{glean_core_version}\n");
        fs::write(embedded_version, version).unwrap();
    }

    assert_eq!(
        glean_core_version, version,
        "Wrong contract version in contract_version.txt. File has been automatically updated. Please commit it."
    );
}