Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ checkout keeps 8888/8889, linked worktrees derive their own pair, and explicit
`WP_ENV_PORT` / `WP_ENV_TESTS_PORT` always win. Several branches can run their
own WordPress at the same time.

`nk-activate-plugins` activates the list in `nkPluginToolkit.plugins`, in order,
in both environments, retrying while the site is still coming up. Wire it into
`nk-activate-plugins` runs `wp core update-db`, then activates the list in
`nkPluginToolkit.plugins`, in order, in both environments, retrying while the
site is still coming up. The database upgrade matters after a switch of the
WordPress core version, when wp-admin redirects to `upgrade.php`. Wire it into
`.wp-env.json`:

```json
Expand Down
18 changes: 14 additions & 4 deletions scripts/activate-plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env node

/**
* Activates the plugins in both `wp-env` environments after a start.
* Upgrades the database and activates the plugins in both `wp-env`
* environments after a start.
*
* Plugins are mounted through `mappings` in `.wp-env.json` rather than through
* `plugins`, so that each directory inside WordPress keeps a fixed name no
Expand Down Expand Up @@ -171,9 +172,18 @@ function activateIn(container) {
wpCli(container, ['plugin', 'deactivate', ...plugins]);
}

const activated = wpCli(container, ['plugin', 'activate', ...plugins]);

if (activated.status === 0) {
// After a switch of the WordPress core version, wp-admin redirects to
// upgrade.php until the database is upgraded, and REST calls from the
// E2E setup (activateTheme) fail. A no-op when the database is current.
const upgraded = wpCli(container, ['core', 'update-db', '--quiet']);
const activated =
upgraded.status === 0
? wpCli(container, ['plugin', 'activate', ...plugins])
: null;

if (!activated) {
output = `wp core update-db failed: ${upgraded.output}`;
} else if (activated.status === 0) {
// `wp plugin activate` can report success for the set while an
// individual plugin stayed inactive, so confirm rather than trust.
const missing = findInactive(container);
Expand Down
45 changes: 45 additions & 0 deletions scripts/self-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,51 @@ check('slow tests are reported', () => {
);
});

check('activation upgrades the database first, in both sites', () => {
const os = require('node:os');
const { spawnSync } = require('node:child_process');
const project = fs.mkdtempSync(path.join(os.tmpdir(), 'nk-activate-'));
const bin = path.join(project, 'node_modules', '.bin');
const log = path.join(project, 'calls.log');

fs.mkdirSync(bin, { recursive: true });
fs.writeFileSync(
path.join(project, 'package.json'),
JSON.stringify({ nkPluginToolkit: { plugins: ['my-plugin'] } })
);
// Records each call; `plugin list` answers with the plugin active.
fs.writeFileSync(
path.join(bin, 'wp-env'),
`#!/bin/sh\necho "$*" >> "${log}"\ncase "$*" in *"plugin list"*) echo '["my-plugin"]';; esac\n`,
{ mode: 0o755 }
);

try {
const result = spawnSync(
process.execPath,
[path.join(root, 'scripts', 'activate-plugin.js')],
{ cwd: project, encoding: 'utf-8' }
);

assert.equal(result.status, 0, result.stderr);

const calls = fs
.readFileSync(log, 'utf-8')
.trim()
.split('\n')
.filter((line) => !line.includes('plugin list'));

assert.deepEqual(calls, [
'run cli wp core update-db --quiet',
'run cli wp plugin activate my-plugin',
'run tests-cli wp core update-db --quiet',
'run tests-cli wp plugin activate my-plugin',
]);
} finally {
fs.rmSync(project, { recursive: true, force: true });
}
});

check('every runtime require is a declared dependency', () => {
const declared = new Set([
...Object.keys(pkg.dependencies || {}),
Expand Down
Loading