aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/plugin/e2e_script_event_test.rs
blob: 5a0267d7f090a31124c338d01ef0602f2eef4725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! Script event E2E compatibility check: upstream Composer and Shirabe each run `dump-autoload`
//! in a fixture project whose `post-autoload-dump` script is a PHP static method, and the method
//! records what the `Composer\Script\Event` it receives exposes. Upstream has no test that reads
//! the Composer object graph out of an event from a script, so the whole fixture is
//! Shirabe-authored (`fixtures/e2e-script-event/`) and nothing has to be fetched; the test skips
//! only while the PHP runtime or the Composer checkout is missing.

use crate::e2e_extension_installer_test::{copy_dir, upstream_composer_bin};
use crate::php_worker::{lock_php_worker, php_runtime_available};
use std::path::{Path, PathBuf};
use tempfile::TempDir;

fn fixture_dir() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/plugin/fixtures/e2e-script-event")
}

struct Run {
    exit_code: i32,
    trace: String,
    stdout: String,
}

/// Runs `dump-autoload` in a fresh copy of the fixture and returns the exit code with what the
/// script wrote to its trace file and through the event's IO.
fn dump_autoload(program: &str, prefix_args: &[&str]) -> Run {
    let work = TempDir::new().unwrap();
    copy_dir(&fixture_dir(), work.path());
    let project = work.path().join("project");
    let output = std::process::Command::new(program)
        .args(prefix_args)
        .arg("dump-autoload")
        .current_dir(&project)
        .env("COMPOSER_HOME", work.path().join("home"))
        .env("COMPOSER_CACHE_DIR", work.path().join("cache"))
        .env("COMPOSER_NO_INTERACTION", "1")
        .env("COLUMNS", "120")
        .env("LINES", "30")
        .output()
        .unwrap();
    Run {
        exit_code: output.status.code().unwrap_or(-1),
        trace: std::fs::read_to_string(project.join("script-event-trace.txt")).unwrap_or_default(),
        stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
    }
}

#[test]
fn test_script_event_object_graph_matches_upstream_composer() {
    if !php_runtime_available() {
        return;
    }
    let Some(composer_bin) = upstream_composer_bin() else {
        return;
    };
    let _worker = lock_php_worker();
    let composer_bin = composer_bin.to_str().unwrap().to_string();

    let upstream = dump_autoload("php", &[composer_bin.as_str()]);
    let shirabe = dump_autoload(env!("CARGO_BIN_EXE_shirabe"), &[]);

    assert_eq!(0, upstream.exit_code, "upstream dump-autoload must succeed");
    assert_eq!(upstream.exit_code, shirabe.exit_code);
    assert_eq!(upstream.trace, shirabe.trace);

    // Pinned as well as compared, so a run where neither side dispatches the script cannot pass.
    assert_eq!(
        "\
event=post-autoload-dump
dev=0
root=shirabe/e2e-script-event
vendor-dir=vendor
bin-dir=bin
",
        upstream.trace
    );

    let io_line = |run: &Run| -> String {
        run.stdout
            .lines()
            .find(|line| line.starts_with("script-event: "))
            .unwrap_or_default()
            .to_string()
    };
    assert_eq!(io_line(&upstream), io_line(&shirabe));
    assert_eq!(
        "script-event: event=post-autoload-dump dev=0 root=shirabe/e2e-script-event \
         vendor-dir=vendor bin-dir=bin",
        io_line(&upstream)
    );
}