diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..8092b7d --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,12 @@ +use std::{ + fs::read_to_string, + path::{Path, PathBuf}, +}; + +pub fn sample_data_path() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/common/sample_data") +} + +pub fn read_sample_file(path: &Path) -> String { + return read_to_string(sample_data_path().join(path)).ok().unwrap(); +} diff --git a/tests/common/sample_data/codecov-rs-reports-json-d2a9ba1.txt b/tests/common/sample_data/codecov-rs-reports-json-d2a9ba1.txt new file mode 100644 index 0000000..e34af8b --- /dev/null +++ b/tests/common/sample_data/codecov-rs-reports-json-d2a9ba1.txt @@ -0,0 +1 @@ +{"files": {"src/report.rs": [0, [0, 45, 45, 0, 0, "100", 0, 0, 0, 0, 0, 0, 0], {"0": [0, 45, 45, 0, 0, "100"], "meta": {"session_count": 1}}, null], "src/report/models.rs": [1, [0, 5, 1, 4, 0, "20.00000", 0, 0, 0, 0, 0, 0, 0], {"0": [0, 5, 1, 4, 0, "20.00000"], "meta": {"session_count": 1}}, null], "src/report/schema.rs": [2, [0, 44, 6, 38, 0, "13.63636", 0, 0, 0, 0, 0, 0, 0], {"0": [0, 44, 6, 38, 0, "13.63636"], "meta": {"session_count": 1}}, null]}, "sessions": {"0": {"t": [3, 94, 52, 42, 0, "55.31915", 0, 0, 0, 0, 0, 0, 0], "d": 1704827412, "a": "v4/raw/2024-01-09/BD18D96000B80FA280C411B0081460E1/d2a9ba133c9b30468d97e7fad1462728571ad699/065067fe-7677-4bd8-93b2-0a8d0b879f78/340c0c0b-a955-46a0-9de9-3a9b5f2e81e2.txt", "f": [], "c": null, "n": null, "N": null, "j": "codecov-rs CI", "u": "https://github.com/codecov/codecov-rs/actions/runs/7465738121", "p": null, "e": null, "st": "uploaded", "se": {}}}} \ No newline at end of file diff --git a/tests/test_pyreport_shim.rs b/tests/test_pyreport_shim.rs new file mode 100644 index 0000000..ed8a676 --- /dev/null +++ b/tests/test_pyreport_shim.rs @@ -0,0 +1,80 @@ +use std::{collections::HashMap, path::Path}; + +use codecov_rs::{ + parsers::{pyreport_shim::parse_report_json, ParseCtx, ReportOutputStream}, + report::{models, Report, ReportBuilder, SqliteReport, SqliteReportBuilder}, +}; +use tempfile::TempDir; +use winnow::Parser; + +mod common; + +type SqliteStream<'a> = ReportOutputStream<&'a str, SqliteReport, SqliteReportBuilder>; + +struct Ctx { + _temp_dir: TempDir, + parse_ctx: ParseCtx, +} + +fn setup() -> Ctx { + let temp_dir = TempDir::new().ok().unwrap(); + let db_file = temp_dir.path().to_owned().join("db.sqlite"); + + let report_builder = SqliteReportBuilder::new(db_file); + let parse_ctx = ParseCtx::new(report_builder); + + Ctx { + _temp_dir: temp_dir, + parse_ctx, + } +} + +#[test] +fn test_parse_report_json() { + let input = common::read_sample_file(Path::new("codecov-rs-reports-json-d2a9ba1.txt")); + + let ctx = setup(); + let mut buf = SqliteStream { + input: &input, + state: ctx.parse_ctx, + }; + + let expected_files = HashMap::from([(0, 1), (1, 2), (2, 3)]); + let expected_sessions = HashMap::from([(0, 1)]); + + assert_eq!( + parse_report_json.parse_next(&mut buf), + Ok((expected_files, expected_sessions)) + ); + + let report = buf.state.report_builder.build(); + + let files = report.list_files().unwrap(); + assert_eq!( + files, + vec![ + models::SourceFile { + id: Some(1), + path: "src/report.rs".to_string(), + }, + models::SourceFile { + id: Some(2), + path: "src/report/models.rs".to_string(), + }, + models::SourceFile { + id: Some(3), + path: "src/report/schema.rs".to_string(), + }, + ] + ); + + let contexts = report.list_contexts().unwrap(); + assert_eq!( + contexts, + vec![models::Context { + id: Some(1), + context_type: models::ContextType::Upload, + name: "codecov-rs CI".to_string(), + },] + ); +}