fn analyze_cargo_package(project_directory: &Path) -> anyhow::Result<Vec<TestDefinition>> {
    info!("Analyzing Cargo package `{}`", project_directory.display());

    assert!(project_directory.is_dir());
    assert!(project_directory.exists());
    assert!(project_directory.is_absolute());

    let pretty_project_path = CargoPackage::pretty_root_path(project_directory);

    let cargo_toml_name = project_directory.join("Cargo.toml");

    if !cargo_toml_name.exists() {
        bail!("No Cargo.toml in {}", project_directory.display());
    }

    let mut test_defs = vec![];

    let src_directory = project_directory.join("src");

    let mut collect_test_def =
        |source_path: PathBuf, executable_name: OsString| -> anyhow::Result<()> {
            if source_path.exists() {
                let contents = std::fs::read_to_string(&source_path)?;
                let script = parse_script(&contents, Some(source_path.as_path()))?;
                let breakpoints = breakpoints::find(&contents);

                let test_definition = TestDefinition::new(
                    source_path.strip_prefix(&project_directory)?,
                    source_path.as_path(),
                    &pretty_project_path,
                    executable_name,
                    script,
                    breakpoints,
                );

                info!(" - Found test case `{}`", test_definition.name);

                test_defs.push(test_definition);
            }

            Ok(())
        };

    // Take care of main.rs
    collect_test_def(
        src_directory.join("main.rs"),
        executable_name(project_directory.file_name().unwrap()),
    )?;

    // Collect executables from the src/bin directory
    let bin_directory = src_directory.join("bin");
    if bin_directory.exists() {
        let rs_ext = OsString::from("rs");

        for dir_entry in std::fs::read_dir(&bin_directory)? {
            let dir_entry = dir_entry?;
            let source_path = dir_entry.path();

            if source_path.extension() == Some(rs_ext.as_os_str()) {
                let executable_name =
                    executable_name(PathBuf::from(dir_entry.file_name()).with_extension(""));

                collect_test_def(source_path, executable_name)?;
            }
        }
    }

    test_defs.sort_by_cached_key(|td| td.executable_name.clone());

    Ok(test_defs)
}
/// Finds and parses all [TestDefinitions]s in a Cargo package. Does not actually look
/// at the package's Cargo.toml, just assumes that `src/main.rs` and each file in `src/bin`
/// specifies a test case.
// TODO: also support tests specified in `tests` directory
Finds and parses all [TestDefinitions]s in a Cargo package. Does not actually look
at the package's Cargo.toml, just assumes that `src/main.rs` and each file in `src/bin`
specifies a test case.
TODO: also support tests specified in `tests` directory
Finds and parses all [ TestDefinitions ] s in a Cargo package . Does not actually look at the package ' s Cargo . toml just assumes that ` src / main . rs ` and each file in ` src / bin ` specifies a test case . TODO : also support tests specified in ` tests ` directory
fn analyze_cargo_package(project_directory: &Path) -> anyhow::Result<Vec<TestDefinition>> {
    info!("Analyzing Cargo package `{}`", project_directory.display());
    assert!(project_directory.is_dir());
    assert!(project_directory.exists());
    assert!(project_directory.is_absolute());
    let pretty_project_path = CargoPackage::pretty_root_path(project_directory);
    let cargo_toml_name = project_directory.join("Cargo.toml");
    if !cargo_toml_name.exists() {
        bail!("No Cargo.toml in {}", project_directory.display());
    }
    let mut test_defs = vec![];
    let src_directory = project_directory.join("src");
    let mut collect_test_def =
        |source_path: PathBuf, executable_name: OsString| -> anyhow::Result<()> {
            if source_path.exists() {
                let contents = std::fs::read_to_string(&source_path)?;
                let script = parse_script(&contents, Some(source_path.as_path()))?;
                let breakpoints = breakpoints::find(&contents);
                let test_definition = TestDefinition::new(
                    source_path.strip_prefix(&project_directory)?,
                    source_path.as_path(),
                    &pretty_project_path,
                    executable_name,
                    script,
                    breakpoints,
                );
                info!(" - Found test case `{}`", test_definition.name);
                test_defs.push(test_definition);
            }
            Ok(())
        };
    // Take care of main.rs
    collect_test_def(
        src_directory.join("main.rs"),
        executable_name(project_directory.file_name().unwrap()),
    )?;
    // Collect executables from the src/bin directory
    let bin_directory = src_directory.join("bin");
    if bin_directory.exists() {
        let rs_ext = OsString::from("rs");
        for dir_entry in std::fs::read_dir(&bin_directory)? {
            let dir_entry = dir_entry?;
            let source_path = dir_entry.path();
            if source_path.extension() == Some(rs_ext.as_os_str()) {
                let executable_name =
                    executable_name(PathBuf::from(dir_entry.file_name()).with_extension(""));
                collect_test_def(source_path, executable_name)?;
            }
        }
    }
    test_defs.sort_by_cached_key(|td| td.executable_name.clone());
    Ok(test_defs)
}
fn analyze_cargo_package ( project_directory : & Path ) -> anyhow :: Result < Vec < TestDefinition > > { info ! ( " " project_directory display ( ) ) ; assert ! ( project_directory is_dir ( ) ) ; assert ! ( project_directory exists ( ) ) ; assert ! ( project_directory is_absolute ( ) ) ; let pretty_project_path = CargoPackage :: pretty_root_path ( project_directory ) ; let cargo_toml_name = project_directory . join ( " " ) ; if ! cargo_toml_name . exists ( ) { bail ! ( " " project_directory display ( ) ) ; } let mut test_defs = vec ! [ ] ; let src_directory = project_directory . join ( " " ) ; let mut collect_test_def = | source_path : PathBuf , executable_name : OsString | -> anyhow :: Result < ( ) > { if source_path . exists ( ) { let contents = std :: fs :: read_to_string ( & source_path ) ? ; let script = parse_script ( & contents , Some ( source_path . as_path ( ) ) ) ? ; let breakpoints = breakpoints :: find ( & contents ) ; let test_definition = TestDefinition :: new ( source_path . strip_prefix ( & project_directory ) ? , source_path . as_path ( ) , & pretty_project_path , executable_name , script , breakpoints , ) ; info ! ( " " test_definition name ) ; test_defs . push ( test_definition ) ; } Ok ( ( ) ) } ; // Take care of main.rs collect_test_def ( src_directory . join ( " " ) , executable_name ( project_directory . file_name ( ) . unwrap ( ) ) , ) ? ; // Collect executables from the src/bin directory let bin_directory = src_directory . join ( " " ) ; if bin_directory . exists ( ) { let rs_ext = OsString :: from ( " " ) ; for dir_entry in std :: fs :: read_dir ( & bin_directory ) ? { let dir_entry = dir_entry ? ; let source_path = dir_entry . path ( ) ; if source_path . extension ( ) == Some ( rs_ext . as_os_str ( ) ) { let executable_name = executable_name ( PathBuf :: from ( dir_entry . file_name ( ) ) . with_extension ( " " ) ) ; collect_test_def ( source_path , executable_name ) ? ; } } } test_defs . sort_by_cached_key ( | td | td . executable_name . clone ( ) ) ; Ok ( test_defs ) }
Finds and parses all [TestDefinitions]s in a Cargo package.
Finds and parses all [ TestDefinitions ] s in a Cargo package .
project_directory
&Path
project_directory
&Path