Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run coverage on whole workspace #71

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ coverage:
threshold: 1%

ignore:
- "ffi"
- "benches"
- "tests"
9 changes: 4 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,17 @@ jobs:
coverage:
runs-on: ubuntu-latest
steps:
- name: Install opencl
run: sudo apt-get install -y libpocl2 mesa-opencl-icd ocl-icd-opencl-dev
- uses: actions/checkout@v3
- name: Install stable
uses: dtolnay/rust-toolchain@stable
with:
components: llvm-tools-preview
- name: cargo install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
- name: cargo generate-lockfile
if: hashFiles('Cargo.lock') == ''
run: cargo generate-lockfile
- name: cargo llvm-cov
run: cargo llvm-cov --locked --all-features --lcov --output-path lcov.info
run: cargo llvm-cov --locked --all-features --lcov --output-path lcov.info --workspace --ignore-filename-regex "main.rs" -- --test-threads=1
- name: Upload to codecov.io
uses: codecov/codecov-action@v3
with:
Expand Down Expand Up @@ -171,7 +170,7 @@ jobs:
toolchain: stable
steps:
- if: matrix.os == 'ubuntu-20.04'
run: sudo apt-get install -y libpocl2 mesa-opencl-icd ocl-icd-opencl-dev
run: sudo apt-get install -y mesa-opencl-icd ocl-icd-opencl-dev
name: Install opencl
- if: matrix.os == 'windows-2019'
name: Install opencl
Expand Down
57 changes: 57 additions & 0 deletions ffi/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,60 @@ pub extern "C" fn set_logging_callback(
}
}
}

#[cfg(test)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i recently learned that there is an easy way to split out tests into separate file, e.g more like in golang. do you like that or not so much?

#[cfg(test)]
#[path = "log_test.rs"]
pub mod test;

and then in log_test.rs

 #[test]
fn test_c_logger() {
...
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it is possible, but by convention in Rust, the unit tests go in the same file as the implementation (https://doc.rust-lang.org/book/ch11-03-test-organization.html). I think it makes sense to keep them close, especially if the tests are simple, there aren't many of them. If there are many/they are complex, it's an indication that maybe they should be integration tests (these land in separate tests directory.

mod tests {
use log::{Level, Log, Metadata, Record};

use super::ExternCLog;

#[test]
fn test_c_logger() {
extern "C" fn cb(record: &super::ExternCRecord) {
assert_eq!(record.level, Level::Info);
assert_eq!(
unsafe {
std::str::from_utf8(std::slice::from_raw_parts(
record.message.ptr as _,
record.message.len,
))
},
Ok("Hello, world!")
);
assert_eq!(record.line, 77,);
assert_eq!(
unsafe {
std::str::from_utf8(std::slice::from_raw_parts(
record.file.ptr as _,
record.file.len,
))
},
Ok("test.rs")
);
assert_eq!(
unsafe {
std::str::from_utf8(std::slice::from_raw_parts(
record.module_path.ptr as _,
record.module_path.len,
))
},
Ok("test_module")
);
}

let logger = ExternCLog::new(Level::Info, cb);

assert!(logger.enabled(&Metadata::builder().level(Level::Info).build()));
assert!(!logger.enabled(&Metadata::builder().level(Level::Debug).build()));

logger.log(
&Record::builder()
.args(format_args!("Hello, world!"))
.level(Level::Info)
.file(Some("test.rs"))
.line(Some(77))
.module_path(Some("test_module"))
.build(),
);
}
}