aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/plugin/e2e_installers_test.rs
blob: 67c51b5cbd7278d3bc0f9a85544fd5093343e4cf (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//! composer/installers E2E compatibility check: upstream Composer and Shirabe each run the same
//! command sequences on a project whose packages are placed by the plugin's `LibraryInstaller`
//! subclass, and the resulting output and project trees are compared.
//!
//! The plugin is fetched by `fixtures/e2e-installers/fetch` into a git-ignored directory; the
//! test skips itself while that directory, the PHP runtime or the Composer checkout is missing.

use crate::e2e_extension_installer_test::{copy_dir, upstream_composer_bin};
use crate::plugin_installer_test::{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-installers")
}

fn plugin_fetched() -> bool {
    fixture_dir()
        .join("ext/composer-installers-2.3.0/src/Composer/Installers/Installer.php")
        .is_file()
}

/// The two implementations under comparison: upstream Composer first, Shirabe second.
fn implementations(composer_bin: &str) -> [(&str, Vec<&str>); 2] {
    [
        ("php", vec![composer_bin]),
        (env!("CARGO_BIN_EXE_shirabe"), vec![]),
    ]
}

struct CommandRun {
    exit_code: i32,
    stdout: Vec<String>,
}

fn run_command(work: &Path, program: &str, prefix_args: &[&str], args: &[&str]) -> CommandRun {
    let output = std::process::Command::new(program)
        .args(prefix_args)
        .args(args)
        .current_dir(work.join("project"))
        .env("COMPOSER_HOME", work.join("home"))
        .env("COMPOSER_CACHE_DIR", work.join("cache"))
        .env("COMPOSER_NO_INTERACTION", "1")
        .env("COLUMNS", "120")
        .env("LINES", "30")
        .output()
        .unwrap();
    CommandRun {
        exit_code: output.status.code().unwrap_or(-1),
        stdout: normalize_output(&String::from_utf8_lossy(&output.stdout), work),
    }
}

/// Output lines the two implementations must agree on: the working directory (which the plugin
/// prints absolutely when it deletes a package) is replaced by a placeholder, and progress bar
/// frames are dropped — Shirabe renders those differently from upstream for every install,
/// including projects with no plugin at all, so they say nothing about this plugin.
fn normalize_output(stdout: &str, work: &Path) -> Vec<String> {
    let work = work.to_str().unwrap();
    stdout
        .lines()
        .filter(|line| {
            let trimmed = line.trim_start();
            !(trimmed.starts_with(|c: char| c.is_ascii_digit()) && trimmed.contains('['))
        })
        .map(|line| line.replace(work, "<work>"))
        .collect()
}

/// Every file under `dir` as (relative path, contents), so two project trees compare as a whole.
fn tree(dir: &Path) -> Vec<(String, String)> {
    let mut files = Vec::new();
    collect(dir, dir, &mut files);
    files.sort();
    files
}

fn collect(root: &Path, dir: &Path, files: &mut Vec<(String, String)>) {
    for entry in std::fs::read_dir(dir).unwrap() {
        let entry = entry.unwrap();
        let path = entry.path();
        if entry.file_type().unwrap().is_dir() {
            collect(root, &path, files);
        } else {
            let relative = path
                .strip_prefix(root)
                .unwrap()
                .to_str()
                .unwrap()
                .to_string();
            files.push((relative, std::fs::read_to_string(&path).unwrap_or_default()));
        }
    }
}

fn edit(path: &Path, from: &str, to: &str) {
    let text = std::fs::read_to_string(path).unwrap();
    assert!(text.contains(from), "{path:?} does not contain {from:?}");
    std::fs::write(path, text.replace(from, to)).unwrap();
}

#[test]
fn test_composer_installers_matches_upstream_composer() {
    if !php_runtime_available() || !plugin_fetched() {
        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 mut results = Vec::new();
    for (program, prefix) in &implementations(&composer_bin) {
        let work = TempDir::new().unwrap();
        copy_dir(&fixture_dir(), work.path());
        let run = run_command(work.path(), program, prefix, &["install"]);
        let tree = tree(&work.path().join("project"));
        results.push((run, tree, work));
    }
    let [upstream, shirabe] = <[_; 2]>::try_from(results).ok().unwrap();

    assert_eq!(0, upstream.0.exit_code, "upstream install must succeed");
    assert_eq!(upstream.0.exit_code, shirabe.0.exit_code);
    assert_eq!(upstream.0.stdout, shirabe.0.stdout);

    // `acme/hello-module` and `acme/hello-theme` land under modules/ and themes/ rather than
    // vendor/, which only the plugin-provided installer's getInstallPath can decide.
    assert!(
        shirabe
            .1
            .iter()
            .any(|(path, _)| path == "modules/hello-module/composer.json"),
        "the plugin-provided installer must place the module outside vendor/"
    );
    assert_eq!(upstream.1, shirabe.1);
}

/// The rest of the installer contract: `update` reinstalls a package in place, a second `install`
/// runs over an already-installed tree, and `remove` reaches the plugin's own `uninstall()`
/// override — the one that chains onto the promise `LibraryInstaller::uninstall` returns.
#[test]
fn test_composer_installers_update_and_remove_match_upstream_composer() {
    if !php_runtime_available() || !plugin_fetched() {
        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 mut results = Vec::new();
    for (program, prefix) in &implementations(&composer_bin) {
        let work = TempDir::new().unwrap();
        copy_dir(&fixture_dir(), work.path());
        let install = run_command(work.path(), program, prefix, &["install"]);

        let module = work.path().join("packages/hello-module");
        edit(
            &module.join("composer.json"),
            "\"version\": \"1.0.0\"",
            "\"version\": \"1.1.0\"",
        );
        std::fs::write(module.join("src/hello.txt"), "hello module payload v1.1\n").unwrap();
        edit(
            &work.path().join("project/composer.json"),
            "\"acme/hello-module\": \"1.0.0\"",
            "\"acme/hello-module\": \"1.1.0\"",
        );

        let update = run_command(
            work.path(),
            program,
            prefix,
            &["update", "acme/hello-module"],
        );
        let reinstall = run_command(work.path(), program, prefix, &["install"]);
        let remove = run_command(
            work.path(),
            program,
            prefix,
            &["remove", "acme/hello-theme"],
        );
        let tree = tree(&work.path().join("project"));
        results.push((install, update, reinstall, remove, tree, work));
    }
    let [upstream, shirabe] = <[_; 2]>::try_from(results).ok().unwrap();

    for (label, upstream, shirabe) in [
        ("install", &upstream.0, &shirabe.0),
        ("update", &upstream.1, &shirabe.1),
        ("reinstall", &upstream.2, &shirabe.2),
        ("remove", &upstream.3, &shirabe.3),
    ] {
        assert_eq!(0, upstream.exit_code, "upstream {label} must succeed");
        assert_eq!(upstream.exit_code, shirabe.exit_code, "{label} exit code");
        assert_eq!(upstream.stdout, shirabe.stdout, "{label} output");
    }
    assert_eq!(upstream.4, shirabe.4);
}

/// The plugin's configuration surface: `installer-paths` in the root package's extra (both the
/// `type:` and the package-name matcher, with `{$name}` templating) and `installer-name` in the
/// installed package's own extra. Both are read back through the package proxy.
#[test]
fn test_composer_installers_custom_paths_match_upstream_composer() {
    if !php_runtime_available() || !plugin_fetched() {
        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 mut results = Vec::new();
    for (program, prefix) in &implementations(&composer_bin) {
        let work = TempDir::new().unwrap();
        copy_dir(&fixture_dir(), work.path());
        edit(
            &work.path().join("project/composer.json"),
            "    \"require\": {",
            "    \"extra\": {\n        \"installer-paths\": {\n            \
             \"web/custom/{$name}/\": [\"type:drupal-theme\"],\n            \
             \"web/mods/{$name}/\": [\"acme/hello-module\"]\n        }\n    },\n    \"require\": {",
        );
        edit(
            &work.path().join("packages/hello-module/composer.json"),
            "    \"type\": \"drupal-module\",",
            "    \"type\": \"drupal-module\",\n    \"extra\": {\"installer-name\": \"renamed-mod\"},",
        );
        let run = run_command(work.path(), program, prefix, &["install"]);
        let tree = tree(&work.path().join("project"));
        results.push((run, tree, work));
    }
    let [upstream, shirabe] = <[_; 2]>::try_from(results).ok().unwrap();

    assert_eq!(0, upstream.0.exit_code, "upstream install must succeed");
    assert_eq!(upstream.0.exit_code, shirabe.0.exit_code);
    assert_eq!(upstream.0.stdout, shirabe.0.stdout);
    for expected in [
        "web/custom/hello-theme/composer.json",
        "web/mods/renamed-mod/composer.json",
    ] {
        assert!(
            shirabe.1.iter().any(|(path, _)| path == expected),
            "expected {expected} in {:?}",
            shirabe.1.iter().map(|(path, _)| path).collect::<Vec<_>>()
        );
    }
    assert_eq!(upstream.1, shirabe.1);
}