Skip to content

Commit

Permalink
force sentry to collect events with env dev for tests (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfrankland authored Oct 10, 2024
1 parent e3957b0 commit f24b648
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 35 deletions.
43 changes: 38 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"api",
"cli",
"cli-tests",
"codeowners",
"context",
"context-js",
Expand Down
20 changes: 20 additions & 0 deletions cli-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
4 changes: 4 additions & 0 deletions cli-tests/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[cfg(test)]
mod upload;

fn main() {}
45 changes: 29 additions & 16 deletions cli/tests/upload.rs → cli-tests/src/upload.rs
Original file line number Diff line number Diff line change
@@ -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<T: AsRef<Path>>(directory: T) {
setup_repo_with_commit(directory).unwrap();
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
10 changes: 3 additions & 7 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -60,3 +53,6 @@ vergen = { version = "8.3.1", features = [
"rustc",
"si",
] }

[features]
force-sentry-env-dev = []
16 changes: 9 additions & 7 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f24b648

Please sign in to comment.