Skip to content

Commit

Permalink
Fix filtering of #[test] and #[cfg(test)] in structure/`depende…
Browse files Browse the repository at this point in the history
…ncies` commands
  • Loading branch information
regexident committed Oct 22, 2024
1 parent 5cd82bd commit b248ae5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Please make sure to add your changes to the appropriate categories:

### Fixed

- n/a
- Fixed filtering of `#[test]` and `#[cfg(test)]` when `--cfg-test` is not used

### Performance

Expand Down
21 changes: 21 additions & 0 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,27 @@ fn tree_contains_self(tree: &ast::UseTree) -> bool {
.unwrap_or(false)
}

pub(crate) fn has_test_cfg(hir: hir::ModuleDef, db: &ide::RootDatabase) -> bool {
let Some(attrs) = hir.attrs(db) else {
return false;
};

let test_key = hir::Symbol::intern("test");
let cfg_exprs: Vec<_> = attrs.cfgs().collect();

cfg_exprs.into_iter().any(|cfg_expr| {
cfg_expr
.fold(&|cfg| {
use ra_ap_cfg::CfgAtom;
match cfg {
CfgAtom::Flag(symbol) => symbol == &test_key,
CfgAtom::KeyValue { .. } => false,
}
})
.is_some()
})
}

pub(crate) fn is_test_function(function: hir::Function, db: &ide::RootDatabase) -> bool {
let attrs = function.attrs(db);
let key = hir::Symbol::intern("test");
Expand Down
12 changes: 10 additions & 2 deletions src/command/dependencies/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use petgraph::{
};

use crate::{
analyzer,
analyzer::{self, has_test_cfg, is_test_function},
graph::{Edge, Graph, GraphWalker, Node, Relationship},
};

Expand Down Expand Up @@ -244,6 +244,10 @@ impl<'a> Filter<'a> {
return false;
}

if !self.options.cfg_test && has_test_cfg(module_def_hir, self.db) {
return false;
}

match module_def_hir {
hir::ModuleDef::Module(module_hir) => self.should_retain_module(module_hir),
hir::ModuleDef::Function(function_hir) => self.should_retain_function(function_hir),
Expand Down Expand Up @@ -273,11 +277,15 @@ impl<'a> Filter<'a> {
true
}

fn should_retain_function(&self, _function_hir: hir::Function) -> bool {
fn should_retain_function(&self, function_hir: hir::Function) -> bool {
if self.options.selection.no_fns {
return false;
}

if !self.options.cfg_test && is_test_function(function_hir, self.db) {
return false;
}

true
}

Expand Down
15 changes: 13 additions & 2 deletions src/command/structure/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use ra_ap_hir::{self as hir};
use ra_ap_ide::{self as ide};
use ra_ap_syntax::ast;

use crate::{analyzer, tree::Tree};
use crate::{
analyzer::{self, has_test_cfg, is_test_function},
tree::Tree,
};

use super::{options::Options, Node};

Expand Down Expand Up @@ -125,6 +128,10 @@ impl<'a> Filter<'a> {
return false;
}

if !self.options.cfg_test && has_test_cfg(module_def_hir, self.db) {
return false;
}

match module_def_hir {
hir::ModuleDef::Module(module_hir) => self.should_retain_module(module_hir),
hir::ModuleDef::Function(function_hir) => self.should_retain_function(function_hir),
Expand All @@ -150,11 +157,15 @@ impl<'a> Filter<'a> {
true
}

fn should_retain_function(&self, _function_hir: hir::Function) -> bool {
fn should_retain_function(&self, function_hir: hir::Function) -> bool {
if self.options.selection.no_fns {
return false;
}

if !self.options.cfg_test && is_test_function(function_hir, self.db) {
return false;
}

true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,23 @@ digraph {
];

"github_issue_80" [label="crate|github_issue_80", fillcolor="#5397c8"]; // "crate" node
"github_issue_80::OnlyExistsWithoutTest" [label="pub struct|OnlyExistsWithoutTest", fillcolor="#81c169"]; // "struct" node
"github_issue_80::Placebo" [label="pub struct|Placebo", fillcolor="#81c169"]; // "struct" node
"github_issue_80::imported" [label="pub mod|imported", fillcolor="#81c169"]; // "mod" node
"github_issue_80::imported::OnlyUsedWithTest" [label="pub struct|imported::OnlyUsedWithTest", fillcolor="#81c169"]; // "struct" node
"github_issue_80::imported::OnlyUsedWithoutTest" [label="pub struct|imported::OnlyUsedWithoutTest", fillcolor="#81c169"]; // "struct" node
"github_issue_80::imported::Placebo" [label="pub struct|imported::Placebo", fillcolor="#81c169"]; // "struct" node
"github_issue_80::importing" [label="pub mod|importing", fillcolor="#81c169"]; // "mod" node
"github_issue_80::only_exists_without_test" [label="pub mod|only_exists_without_test", fillcolor="#81c169"]; // "mod" node
"github_issue_80::only_exists_without_test::OnlyExistsWithoutTest" [label="pub struct|only_exists_without_test::OnlyExistsWithoutTest", fillcolor="#81c169"]; // "struct" node
"github_issue_80::only_exists_without_test::Placebo" [label="pub struct|only_exists_without_test::Placebo", fillcolor="#81c169"]; // "struct" node

"github_issue_80" -> "github_issue_80::OnlyExistsWithoutTest" [label="owns", color="#000000", style="solid"] [constraint=true]; // "owns" edge
"github_issue_80" -> "github_issue_80::Placebo" [label="owns", color="#000000", style="solid"] [constraint=true]; // "owns" edge
"github_issue_80" -> "github_issue_80::imported" [label="owns", color="#000000", style="solid"] [constraint=true]; // "owns" edge
"github_issue_80" -> "github_issue_80::imported::OnlyUsedWithoutTest" [label="uses", color="#7f7f7f", style="dashed"] [constraint=false]; // "uses" edge
"github_issue_80" -> "github_issue_80::importing" [label="owns", color="#000000", style="solid"] [constraint=true]; // "owns" edge
"github_issue_80" -> "github_issue_80::only_exists_without_test" [label="owns", color="#000000", style="solid"] [constraint=true]; // "owns" edge
"github_issue_80" -> "github_issue_80::only_exists_without_test::Placebo" [label="owns", color="#000000", style="solid"] [constraint=true]; // "owns" edge
"github_issue_80::imported" -> "github_issue_80::imported::OnlyUsedWithTest" [label="owns", color="#000000", style="solid"] [constraint=true]; // "owns" edge
"github_issue_80::imported" -> "github_issue_80::imported::OnlyUsedWithoutTest" [label="owns", color="#000000", style="solid"] [constraint=true]; // "owns" edge
"github_issue_80::imported" -> "github_issue_80::imported::Placebo" [label="owns", color="#000000", style="solid"] [constraint=true]; // "owns" edge
"github_issue_80::importing" -> "github_issue_80::imported::OnlyUsedWithoutTest" [label="uses", color="#7f7f7f", style="dashed"] [constraint=false]; // "uses" edge
"github_issue_80::importing" -> "github_issue_80::imported::Placebo" [label="uses", color="#7f7f7f", style="dashed"] [constraint=false]; // "uses" edge
"github_issue_80::only_exists_without_test" -> "github_issue_80::only_exists_without_test::OnlyExistsWithoutTest" [label="owns", color="#000000", style="solid"] [constraint=true]; // "owns" edge
"github_issue_80::only_exists_without_test" -> "github_issue_80::only_exists_without_test::Placebo" [label="owns", color="#000000", style="solid"] [constraint=true]; // "owns" edge

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ STDERR:
STDOUT:

crate github_issue_80
├── struct OnlyExistsWithoutTest: pub #[cfg(not(test))]
├── struct Placebo: pub
├── mod imported: pub
│ ├── struct OnlyUsedWithTest: pub
│ ├── struct OnlyUsedWithoutTest: pub
│ └── struct Placebo: pub
├── mod importing: pub
└── mod only_exists_without_test: pub #[cfg(not(test))]
├── struct OnlyExistsWithoutTest: pub #[cfg(not(test))]
└── struct Placebo: pub
└── mod importing: pub

0 comments on commit b248ae5

Please sign in to comment.