Skip to main content

vertigo_testing/
build.rs

1//! Builds what a suite serves: the WASM apps, with the vertigo-cli library, and the backend,
2//! with cargo.
3//!
4//! The apps are built by the same vertigo-cli the backend or [`crate::serve`] uses for SSR, so
5//! a vertigo change is always tested on both sides at once.
6//!
7//! With `E2E_SKIP_BUILD` set, nothing is built, only checked to be there from the last run.
8
9use std::path::{Path, PathBuf};
10
11use anyhow::{Context, Result, anyhow, bail, ensure};
12use vertigo_cli::{
13    BuildOpts, CommonOpts,
14    build::{self, BuildOptsInner},
15};
16
17use crate::env::Settings;
18
19/// Builds the app `package` into `dest_dir`, as `vertigo build` does.
20pub async fn vertigo_app(settings: &Settings, package: &str, dest_dir: &Path) -> Result<()> {
21    if settings.skip_build {
22        println!("E2E_SKIP_BUILD is set, reusing {}", dest_dir.display());
23    } else {
24        println!("Building {package} into {}", dest_dir.display());
25
26        let opts = BuildOpts {
27            inner: BuildOptsInner {
28                package_name: Some(package.to_string()),
29                public_path: None,
30                wasm_opt: Some(settings.wasm_opt),
31                release_mode: Some(settings.release),
32                // the suites serve the build without `--precompress`
33                precompress: Some(false),
34                wasm_run_source_map: false,
35                cargo_opts: vec![],
36            },
37            common: CommonOpts {
38                dest_dir: dest_dir.to_string_lossy().into_owned(),
39                log_local_time: None,
40            },
41        };
42
43        let package = package.to_string();
44        tokio::task::spawn_blocking(move || {
45            build::run(opts).map_err(|err| anyhow!("building {package} failed: {err:?}"))
46        })
47        .await??;
48    }
49
50    let index = dest_dir.join("index.json");
51    ensure!(index.is_file(), "{} is missing", index.display());
52    Ok(())
53}
54
55/// Builds the binary `name` of the package `name` in the workspace and returns its path.
56pub async fn cargo_bin(settings: &Settings, name: &str) -> Result<PathBuf> {
57    let bin = settings.target_dir.join(settings.profile()).join(name);
58
59    if !settings.skip_build {
60        println!("Building {name}");
61
62        let cargo = std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
63        let mut command = tokio::process::Command::new(cargo);
64        command.current_dir(&settings.workspace_root).args([
65            "build",
66            "--package",
67            name,
68            "--bin",
69            name,
70        ]);
71        // Not as the test binary's package, see `inherited_package_env`
72        for name in build::inherited_package_env() {
73            command.env_remove(name);
74        }
75        if settings.release {
76            command.arg("--release");
77        }
78
79        let status = command.status().await.context("can't run cargo")?;
80        if !status.success() {
81            bail!("building {name} failed ({status})");
82        }
83    }
84
85    ensure!(bin.is_file(), "binary {} is missing", bin.display());
86    Ok(bin)
87}