Skip to content

Commit

Permalink
Port test-tutorial script to JS (#1896)
Browse files Browse the repository at this point in the history
* Port test-tutorial script to JS

* Remove todos

* PR Review comments

* Apply suggestions from code review

Co-authored-by: Peter Pistorius <peter.pistorius@gmail.com>

* 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 <peter.pistorius@gmail.com>
Co-authored-by: David S Price <thedavid@thedavidprice.com>
  • Loading branch information
3 people committed Mar 4, 2021
1 parent f39a8bb commit df8d0c2
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 64 deletions.
9 changes: 4 additions & 5 deletions tasks/e2e/cypress/integration/tutorial/tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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()
Expand Down
60 changes: 1 addition & 59 deletions tasks/test-tutorial
Original file line number Diff line number Diff line change
@@ -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
122 changes: 122 additions & 0 deletions tasks/test-tutorial.js
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit df8d0c2

Please sign in to comment.