Git Product home page Git Product logo

Comments (3)

sourcefrog avatar sourcefrog commented on June 8, 2024

Thanks very much for the clear report and reproduction! I had not realized that you could have mod statements that correspond to multiple directories but it makes sense.

I think visit_item_mod should already be tracking the nested namespaces.

cargo-mutants/src/visit.rs

Lines 341 to 356 in 4424126

/// Visit `mod foo { ... }` or `mod foo;`.
fn visit_item_mod(&mut self, node: &'ast syn::ItemMod) {
let mod_name = &node.ident.unraw().to_string();
let _span = trace_span!("mod", line = node.mod_token.span.start().line, mod_name).entered();
if attrs_excluded(&node.attrs) {
trace!("mod excluded by attrs");
return;
}
// If there's no content in braces, then this is a `mod foo;`
// statement referring to an external file. We remember the module
// name and then later look for the file.
if node.content.is_none() {
self.external_mods.push(mod_name.to_owned());
}
self.in_namespace(mod_name, |v| syn::visit::visit_item_mod(v, node));
}

The problem is that the accumulated list of external_mods only takes the last component of the path. Maybe it should use the whole stack... although, we need to probably be careful to get this right if the mod we're already looking at is some layers down.

The interaction of the stack with the existing filenames suggests to me that maybe a better test is something with a mix of a file that is itself 2 levels down, and that then has an external mod that is then some further levels down.

A PR would be very welcome.

Some ideas on how to start:

  • Copy your tree under testdata with an appropriate directory and package name
  • Add it to the workspace exclusions in /Cargo.toml
  • Add the namespaces to the stack in visit.rs
  • Add a test in visit.rs for reading the testdata, like in

    cargo-mutants/src/visit.rs

    Lines 589 to 599 in 4424126

    /// We don't visit functions inside files marked with `#![cfg(test)]`.
    #[test]
    fn no_mutants_in_files_with_inner_cfg_test_attribute() {
    let options = Options::default();
    let console = Console::new();
    let workspace = Workspace::open("testdata/cfg_test_inner").unwrap();
    let discovered = workspace
    .discover(&PackageFilter::All, &options, &console)
    .unwrap();
    assert_eq!(discovered.mutants.as_slice(), &[]);
    }

from cargo-mutants.

sourcefrog avatar sourcefrog commented on June 8, 2024

Also, not necessarily in the same PR: at the moment

for mod_name in &external_mods {
if let Some(mod_path) = find_mod_source(workspace_dir, &source_file, mod_name)? {
file_queue.push_back(SourceFile::new(
workspace_dir,
mod_path,
&source_file.package,
false,
)?)
}
}
only warns if it can't find a mod, but at least for tests we should probably have an option to make this an error.

cargo-mutants/src/visit.rs

Lines 457 to 468 in 4424126

let mut tried_paths = Vec::new();
for &tail in &[".rs", "/mod.rs"] {
let relative_path = search_dir.join(mod_name.to_owned() + tail);
let full_path = tree_root.join(&relative_path);
if full_path.is_file() {
trace!("found submodule in {full_path}");
return Ok(Some(relative_path));
} else {
tried_paths.push(full_path);
}
}
warn!(?parent_path, %mod_name, ?tried_paths, "referent of mod not found");

from cargo-mutants.

sourcefrog avatar sourcefrog commented on June 8, 2024

Thanks, I merged that PR, but I suspect there is some more to do here because inline mod statements can have a path attribute that controls which directory they should read: https://doc.rust-lang.org/reference/items/modules.html#the-path-attribute

This will still fix some trees and I'll file a separate issue for that.

from cargo-mutants.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.