Skip to content

Commit

Permalink
feat: add env var to disable config validation
Browse files Browse the repository at this point in the history
Apparently `#[cfg(test)]` is disabled when compiling binaries for
integration testing, so we have to use an env var.
  • Loading branch information
Stonks3141 committed Feb 13, 2023
1 parent 9848c3d commit a179542
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ check *ARGS:

# Run `cargo test` with ARGS and print execution time
test *ARGS:
export DISABLE_VALIDATE_CONFIG=1
cargo test --all-features --workspace {{ARGS}} -- -Z unstable-options --report-time

# Publish all crates in the workspace
publish TOKEN:
cargo ws publish --yes --from-git --token {{ TOKEN }}
cargo ws publish --yes --from-git --token {{TOKEN}}

# Build the program and install to PATH
install PATH='~/.local/bin/':
Expand Down
12 changes: 6 additions & 6 deletions crates/pet-monitor-app/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ use axum::{
};
use axum_macros::debug_handler;
use futures_lite::{Stream, StreamExt};
#[cfg(not(test))]
use mp4_stream::capabilities::check_config;
use mp4_stream::{capabilities::Capabilities, config::Config, StreamSubscriber};
use mp4_stream::{
capabilities::{check_config, Capabilities},
config::Config,
StreamSubscriber,
};
use serde::Deserialize;
use tokio::task::spawn_blocking;
use tower_cookies::{Cookie, Cookies};
Expand Down Expand Up @@ -166,7 +168,6 @@ struct ConfigForm {
v4l2_controls: Option<std::collections::HashMap<String, String>>,
}

#[cfg_attr(test, allow(unused_variables))]
#[debug_handler(state = AppState)]
#[instrument(skip(_token, ctx, caps))]
pub(crate) async fn set_config(
Expand Down Expand Up @@ -196,8 +197,7 @@ pub(crate) async fn set_config(
return Err(StatusCode::UNAUTHORIZED);
}

#[cfg(not(test))]
{
if !std::env::var("DISABLE_VALIDATE_CONFIG").map_or(false, |it| it == "1") {
let config_clone = config.clone();
if let Err(e) =
tokio::task::spawn_blocking(move || check_config(&config_clone, &caps)).await
Expand Down
12 changes: 6 additions & 6 deletions crates/pet-monitor-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ use hyper::server::{
accept::Accept,
conn::{AddrIncoming, Http},
};
#[cfg(not(test))]
use mp4_stream::capabilities::check_config;
use mp4_stream::{
capabilities::{get_capabilities_all, Capabilities},
capabilities::{check_config, get_capabilities_all, Capabilities},
stream_media_segments, StreamSubscriber,
};
use std::{
Expand All @@ -32,7 +30,7 @@ use tokio_rustls::{
rustls::{Certificate, PrivateKey, ServerConfig},
TlsAcceptor,
};
use tower::MakeService;
use tower::{limit::rate::RateLimitLayer, MakeService};
use tower_cookies::CookieManagerLayer;
use tower_http::trace::TraceLayer;

Expand All @@ -49,8 +47,9 @@ pub async fn start(conf_path: Option<PathBuf>, ctx: Context, stream: bool) -> ey
let (ctx_manager, cfg_rx) = ContextManager::new(ctx.clone(), conf_path.clone());

let caps = get_capabilities_all()?;
#[cfg(not(test))]
check_config(&ctx.config, &caps)?;
if !std::env::var("DISABLE_VALIDATE_CONFIG").map_or(false, |it| it == "1") {
check_config(&ctx.config, &caps)?;
}

let mut state = AppState {
ctx: ctx_manager.clone(),
Expand All @@ -62,6 +61,7 @@ pub async fn start(conf_path: Option<PathBuf>, ctx: Context, stream: bool) -> ey
.route("/", get(handlers::base))
.route("/login.html", get(handlers::files))
.route("/login.html", post(handlers::login))
// .route_layer(RateLimitLayer::new(128, std::time::Duration::from_secs(1)))
.route("/stream.html", get(handlers::files))
.route("/config.html", get(handlers::config))
.route("/config.html", post(handlers::set_config))
Expand Down

0 comments on commit a179542

Please sign in to comment.