From df8d0c2f36ac42edf61d073105b205cd80d88f36 Mon Sep 17 00:00:00 2001 From: Daniel Choudhury Date: Thu, 4 Mar 2021 22:45:26 +0000 Subject: [PATCH] Port test-tutorial script to JS (#1896) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Port test-tutorial script to JS * Remove todos * PR Review comments * Apply suggestions from code review Co-authored-by: Peter Pistorius * Fix comma position * Update based on github workflow file * Additional logging by default | Throw properly in script * Adopt new logic for ☁ī¸ e2e * Dont link when not creatingProject * handle edge case reset for existing projects * fix broken linking logic Co-authored-by: Peter Pistorius Co-authored-by: David S Price --- .../cypress/integration/tutorial/tutorial.js | 9 +- tasks/test-tutorial | 60 +-------- tasks/test-tutorial.js | 122 ++++++++++++++++++ 3 files changed, 127 insertions(+), 64 deletions(-) create mode 100644 tasks/test-tutorial.js diff --git a/tasks/e2e/cypress/integration/tutorial/tutorial.js b/tasks/e2e/cypress/integration/tutorial/tutorial.js index 7d68ca6f6238..7f1f017665bc 100644 --- a/tasks/e2e/cypress/integration/tutorial/tutorial.js +++ b/tasks/e2e/cypress/integration/tutorial/tutorial.js @@ -90,7 +90,9 @@ describe('The Redwood Tutorial - Golden path edition', () => { cy.writeFile(path.join(BASE_DIR, 'api/db/schema.prisma'), Step4_1_DbSchema) cy.exec(`rm ${BASE_DIR}/api/db/dev.db`, { failOnNonZeroExit: false }) // need to also handle case where Prisma Client be out of sync - cy.exec(`cd ${BASE_DIR}; yarn rw prisma migrate reset --skip-seed --force`) + cy.exec( + `cd ${BASE_DIR}; yarn rimraf ./api/db/migrations && yarn rw prisma migrate reset --skip-seed --force` + ) cy.exec(`cd ${BASE_DIR}; yarn rw prisma migrate dev`) cy.exec(`cd ${BASE_DIR}; yarn rw g scaffold post --force`) @@ -225,10 +227,7 @@ describe('The Redwood Tutorial - Golden path edition', () => { path.join(BASE_DIR, 'web/src/pages/ContactPage/ContactPage.js'), Step7_2_ContactPage ) - cy.writeFile( - path.join(BASE_DIR, 'web/src/index.css'), - Step7_3_Css - ) + cy.writeFile(path.join(BASE_DIR, 'web/src/index.css'), Step7_3_Css) cy.contains('Contact').click() cy.contains('Save').click() diff --git a/tasks/test-tutorial b/tasks/test-tutorial index 8a3a7c6d6687..c6ba217af405 100755 --- a/tasks/test-tutorial +++ b/tasks/test-tutorial @@ -1,60 +1,2 @@ #!/bin/bash -set -e - -realpath() { - path=`eval echo "$1"` - folder=$(dirname "$path") - echo $(cd "$folder"; pwd)/$(basename "$path"); -} - -SCRIPT=`realpath $0` -SCRIPTPATH=`dirname $SCRIPT` - -# Install the dependencies required to run the e2e tests. -cd "$SCRIPTPATH/e2e" -yarn install - -if [ -z "$1" ] - then - echo "You have not supplied a path to a RedwoodJS project." - echo "We will create one for you." - echo "We will copy './packages/create-redwood-app/template' and the packages/*" - TMP_DIR=$(mktemp -d -t redwood) -else - echo "You have supplied a path $1, we will not create a new " - echo "Redwood project, we will use the app you have specified." - TMP_DIR=$1 -fi - -if [[ -z "$1" || "$CREATE_RWJS_PROJECT" == "1" ]] - then - echo "Creating new RedwoodJS Project..." - cd ../../packages/create-redwood-app - yarn babel-node src/create-redwood-app.js $TMP_DIR --no-yarn-install -fi - -if [[ -z "$1" || "$CREATE_RWJS_PROJECT" == "1" ]] - then - cd "$SCRIPTPATH/../" - # build all the packages, but not the TypeScript - # since it is slow and we do not need it. - yarn build:clean && yarn lerna run build:js -fi - -cd $TMP_DIR - -if [[ -z "$1" || "$CREATE_RWJS_PROJECT" == "1" ]]; - then - echo "Linking packages from $SCRIPTPATH" - # make the e2e tests use the packages from this Redwood Framework Repo. - ln -s "$SCRIPTPATH/../packages" -fi - -yarn install - -if [ -z "$CI" ] - then - yarn rw dev --fwd="--open=false" & cd "$SCRIPTPATH/e2e"; yarn cypress open --env RW_PATH=$TMP_DIR - else - echo "CI env-var set skipping Cypress run." -fi +yarn babel-node tasks/test-tutorial.js $1 diff --git a/tasks/test-tutorial.js b/tasks/test-tutorial.js new file mode 100644 index 000000000000..6cf002beb154 --- /dev/null +++ b/tasks/test-tutorial.js @@ -0,0 +1,122 @@ +/* eslint-env node, es6*/ + +import fs from 'fs' +import os from 'os' +import path from 'path' + +import execa from 'execa' + +const createNewRedwoodProject = async (projectPath, frameworkPath) => { + console.log( + '------------------------ start create redwood app -------------------------' + ) + await execa( + 'yarn babel-node', + ['src/create-redwood-app.js', projectPath, '--no-yarn-install'], + { + cwd: path.join(frameworkPath, 'packages/create-redwood-app'), + shell: true, + stdio: 'inherit', + } + ) +} + +const testTutorial = async () => { + // First two args are "node" and path to script + const [, , pathToProject] = process.argv + + let projectPath = pathToProject + const frameworkPath = path.join(__dirname, '..') + const e2ePath = path.join(frameworkPath, 'tasks/e2e') + const shouldCreateNewProject = process.env.CREATE_RWJS_PROJECT === '1' + + console.log(`📁 ~ projectPath`, projectPath) + console.log(`🌲 ~ frameworkPath`, frameworkPath) + console.log(`🚀 ~ e2ePath`, e2ePath) + + try { + await execa('yarn install', { + cwd: e2ePath, + shell: true, + stdio: 'inherit', + }) + + if (pathToProject) { + console.log('🗂ī¸ You have supplied the path "${projectPath}" \n') + + // For e2e tests in CI + if (shouldCreateNewProject) { + createNewRedwoodProject(projectPath, frameworkPath) + } else { + // Normally when a path is specified, no need to create a new project + console.log( + [ + 'Assuming pre-existing Redwood project', + 'Not creating a new one', + ].join('\n') + ) + } + } else { + console.log('\n ℹī¸ You have not supplied a path to a Redwood project.') + console.log('We will create one for you. \n \n') + console.log( + "📋 We will copy './packages/create-redwood-app/template' and link packages/* \n" + ) + + // Use temporary project path, because no user supplied one + projectPath = fs.mkdtempSync(path.join(os.tmpdir(), 'redwood-e2e-')) + + createNewRedwoodProject(projectPath, frameworkPath) + } + + const packagesPath = path.join(frameworkPath, 'packages') + + // Clean, Build, and Link packages from framework, but only if creating a new one + if (!pathToProject || shouldCreateNewProject) { + await execa('yarn build:clean && yarn lerna run build:js', { + cwd: frameworkPath, + shell: true, + stdio: 'inherit', + }) + + fs.symlinkSync(packagesPath, path.join(projectPath, 'packages')) + } + + await execa('yarn install', { + shell: true, + stdio: 'inherit', + cwd: projectPath, + }) + + // Make sure rw dev can run + fs.chmodSync(path.join(projectPath, 'node_modules/.bin/rw'), '755') + + if (process.env.CI) { + console.log( + '\n ⏊ Skipping cypress and dev server launch, handled by github workflow' + ) + } else { + await execa('yarn rw dev --fwd="--open=false" &', { + shell: true, + stdio: 'inherit', + cwd: projectPath, + }) + + // @Note: using env to set RW_PATH does not work correctly + await execa('yarn cypress', ['open', `--env RW_PATH=${projectPath}`], { + shell: true, + stdio: 'inherit', + env: { + ...process.env, + }, + cwd: e2ePath, + }) + } + } catch (e) { + console.error('🛑 test-tutorial script failed') + console.error(e) + process.exit(1) + } +} + +testTutorial()