Skip to main content

flowey_lib_hvlite/_jobs/
build_and_run_nextest_unit_tests.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Build and run the cargo-nextest based unit tests.
5
6use crate::build_nextest_unit_tests::BuildNextestUnitTestMode;
7use crate::common::CommonProfile;
8use crate::run_cargo_nextest_run::NextestProfile;
9use flowey::node::prelude::*;
10
11flowey_request! {
12    pub struct Params {
13        /// Friendly label for report JUnit test results
14        pub junit_test_label: String,
15        /// Build and run unit tests for the specified target
16        pub target: target_lexicon::Triple,
17        /// Build and run unit tests with the specified cargo profile
18        pub profile: CommonProfile,
19        /// Nextest profile to use when running the source code
20        pub nextest_profile: NextestProfile,
21
22        /// Whether the job should fail if any test has failed
23        pub fail_job_on_test_fail: bool,
24        /// If provided, also publish junit.xml test results as an artifact.
25        pub artifact_dir: Option<ReadVar<PathBuf>>,
26        pub done: WriteVar<SideEffect>,
27    }
28}
29
30new_simple_flow_node!(struct Node);
31
32impl SimpleFlowNode for Node {
33    type Request = Params;
34
35    fn imports(ctx: &mut ImportCtx<'_>) {
36        ctx.import::<crate::build_nextest_unit_tests::Node>();
37    }
38
39    fn process_request(request: Self::Request, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
40        let Params {
41            junit_test_label,
42            target,
43            profile,
44            nextest_profile,
45            fail_job_on_test_fail,
46            artifact_dir,
47            done,
48        } = request;
49
50        let (publish_done, publish_done_write) = ctx.new_var();
51        let results = ctx.reqv(|v| crate::build_nextest_unit_tests::Request {
52            profile,
53            target,
54            build_mode: BuildNextestUnitTestMode::ImmediatelyRun {
55                nextest_profile,
56                junit_test_label,
57                artifact_dir,
58                results: v,
59                publish_done: publish_done_write,
60            },
61        });
62
63        ctx.emit_rust_step("report test results to overall pipeline status", |ctx| {
64            publish_done.claim(ctx);
65            done.claim(ctx);
66
67            let results = results.clone().claim(ctx);
68            move |rt| {
69                let results = rt.read(results);
70                if results.iter().all(|r| r.all_tests_passed) {
71                    log::info!("all tests passed!");
72                } else {
73                    if fail_job_on_test_fail {
74                        anyhow::bail!("encountered test failures.")
75                    } else {
76                        log::error!("encountered test failures.")
77                    }
78                }
79
80                Ok(())
81            }
82        });
83
84        Ok(())
85    }
86}