-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python bindings for parse_pyreport and SqliteReportBuilder
- Loading branch information
1 parent
0aede3c
commit 6f2b3fa
Showing
7 changed files
with
82 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
pub use codecov_rs::error::CodecovError as RsCodecovError; | ||
use pyo3::{exceptions::PyRuntimeError, prelude::*}; | ||
|
||
pub struct PyCodecovError(RsCodecovError); | ||
|
||
impl From<PyCodecovError> for PyErr { | ||
fn from(error: PyCodecovError) -> Self { | ||
PyRuntimeError::new_err(error.0.to_string()) | ||
} | ||
} | ||
|
||
impl From<RsCodecovError> for PyCodecovError { | ||
fn from(other: RsCodecovError) -> Self { | ||
Self(other) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,41 @@ | ||
use std::{fs::File, path::PathBuf}; | ||
|
||
use codecov_rs::{parsers, report}; | ||
use pyo3::prelude::*; | ||
|
||
// See if non-pyo3-annotated Rust lines are still instrumented | ||
fn raw_rust_add(a: usize, b: usize) -> usize { | ||
println!("hello"); | ||
a + b | ||
} | ||
use crate::error::PyCodecovError; | ||
|
||
mod error; | ||
|
||
#[pyclass] | ||
pub struct SqliteReportBuilder(report::SqliteReportBuilder); | ||
|
||
#[pymethods] | ||
impl SqliteReportBuilder { | ||
pub fn filepath(&self) -> PyResult<&PathBuf> { | ||
Ok(&self.0.filename) | ||
} | ||
|
||
#[staticmethod] | ||
#[pyo3(signature = (report_json_filepath, chunks_filepath, out_path))] | ||
pub fn from_pyreport( | ||
report_json_filepath: &str, | ||
chunks_filepath: &str, | ||
out_path: &str, | ||
) -> PyResult<SqliteReportBuilder> { | ||
let mut report_builder = | ||
report::SqliteReportBuilder::open(out_path.into()).map_err(PyCodecovError::from)?; | ||
|
||
#[pyfunction] | ||
fn dummy_add(a: usize, b: usize) -> PyResult<usize> { | ||
Ok(raw_rust_add(a, b)) | ||
let report_json_file = File::open(report_json_filepath)?; | ||
let chunks_file = File::open(chunks_filepath)?; | ||
parsers::pyreport::parse_pyreport(&report_json_file, &chunks_file, &mut report_builder) | ||
.map_err(PyCodecovError::from)?; | ||
Ok(SqliteReportBuilder(report_builder)) | ||
} | ||
} | ||
|
||
#[pymodule] | ||
fn _bindings(_py: Python, m: &Bound<PyModule>) -> PyResult<()> { | ||
m.add_function(wrap_pyfunction!(dummy_add, m)?)?; | ||
m.add_class::<SqliteReportBuilder>()?; | ||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from ._bindings import SqliteReportBuilder | ||
|
||
SqliteReportBuilder.__module__ = __name__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class SqliteReportBuilder: | ||
@staticmethod | ||
def from_pyreport( | ||
report_json_filepath: str, chunks_filepath: str, out_filepath: str | None = None | ||
) -> SqliteReportBuilder: ... | ||
def filepath(self) -> str: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,26 @@ | ||
def test_dummy_add(): | ||
from codecov_rs.dummy_add import dummy_add | ||
from pathlib import Path | ||
from tempfile import NamedTemporaryFile | ||
|
||
assert dummy_add(3, 4) == 7 | ||
from codecov_rs.report import SqliteReportBuilder | ||
|
||
PROJECT_ROOT = Path(__file__).parent.parent.parent | ||
|
||
|
||
def get_fixture_path(path_from_root: str) -> str: | ||
return str(PROJECT_ROOT / path_from_root) | ||
|
||
|
||
def test_from_pyreport(): | ||
report_json_filepath = get_fixture_path( | ||
"test_utils/fixtures/pyreport/codecov-rs-reports-json-d2a9ba1.txt" | ||
) | ||
chunks_filepath = get_fixture_path( | ||
"test_utils/fixtures/pyreport/codecov-rs-chunks-d2a9ba1.txt" | ||
) | ||
|
||
with NamedTemporaryFile() as out_file: | ||
report_builder = SqliteReportBuilder.from_pyreport( | ||
report_json_filepath, chunks_filepath, out_file.name | ||
) | ||
print(report_builder.filepath()) | ||
assert report_builder.filepath() is not None |