Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 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 crates/bin/cratesfyi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = "2024"
anyhow = { workspace = true }
clap = { workspace = true }
docs_rs_builder = { path = "../docs_rs_builder" }
docs_rs_config = { path = "../../lib/docs_rs_config" }
docs_rs_context = { path = "../../lib/docs_rs_context" }
docs_rs_logging = { path = "../../lib/docs_rs_logging" }
docs_rs_utils = { path = "../../lib/docs_rs_utils" }
Expand Down
1 change: 1 addition & 0 deletions crates/bin/cratesfyi/src/daemon.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{Error, anyhow};
use docs_rs_builder::{RustwideBuilder, queue_builder};
use docs_rs_config::AppConfig as _;
use docs_rs_context::Context;
use docs_rs_watcher::{
start_background_queue_rebuild, start_background_repository_stats_updater,
Expand Down
2 changes: 2 additions & 0 deletions crates/bin/docs_rs_admin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ tracing = { workspace = true }

[dev-dependencies]
bon = { workspace = true }
docs_rs_config = { path = "../../lib/docs_rs_config", features = ["testing"] }
docs_rs_context = { path = "../../lib/docs_rs_context", features = ["testing"] }
docs_rs_database = { path = "../../lib/docs_rs_database", features = ["testing"] }
docs_rs_opentelemetry = { path = "../../lib/docs_rs_opentelemetry", features = ["testing"] }
docs_rs_storage = { path = "../../lib/docs_rs_storage", features = ["testing"] }
Expand Down
77 changes: 7 additions & 70 deletions crates/bin/docs_rs_admin/src/testing/test_env.rs
Original file line number Diff line number Diff line change
@@ -1,75 +1,12 @@
use anyhow::Result;
use bon::bon;
use docs_rs_build_queue::AsyncBuildQueue;
use docs_rs_context::Context;
use docs_rs_database::{AsyncPoolClient, Config as DatabaseConfig, testing::TestDatabase};
use docs_rs_opentelemetry::testing::TestMetrics;
use docs_rs_storage::{Config as StorageConfig, StorageKind, testing::TestStorage};
use docs_rs_test_fakes::FakeRelease;
use std::sync::Arc;
use docs_rs_config::AppConfig;

pub(crate) struct TestEnvironment {
pub(crate) context: Arc<Context>,
// so we can allow asserting collected metrics later.
#[allow(dead_code)] // we need to keep the metrics so they can be collected & cleaned
pub(crate) metrics: TestMetrics,
#[allow(dead_code)] // we need to keep the storage so it can be cleaned up.
pub(crate) storage: TestStorage,
#[allow(dead_code)] // we need to keep the storage so it can be cleaned up.
pub(crate) db: TestDatabase,
}

#[bon]
impl TestEnvironment {
pub(crate) async fn new() -> Result<Self> {
// NOTE: compiler crashes when I change the return to
// `Self::builder().build().await
#[allow(clippy::needless_question_mark)]
Ok(Self::builder().build().await?)
}

#[builder(finish_fn = build)]
pub(crate) async fn builder() -> Result<Self> {
docs_rs_logging::testing::init();

let metrics = TestMetrics::new();

let db_config = DatabaseConfig::test_config()?;
let db = TestDatabase::new(&db_config, metrics.provider()).await?;

let storage_config = Arc::new(StorageConfig::test_config(StorageKind::Memory)?);

let test_storage =
TestStorage::from_config(storage_config.clone(), metrics.provider()).await?;
pub(crate) struct DummyConfig;

Ok(Self {
context: Context::builder()
.with_runtime()
.await?
.meter_provider(metrics.provider().clone())
.pool(db_config.into(), db.pool().clone())
.storage(storage_config.clone(), test_storage.storage())
.with_build_queue()?
.build()?
.into(),
db,
storage: test_storage,
metrics,
})
}

pub(crate) fn build_queue(&self) -> Result<&Arc<AsyncBuildQueue>> {
self.context.build_queue()
}

pub(crate) async fn async_conn(&self) -> Result<AsyncPoolClient> {
self.context.pool()?.get_async().await.map_err(Into::into)
}

pub async fn fake_release(&self) -> FakeRelease<'_> {
FakeRelease::new(
self.context.pool().unwrap().clone(),
self.context.storage().unwrap().clone(),
)
impl AppConfig for DummyConfig {
fn from_environment() -> Result<Self> {
Ok(Self {})
}
}

pub(crate) type TestEnvironment = docs_rs_context::testing::TestEnvironment<DummyConfig>;

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

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

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

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

6 changes: 3 additions & 3 deletions crates/bin/docs_rs_builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ clap = { workspace = true }
docs_rs_build_limits = { path = "../../lib/docs_rs_build_limits" }
docs_rs_build_queue = { path = "../../lib/docs_rs_build_queue" }
docs_rs_cargo_metadata = { path = "../../lib/docs_rs_cargo_metadata" }
docs_rs_config = { path = "../../lib/docs_rs_config" }
docs_rs_context = { path = "../../lib/docs_rs_context" }
docs_rs_database = { path = "../../lib/docs_rs_database" }
docs_rs_env_vars = { path = "../../lib/docs_rs_env_vars" }
Expand Down Expand Up @@ -41,6 +42,8 @@ tracing-log = "0.2.0"

[dev-dependencies]
docs_rs_build_queue = { path = "../../lib/docs_rs_build_queue", features = ["testing"] }
docs_rs_config = { path = "../../lib/docs_rs_config", features = ["testing"] }
docs_rs_context = { path = "../../lib/docs_rs_context", features = ["testing"] }
docs_rs_database = { path = "../../lib/docs_rs_database", features = ["testing"] }
docs_rs_fastly= { path = "../../lib/docs_rs_fastly", features = ["testing"] }
docs_rs_headers = { path = "../../lib/docs_rs_headers", features = ["testing"] }
Expand All @@ -49,6 +52,3 @@ docs_rs_types = { path = "../../lib/docs_rs_types", features = ["testing"] }
docs_rs_utils = { path = "../../lib/docs_rs_utils", features = ["testing"] }
pretty_assertions = { workspace = true }
test-case = { workspace = true }

[features]
testing = []
12 changes: 6 additions & 6 deletions crates/bin/docs_rs_builder/src/build_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ mod tests {

#[test]
fn test_invalidate_cdn_after_error() -> Result<()> {
let env = TestEnvironment::new_with_runtime()?;
let env = TestEnvironment::new()?;
let queue = env.blocking_build_queue()?;

let builder_metrics = BuilderMetrics::new(env.context.meter_provider());
let builder_metrics = BuilderMetrics::new(env.meter_provider());

const WILL_FAIL: KrateName = KrateName::from_static("will_fail");

queue.add_crate(&WILL_FAIL, &V1, 0, None)?;

process_next_crate(&env.context, &builder_metrics, |krate| {
process_next_crate(&env, &builder_metrics, |krate| {
assert_eq!(WILL_FAIL, krate.name);
anyhow::bail!("simulate a failure");
})?;
Expand All @@ -115,14 +115,14 @@ mod tests {

#[test]
fn test_invalidate_cdn_after_build() -> Result<()> {
let env = TestEnvironment::new_with_runtime()?;
let env = TestEnvironment::new()?;
let queue = env.blocking_build_queue()?;
let builder_metrics = BuilderMetrics::new(env.context.meter_provider());
let builder_metrics = BuilderMetrics::new(env.meter_provider());

const WILL_SUCCEED: KrateName = KrateName::from_static("will_succeed");
queue.add_crate(&WILL_SUCCEED, &V1, -1, None)?;

process_next_crate(&env.context, &builder_metrics, |krate| {
process_next_crate(&env, &builder_metrics, |krate| {
assert_eq!(WILL_SUCCEED, krate.name);
Ok(BuildPackageSummary::default())
})?;
Expand Down
Loading
Loading