Skip to content

Commit

Permalink
Ignore attributes on functions (#1384)
Browse files Browse the repository at this point in the history
  • Loading branch information
xd009642 authored Sep 17, 2023
1 parent 6ff6916 commit 19a0663
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
From 2019 onwards, all notable changes to tarpaulin will be documented in this
file.

## [Unreleased]
## [0.27.0] 2023-09-17
### Added
- Added `--fail-immediately` flag to abort execution the moment the first test failure occurs

### Changed
- Upgraded from clap v2 to v4. This has a few changes, notably any arguments which can be specified more
than once require multiple entries so `--run-types doc test` needs to be turned into `--run-types doc --run-types test`
- Ignore attributes on methods, functions and trait methods

## [0.26.1] 2023-07-02
### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-tarpaulin"
version = "0.26.1"
version = "0.27.0"
authors = ["Daniel McKenna <[email protected]>"]
description = "Cargo-Tarpaulin is a tool to determine code coverage achieved via tests"
repository = "https://github.com/xd009642/tarpaulin"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ Below is the help-text for a thorough explanation of the flags and features
available:

```
cargo-tarpaulin version: 0.26.1
Cargo-Tarpaulin is a tool to determine code coverage achieved via tests
Usage: cargo [OPTIONS] [-- <ARGS>...]
Usage: cargo tarpaulin [OPTIONS] [-- <ARGS>...]
Arguments:
[ARGS]... Arguments to be passed to the test executables can be used to filter or skip certain tests
Expand Down Expand Up @@ -105,6 +104,7 @@ Options:
-r, --root <DIR> Calculates relative paths to root directory. If --manifest-path isn't specified it will look for a Cargo.toml in root
--manifest-path <PATH> Path to Cargo.toml
--ciserver <SERVICE> CI server being used, if unspecified tarpaulin may automatically infer for coveralls uploads
--fail-immediately Option to fail immediately after a single test fails
-h, --help Print help
-V, --version Print version
```
Expand Down
7 changes: 7 additions & 0 deletions src/source_analysis/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ impl SourceAnalysis {
self.visit_generics(&func.sig.generics, ctx);
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
let line_number = func.sig.fn_token.span().start().line;
let mut start_line = line_number;
for attr in &func.attrs {
start_line = start_line.min(attr.span().start().line);
}
if start_line < line_number {
analysis.add_to_ignore(start_line..line_number);
}
analysis.ignore.remove(&Lines::Line(line_number));
// Ignore multiple lines of fn decl
let decl_start = func.sig.fn_token.span().start().line + 1;
Expand Down
36 changes: 36 additions & 0 deletions src/source_analysis/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1550,3 +1550,39 @@ fn ignore_comment() {
assert!(lines.ignore.contains(&Lines::Line(11)));
assert!(lines.ignore.contains(&Lines::Line(12)));
}

#[test]
fn py_attr() {
let config = Config::default();
let ctx = Context {
config: &config,
file_contents: "use pyo3::prelude::{pyfunction, PyResult};
#[pyfunction]
pub fn print_something() -> PyResult<()> {
println!(\"foo\");
Ok(())
}
struct Blah;
#[pyimpl]
impl Blah {
#[pyfunction]
fn blah() -> Self {
Self
}
}
",
file: Path::new(""),
ignore_mods: RefCell::new(HashSet::new()),
};
let parser = parse_file(ctx.file_contents).unwrap();
let mut analysis = SourceAnalysis::new();
analysis.process_items(&parser.items, &ctx);
let lines = &analysis.lines[Path::new("")];
assert!(lines.ignore.contains(&Lines::Line(1)));
assert!(lines.ignore.contains(&Lines::Line(3)));
assert!(lines.ignore.contains(&Lines::Line(11)));
assert!(lines.ignore.contains(&Lines::Line(13)));
}

0 comments on commit 19a0663

Please sign in to comment.