Skip to content

Commit

Permalink
ref(tests): Decouple test env vars from trycmd code
Browse files Browse the repository at this point in the history
As a part of #2194, I intend to introduce the [`assert_cmd` crate](https://docs.rs/assert_cmd/latest/assert_cmd/) to have more control over how the assertions for the chunk upload tests are run. This control would be difficult to achieve with `trycmd`.

So, here, I am splitting off the logic for setting environment variables to make it also reusable for `assert_cmd` tests
  • Loading branch information
szokeasaurusrex committed Nov 6, 2024
1 parent 632ab83 commit c46bb3c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
24 changes: 13 additions & 11 deletions tests/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod send_envelope;
mod send_event;
mod send_metric;
mod sourcemaps;
mod test_utils;
mod token_validation;
mod uninstall;
mod update;
Expand All @@ -31,27 +32,28 @@ use std::path::Path;
use mockito::{self, mock, server_url, Matcher, Mock};
use trycmd::TestCases;

use test_utils::env;

pub const UTC_DATE_FORMAT: &str = r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{6,9}Z";
const VERSION: &str = env!("CARGO_PKG_VERSION");

pub fn register_test_without_token(path: &str) -> TestCases {
let test_case = TestCases::new();
let server_addr = mockito::server_address();
test_case
.env("SENTRY_INTEGRATION_TEST", "1")
.env("SENTRY_URL", server_url())
.env("SENTRY_ORG", "wat-org")
.env("SENTRY_PROJECT", "wat-project")
.env("SENTRY_DSN", format!("http://test@{}/1337", server_addr))
.case(format!("tests/integration/_cases/{path}"));

env::set(|k, v| {
test_case.env(k, v);
});

test_case.case(format!("tests/integration/_cases/{path}"));
test_case.insert_var("[VERSION]", VERSION).unwrap();
test_case
}
pub fn register_test(path: &str) -> TestCases {
let auth_token = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";

let test_case = register_test_without_token(path);
test_case.env("SENTRY_AUTH_TOKEN", auth_token);

env::set_auth_token(|k, v| {
test_case.env(k, v);
});

test_case
}
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/test_utils/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! Utilities for setting environment variables in integration tests.

use std::borrow::Cow;

/// Set the environment variables, which should be set for all integration tests,
/// using the provided setter function.
/// The setter function takes as parameters the environment variable name, and the
/// value to set it to, in that order.
pub fn set(mut setter: impl FnMut(&'static str, Cow<'static, str>)) {
let dsn = format!("http://test@{}/1337", mockito::server_address()).into();

setter("SENTRY_INTEGRATION_TEST", "1".into());
setter("SENTRY_ORG", "wat-org".into());
setter("SENTRY_PROJECT", "wat-project".into());
setter("SENTRY_URL", mockito::server_url().into());
setter("SENTRY_DSN", dsn);
}

/// Set the auth token environment variable using the provided setter function.
pub fn set_auth_token(setter: impl FnOnce(&'static str, Cow<'static, str>)) {
setter(
"SENTRY_AUTH_TOKEN",
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef".into(),
);
}
3 changes: 3 additions & 0 deletions tests/integration/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! A collection of utilities for integration tests.

pub mod env;

0 comments on commit c46bb3c

Please sign in to comment.