|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import { spawnSync } from "node:child_process" |
| 3 | +import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs" |
| 4 | +import { dirname, resolve } from "node:path" |
| 5 | +import { fileURLToPath } from "node:url" |
| 6 | + |
| 7 | +const root = resolve(dirname(fileURLToPath(import.meta.url)), "..") |
| 8 | +const cli = resolve(root, "packages/cli/dist/index.js") |
| 9 | +const artifacts = resolve(root, "artifacts/recipe-run-composer-autoload-extra-plugin-smoke") |
| 10 | +const pluginSource = resolve(artifacts, "source-plugin") |
| 11 | +const recipePath = resolve(artifacts, "recipe.json") |
| 12 | + |
| 13 | +rmSync(artifacts, { recursive: true, force: true }) |
| 14 | +mkdirSync(resolve(pluginSource, "src"), { recursive: true }) |
| 15 | + |
| 16 | +writeFileSync(resolve(pluginSource, "composer.json"), `${JSON.stringify({ |
| 17 | + name: "wp-codebox/composer-autoload-smoke", |
| 18 | + autoload: { classmap: ["src/"] }, |
| 19 | + config: { "allow-plugins": false }, |
| 20 | +}, null, 2)}\n`) |
| 21 | + |
| 22 | +writeFileSync(resolve(pluginSource, "composer-autoload-smoke.php"), `<?php |
| 23 | +/** |
| 24 | + * Plugin Name: WP Codebox Composer Autoload Smoke |
| 25 | + */ |
| 26 | +
|
| 27 | +defined( 'ABSPATH' ) || exit; |
| 28 | +
|
| 29 | +$autoload = __DIR__ . '/vendor/autoload.php'; |
| 30 | +if ( is_file( $autoload ) ) { |
| 31 | + require_once $autoload; |
| 32 | +} |
| 33 | +
|
| 34 | +register_activation_hook( __FILE__, static function (): void { |
| 35 | + if ( ! class_exists( \\WpCodeboxComposerSmoke\\Fixture::class ) ) { |
| 36 | + throw new RuntimeException( 'Composer classmap fixture was not autoloaded.' ); |
| 37 | + } |
| 38 | +
|
| 39 | + update_option( 'wp_codebox_composer_smoke_value', \\WpCodeboxComposerSmoke\\Fixture::value() ); |
| 40 | +} ); |
| 41 | +`) |
| 42 | + |
| 43 | +writeFileSync(resolve(pluginSource, "src", "Fixture.php"), `<?php |
| 44 | +
|
| 45 | +namespace WpCodeboxComposerSmoke; |
| 46 | +
|
| 47 | +final class Fixture { |
| 48 | + public static function value(): int { |
| 49 | + return 992; |
| 50 | + } |
| 51 | +} |
| 52 | +`) |
| 53 | + |
| 54 | +writeFileSync(recipePath, `${JSON.stringify({ |
| 55 | + schema: "wp-codebox/workspace-recipe/v1", |
| 56 | + runtime: { |
| 57 | + backend: "wordpress-playground", |
| 58 | + name: "recipe-run-composer-autoload-extra-plugin-smoke", |
| 59 | + wp: "7.0", |
| 60 | + blueprint: { steps: [] }, |
| 61 | + }, |
| 62 | + inputs: { |
| 63 | + extra_plugins: [ |
| 64 | + { |
| 65 | + source: pluginSource, |
| 66 | + slug: "composer-autoload-smoke", |
| 67 | + pluginFile: "composer-autoload-smoke/composer-autoload-smoke.php", |
| 68 | + }, |
| 69 | + ], |
| 70 | + }, |
| 71 | + workflow: { |
| 72 | + steps: [ |
| 73 | + { |
| 74 | + command: "wordpress.run-php", |
| 75 | + args: [ |
| 76 | + "code=if (!class_exists('WpCodeboxComposerSmoke\\\\Fixture')) { throw new RuntimeException('autoloaded class missing after plugin boot'); } echo wp_json_encode(array('value' => get_option('wp_codebox_composer_smoke_value'), 'active' => is_plugin_active('composer-autoload-smoke/composer-autoload-smoke.php')));", |
| 77 | + ], |
| 78 | + }, |
| 79 | + ], |
| 80 | + }, |
| 81 | +}, null, 2)}\n`) |
| 82 | + |
| 83 | +assert.equal(existsSync(resolve(pluginSource, "vendor", "autoload.php")), false, "fixture source should start without Composer vendor/autoload.php") |
| 84 | + |
| 85 | +const result = spawnSync(process.execPath, [ |
| 86 | + cli, |
| 87 | + "recipe-run", |
| 88 | + "--recipe", |
| 89 | + recipePath, |
| 90 | + "--artifacts", |
| 91 | + artifacts, |
| 92 | + "--json", |
| 93 | +], { cwd: root, encoding: "utf8" }) |
| 94 | + |
| 95 | +assert.equal(result.status, 0, result.stderr || result.stdout) |
| 96 | +const output = JSON.parse(result.stdout) |
| 97 | +assert.equal(output.success, true) |
| 98 | + |
| 99 | +const workflowExecution = output.executions.find((execution: { command: string; recipePhase?: string }) => execution.command === "wordpress.run-php" && execution.recipePhase === "steps") |
| 100 | +assert.ok(workflowExecution) |
| 101 | + |
| 102 | +const workflowResult = JSON.parse(workflowExecution.stdout) |
| 103 | +assert.equal(workflowResult.value, "992") |
| 104 | +assert.equal(workflowResult.active, true) |
| 105 | +assert.equal(existsSync(resolve(pluginSource, "vendor", "autoload.php")), false, "recipe-run must not mutate the caller plugin checkout") |
| 106 | +assert.equal(existsSync(resolve(pluginSource, "composer.lock")), false, "recipe-run must not write Composer lockfiles into the caller plugin checkout") |
| 107 | + |
| 108 | +console.log("recipe run Composer autoload extra plugin smoke passed") |
0 commit comments