Skip to content

Commit

Permalink
complete runOne tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shazron committed Aug 1, 2023
1 parent c61e05f commit fae74a9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ governing permissions and limitations under the License.
*/

const { runAll } = require('./run')
const repositoriesJson = require('../repositories.json')

require('dotenv').config() // load .env if available

runAll()
runAll(repositoriesJson)
.catch(e => {
console.error(e)
// eslint-disable-next-line no-process-exit
Expand Down
46 changes: 29 additions & 17 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ governing permissions and limitations under the License.

const execa = require('execa')
const chalk = require('chalk').default
const repositories = require('../repositories.json')
const fs = require('fs-extra')
const auth = require('./auth')
const path = require('path')
Expand All @@ -25,25 +24,37 @@ const RES_DIR = '.repos'
*
* @param {string} name the name of the repo (key in repositories.json)
* @param {object} params the parameters for the e2e run
* @param {object} [params.mapEnv] the parameters to map
* @param {Array<string>} params.requiredEnv the required env variables to check for
* @param {Array<string>} params.doNotLog the env variables not to log values for
* @param {string} params.repository the git repository (url) to clone
* @param {string} params.branch the branch of the git repository to clone
*/
function runOne (name, params) {
console.log(chalk.blue(`> e2e tests for ${chalk.bold(name)}, repo: ${chalk.bold(params.repository)}, branch: ${chalk.bold(params.branch)}`))
const {
mapEnv,
requiredEnv,
doNotLog = [],
repository,
branch
} = params
console.log(chalk.blue(`> e2e tests for ${chalk.bold(name)}, repo: ${chalk.bold(repository)}, branch: ${chalk.bold(branch)}`))

if (params.mapEnv) {
console.log(chalk.dim(` - mapping env vars: ${chalk.bold(Object.entries(params.mapEnv).map(([k, v]) => k + '->' + v).toString())}`))
mapEnvVariables(params.mapEnv)
if (mapEnv) {
console.log(chalk.dim(` - mapping env vars: ${chalk.bold(Object.entries(mapEnv).map(([k, v]) => k + '->' + v).toString())}`))
mapEnvVariables(mapEnv)
}

console.log(chalk.dim(` - checking existance of env vars: ${chalk.bold(params.requiredEnv.toString())}`))
checkEnv(params.requiredEnv)
console.log(chalk.dim(` - checking existance of env vars: ${chalk.bold(requiredEnv.toString())}`))
checkEnv(requiredEnv)

logEnv(params.requiredEnv, params.doNotLog)
logEnv(requiredEnv, doNotLog)

console.log(chalk.dim(` - cloning repo ${chalk.bold(params.repository)}..`))
execa.sync('git', ['clone', params.repository, name], { stderr: 'inherit' })
console.log(chalk.dim(` - cloning repo ${chalk.bold(repository)}..`))
execa.sync('git', ['clone', repository, name], { stderr: 'inherit' })
process.chdir(name)
console.log(chalk.dim(` - checking out branch ${chalk.bold(params.branch)}..`))
execa.sync('git', ['checkout', params.branch], { stderr: 'inherit' })
console.log(chalk.dim(` - checking out branch ${chalk.bold(branch)}..`))
execa.sync('git', ['checkout', branch], { stderr: 'inherit' })
console.log(chalk.dim(' - installing npm packages..'))
execa.sync('npm', ['install'], { stderr: 'inherit' })
console.log(chalk.bold(' - running tests..'))
Expand All @@ -55,9 +66,10 @@ function runOne (name, params) {
/**
* Run all the e2e tests.
*
* @param {object} repositoriesJson the data on all the repositories to run the tests on
* @param {string} [artifactsDir=.repos] the folder to create all run artifacts in
*/
async function runAll (artifactsDir = RES_DIR) {
async function runAll (repositoriesJson, artifactsDir = RES_DIR) {
console.log(chalk.blue.bold(`-- e2e testing for ${Object.keys(repositories).toString()} --`))
console.log()

Expand All @@ -66,7 +78,7 @@ async function runAll (artifactsDir = RES_DIR) {
fs.emptyDirSync(artifactsDir)
process.chdir(artifactsDir)

const testsWithJwt = Object.entries(repositories).filter(([k, v]) => !v.disabled && v.requiredAuth === 'jwt').map(([k, v]) => k)
const testsWithJwt = Object.entries(repositoriesJson).filter(([k, v]) => !v.disabled && v.requiredAuth === 'jwt').map(([k, v]) => k)
if (testsWithJwt.length > 0) {
const jwtVars = ['JWT_CLIENTID', 'JWT_CLIENT_SECRET', 'JWT_PRIVATE_KEY', 'JWT_ORG_ID', 'JWT_TECH_ACC_ID']

Expand Down Expand Up @@ -96,17 +108,17 @@ async function runAll (artifactsDir = RES_DIR) {
const jwtToken = await auth.getJWTToken(options, signedJwt)
process.env.JWT_TOKEN = jwtToken.access_token
}
const testsWithOauth = Object.entries(repositories).filter(([k, v]) => !v.disabled && v.requiredAuth === 'oauth').map(([k, v]) => k)
const testsWithOauth = Object.entries(repositoriesJson).filter(([k, v]) => !v.disabled && v.requiredAuth === 'oauth').map(([k, v]) => k)
if (testsWithOauth.length > 0) {
console.log(chalk.dim(`tests '${testsWithOauth}' require OAuth`))
checkEnv(['OAUTH_TOKEN_ACTION_URL', 'OAUTH_CLIENTID'])
const oauthToken = await auth.getOauthToken(process.env.OAUTH_TOKEN_ACTION_URL)
process.env.OAUTH_TOKEN = oauthToken.access_token
}

Object.keys(repositories).forEach(k => {
Object.keys(repositoriesJson).forEach(k => {
try {
const params = repositories[k]
const params = repositoriesJson[k]
if (params.disabled) {
console.log(`skipping e2e test for ${k} (disabled)`)
} else {
Expand Down
44 changes: 44 additions & 0 deletions test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,52 @@ const {
runOne,
runAll
} = require('../src/run')
const execa = require('execa')

jest.mock('execa')
jest.mock('fs-extra')

const ORIGINAL_ENV = process.env

beforeEach(() => {
process.exit = jest.fn()
process.chdir = jest.fn()
jest.resetModules()
process.env = { ...ORIGINAL_ENV }
})

afterEach(() => {
process.exit.mockReset()
process.chdir.mockReset()
process.env = ORIGINAL_ENV
})

test('exports', () => {
expect(typeof runOne).toEqual('function')
expect(typeof runAll).toEqual('function')
})

test('runOne', () => {
process.env = { ...ORIGINAL_ENV, foo: 'bar' }

const name = 'my-repo'
const params = {
requiredEnv: ['foo'],
mapEnv: { foo: 'foo-alternate' },
repository: 'https://my-repo',
branch: 'main'
}
const execaOptions = { stderr: 'inherit' }
expect(() => runOne(name, params)).not.toThrow()
expect(execa.sync).toHaveBeenCalledWith('git', ['clone', params.repository, name], execaOptions)
expect(process.chdir).toHaveBeenCalledWith(name)
expect(execa.sync).toHaveBeenCalledWith('git', ['checkout', params.branch], execaOptions)
expect(execa.sync).toHaveBeenCalledWith('npm', ['install'], execaOptions)
expect(execa.sync).toHaveBeenCalledWith('npm', ['run', 'e2e'], execaOptions)
expect(process.chdir).toHaveBeenCalledWith('..')

expect(process.env['foo-alternate']).toEqual('bar')
})

test('runAll', () => {
})

0 comments on commit fae74a9

Please sign in to comment.