run.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env node
  2. const path = require( 'path' );
  3. const { v4: uuid } = require( 'uuid' );
  4. const os = require( 'os' );
  5. const fs = require( 'fs' );
  6. const chalk = require( 'chalk' );
  7. const childProcess = require( 'child_process' );
  8. // Config
  9. // This should be updated every time we update the WordPress packages.
  10. const GUTENBERG_VERSION = '@wordpress/block-editor@5.0.1';
  11. // Utils
  12. /**
  13. * Utility to run a child script
  14. *
  15. * @param {string} script Script to run.
  16. * @param {string=} cwd Working directory.
  17. */
  18. function runShellScript( script, cwd ) {
  19. childProcess.execSync( script, {
  20. cwd,
  21. env: {
  22. NO_CHECKS: 'true',
  23. PATH: process.env.PATH,
  24. HOME: process.env.HOME,
  25. },
  26. stdio: [ 'inherit', 'ignore', 'inherit' ],
  27. } );
  28. }
  29. /**
  30. * Small utility used to read an uncached version of a JSON file
  31. *
  32. * @param {string} fileName
  33. */
  34. function readJSONFile( fileName ) {
  35. const data = fs.readFileSync( fileName, 'utf8' );
  36. return JSON.parse( data );
  37. }
  38. /**
  39. * Generates a random temporary path in the OS's tmp dir.
  40. *
  41. * @return {string} Temporary Path.
  42. */
  43. function getRandomTemporaryPath() {
  44. return path.join( os.tmpdir(), uuid() );
  45. }
  46. // Useful constants
  47. const title = chalk.bold;
  48. const success = chalk.bold.green;
  49. const rootFolder = path.resolve( __dirname, '../../' );
  50. const testLauncherPath = getRandomTemporaryPath();
  51. const testEnvironmentPath = getRandomTemporaryPath();
  52. const sampleEnvConfig = path.resolve( __dirname, '.wp-env.sample.json' );
  53. // Welcome
  54. console.log( title( '>> 🏁 Welcome, this command is going to prepare a running WordPress environment and run the Gutenberg e2e tests against it.' ) );
  55. console.log( title( '>> It uses the current WordPress folder from which it\'s being run and a separate Gutenberg clone performed by the command.' ) );
  56. console.log( title( '>> Configuration: ' ) );
  57. console.log( 'Gutenberg Version: ' + success( GUTENBERG_VERSION ) );
  58. console.log( 'Test Launcher Path (Gutenberg): ' + success( testLauncherPath ) );
  59. console.log( 'Test Environment Path (wp-env): ' + success( testEnvironmentPath ) );
  60. // Steps
  61. // 1- Preparing the WordPress environment
  62. console.log( '>> Preparing the WordPress clone' );
  63. runShellScript( 'npm install && FORCE_REDUCED_MOTION=true npm run build', rootFolder );
  64. // 2- Preparing the Gutenberg clone
  65. // The tests and the launcher comes from the Gutenberg repository e2e tests
  66. console.log( title( '>> Preparing the e2e tests launcher' ) );
  67. runShellScript( 'git clone https://github.com/WordPress/gutenberg.git ' + testLauncherPath + ' --depth=1 --no-single-branch' );
  68. runShellScript( 'git checkout ' + GUTENBERG_VERSION, testLauncherPath );
  69. runShellScript( 'npm install && npm run build', testLauncherPath );
  70. // 3- Running the WordPress environment using wp-env
  71. // The environment should include the WordPress install and the e2e tests plugins.s
  72. console.log( title( '>> Preparing the environment' ) );
  73. runShellScript( 'mkdir -p ' + testEnvironmentPath );
  74. const envConfig = readJSONFile( sampleEnvConfig );
  75. envConfig.core = path.resolve( rootFolder, 'build' );
  76. envConfig.mappings[ 'wp-content/mu-plugins' ] = path.resolve( testLauncherPath, 'packages/e2e-tests/mu-plugins' );
  77. envConfig.mappings[ 'wp-content/plugins/test-plugins' ] = path.resolve( testLauncherPath, 'packages/e2e-tests/plugins' );
  78. fs.writeFileSync(
  79. path.resolve( testEnvironmentPath, '.wp-env.json' ),
  80. JSON.stringify( envConfig, null, '\t' ) + '\n'
  81. );
  82. // 4- Starting the environment
  83. console.log( title( '>> Starting the environment' ) );
  84. runShellScript( path.resolve( testLauncherPath, 'node_modules/.bin/wp-env' ) + ' start ', testEnvironmentPath );
  85. // 5- Running the tests
  86. console.log( title( '>> Running the e2e tests' ) );
  87. runShellScript( 'npm run test-e2e packages/e2e-tests/specs/editor', testLauncherPath );