Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port test-tutorial script to JS #1896

Merged
merged 14 commits into from
Mar 4, 2021
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
120 changes: 120 additions & 0 deletions tasks/test-tutorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/* eslint-env node, es6*/

import fs from 'fs'
dac09 marked this conversation as resolved.
Show resolved Hide resolved
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')

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 (process.env.CREATE_RWJS_PROJECT === '1') {
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)
}

// Clean and build framework
await execa('yarn build:clean && yarn lerna run build:js', {
cwd: frameworkPath,
shell: true,
stdio: 'inherit',
})

const packagesPath = path.join(frameworkPath, 'packages')

// Link packages from framework
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()