11import assert from "node:assert/strict"
2+ import { spawnSync } from "node:child_process"
3+ import { mkdtempSync , writeFileSync } from "node:fs"
4+ import { tmpdir } from "node:os"
5+ import { dirname , join , resolve } from "node:path"
6+ import { fileURLToPath } from "node:url"
27import { DEFAULT_WORDPRESS_VERSION , buildWordPressBenchRecipe , buildWordPressPhpunitRecipe , createBenchmarkDefinitionJsonSchema , createBenchResultsJsonSchema , createWorkspaceRecipeJsonSchema , recipeCommandDefinitions } from "@automattic/wp-codebox-core"
38import Ajv2020 from "ajv/dist/2020.js"
49
10+ const root = resolve ( dirname ( fileURLToPath ( import . meta. url ) ) , ".." )
11+ const cli = resolve ( root , "packages/cli/dist/index.js" )
12+
513const recipeCommandIds = recipeCommandDefinitions ( ) . filter ( ( command ) => command . recipe ) . map ( ( command ) => command . id )
614const ajv = new Ajv2020 ( { strict : false } )
715const validate = ajv . compile ( createWorkspaceRecipeJsonSchema ( { recipeCommandIds } ) )
@@ -23,6 +31,10 @@ const phpunitRecipe = buildWordPressPhpunitRecipe({
2331 bootstrapMode : "project" ,
2432 projectBootstrap : "tests/bootstrap.php" ,
2533 multisite : true ,
34+ prepareSteps : [ {
35+ command : "host/prepare-php" ,
36+ args : [ 'input-json={"args":["bin/generate-feature-config.php"],"cwd":"/repo/demo-plugin-source"}' ] ,
37+ } ] ,
2638 mounts : [
2739 { source : "/repo/vendor" , target : "/wp-codebox-vendor" , mode : "readonly" } ,
2840 ] ,
@@ -34,6 +46,10 @@ assert.equal(buildWordPressBenchRecipe({ pluginSlug: "demo-plugin" }).runtime?.w
3446assert . equal ( phpunitRecipe . inputs ?. mounts ?. [ 0 ] ?. mode , "readwrite" )
3547assert . deepEqual ( phpunitRecipe . inputs ?. mounts ?. [ 0 ] , { source : "/repo/demo-plugin-source" , target : "/wordpress/wp-content/plugins/demo-plugin" , mode : "readwrite" } )
3648assert . deepEqual ( phpunitRecipe . inputs ?. mounts ?. [ 1 ] , { source : "/repo/vendor" , target : "/wp-codebox-vendor" , mode : "readonly" } )
49+ assert . deepEqual ( phpunitRecipe . workflow . before , [ {
50+ command : "host/prepare-php" ,
51+ args : [ 'input-json={"args":["bin/generate-feature-config.php"],"cwd":"/repo/demo-plugin-source"}' ] ,
52+ } ] )
3753assert . deepEqual ( phpunitRecipe . workflow . steps [ 0 ] ?. args , [
3854 "plugin-slug=demo-plugin" ,
3955 "cwd=tests/phpunit" ,
@@ -52,6 +68,19 @@ assert.deepEqual(phpunitRecipe.workflow.steps[0]?.args, [
5268] )
5369assert . ok ( validate ( phpunitRecipe ) , ajv . errorsText ( validate . errors ) )
5470
71+ const workspace = mkdtempSync ( join ( tmpdir ( ) , "wp-codebox-phpunit-prepare-builder-" ) )
72+ const recipePath = join ( workspace , "recipe.json" )
73+ writeFileSync ( recipePath , `${ JSON . stringify ( {
74+ schema : "wp-codebox/workspace-recipe/v1" ,
75+ workflow : {
76+ before : phpunitRecipe . workflow . before ,
77+ steps : [ { command : "wordpress.run-php" , args : [ "code=echo 'ok';" ] } ] ,
78+ } ,
79+ } , null , 2 ) } \n`)
80+ const validateRecipe = spawnSync ( process . execPath , [ cli , "recipe" , "validate" , "--recipe" , recipePath , "--json" ] , { cwd : root , encoding : "utf8" } )
81+ assert . equal ( validateRecipe . status , 0 , validateRecipe . stderr || validateRecipe . stdout )
82+ assert . equal ( JSON . parse ( validateRecipe . stdout ) . valid , true )
83+
5584const benchRecipe = buildWordPressBenchRecipe ( {
5685 wordpressVersion : "7.0" ,
5786 componentId : "demo-component" ,
0 commit comments