From f24b648ee7c686085836e6802482f28aba47073c Mon Sep 17 00:00:00 2001 From: Dylan Frankland Date: Wed, 9 Oct 2024 18:11:19 -0700 Subject: [PATCH] force sentry to collect events with env dev for tests (#122) --- Cargo.lock | 43 +++++++++++++++++++++--- Cargo.toml | 1 + cli-tests/Cargo.toml | 20 ++++++++++++ cli-tests/src/main.rs | 4 +++ {cli/tests => cli-tests/src}/upload.rs | 45 +++++++++++++++++--------- cli/Cargo.toml | 10 ++---- cli/src/main.rs | 16 +++++---- 7 files changed, 104 insertions(+), 35 deletions(-) create mode 100644 cli-tests/Cargo.toml create mode 100644 cli-tests/src/main.rs rename {cli/tests => cli-tests/src}/upload.rs (88%) diff --git a/Cargo.lock b/Cargo.lock index 25d3b336..5a6ffeda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -401,6 +401,26 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" +[[package]] +name = "cli-tests" +version = "0.1.0" +dependencies = [ + "api", + "assert_cmd", + "assert_matches", + "chrono", + "context", + "escargot", + "junit-mock", + "lazy_static", + "more-asserts", + "serde_json", + "tempfile", + "test_utils", + "tokio", + "trunk-analytics-cli", +] + [[package]] name = "clru" version = "0.6.2" @@ -621,6 +641,18 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "escargot" +version = "0.5.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c000f23e9d459aef148b7267e02b03b94a0aaacf4ec64c65612f67e02f525fb6" +dependencies = [ + "log", + "once_cell", + "serde", + "serde_json", +] + [[package]] name = "exitcode" version = "1.1.2" @@ -1710,6 +1742,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "more-asserts" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fafa6961cabd9c63bcd77a45d7e3b7f3b552b70417831fb0f56db717e72407e" + [[package]] name = "native-tls" version = "0.2.12" @@ -3014,9 +3052,6 @@ version = "0.0.0" dependencies = [ "anyhow", "api", - "assert_cmd", - "assert_matches", - "axum", "chrono", "clap", "codeowners", @@ -3024,7 +3059,6 @@ dependencies = [ "env_logger", "exitcode", "glob", - "junit-mock", "log", "openssl", "quick-junit", @@ -3035,7 +3069,6 @@ dependencies = [ "serde_json", "tar", "tempfile", - "test_utils", "tokio", "tokio-retry", "uuid", diff --git a/Cargo.toml b/Cargo.toml index b20645ec..5eccff93 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "api", "cli", + "cli-tests", "codeowners", "context", "context-js", diff --git a/cli-tests/Cargo.toml b/cli-tests/Cargo.toml new file mode 100644 index 00000000..386cb275 --- /dev/null +++ b/cli-tests/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "cli-tests" +version = "0.1.0" +edition = "2021" + +[dev-dependencies] +api = { path = "../api" } +assert_cmd = "2.0.16" +assert_matches = "1.5.0" +chrono = "0.4.33" +context = { path = "../context" } +escargot = "0.5.12" +junit-mock = { path = "../junit-mock" } +lazy_static = "1.4" +more-asserts = "0.3.1" +serde_json = "1.0.68" +tempfile = "3.2.0" +test_utils = { path = "../test_utils" } +tokio = { version = "*" } +trunk-analytics-cli = { path = "../cli", features = ["force-sentry-env-dev"] } diff --git a/cli-tests/src/main.rs b/cli-tests/src/main.rs new file mode 100644 index 00000000..ef85ecf3 --- /dev/null +++ b/cli-tests/src/main.rs @@ -0,0 +1,4 @@ +#[cfg(test)] +mod upload; + +fn main() {} diff --git a/cli/tests/upload.rs b/cli-tests/src/upload.rs similarity index 88% rename from cli/tests/upload.rs rename to cli-tests/src/upload.rs index aca5515d..38db58eb 100644 --- a/cli/tests/upload.rs +++ b/cli-tests/src/upload.rs @@ -1,19 +1,38 @@ -use std::fs; -use std::io::BufReader; -use std::path::Path; +use std::{ + env, fs, + io::BufReader, + path::{Path, PathBuf}, +}; use api::{CreateRepoRequest, GetQuarantineBulkTestStatusRequest}; use assert_cmd::Command; use assert_matches::assert_matches; use context::repo::RepoUrlParts as Repo; +use escargot::{CargoBuild, CargoRun}; use junit_mock::JunitMock; +use lazy_static::lazy_static; use tempfile::tempdir; use test_utils::{ mock_git_repo::setup_repo_with_commit, mock_server::{spawn_mock_server, RequestPayload}, }; -use trunk_analytics_cli::codeowners::CodeOwners; -use trunk_analytics_cli::types::{BundleMeta, FileSetType}; +use trunk_analytics_cli::{ + codeowners::CodeOwners, + types::{BundleMeta, FileSetType}, +}; + +lazy_static! { + static ref CARGO_MANIFEST_DIR: PathBuf = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + static ref CARGO_RUN: CargoRun = CargoBuild::new() + .bin("trunk-analytics-cli") + .target_dir(CARGO_MANIFEST_DIR.join("../target")) + .manifest_path(CARGO_MANIFEST_DIR.join("../cli/Cargo.toml")) + .features("force-sentry-env-dev") + .current_release() + .current_target() + .run() + .unwrap(); +} fn generate_mock_git_repo>(directory: T) { setup_repo_with_commit(directory).unwrap(); @@ -43,9 +62,7 @@ async fn upload_bundle() { let state = spawn_mock_server().await; - let mut cmd = Command::cargo_bin("trunk-analytics-cli").unwrap(); - - let assert = cmd + let assert = Command::new(CARGO_RUN.path()) .current_dir(&temp_dir) .env("TRUNK_PUBLIC_API_ADDRESS", &state.host) .env("CI", "1") @@ -127,7 +144,7 @@ async fn upload_bundle() { assert_eq!(bundle_meta.envs.get("CI"), Some(&String::from("1"))); let time_since_upload = chrono::Utc::now() - chrono::DateTime::from_timestamp(bundle_meta.upload_time_epoch as i64, 0).unwrap(); - assert_eq!(time_since_upload.num_minutes(), 0); + more_asserts::assert_lt!(time_since_upload.num_minutes(), 5); assert_eq!(bundle_meta.test_command, None); assert!(bundle_meta.os_info.is_some()); assert!(bundle_meta.quarantined_tests.is_empty()); @@ -155,7 +172,7 @@ async fn upload_bundle() { ); let time_since_junit_modified = chrono::Utc::now() - chrono::DateTime::from_timestamp_nanos(bundled_file.last_modified_epoch_ns as i64); - assert_eq!(time_since_junit_modified.num_minutes(), 0); + more_asserts::assert_lt!(time_since_junit_modified.num_minutes(), 5); assert_eq!(bundled_file.owners, ["@user"]); assert_eq!(bundled_file.team, None); @@ -185,9 +202,7 @@ async fn upload_bundle_no_files() { let state = spawn_mock_server().await; - let mut cmd = Command::cargo_bin("trunk-analytics-cli").unwrap(); - - let assert = cmd + let assert = Command::new(CARGO_RUN.path()) .current_dir(&temp_dir) .env("TRUNK_PUBLIC_API_ADDRESS", &state.host) .env("CI", "1") @@ -215,9 +230,7 @@ async fn upload_bundle_no_files_allow_missing_junit_files() { let state = spawn_mock_server().await; - let mut cmd = Command::cargo_bin("trunk-analytics-cli").unwrap(); - - let assert = cmd + let assert = Command::new(CARGO_RUN.path()) .current_dir(&temp_dir) .env("TRUNK_PUBLIC_API_ADDRESS", &state.host) .env("CI", "1") diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 72e677fb..789c3976 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -44,13 +44,6 @@ openssl = { version = "0.10.66", features = ["vendored"] } uuid = { version = "1.10.0", features = ["v5"] } quick-junit = "0.5.0" -[dev-dependencies] -assert_cmd = "2.0.16" -assert_matches = "1.5.0" -axum = { version = "0.7.5", features = ["macros"] } -junit-mock = { path = "../junit-mock" } -test_utils = { path = "../test_utils" } - [build-dependencies] vergen = { version = "8.3.1", features = [ "build", @@ -60,3 +53,6 @@ vergen = { version = "8.3.1", features = [ "rustc", "si", ] } + +[features] +force-sentry-env-dev = [] diff --git a/cli/src/main.rs b/cli/src/main.rs index da4e506e..81a81e47 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -124,13 +124,15 @@ const RETRY_COUNT: usize = 5; // "the Sentry client must be initialized before starting an async runtime or spawning threads" // https://docs.sentry.io/platforms/rust/#async-main-function fn main() -> anyhow::Result<()> { - let _guard = sentry::init(( - SENTRY_DSN, - sentry::ClientOptions { - release: sentry::release_name!(), - ..Default::default() - }, - )); + let mut options = sentry::ClientOptions::default(); + options.release = sentry::release_name!(); + + #[cfg(feature = "force-sentry-env-dev")] + { + options.environment = Some("development".into()) + } + + let _guard = sentry::init((SENTRY_DSN, options)); tokio::runtime::Builder::new_multi_thread() .enable_all()