Skip to content

Commit 8cb70b7

Browse files
committed
refactor: extract common code
1 parent ceecbf7 commit 8cb70b7

File tree

4 files changed

+23
-32
lines changed

4 files changed

+23
-32
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/importer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ walker-common = { workspace = true }
5353
zip = { workspace = true }
5454

5555
[dev-dependencies]
56+
actix-http = { workspace = true }
5657
bytesize = { workspace = true }
5758
test-log = { workspace = true, features = ["log", "trace"] }
5859
test-context = { workspace = true }

modules/importer/src/test.rs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
use super::model::{
44
CommonImporter, Importer, ImporterConfiguration, ImporterData, SbomImporter, State,
55
};
6+
use actix_http::{Request, body::BoxBody};
67
use actix_web::{
78
App,
9+
dev::{Service, ServiceResponse},
810
http::{StatusCode, header},
911
test as actix,
1012
};
@@ -52,11 +54,11 @@ fn mock_importer(result: &Importer, source: impl Into<String>) -> Importer {
5254
}
5355
}
5456

55-
#[test_context(TrustifyContext, skip_teardown)]
56-
#[test(actix_web::test)]
57-
async fn default(ctx: TrustifyContext) {
58-
let db = ctx.db;
59-
let app = actix::init_service(
57+
async fn app(
58+
ctx: &TrustifyContext,
59+
) -> impl Service<Request, Response = ServiceResponse<BoxBody>, Error = actix_web::Error> {
60+
let db = ctx.db.clone();
61+
actix::init_service(
6062
App::new()
6163
.into_utoipa_app()
6264
.add_test_authorizer()
@@ -66,7 +68,13 @@ async fn default(ctx: TrustifyContext) {
6668
)
6769
.into_app(),
6870
)
69-
.await;
71+
.await
72+
}
73+
74+
#[test_context(TrustifyContext, skip_teardown)]
75+
#[test(actix_web::test)]
76+
async fn default(ctx: TrustifyContext) {
77+
let app = app(&ctx).await;
7078

7179
// create one
7280

@@ -150,18 +158,7 @@ async fn default(ctx: TrustifyContext) {
150158
#[test_context(TrustifyContext, skip_teardown)]
151159
#[test(actix_web::test)]
152160
async fn oplock(ctx: TrustifyContext) {
153-
let db = ctx.db;
154-
let app = actix::init_service(
155-
App::new()
156-
.into_utoipa_app()
157-
.add_test_authorizer()
158-
.service(
159-
utoipa_actix_web::scope("/api")
160-
.configure(|svc| super::endpoints::configure(svc, db)),
161-
)
162-
.into_app(),
163-
)
164-
.await;
161+
let app = app(&ctx).await;
165162

166163
// create one
167164

@@ -307,18 +304,7 @@ async fn oplock(ctx: TrustifyContext) {
307304
#[test_context(TrustifyContext, skip_teardown)]
308305
#[test(actix_web::test)]
309306
async fn patch(ctx: TrustifyContext) {
310-
let db = ctx.db;
311-
let app = actix::init_service(
312-
App::new()
313-
.into_utoipa_app()
314-
.add_test_authorizer()
315-
.service(
316-
utoipa_actix_web::scope("/api")
317-
.configure(|svc| super::endpoints::configure(svc, db)),
318-
)
319-
.into_app(),
320-
)
321-
.await;
307+
let app = app(&ctx).await;
322308

323309
// create one
324310

test-context/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ use tokio_util::{bytes::Bytes, io::ReaderStream};
2222
use tracing::instrument;
2323
use trustify_common::{self as common, db, decompress::decompress_async, hashing::Digests};
2424
use trustify_entity::labels::Labels;
25-
use trustify_module_ingestor::service::Cache;
2625
use trustify_module_ingestor::{
2726
graph::Graph,
2827
model::IngestResult,
29-
service::{Format, IngestorService},
28+
service::{Cache, Format, IngestorService},
3029
};
3130
use trustify_module_storage::service::fs::FileSystemBackend;
3231

32+
/// A common test content.
33+
///
34+
/// **NOTE:** Dropping it will tear down the embedded database. So it must be kept until the end
35+
/// of the test.
3336
#[allow(dead_code)]
3437
pub struct TrustifyContext {
3538
pub db: db::Database,

0 commit comments

Comments
 (0)