Skip to content

Commit

Permalink
ref(test): Bump mockito mocking library
Browse files Browse the repository at this point in the history
Bumping to the latest version will allow us to take advantage of new features, such as being able to generate the response of a mock dynamically with a function.

Closes #2223
  • Loading branch information
szokeasaurusrex committed Nov 15, 2024
1 parent 8e900c9 commit e21e395
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 41 deletions.
161 changes: 157 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ secrecy = "0.8.0"
[dev-dependencies]
assert_cmd = "2.0.11"
insta = { version = "1.26.0", features = ["redactions", "yaml"] }
mockito = "0.31.1"
mockito = "1.6.1"
predicates = "2.1.5"
rstest = "0.18.2"
tempfile = "3.8.1"
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/debug_files/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn ensure_correct_assemble_call() {
.split(' '),
);

env::set_all(|k, v| {
env::set_all(manager.server_info(), |k, v| {
command.env(k, v.as_ref());
});

Expand Down
7 changes: 3 additions & 4 deletions tests/integration/organizations.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use mockito::server_url;

use crate::integration::{MockEndpointBuilder, TestManager};

#[test]
fn command_organizations() {
let manager = TestManager::new();
let region_response = format!(
r#"{{
"regions": [{{
"name": "monolith",
"url": "{}"
}}]
}}"#,
server_url(),
manager.server_url(),
);

TestManager::new()
manager
// Mocks are for the organizations list command.
.mock_endpoint(
MockEndpointBuilder::new("GET", "/api/0/organizations/?cursor=", 200)
Expand Down
28 changes: 23 additions & 5 deletions tests/integration/test_utils/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,35 @@

use std::borrow::Cow;

use mockito::ServerGuard;

pub struct MockServerInfo {
url: String,
host_with_port: String,
}

impl From<&ServerGuard> for MockServerInfo {
fn from(server: &ServerGuard) -> Self {
Self {
url: server.url(),
host_with_port: server.host_with_port(),
}
}
}

/// 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();
/// Information about the mock server is needed to set the SENTRY_URL and SENTRY_DSN.
/// This is obtained from `TestManager`.
pub fn set(server_info: MockServerInfo, mut setter: impl FnMut(&str, Cow<str>)) {
let dsn = format!("http://test@{}/1337", server_info.host_with_port).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_URL", server_info.url.into());
setter("SENTRY_DSN", dsn);
setter("RUST_BACKTRACE", "0".into());
}
Expand All @@ -27,7 +45,7 @@ pub fn set_auth_token(setter: impl FnOnce(&'static str, Cow<'static, str>)) {

/// Set all environment variables, including the auth token and the environments
/// set by `set`.
pub fn set_all(mut setter: impl FnMut(&'static str, Cow<'static, str>)) {
set(&mut setter);
pub fn set_all(server_info: MockServerInfo, mut setter: impl FnMut(&str, Cow<str>)) {
set(server_info, &mut setter);
set_auth_token(setter);
}
6 changes: 4 additions & 2 deletions tests/integration/test_utils/mock_common_endpoints.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::fmt::Display;

use crate::integration::test_utils::MockEndpointBuilder;

/// Returns an iterator over builders for the common upload endpoints.
/// These can be used to generate mocks for the upload endpoints.
pub(super) fn common_upload_endpoints(
server_url: impl Display,
behavior: ServerBehavior,
chunk_options: ChunkOptions,
) -> impl Iterator<Item = MockEndpointBuilder> {
Expand Down Expand Up @@ -37,8 +40,7 @@ pub(super) fn common_upload_endpoints(
\"hashAlgorithm\": \"sha1\",
\"accept\": [{}]
}}",
mockito::server_url(),
accept,
server_url, accept,
);

vec![
Expand Down
Loading

0 comments on commit e21e395

Please sign in to comment.