Skip to content

Commit

Permalink
Move testsuite setup outside of AVA for CI
Browse files Browse the repository at this point in the history
  • Loading branch information
malept committed Jan 1, 2018
1 parent 363f637 commit e266525
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 75 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ before_install: test/ci/before_install.sh
install:
- npm install
- npm update
before_script: test/ci/_before_script.js
after_success: npm run coveralls
branches:
only:
Expand Down
86 changes: 86 additions & 0 deletions test/_setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict'

const common = require('../common')
const config = require('./config.json')
const exec = require('mz/child_process').exec
const fs = require('fs-extra')
const os = require('os')
const path = require('path')
const targets = require('../targets')

function fixtureSubdir (subdir) {
return path.join(__dirname, 'fixtures', subdir)
}

function downloadAll (version) {
console.log(`Calling electron-download for ${version} before running tests...`)
const combinations = common.createDownloadCombos({electronVersion: config.version, all: true}, targets.officialPlatforms, targets.officialArchs, (platform, arch) => {
// Skip testing darwin/mas target on Windows since electron-packager itself skips it
// (see https://github.com/electron-userland/electron-packager/issues/71)
return common.isPlatformMac(platform) && process.platform === 'win32'
})

return Promise.all(combinations.map(combination => {
return common.downloadElectronZip(Object.assign({}, combination, {
cache: path.join(os.homedir(), '.electron'),
quiet: !!process.env.CI,
version: version
}))
}))
}

// Download all Electron distributions before running tests to avoid timing out due to network
// speed. Most tests run with the config.json version, but we have some tests using 0.37.4, and an
// electron module specific test using 1.3.1.
function preDownloadElectron () {
const versions = [
config.version,
'0.37.4',
'1.3.1'
]
return Promise.all(versions.map(downloadAll))
}

function npmInstallForFixture (fixture) {
const fixtureDir = fixtureSubdir(fixture)
return fs.exists(path.join(fixtureDir, 'node_modules'))
.then(exists => {
if (exists) {
return true
} else {
console.log(`Running npm install in fixtures/${fixture}...`)
return exec('npm install --no-bin-links', {cwd: fixtureDir})
}
})
}

function npmInstallForFixtures () {
const fixtures = [
'basic',
'basic-renamed-to-electron',
'infer-missing-version-only',
'el-0374'
]
return Promise.all(fixtures.map(npmInstallForFixture))
}

const WORK_CWD = path.join(__dirname, 'work')

function ensureEmptyWorkDirExists () {
return fs.remove(WORK_CWD)
.then(() => fs.mkdirs(WORK_CWD))
}

module.exports = {
fixtureSubdir: fixtureSubdir,
setupTestsuite: function setupTestsuite () {
return preDownloadElectron()
.then(npmInstallForFixtures)
.catch(error => {
console.error(error.stack || error)
return process.exit(1)
})
.then(ensureEmptyWorkDirExists)
},
WORK_CWD: WORK_CWD
}
85 changes: 10 additions & 75 deletions test/_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,26 @@
const bufferEqual = require('buffer-equal')
const common = require('../common')
const config = require('./config.json')
const exec = require('mz/child_process').exec
const fs = require('fs-extra')
const os = require('os')
const packager = require('../index')
const path = require('path')
const targets = require('../targets')
const setup = require('./_setup')
const tempy = require('tempy')
const test = require('ava')

function downloadAll (version) {
console.log(`Calling electron-download for ${version} before running tests...`)
const combinations = common.createDownloadCombos({electronVersion: config.version, all: true}, targets.officialPlatforms, targets.officialArchs, (platform, arch) => {
// Skip testing darwin/mas target on Windows since electron-packager itself skips it
// (see https://github.com/electron-userland/electron-packager/issues/71)
return common.isPlatformMac(platform) && process.platform === 'win32'
})

return Promise.all(combinations.map(combination => {
return common.downloadElectronZip(Object.assign({}, combination, {
cache: path.join(os.homedir(), '.electron'),
quiet: !!process.env.CI,
version: version
}))
}))
}

// Download all Electron distributions before running tests to avoid timing out due to network
// speed. Most tests run with the config.json version, but we have some tests using 0.37.4, and an
// electron module specific test using 1.3.1.
function preDownloadElectron () {
const versions = [
config.version,
'0.37.4',
'1.3.1'
]
return Promise.all(versions.map(downloadAll))
}

function npmInstallForFixture (fixture) {
const fixtureDir = module.exports.fixtureSubdir(fixture)
return fs.exists(path.join(fixtureDir, 'node_modules'))
.then(exists => {
if (exists) {
return true
} else {
console.log(`Running npm install in fixtures/${fixture}...`)
return exec('npm install --no-bin-links', {cwd: fixtureDir})
}
})
}

function npmInstallForFixtures () {
const fixtures = [
'basic',
'basic-renamed-to-electron',
'infer-missing-version-only',
'el-0374'
]
return Promise.all(fixtures.map(npmInstallForFixture))
}

const ORIGINAL_CWD = process.cwd()
const WORK_CWD = path.join(__dirname, 'work')

function ensureEmptyWorkDirExists () {
return fs.remove(WORK_CWD)
.then(() => fs.mkdirs(WORK_CWD))
.then(() => process.chdir(WORK_CWD))
}

test.before(t =>
preDownloadElectron()
.then(npmInstallForFixtures)
.catch(error => {
console.error(error.stack || error)
return process.exit(1)
})
.then(ensureEmptyWorkDirExists)
)
test.before(t => {
if (!process.env.CI) {
return setup.setupTestsuite()
.then(() => process.chdir(setup.WORK_CWD))
}
return Promise.resolve(process.chdir(setup.WORK_CWD))
})

test.after.always(t => {
process.chdir(ORIGINAL_CWD)
return fs.remove(WORK_CWD)
return fs.remove(setup.WORK_CWD)
})

test.beforeEach(t => {
Expand Down Expand Up @@ -120,9 +57,7 @@ module.exports = {
return bufferEqual(buffer1, buffer2)
})
},
fixtureSubdir: function fixtureSubdir (subdir) {
return path.join(__dirname, 'fixtures', subdir)
},
fixtureSubdir: setup.fixtureSubdir,
generateResourcesPath: function generateResourcesPath (opts) {
return common.isPlatformMac(opts.platform)
? path.join(opts.name + '.app', 'Contents', 'Resources')
Expand Down
5 changes: 5 additions & 0 deletions test/ci/_before_script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

'use strict'

require('../_setup').setupTestsuite()
1 change: 1 addition & 0 deletions test/ci/appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ install:
test_script:
- node --version
- npm --version
- node test/ci/_before_script.js
- npm test

build: off

0 comments on commit e266525

Please sign in to comment.