extension_tests.rs

  1use gh_workflow::*;
  2use indoc::indoc;
  3
  4use crate::tasks::workflows::{
  5    run_tests::{orchestrate, tests_pass},
  6    runners,
  7    steps::{self, CommonJobConditions, FluentBuilder, NamedJob, named},
  8    vars::{PathCondition, StepOutput, one_workflow_per_non_main_branch},
  9};
 10
 11pub(crate) const ZED_EXTENSION_CLI_SHA: &str = "7cfce605704d41ca247e3f84804bf323f6c6caaf";
 12
 13// This should follow the set target in crates/extension/src/extension_builder.rs
 14const EXTENSION_RUST_TARGET: &str = "wasm32-wasip2";
 15
 16// This is used by various extensions repos in the zed-extensions org to run automated tests.
 17pub(crate) fn extension_tests() -> Workflow {
 18    let should_check_rust = PathCondition::new("check_rust", r"^(Cargo.lock|Cargo.toml|.*\.rs)$");
 19    let should_check_extension = PathCondition::new("check_extension", r"^.*\.scm$");
 20
 21    let orchestrate = orchestrate(&[&should_check_rust, &should_check_extension]);
 22
 23    let jobs = [
 24        orchestrate,
 25        should_check_rust.guard(check_rust()),
 26        should_check_extension.guard(check_extension()),
 27    ];
 28
 29    let tests_pass = tests_pass(&jobs);
 30
 31    named::workflow()
 32        .add_event(Event::default().workflow_call(WorkflowCall::default()))
 33        .concurrency(one_workflow_per_non_main_branch())
 34        .add_env(("CARGO_TERM_COLOR", "always"))
 35        .add_env(("RUST_BACKTRACE", 1))
 36        .add_env(("CARGO_INCREMENTAL", 0))
 37        .add_env(("ZED_EXTENSION_CLI_SHA", ZED_EXTENSION_CLI_SHA))
 38        .add_env(("RUSTUP_TOOLCHAIN", "stable"))
 39        .add_env(("CARGO_BUILD_TARGET", EXTENSION_RUST_TARGET))
 40        .map(|workflow| {
 41            jobs.into_iter()
 42                .chain([tests_pass])
 43                .fold(workflow, |workflow, job| {
 44                    workflow.add_job(job.name, job.job)
 45                })
 46        })
 47}
 48
 49fn install_rust_target() -> Step<Run> {
 50    named::bash(format!("rustup target add {EXTENSION_RUST_TARGET}",))
 51}
 52
 53fn run_clippy() -> Step<Run> {
 54    named::bash("cargo clippy --release --all-targets --all-features -- --deny warnings")
 55}
 56
 57fn check_rust() -> NamedJob {
 58    let job = Job::default()
 59        .with_repository_owner_guard()
 60        .runs_on(runners::LINUX_LARGE)
 61        .timeout_minutes(6u32)
 62        .add_step(steps::checkout_repo())
 63        .add_step(steps::cache_rust_dependencies_namespace())
 64        .add_step(install_rust_target())
 65        .add_step(steps::cargo_fmt())
 66        .add_step(run_clippy())
 67        .add_step(steps::cargo_install_nextest())
 68        .add_step(
 69            steps::cargo_nextest(runners::Platform::Linux)
 70                // Set the target to the current platform again
 71                .with_target("$(rustc -vV | sed -n 's|host: ||p')")
 72                .add_env(("NEXTEST_NO_TESTS", "warn")),
 73        );
 74
 75    named::job(job)
 76}
 77
 78pub(crate) fn check_extension() -> NamedJob {
 79    let (cache_download, cache_hit) = cache_zed_extension_cli();
 80    let job = Job::default()
 81        .with_repository_owner_guard()
 82        .runs_on(runners::LINUX_LARGE_RAM)
 83        .timeout_minutes(4u32)
 84        .add_step(steps::checkout_repo())
 85        .add_step(cache_download)
 86        .add_step(download_zed_extension_cli(cache_hit))
 87        .add_step(check());
 88
 89    named::job(job)
 90}
 91
 92pub fn cache_zed_extension_cli() -> (Step<Use>, StepOutput) {
 93    let step = named::uses(
 94        "actions",
 95        "cache",
 96        "0057852bfaa89a56745cba8c7296529d2fc39830",
 97    )
 98    .id("cache-zed-extension-cli")
 99    .with(
100        Input::default()
101            .add("path", "zed-extension")
102            .add("key", "zed-extension-${{ env.ZED_EXTENSION_CLI_SHA }}"),
103    );
104    let output = StepOutput::new(&step, "cache-hit");
105    (step, output)
106}
107
108pub fn download_zed_extension_cli(cache_hit: StepOutput) -> Step<Run> {
109    named::bash(
110    indoc! {
111        r#"
112        wget --quiet "https://zed-extension-cli.nyc3.digitaloceanspaces.com/$ZED_EXTENSION_CLI_SHA/x86_64-unknown-linux-gnu/zed-extension"
113        chmod +x zed-extension
114        "#,
115    }
116    ).if_condition(Expression::new(format!("{} != 'true'", cache_hit.expr())))
117}
118
119pub fn check() -> Step<Run> {
120    named::bash(indoc! {
121        r#"
122        mkdir -p /tmp/ext-scratch
123        mkdir -p /tmp/ext-output
124        ./zed-extension --source-dir . --scratch-dir /tmp/ext-scratch --output-dir /tmp/ext-output
125        "#
126    })
127}