Skip to content

Commit

Permalink
Merge pull request #781 from malept/use-ava
Browse files Browse the repository at this point in the history
Use AVA instead of tape
  • Loading branch information
malept committed Jan 1, 2018
2 parents 18f0650 + e266525 commit bc2080c
Show file tree
Hide file tree
Showing 21 changed files with 1,178 additions and 1,489 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,6 +3,7 @@
*.key
*.pem
node_modules
test/fixtures/basic/main-link.js
test/work
.DS_Store
.nyc_output
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
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
19 changes: 11 additions & 8 deletions package.json
Expand Up @@ -37,19 +37,19 @@
"yargs-parser": "^8.0.0"
},
"devDependencies": {
"ava": "^0.24.0",
"buffer-equal": "^1.0.0",
"coveralls": "^3.0.0",
"eslint": "^4.1.0",
"eslint-config-standard": "^10.0.0",
"eslint-plugin-ava": "^4.3.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-node": "^5.1.0",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.0",
"eslint-plugin-tape": "^1.1.0",
"nyc": "^11.0.0",
"pkg-up": "^2.0.0",
"rimraf": "^2.3.2",
"tape": "^4.0.0",
"tempy": "^0.2.1",
"which": "^1.2.14"
},
"engines": {
Expand All @@ -58,25 +58,27 @@
"scripts": {
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"lint": "eslint .",
"pretest": "rimraf test/work",
"test": "npm run lint && nyc tape test"
"test": "npm run lint && nyc ava test/index.js"
},
"directories": {
"test": "test"
},
"keywords": [],
"ava": {
"timeout": "60s"
},
"eslintConfig": {
"extends": [
"plugin:ava/recommended",
"plugin:promise/recommended",
"plugin:tape/recommended",
"standard"
],
"parserOptions": {
"sourceType": "script"
},
"plugins": [
"promise",
"tape"
"ava",
"promise"
],
"rules": {
"indent": [
Expand All @@ -89,6 +91,7 @@
"SwitchCase": 1
}
],
"ava/prefer-async-await": 0,
"strict": "error"
}
}
Expand Down
86 changes: 86 additions & 0 deletions test/_setup.js
@@ -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
}
117 changes: 117 additions & 0 deletions test/_util.js
@@ -0,0 +1,117 @@
'use strict'

// Keeping this module because it handles non-buffers gracefully
const bufferEqual = require('buffer-equal')
const common = require('../common')
const config = require('./config.json')
const fs = require('fs-extra')
const packager = require('../index')
const path = require('path')
const setup = require('./_setup')
const tempy = require('tempy')
const test = require('ava')

const ORIGINAL_CWD = process.cwd()

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(setup.WORK_CWD)
})

test.beforeEach(t => {
t.context.workDir = tempy.directory()
t.context.tempDir = tempy.directory()
})

test.afterEach.always(t => {
return fs.remove(t.context.workDir)
.then(() => fs.remove(t.context.tempDir))
})

function testSinglePlatform (name, testFunction, testFunctionArgs, parallel) {
module.exports.packagerTest(name, (t, opts) => {
Object.assign(opts, module.exports.singlePlatformOptions())
return testFunction.apply(null, [t, opts].concat(testFunctionArgs))
}, parallel)
}

module.exports = {
allPlatformArchCombosCount: 8,
areFilesEqual: function areFilesEqual (file1, file2) {
let buffer1, buffer2

return fs.readFile(file1)
.then((data) => {
buffer1 = data
return fs.readFile(file2)
}).then((data) => {
buffer2 = data
return bufferEqual(buffer1, buffer2)
})
},
fixtureSubdir: setup.fixtureSubdir,
generateResourcesPath: function generateResourcesPath (opts) {
return common.isPlatformMac(opts.platform)
? path.join(opts.name + '.app', 'Contents', 'Resources')
: 'resources'
},
invalidOptionTest: function invalidOptionTest (opts) {
return t => t.throws(packager(opts))
},
packageAndEnsureResourcesPath: function packageAndEnsureResourcesPath (t, opts) {
let resourcesPath

return packager(opts)
.then(paths => {
resourcesPath = path.join(paths[0], module.exports.generateResourcesPath(opts))
return fs.stat(resourcesPath)
}).then(stats => {
t.true(stats.isDirectory(), 'The output directory should contain the expected resources subdirectory')
return resourcesPath
})
},
packagerTest: function packagerTest (name, testFunction, parallel) {
const testDefinition = parallel ? test : test.serial
testDefinition(name, t => {
return testFunction(t, {
name: 'packagerTest',
out: t.context.workDir,
tmpdir: t.context.tempDir
})
})
},
singlePlatformOptions: function singlePlatformOptions () {
return {
platform: 'linux',
arch: 'x64',
electronVersion: config.version
}
},
// Rest parameters are added (not behind a feature flag) in Node 6
testSinglePlatform: function (name, testFunction /*, ...testFunctionArgs */) {
const testFunctionArgs = Array.prototype.slice.call(arguments, 2)
return testSinglePlatform(name, testFunction, testFunctionArgs, false)
},
// Rest parameters are added (not behind a feature flag) in Node 6
testSinglePlatformParallel: function (name, testFunction /*, ...testFunctionArgs */) {
const testFunctionArgs = Array.prototype.slice.call(arguments, 2)
return testSinglePlatform(name, testFunction, testFunctionArgs, true)
},
verifyPackageExistence: function verifyPackageExistence (finalPaths) {
return Promise.all(finalPaths.map((finalPath) => {
return fs.stat(finalPath)
.then(
stats => stats.isDirectory(),
() => false
)
}))
}
}

0 comments on commit bc2080c

Please sign in to comment.