LCOV - code coverage report
Current view: top level - internal/src - examples.rs (source / functions) Hit Total Coverage
Test: unnamed Lines: 49 54 90.7 %
Date: 2024-10-23 19:20:50 Functions: 4 7 57.1 %

          Line data    Source code
       1             : use crate::{rustup::SanitizeEnvironment, CommandExt};
       2             : use anyhow::{anyhow, Context, Result};
       3             : use std::path::{Path, PathBuf};
       4             : use walkdir::WalkDir;
       5             : 
       6           2 : pub fn build() -> Result<()> {
       7           2 :     // smoelius: The examples use `dylint-link` as the linker, so it must be built first.
       8           2 :     #[cfg_attr(dylint_lib = "general", allow(abs_home_path))]
       9           2 :     crate::cargo::build("dylint-link")
      10           2 :         .build()
      11           2 :         .sanitize_environment()
      12           2 :         .current_dir(Path::new(env!("CARGO_MANIFEST_DIR")).join("../dylint-link"))
      13           2 :         .success()?;
      14             : 
      15          16 :     for example in iter(true)? {
      16          16 :         let example = example?;
      17          16 :         let file_name = example
      18          16 :             .file_name()
      19          16 :             .ok_or_else(|| anyhow!("Could not get file name"))?;
      20          16 :         crate::cargo::build(&format!("example `{}`", file_name.to_string_lossy()))
      21          16 :             .build()
      22          16 :             .sanitize_environment()
      23          16 :             .current_dir(&example)
      24          16 :             .success()?;
      25             :     }
      26             : 
      27           2 :     Ok(())
      28           2 : }
      29             : 
      30           2 : pub fn iter(workspace: bool) -> Result<impl Iterator<Item = Result<PathBuf>>> {
      31           2 :     #[cfg_attr(dylint_lib = "general", allow(abs_home_path))]
      32           2 :     let path_buf = Path::new(env!("CARGO_MANIFEST_DIR")).join("../examples");
      33           2 :     // smoelius: Use `cargo_util::paths::normalize_path` instead of `canonicalize` so as not to
      34           2 :     // "taint" the path with a path prefix on Windows.
      35           2 :     let examples = cargo_util::paths::normalize_path(&path_buf);
      36           2 :     let iter = WalkDir::new(examples)
      37           2 :         .into_iter()
      38         432 :         .filter_entry(|entry| entry.depth() <= 2);
      39           2 :     Ok(iter
      40         122 :         .map(move |entry| -> Result<Option<PathBuf>> {
      41         122 :             let entry = entry?;
      42         122 :             let path = entry.path();
      43         122 :             let rust_toolchain_path = path.join("rust-toolchain");
      44         122 :             let cargo_toml_path = path.join("Cargo.toml");
      45         122 :             if entry.depth() < 1 || !path.is_dir() {
      46          36 :                 return Ok(None);
      47          86 :             }
      48          86 :             if workspace
      49          86 :                 && rust_toolchain_path.try_exists().with_context(|| {
      50           0 :                     format!("Could not determine whether {rust_toolchain_path:?} exists")
      51          86 :                 })?
      52             :             {
      53          16 :                 return Ok(Some(path.to_path_buf()));
      54          70 :             }
      55          70 :             if !workspace
      56           0 :                 && cargo_toml_path.try_exists().with_context(|| {
      57           0 :                     format!("Could not determine whether {cargo_toml_path:?} exists")
      58           0 :                 })?
      59             :             {
      60           0 :                 return Ok(Some(path.to_path_buf()));
      61          70 :             }
      62          70 :             Ok(None)
      63         122 :         })
      64           2 :         .filter_map(Result::transpose))
      65           2 : }

Generated by: LCOV version 1.14