|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import { execFile as execFileCallback } from "node:child_process" |
| 3 | +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises" |
| 4 | +import { tmpdir } from "node:os" |
| 5 | +import { join } from "node:path" |
| 6 | +import { promisify } from "node:util" |
| 7 | + |
| 8 | +import { installPluginComposerAutoloadersCode, type PreparedExtraPlugin } from "../packages/cli/src/recipe-sources.js" |
| 9 | + |
| 10 | +const execFile = promisify(execFileCallback) |
| 11 | +const root = await mkdtemp(join(tmpdir(), "wp-codebox-extra-plugin-autoloaders-")) |
| 12 | +const pluginsDir = join(root, "plugins") |
| 13 | +const muPluginsDir = join(root, "mu-plugins") |
| 14 | + |
| 15 | +function plugin(slug: string, activate: boolean): PreparedExtraPlugin { |
| 16 | + return { |
| 17 | + source: join(pluginsDir, slug), |
| 18 | + slug, |
| 19 | + target: `/wordpress/wp-content/plugins/${slug}`, |
| 20 | + pluginFile: `${slug}/${slug}.php`, |
| 21 | + activate, |
| 22 | + loadAs: "plugin", |
| 23 | + cleanupPaths: [], |
| 24 | + provenance: { kind: "local", original: join(pluginsDir, slug) }, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +try { |
| 29 | + await mkdir(join(pluginsDir, "active-provider", "vendor"), { recursive: true }) |
| 30 | + await mkdir(join(pluginsDir, "inactive-plugin", "vendor"), { recursive: true }) |
| 31 | + await mkdir(join(pluginsDir, "inactive-plugin", "tests"), { recursive: true }) |
| 32 | + await mkdir(muPluginsDir, { recursive: true }) |
| 33 | + |
| 34 | + await writeFile(join(pluginsDir, "active-provider", "vendor", "autoload.php"), "<?php function wp_codebox_active_provider_loaded() { return true; }\n") |
| 35 | + await writeFile(join(pluginsDir, "inactive-plugin", "composer.json"), JSON.stringify({ |
| 36 | + autoload: { "psr-4": { "InactivePlugin\\\\": "src/" } }, |
| 37 | + "autoload-dev": { files: ["tests/autoload-dev.php"] }, |
| 38 | + })) |
| 39 | + await writeFile(join(pluginsDir, "inactive-plugin", "tests", "autoload-dev.php"), "<?php function wp_codebox_inactive_plugin_dev_sentinel() { return true; }\n") |
| 40 | + await writeFile(join(pluginsDir, "inactive-plugin", "vendor", "autoload.php"), "<?php require_once dirname(__DIR__) . '/tests/autoload-dev.php';\n") |
| 41 | + |
| 42 | + const installCode = installPluginComposerAutoloadersCode([ |
| 43 | + plugin("active-provider", true), |
| 44 | + plugin("inactive-plugin", false), |
| 45 | + ]) |
| 46 | + assert.ok(installCode) |
| 47 | + assert.doesNotMatch(installCode, /inactive-plugin/, "inactive plugin inputs are absent from the generated preload contract") |
| 48 | + |
| 49 | + const php = ` |
| 50 | +function wp_json_encode($value, $options = 0) { return json_encode($value, $options); } |
| 51 | +define('ABSPATH', ${JSON.stringify(`${root}/`)}); |
| 52 | +define('WP_PLUGIN_DIR', ${JSON.stringify(pluginsDir)}); |
| 53 | +define('WPMU_PLUGIN_DIR', ${JSON.stringify(muPluginsDir)}); |
| 54 | +${installCode} |
| 55 | +echo "\n---RESULT---\n"; |
| 56 | +require WPMU_PLUGIN_DIR . '/wp-codebox-composer-autoloaders.php'; |
| 57 | +echo json_encode(array( |
| 58 | + 'active_provider_loaded' => function_exists('wp_codebox_active_provider_loaded'), |
| 59 | + 'inactive_dev_sentinel_loaded' => function_exists('wp_codebox_inactive_plugin_dev_sentinel'), |
| 60 | +)); |
| 61 | +` |
| 62 | + const { stdout } = await execFile("php", ["-r", php]) |
| 63 | + const [evidenceJson, resultJson] = stdout.split("\n---RESULT---\n") |
| 64 | + assert.deepEqual(JSON.parse(evidenceJson), { |
| 65 | + command: "install-composer-autoloaders", |
| 66 | + plugins: ["active-provider/active-provider.php"], |
| 67 | + autoloaders: ["active-provider/vendor/autoload.php"], |
| 68 | + loader: join(muPluginsDir, "wp-codebox-composer-autoloaders.php"), |
| 69 | + }, "setup evidence names each intentionally preloaded autoloader") |
| 70 | + assert.deepEqual(JSON.parse(resultJson), { |
| 71 | + active_provider_loaded: true, |
| 72 | + inactive_dev_sentinel_loaded: false, |
| 73 | + }) |
| 74 | + |
| 75 | + console.log("recipe extra plugin composer autoloaders ok") |
| 76 | +} finally { |
| 77 | + await rm(root, { recursive: true, force: true }) |
| 78 | +} |
0 commit comments