Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed Nov 8, 2023
1 parent b723e41 commit d52213a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 69 deletions.
28 changes: 5 additions & 23 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
//! Run Cargo as a subprocess, including timeouts and propagating signals.
use std::env;
use std::sync::Arc;
use std::time::{Duration, Instant};

use anyhow::{anyhow, ensure, Context, Result};
use camino::{Utf8Path, Utf8PathBuf};
use cargo_metadata::Metadata;
use anyhow::Result;
use camino::Utf8Path;
use itertools::Itertools;
use serde_json::Value;
use tracing::debug_span;
#[allow(unused_imports)]
use tracing::{debug, error, info, span, trace, warn, Level};
use tracing::{debug, debug_span};

use crate::outcome::PhaseResult;
use crate::package::Package;
use crate::process::{get_command_output, Process};
use crate::process::Process;
use crate::*;

/// Run cargo build, check, or test.
Expand Down Expand Up @@ -51,18 +46,6 @@ pub fn run_cargo(
})
}

pub fn run_cargo_metadata(workspace_dir: &Utf8Path) -> Result<Metadata> {
let cargo_toml_path = workspace_dir.join("Cargo.toml");
debug!(?cargo_toml_path, ?workspace_dir, "run cargo metadata");
check_interrupted()?;
let metadata = cargo_metadata::MetadataCommand::new()
.manifest_path(&cargo_toml_path)
.exec()
.context("run cargo metadata")?;
check_interrupted()?;
Ok(metadata)
}

/// Return the name of the cargo binary.
pub fn cargo_bin() -> String {
// When run as a Cargo subcommand, which is the usual/intended case,
Expand Down Expand Up @@ -140,9 +123,8 @@ fn rustflags() -> String {

#[cfg(test)]
mod test {
use std::ffi::OsStr;
use std::sync::Arc;

use itertools::Itertools;
use pretty_assertions::assert_eq;

use crate::{Options, Phase};
Expand Down
15 changes: 9 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ use crate::options::Options;
use crate::outcome::{Phase, ScenarioOutcome};
use crate::path::Utf8PathSlashes;
use crate::scenario::Scenario;
use crate::source::SourceFile;
use crate::visit::walk_tree;
use crate::workspace::PackageFilter;
use crate::workspace::Workspace;
use crate::workspace::{PackageFilter, Workspace};

const VERSION: &str = env!("CARGO_PKG_VERSION");
const NAME: &str = env!("CARGO_PKG_NAME");
Expand Down Expand Up @@ -227,7 +224,7 @@ fn main() -> Result<()> {
interrupt::install_handler();

let start_dir: &Utf8Path = args.dir.as_deref().unwrap_or(Utf8Path::new("."));
let workspace = Workspace::open(&start_dir)?;
let workspace = Workspace::open(start_dir)?;
// let discovered_workspace = discover_packages(start_dir, false, &args.mutate_packages)?;
// let workspace_dir = &discovered_workspace.workspace_dir;
let config = if args.no_config {
Expand All @@ -238,7 +235,13 @@ fn main() -> Result<()> {
debug!(?config);
let options = Options::new(&args, &config)?;
debug!(?options);
let package_filter = PackageFilter::All; // TODO: From args
let package_filter = if !args.mutate_packages.is_empty() {
PackageFilter::explicit(&args.mutate_packages)
} else {
// TODO: --workspace
// TODO: Actually, Auto(start_dir) if not otherwise set.
PackageFilter::All
};
let discovered = workspace.discover(&package_filter, &options, &console)?;
if args.list_files {
console.clear();
Expand Down
2 changes: 1 addition & 1 deletion src/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ mod test {
#[test]
fn mutate_factorial() -> Result<()> {
let tree_path = Utf8Path::new("testdata/tree/factorial");
let mutants = Workspace::open(&tree_path)?.mutants(
let mutants = Workspace::open(tree_path)?.mutants(
&PackageFilter::All,
&Options::default(),
&Console::new(),
Expand Down
10 changes: 1 addition & 9 deletions src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@

//! Discover and represent cargo packages within a workspace.
use std::sync::Arc;

use anyhow::{anyhow, Context};
use camino::{Utf8Path, Utf8PathBuf};
use itertools::Itertools;
use tracing::{debug_span, warn};

use crate::source::SourceFile;
use crate::*;
use camino::Utf8PathBuf;

/// A package built and tested as a unit.
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ src/build_dir.rs: replace copy_tree -> Result<TempDir> with Ok(Default::default(
src/build_dir.rs: replace copy_tree -> Result<TempDir> with Err(::anyhow::anyhow!("mutated!"))
src/cargo.rs: replace run_cargo -> Result<PhaseResult> with Ok(Default::default())
src/cargo.rs: replace run_cargo -> Result<PhaseResult> with Err(::anyhow::anyhow!("mutated!"))
src/cargo.rs: replace run_cargo_metadata -> Result<Metadata> with Ok(Default::default())
src/cargo.rs: replace run_cargo_metadata -> Result<Metadata> with Err(::anyhow::anyhow!("mutated!"))
src/cargo.rs: replace cargo_bin -> String with String::new()
src/cargo.rs: replace cargo_bin -> String with "xyzzy".into()
src/cargo.rs: replace cargo_argv -> Vec<String> with vec![]
Expand Down Expand Up @@ -335,9 +333,9 @@ src/workspace.rs: replace Workspace::open -> Result<Self> with Err(::anyhow::any
src/workspace.rs: replace Workspace::packages -> Result<Vec<Arc<Package>>> with Ok(vec![])
src/workspace.rs: replace Workspace::packages -> Result<Vec<Arc<Package>>> with Ok(vec![Arc::new(Default::default())])
src/workspace.rs: replace Workspace::packages -> Result<Vec<Arc<Package>>> with Err(::anyhow::anyhow!("mutated!"))
src/workspace.rs: replace Workspace::top_package_sources -> Result<Vec<Utf8PathBuf>> with Ok(vec![])
src/workspace.rs: replace Workspace::top_package_sources -> Result<Vec<Utf8PathBuf>> with Ok(vec![Default::default()])
src/workspace.rs: replace Workspace::top_package_sources -> Result<Vec<Utf8PathBuf>> with Err(::anyhow::anyhow!("mutated!"))
src/workspace.rs: replace Workspace::package_tops -> Result<Vec<PackageTop>> with Ok(vec![])
src/workspace.rs: replace Workspace::package_tops -> Result<Vec<PackageTop>> with Ok(vec![Default::default()])
src/workspace.rs: replace Workspace::package_tops -> Result<Vec<PackageTop>> with Err(::anyhow::anyhow!("mutated!"))
src/workspace.rs: replace Workspace::top_sources -> Result<Vec<Arc<SourceFile>>> with Ok(vec![])
src/workspace.rs: replace Workspace::top_sources -> Result<Vec<Arc<SourceFile>>> with Ok(vec![Arc::new(Default::default())])
src/workspace.rs: replace Workspace::top_sources -> Result<Vec<Arc<SourceFile>>> with Err(::anyhow::anyhow!("mutated!"))
Expand Down
55 changes: 31 additions & 24 deletions src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ impl PackageFilter {
}
}

/// A package and the top source files within it.
struct PackageTop {
package: Arc<Package>,
top_sources: Vec<Utf8PathBuf>,
}

impl Workspace {
pub fn open(start_dir: &Utf8Path) -> Result<Self> {
let dir = find_workspace(start_dir)?;
Expand All @@ -61,7 +67,16 @@ impl Workspace {

/// Find packages to mutate, subject to some filtering.
pub fn packages(&self, package_filter: &PackageFilter) -> Result<Vec<Arc<Package>>> {
let mut packages = Vec::new();
Ok(self
.package_tops(package_filter)?
.into_iter()
.map(|pt| pt.package)
.collect())
}

/// Find all the packages and their top source files.
fn package_tops(&self, package_filter: &PackageFilter) -> Result<Vec<PackageTop>> {
let mut tops = Vec::new();
for package_metadata in filter_package_metadata(&self.metadata, package_filter)
.into_iter()
.sorted_by_key(|p| &p.name)
Expand All @@ -85,39 +100,30 @@ impl Workspace {
name: package_metadata.name.clone(),
relative_manifest_path,
});
packages.push(package);
tops.push(PackageTop {
package,
top_sources: direct_package_sources(&self.dir, package_metadata)?,
});
}
if let PackageFilter::Explicit(names) = package_filter {
for wanted in names {
if !packages.iter().any(|found| found.name == *wanted) {
if !tops.iter().any(|found| found.package.name == *wanted) {
warn!("package {wanted:?} not found in source tree");
}
}
}
Ok(packages)
}

/// Return the top source files (like `src/lib.rs`) for a named package.
fn top_package_sources(&self, package_name: &str) -> Result<Vec<Utf8PathBuf>> {
if let Some(package_metadata) = self
.metadata
.workspace_packages()
.iter()
.find(|p| p.name == package_name)
{
direct_package_sources(&self.dir, package_metadata)
} else {
Err(anyhow!(
"package {package_name:?} not found in workspace metadata"
))
}
Ok(tops)
}

/// Find all the top source files for selected packages.
pub fn top_sources(&self, package_filter: &PackageFilter) -> Result<Vec<Arc<SourceFile>>> {
fn top_sources(&self, package_filter: &PackageFilter) -> Result<Vec<Arc<SourceFile>>> {
let mut sources = Vec::new();
for package in self.packages(package_filter)? {
for source_path in self.top_package_sources(&package.name)? {
for PackageTop {
package,
top_sources,
} in self.package_tops(package_filter)?
{
for source_path in top_sources {
sources.push(Arc::new(SourceFile::new(
&self.dir,
source_path.to_owned(),
Expand All @@ -143,6 +149,7 @@ impl Workspace {
)
}

/// Return all mutants generated from this workspace.
pub fn mutants(
&self,
package_filter: &PackageFilter,
Expand Down Expand Up @@ -254,7 +261,7 @@ mod test {

#[test]
fn error_opening_outside_of_crate() {
Workspace::open(&Utf8Path::new("/")).unwrap_err();
Workspace::open(Utf8Path::new("/")).unwrap_err();
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn list_warns_about_unmatched_packages() {
])
.assert()
.stdout(predicates::str::contains(
"package notapackage not found in source tree",
"package \"notapackage\" not found in source tree",
))
.code(0);
}
Expand Down

0 comments on commit d52213a

Please sign in to comment.