From 7c7f06f62b80a4189a8078af5c7d571d108b3ec5 Mon Sep 17 00:00:00 2001 From: samypr100 <3933065+samypr100@users.noreply.github.com> Date: Thu, 11 Apr 2024 09:41:09 +0000 Subject: [PATCH] feat: convert linehaul tests to use snapshots (#2923) ## Summary Closes #2564 ## Test Plan 1. Changed existing linehaul tests to leverage insta. 2. Ran tests in various linux distros (Debian, Ubuntu, Centos, Fedora, Alpine) to ensure they also pass locally again. --------- Co-authored-by: konstin --- Cargo.lock | 53 +++++++++ crates/uv-client/Cargo.toml | 2 +- crates/uv-client/tests/user_agent_version.rs | 118 ++++++++++++------- 3 files changed, 128 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a70416778..79b79ad7f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1745,6 +1745,8 @@ dependencies = [ "console", "lazy_static", "linked-hash-map", + "pest", + "pest_derive", "regex", "serde", "similar", @@ -2420,6 +2422,51 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +[[package]] +name = "pest" +version = "2.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56f8023d0fb78c8e03784ea1c7f3fa36e68a723138990b8d5a47d916b651e7a8" +dependencies = [ + "memchr", + "thiserror", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0d24f72393fd16ab6ac5738bc33cdb6a9aa73f8b902e8fe29cf4e67d7dd1026" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdc17e2a6c7d0a492f0158d7a4bd66cc17280308bbaff78d5bef566dca35ab80" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn 2.0.58", +] + +[[package]] +name = "pest_meta" +version = "2.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "934cd7631c050f4674352a6e835d5f6711ffbfb9345c2fc0107155ac495ae293" +dependencies = [ + "once_cell", + "pest", + "sha2", +] + [[package]] name = "petgraph" version = "0.6.4" @@ -4071,6 +4118,12 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "ucd-trie" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9" + [[package]] name = "unicode-bidi" version = "0.3.15" diff --git a/crates/uv-client/Cargo.toml b/crates/uv-client/Cargo.toml index 25ec0d8d1..bd8b7521d 100644 --- a/crates/uv-client/Cargo.toml +++ b/crates/uv-client/Cargo.toml @@ -51,5 +51,5 @@ anyhow = { workspace = true } http-body-util = { version = "0.1.0" } hyper = { version = "1.2.0", features = ["server", "http1"] } hyper-util = { version = "0.1.3", features = ["tokio"] } -insta = { version = "1.36.1" } +insta = { version = "1.36.1" , features = ["filters", "json", "redactions"] } tokio = { workspace = true, features = ["fs", "macros"] } diff --git a/crates/uv-client/tests/user_agent_version.rs b/crates/uv-client/tests/user_agent_version.rs index 383e0b758..2dd67f5f9 100644 --- a/crates/uv-client/tests/user_agent_version.rs +++ b/crates/uv-client/tests/user_agent_version.rs @@ -7,10 +7,10 @@ use hyper::server::conn::http1; use hyper::service::service_fn; use hyper::{Request, Response}; use hyper_util::rt::TokioIo; -use tokio::net::TcpListener; - +use insta::{assert_json_snapshot, assert_snapshot, with_settings}; use pep508_rs::{MarkerEnvironment, StringVersion}; use platform_tags::{Arch, Os, Platform}; +use tokio::net::TcpListener; use uv_cache::Cache; use uv_client::LineHaul; use uv_client::RegistryClientBuilder; @@ -179,52 +179,82 @@ async fn test_user_agent_has_linehaul() -> Result<()> { // Deserializing Linehaul let linehaul: LineHaul = serde_json::from_str(uv_linehaul)?; - // Assert uv version - assert_eq!(uv_version, format!("uv/{}", version())); - - // Assert linehaul - let installer_info = linehaul.installer.unwrap(); - let system_info = linehaul.system.unwrap(); - let impl_info = linehaul.implementation.unwrap(); - - assert_eq!(installer_info.name.as_deref(), Some("uv")); - assert_eq!(installer_info.version.as_deref(), Some(version())); - - assert_eq!(system_info.name, Some(markers.platform_system)); - assert_eq!(system_info.release, Some(markers.platform_release)); - - assert_eq!(impl_info.name, Some(markers.platform_python_implementation)); - assert_eq!( - impl_info.version, - Some(markers.python_full_version.version.to_string()) - ); - - assert_eq!( - linehaul.python, - Some(markers.python_full_version.version.to_string()) - ); - assert_eq!(linehaul.cpu, Some(markers.platform_machine)); - - assert_eq!(linehaul.openssl_version, None); - assert_eq!(linehaul.setuptools_version, None); - assert_eq!(linehaul.rustc_version, None); + // Assert linehaul user agent + let filters = vec![(version(), "[VERSION]")]; + with_settings!({ + filters => filters + }, { + // Assert uv version + assert_snapshot!(uv_version, @"uv/[VERSION]"); + // Assert linehaul json + assert_json_snapshot!(&linehaul, { + ".distro" => "[distro]", + ".ci" => "[ci]" + }, @r###" + { + "installer": { + "name": "uv", + "version": "[VERSION]" + }, + "python": "3.12.2", + "implementation": { + "name": "CPython", + "version": "3.12.2" + }, + "distro": "[distro]", + "system": { + "name": "Linux", + "release": "6.5.0-1016-azure" + }, + "cpu": "x86_64", + "openssl_version": null, + "setuptools_version": null, + "rustc_version": null, + "ci": "[ci]" + } + "###); + }); + // Assert distro if cfg!(windows) { - assert_eq!(linehaul.distro, None); + assert_json_snapshot!(&linehaul.distro, @"null"); } else if cfg!(target_os = "linux") { - let Some(distro_info) = linehaul.distro else { - panic!("got no distro, but expected one in linehaul") - }; - assert_eq!(distro_info.id.as_deref(), Some("jammy")); - assert_eq!(distro_info.name.as_deref(), Some("Ubuntu")); - assert_eq!(distro_info.version.as_deref(), Some("22.04")); - assert!(distro_info.libc.is_some()); + assert_json_snapshot!(&linehaul.distro, { + ".id" => "[distro.id]", + ".name" => "[distro.name]", + ".version" => "[distro.version]" + // We mock the libc version already + }, @r###" + { + "name": "[distro.name]", + "version": "[distro.version]", + "id": "[distro.id]", + "libc": { + "lib": "glibc", + "version": "2.38" + } + }"### + ); + // Check dynamic values + let distro_info = linehaul + .distro + .expect("got no distro, but expected one in linehaul"); + // Gather distribution info from /etc/os-release. + let release_info = sys_info::linux_os_release() + .expect("got no os release info, but expected one in linux"); + assert_eq!(distro_info.id, release_info.version_codename); + assert_eq!(distro_info.name, release_info.name); + assert_eq!(distro_info.version, release_info.version_id); } else if cfg!(target_os = "macos") { - let distro_info = linehaul.distro.unwrap(); - assert_eq!(distro_info.id, None); - assert_eq!(distro_info.name.as_deref(), Some("macOS")); - assert_eq!(distro_info.version.as_deref(), Some("14.4")); - assert_eq!(distro_info.libc, None); + // We mock the macOS distro + assert_json_snapshot!(&linehaul.distro, @r###" + { + "name": "macOS", + "version": "14.4", + "id": null, + "libc": null + }"### + ); } Ok(())