Skip to content

Commit

Permalink
python bindings for parse_pyreport and SqliteReportBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-codecov committed Sep 13, 2024
1 parent 0aede3c commit 6f2b3fa
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 18 deletions.
16 changes: 16 additions & 0 deletions bindings/src/error.rs
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)
}
}
41 changes: 32 additions & 9 deletions bindings/src/lib.rs
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(())
}
3 changes: 0 additions & 3 deletions python/codecov_rs/dummy_add.py

This file was deleted.

3 changes: 0 additions & 3 deletions python/codecov_rs/dummy_add.pyi

This file was deleted.

3 changes: 3 additions & 0 deletions python/codecov_rs/report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from ._bindings import SqliteReportBuilder

SqliteReportBuilder.__module__ = __name__
6 changes: 6 additions & 0 deletions python/codecov_rs/report.pyi
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: ...
28 changes: 25 additions & 3 deletions python/tests/test_lib.py
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

0 comments on commit 6f2b3fa

Please sign in to comment.