Skip to content

Commit

Permalink
feat: Measure code coverage differently (#79)
Browse files Browse the repository at this point in the history
* Measure code coverage differently

* Add missing env-filter feature

* ci test run in release
  • Loading branch information
fasterthanlime authored Mar 19, 2024
1 parent 1ff81a4 commit 375c4d6
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .config/nextest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
slow-timeout = { period = "5s", terminate-after = 3 }

[profile.ci]
slow-timeout = { period = "5s", terminate-after = 6 }
slow-timeout = { period = "10s", terminate-after = 12 }
25 changes: 1 addition & 24 deletions Cargo.lock

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

11 changes: 8 additions & 3 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ ci-test:
#!/bin/bash -eux
source <(cargo llvm-cov show-env --export-prefix)
cargo llvm-cov clean --workspace
cargo nextest run --all-features --profile ci
ONE_BYTE_READ=1 cargo nextest run --all-features --release --profile ci
cargo llvm-cov report --lcov --output-path coverage.lcov

export RUST_LOG=trace
cargo nextest run --release --all-features --profile ci
export ONE_BYTE_READ=1
cargo nextest run --release --all-features --profile ci

cargo llvm-cov report --release --ignore-filename-regex 'corpus/mod\.rs$' --lcov --output-path coverage.lcov
cargo llvm-cov report --release --ignore-filename-regex 'corpus/mod\.rs$' --html
1 change: 0 additions & 1 deletion rc-zip-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ chrono = "0.4.33"
clap = { version = "4.4.18", features = ["derive"] }
humansize = "2.1.3"
indicatif = "0.17.7"
test-log = { version = "0.2.14", default-features = false, features = ["tracing-subscriber", "trace"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
rc-zip = { version = "5.0.1", path = "../rc-zip", features = ["corpus"] }
cfg-if = "1.0.0"
16 changes: 12 additions & 4 deletions rc-zip-sync/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,29 @@ fn check_case<F: HasCursor>(test: &Case, archive: Result<ArchiveHandle<'_, F>, E
}
}

#[test_log::test]
#[test]
fn read_from_slice() {
corpus::install_test_subscriber();

let bytes = std::fs::read(zips_dir().join("test.zip")).unwrap();
let slice = &bytes[..];
let archive = slice.read_zip().unwrap();
assert_eq!(archive.entries().count(), 2);
}

#[test_log::test]
#[test]
fn read_from_file() {
corpus::install_test_subscriber();

let f = File::open(zips_dir().join("test.zip")).unwrap();
let archive = f.read_zip().unwrap();
assert_eq!(archive.entries().count(), 2);
}

#[test_log::test]
#[test]
fn real_world_files() {
corpus::install_test_subscriber();

for case in corpus::test_cases() {
tracing::info!("============ testing {}", case.name);

Expand All @@ -65,8 +71,10 @@ fn real_world_files() {
}
}

#[test_log::test]
#[test]
fn streaming() {
corpus::install_test_subscriber();

for case in corpus::streaming_test_cases() {
let guarded_path = case.absolute_path();
let file = File::open(&guarded_path.path).unwrap();
Expand Down
1 change: 0 additions & 1 deletion rc-zip-tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ bzip2 = ["rc-zip/bzip2"]
zstd = ["rc-zip/zstd"]

[dev-dependencies]
test-log = { version = "0.2.14", default-features = false, features = ["tracing-subscriber", "trace"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
rc-zip = { version = "5.0.1", path = "../rc-zip", features = ["corpus"] }
tokio = { version = "1.35.1", features = ["rt", "macros"] }
16 changes: 12 additions & 4 deletions rc-zip-tokio/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,29 @@ async fn check_case<F: HasCursor>(test: &Case, archive: Result<ArchiveHandle<'_,
}
}

#[test_log::test(tokio::test)]
#[tokio::test]
async fn read_from_slice() {
corpus::install_test_subscriber();

let bytes = std::fs::read(zips_dir().join("test.zip")).unwrap();
let slice = &bytes[..];
let archive = slice.read_zip().await.unwrap();
assert_eq!(archive.entries().count(), 2);
}

#[test_log::test(tokio::test)]
#[tokio::test]
async fn read_from_file() {
corpus::install_test_subscriber();

let f = Arc::new(RandomAccessFile::open(zips_dir().join("test.zip")).unwrap());
let archive = f.read_zip().await.unwrap();
assert_eq!(archive.entries().count(), 2);
}

#[test_log::test(tokio::test)]
#[tokio::test]
async fn real_world_files() {
corpus::install_test_subscriber();

for case in corpus::test_cases() {
tracing::info!("============ testing {}", case.name);

Expand All @@ -62,8 +68,10 @@ async fn real_world_files() {
}
}

#[test_log::test(tokio::test)]
#[tokio::test]
async fn streaming() {
corpus::install_test_subscriber();

for case in corpus::streaming_test_cases() {
let guarded_path = case.absolute_path();
let file = tokio::fs::File::open(&guarded_path.path).await.unwrap();
Expand Down
7 changes: 3 additions & 4 deletions rc-zip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ lzma-rs = { version = "0.3.0", optional = true, features = ["stream"] }
zstd = { version = "0.13.0", optional = true }
ownable = "0.6.2"
temp-dir = { version = "0.1.12", optional = true }
tracing-subscriber = { version = "0.3.18", optional = true, features = ["env-filter"] }

[features]
corpus = ["dep:temp-dir", "dep:bzip2"]
corpus = ["dep:temp-dir", "dep:bzip2", "dep:tracing-subscriber"]
deflate = ["dep:miniz_oxide"]
deflate64 = ["dep:deflate64"]
bzip2 = ["dep:bzip2"]
lzma = ["dep:lzma-rs"]
zstd = ["dep:zstd"]

[dev-dependencies]
test-log = { version = "0.2.14", default-features = false, features = ["tracing-subscriber", "trace"] }
tracing-subscriber = ["dep:tracing-subscriber"]
54 changes: 54 additions & 0 deletions rc-zip/src/corpus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{fs::File, path::PathBuf};

use chrono::{DateTime, FixedOffset, TimeZone, Timelike, Utc};
use temp_dir::TempDir;
use tracing::span;

use crate::{
encoding::Encoding,
Expand Down Expand Up @@ -348,3 +349,56 @@ pub fn check_file_against(file: &CaseFile, entry: &Entry, actual_bytes: &[u8]) {
}
}
}

// This test subscriber is used to suppress trace-level logs (yet executes
// the code, for coverage reasons)
pub fn install_test_subscriber() {
let env_filter = tracing_subscriber::EnvFilter::builder().from_env_lossy();
let sub = tracing_subscriber::FmtSubscriber::builder()
.with_env_filter(env_filter)
.with_test_writer()
.finish();
let sub = DebugOnlySubscriber { inner: sub };
tracing::subscriber::set_global_default(sub).unwrap()
}

struct DebugOnlySubscriber<S> {
inner: S,
}

impl<S> tracing::Subscriber for DebugOnlySubscriber<S>
where
S: tracing::Subscriber,
{
fn enabled(&self, _metadata: &tracing::Metadata<'_>) -> bool {
true
}

fn new_span(&self, span: &span::Attributes<'_>) -> span::Id {
self.inner.new_span(span)
}

fn record(&self, span: &span::Id, values: &span::Record<'_>) {
self.inner.record(span, values)
}

fn record_follows_from(&self, span: &span::Id, follows: &span::Id) {
self.inner.record_follows_from(span, follows)
}

fn event(&self, event: &tracing::Event<'_>) {
if *event.metadata().level() == tracing::Level::TRACE {
return;
}

self.inner.event(event)
}

fn enter(&self, span: &span::Id) {
self.inner.enter(span)
}

fn exit(&self, span: &span::Id) {
self.inner.exit(span)
}
}
4 changes: 3 additions & 1 deletion rc-zip/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use rc_zip::{
fsm::{ArchiveFsm, FsmResult},
};

#[test_log::test]
#[test]
fn state_machine() {
corpus::install_test_subscriber();

let cases = corpus::test_cases();
let case = cases.iter().find(|x| x.name == "zip64.zip").unwrap();
let bytes = case.bytes();
Expand Down

0 comments on commit 375c4d6

Please sign in to comment.