mirror of
https://github.com/Noratrieb/cargo-minimize.git
synced 2026-01-14 08:25:01 +01:00
more tests
This commit is contained in:
parent
75108c8553
commit
4f4afa627d
7 changed files with 69 additions and 24 deletions
17
src/lib.rs
17
src/lib.rs
|
|
@ -15,6 +15,8 @@ mod expand;
|
|||
use anyhow::{Context, Result};
|
||||
use dylib_flag::RustFunction;
|
||||
use processor::Minimizer;
|
||||
use tracing::Level;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
|
||||
|
||||
use crate::processor::Processor;
|
||||
|
||||
|
|
@ -92,6 +94,21 @@ pub fn minimize(options: Options) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn init_recommended_tracing_subscriber() {
|
||||
let registry = Registry::default().with(
|
||||
EnvFilter::builder()
|
||||
.with_default_directive(Level::INFO.into())
|
||||
.from_env()
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
let tree_layer = tracing_tree::HierarchicalLayer::new(2)
|
||||
.with_targets(true)
|
||||
.with_bracketed_fields(true);
|
||||
|
||||
registry.with(tree_layer).init();
|
||||
}
|
||||
|
||||
impl Default for Options {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
|
|
|
|||
23
src/main.rs
23
src/main.rs
|
|
@ -1,29 +1,12 @@
|
|||
use anyhow::Result;
|
||||
use cargo_minimize::{Cargo, Parser};
|
||||
use tracing::{error, info, Level};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
|
||||
use tracing::error;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
fn main() {
|
||||
let Cargo::Minimize(options) = Cargo::parse();
|
||||
|
||||
let registry = Registry::default().with(
|
||||
EnvFilter::builder()
|
||||
.with_default_directive(Level::INFO.into())
|
||||
.from_env()
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
info!("Starting cargo-minimize");
|
||||
|
||||
let tree_layer = tracing_tree::HierarchicalLayer::new(2)
|
||||
.with_targets(true)
|
||||
.with_bracketed_fields(true);
|
||||
|
||||
registry.with(tree_layer).init();
|
||||
cargo_minimize::init_recommended_tracing_subscriber();
|
||||
|
||||
if let Err(err) = cargo_minimize::minimize(options) {
|
||||
error!("An error occured:\n{err}");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
use anyhow::{Context, Result};
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
|
||||
#[derive(PartialEq, Eq, Clone, Hash)]
|
||||
pub(crate) struct SourceFile {
|
||||
pub(crate) path: PathBuf,
|
||||
}
|
||||
|
|
@ -77,3 +78,9 @@ impl Changes {
|
|||
self.any_change
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for SourceFile {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
<Path as Debug>::fmt(&self.path, f)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -130,6 +130,7 @@ impl Minimizer {
|
|||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, pass, invalidated_files, changes), fields(pass = %pass.name()), level = "debug")]
|
||||
fn process_file<'file>(
|
||||
&self,
|
||||
pass: &mut dyn Processor,
|
||||
|
|
|
|||
|
|
@ -32,6 +32,8 @@ impl Minimizer {
|
|||
.get_diags()
|
||||
.context("getting suggestions from rustc")?;
|
||||
|
||||
debug!(?diags, "Got diagnostics");
|
||||
|
||||
let mut suggestions_for_file = HashMap::<_, Vec<_>>::new();
|
||||
for suggestion in &suggestions {
|
||||
suggestions_for_file
|
||||
|
|
@ -197,7 +199,8 @@ impl<'a> FindUnusedFunction<'a> {
|
|||
"encountered multiline span in dead_code"
|
||||
);
|
||||
|
||||
if Path::new(&span.file_name) != file.path {
|
||||
// When the project directory is remapped, the path may be absolute or generally have some prefix.
|
||||
if !file.path.ends_with(&span.file_name) {
|
||||
return None;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use std::process::Command;
|
||||
use std::{process::Command, sync::Mutex};
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use cargo_minimize::Options;
|
||||
|
|
@ -8,7 +8,20 @@ fn canonicalize(code: &str) -> Result<String> {
|
|||
Ok(prettyplease::unparse(&ast))
|
||||
}
|
||||
|
||||
static HAS_SUBSCRIBER: Mutex<bool> = Mutex::new(false);
|
||||
|
||||
fn init_subscriber() {
|
||||
let mut has_subscriber = HAS_SUBSCRIBER.lock().unwrap();
|
||||
if !*has_subscriber {
|
||||
cargo_minimize::init_recommended_tracing_subscriber();
|
||||
*has_subscriber = true;
|
||||
}
|
||||
drop(has_subscriber);
|
||||
}
|
||||
|
||||
pub fn run_test(code: &str, minimizes_to: &str, options: impl FnOnce(&mut Options)) -> Result<()> {
|
||||
init_subscriber();
|
||||
|
||||
let dir = tempfile::tempdir()?;
|
||||
|
||||
let mut cargo = Command::new("cargo");
|
||||
|
|
@ -48,4 +61,3 @@ pub fn run_test(code: &str, minimizes_to: &str, options: impl FnOnce(&mut Option
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,3 +22,25 @@ fn hello_world_no_verify() -> Result<()> {
|
|||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unused() -> Result<()> {
|
||||
// After everybody_loops, `unused` becomes dead and should be removed.
|
||||
run_test(
|
||||
r##"
|
||||
fn unused() {}
|
||||
|
||||
fn main() {
|
||||
unused();
|
||||
}
|
||||
"##,
|
||||
r##"
|
||||
fn main() {
|
||||
loop {}
|
||||
}
|
||||
"##,
|
||||
|opts| {
|
||||
opts.no_verify = true;
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue