Skip to content

Commit

Permalink
Add tests for pnpm and yarn (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinland authored and bcomnes committed Oct 4, 2023
1 parent 6cba3d7 commit b2a483f
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 146 deletions.
19 changes: 17 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,30 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node: ['lts/*', 18]
package-manager: ['npm', 'pnpm', 'yarn']

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- run: npm i
- run: npm test
- name: Install pnpm
if: ${{ matrix.package-manager == 'pnpm' }}
uses: pnpm/action-setup@v2
with:
version: 8
- name: Install dependencies via yarn
if: ${{ matrix.package-manager == 'yarn' }}
uses: bahmutov/npm-install@v1
with:
useLockFile: false
useYarn: true
install-command: yarn install
- name: Install dependencies
if: ${{ matrix.package-manager != 'yarn' }}
run: ${{ matrix.package-manager }} install
- run: ${{ matrix.package-manager }} run test
- uses: codecov/codecov-action@v3
if: ${{ github.event_name == 'pull_request' }}
with:
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enable-pre-post-scripts=true
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
"clean": "rm -rf coverage jsdoc \"test-workspace/{build,test.txt}\"",
"docs": "jsdoc -c jsdoc.json",
"lint": "standard",
"pretest": "node scripts/make-slink.js && npm run lint",
"test": "c8 npm run _mocha",
"watch": "npm run _mocha -- --watch --growl",
"pretest": "node scripts/make-slink.js && yarpm run lint",
"test": "c8 yarpm run _mocha",
"watch": "yarpm run _mocha -- --watch --growl",
"version": "auto-changelog -p --template keepachangelog auto-changelog --breaking-pattern 'BREAKING CHANGE:' && git add CHANGELOG.md",
"preversion": "npm test",
"preversion": "yarpm test",
"postversion": "git push --follow-tags && gh-release -y"
},
"dependencies": {
Expand All @@ -46,8 +46,10 @@
"jsdoc": "^4.0.0",
"mocha": "^10.0.0",
"p-queue": "^7.3.4",
"rimraf": "^3.0.2",
"standard": "^17.1.0",
"yarn": "^1.12.3",
"standard": "^17.1.0"
"yarpm": "^1.2.0"
},
"repository": {
"type": "git",
Expand Down
5 changes: 1 addition & 4 deletions test/aggregate-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
const assert = require('assert').strict
const nodeApi = require('../lib')
const BufferStream = require('./lib/buffer-stream')
const util = require('./lib/util')
const runAll = util.runAll
const runPar = util.runPar
const runSeq = util.runSeq
const { runAll, runPar, runSeq } = require('./lib/util')

// ------------------------------------------------------------------------------
// Test
Expand Down
7 changes: 1 addition & 6 deletions test/argument-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@

const assert = require('assert').strict
const nodeApi = require('../lib')
const util = require('./lib/util')
const result = util.result
const removeResult = util.removeResult
const runAll = util.runAll
const runPar = util.runPar
const runSeq = util.runSeq
const { it, result, removeResult, runAll, runPar, runSeq } = require('./lib/util')

// ------------------------------------------------------------------------------
// Test
Expand Down
11 changes: 3 additions & 8 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@
const assert = require('assert').strict
const nodeApi = require('../lib')
const BufferStream = require('./lib/buffer-stream')
const util = require('./lib/util')
const result = util.result
const removeResult = util.removeResult
const runAll = util.runAll
const runPar = util.runPar
const runSeq = util.runSeq
const { describe, isPnpm, result, removeResult, runAll, runPar, runSeq } = require('./lib/util')

// ------------------------------------------------------------------------------
// Test
Expand Down Expand Up @@ -221,14 +216,14 @@ describe('[common]', () => {
})
})

describe('should be able to use `restart` built-in task:', () => {
describe.npmOnly('should be able to use `restart` built-in task:', () => {
it('Node API', () => nodeApi('restart'))
it('npm-run-all command', () => runAll(['restart']))
it('run-s command', () => runSeq(['restart']))
it('run-p command', () => runPar(['restart']))
})

describe('should be able to use `env` built-in task:', () => {
describe.skip.if(isPnpm())('should be able to use `env` built-in task:', () => {
it('Node API', () => nodeApi('env'))
it('npm-run-all command', () => runAll(['env']))
it('run-s command', () => runSeq(['env']))
Expand Down
9 changes: 2 additions & 7 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@

const assert = require('assert').strict
const nodeApi = require('../lib')
const util = require('./lib/util')
const result = util.result
const removeResult = util.removeResult
const runAll = util.runAll
const runPar = util.runPar
const runSeq = util.runSeq
const { describe, isPnpm, result, removeResult, runAll, runPar, runSeq } = require('./lib/util')

// ------------------------------------------------------------------------------
// Test
// ------------------------------------------------------------------------------

describe('[config] it should have an ability to set config variables:', () => {
describe.skip.if(isPnpm())('[config] it should have an ability to set config variables:', () => {
before(() => process.chdir('test-workspace'))
after(() => process.chdir('..'))

Expand Down
7 changes: 1 addition & 6 deletions test/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@

const assert = require('assert').strict
const nodeApi = require('../lib')
const util = require('./lib/util')
const delay = util.delay
const removeResult = util.removeResult
const runAll = util.runAll
const runPar = util.runPar
const runSeq = util.runSeq
const { delay, removeResult, runAll, runPar, runSeq } = require('./lib/util')

// ------------------------------------------------------------------------------
// Helpers
Expand Down
33 changes: 33 additions & 0 deletions test/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-env mocha */
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
Expand All @@ -9,6 +10,7 @@
// Requirements
// ------------------------------------------------------------------------------

const { describe, it } = require('mocha')
const cp = require('child_process')
const fs = require('fs')
const path = require('path')
Expand All @@ -23,6 +25,37 @@ const NPM_RUN_ALL = path.resolve(__dirname, '../../bin/npm-run-all/index.js')
const RUN_P = path.resolve(__dirname, '../../bin/run-p/index.js')
const RUN_S = path.resolve(__dirname, '../../bin/run-s/index.js')

/**
* Determines whether we are running in npm, pnpm, or yarn
*/
function getPackageManagerName () {
const npmPath = process.env.npm_execpath
if (npmPath) {
const basename = path.basename(npmPath)
if (['npm', 'npx'].some(cmd => basename.startsWith(cmd))) {
return 'npm'
} else if (['pnpm', 'pnpx'].some(cmd => basename.startsWith(cmd))) {
return 'pnpm'
} else if (basename.startsWith('yarn')) {
return 'yarn'
}
}
throw new Error('Unable to determine what package manager is in use')
}

// Augment mocha's describe and it with 'skip' and 'npmOnly'
const isNpm = () => getPackageManagerName() === 'npm'
describe.npmOnly = isNpm() ? it : it.skip
describe.skip.if = (condition) => condition ? describe.skip : describe
it.npmOnly = isNpm() ? describe : describe.skip
it.skip.if = (condition) => condition ? it.skip : it
module.exports.getPackageManagerName = getPackageManagerName
module.exports.isNpm = isNpm
module.exports.isPnpm = () => getPackageManagerName() === 'pnpm'
module.exports.isYarn = () => getPackageManagerName() === 'yarn'
module.exports.describe = describe
module.exports.it = it

/**
* Spawns the given script with the given arguments.
*
Expand Down
5 changes: 1 addition & 4 deletions test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
// ------------------------------------------------------------------------------

const assert = require('assert').strict
const util = require('./lib/util')
const result = util.result
const removeResult = util.removeResult
const runAll = util.runAll
const { result, removeResult, runAll } = require('./lib/util')

// ------------------------------------------------------------------------------
// Test
Expand Down
142 changes: 67 additions & 75 deletions test/package-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,104 +13,96 @@
const { execSync } = require('child_process')
const assert = require('assert').strict
const nodeApi = require('../lib')
const util = require('./lib/util')
const result = util.result
const removeResult = util.removeResult
const runAll = util.runAll
const runPar = util.runPar
const runSeq = util.runSeq
const { isNpm, it, result, removeResult, runAll, runPar, runSeq } = require('./lib/util')

// ------------------------------------------------------------------------------
// Test
// ------------------------------------------------------------------------------

describe("[package-config] it should have an ability to overwrite package's config:", () => {
const [major] = execSync('yarpm --version', { encoding: 'utf8' }).trim().split('.')
const supportsOverrides = major <= 6 && !isNpm()

it.skip.if(!supportsOverrides)("[package-config] it should have an ability to overwrite package's config:", () => {
before(() => process.chdir('test-workspace'))
after(() => process.chdir('..'))

beforeEach(removeResult)

const [major] = execSync('npm --version', { encoding: 'utf8' }).trim().split('.')
it('Node API should address "packageConfig" option', async () => {
await nodeApi('test-task:package-config', { packageConfig: { 'npm-run-all-test': { test: 'OVERWRITTEN' } } })
assert.equal(result(), 'OVERWRITTEN')
})

it('Node API should address "packageConfig" option for multiple variables', async () => {
await nodeApi('test-task:package-config2', { packageConfig: { 'npm-run-all-test': { test: '1', test2: '2', test3: '3' } } })
assert.equal(result(), '1\n2\n3')
})

describe('CLI commands should address "--a:b=c" style options', () => {
it('npm-run-all command', async () => {
await runAll(['test-task:package-config', '--npm-run-all-test:test=OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})

const supportsOverrides = major <= 6
it('run-s command', async () => {
await runSeq(['test-task:package-config', '--npm-run-all-test:test=OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})

if (supportsOverrides) {
it('Node API should address "packageConfig" option', async () => {
await nodeApi('test-task:package-config', { packageConfig: { 'npm-run-all-test': { test: 'OVERWRITTEN' } } })
it('run-p command', async () => {
await runPar(['test-task:package-config', '--npm-run-all-test:test=OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})
})

it('Node API should address "packageConfig" option for multiple variables', async () => {
await nodeApi('test-task:package-config2', { packageConfig: { 'npm-run-all-test': { test: '1', test2: '2', test3: '3' } } })
describe('CLI commands should address "--a:b=c" style options for multiple variables', () => {
it('npm-run-all command', async () => {
await runAll(['test-task:package-config2', '--npm-run-all-test:test=1', '--npm-run-all-test:test2=2', '--npm-run-all-test:test3=3'])
assert.equal(result(), '1\n2\n3')
})

describe('CLI commands should address "--a:b=c" style options', () => {
it('npm-run-all command', async () => {
await runAll(['test-task:package-config', '--npm-run-all-test:test=OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})

it('run-s command', async () => {
await runSeq(['test-task:package-config', '--npm-run-all-test:test=OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})

it('run-p command', async () => {
await runPar(['test-task:package-config', '--npm-run-all-test:test=OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})
it('run-s command', async () => {
await runSeq(['test-task:package-config2', '--npm-run-all-test:test=1', '--npm-run-all-test:test2=2', '--npm-run-all-test:test3=3'])
assert.equal(result(), '1\n2\n3')
})

describe('CLI commands should address "--a:b=c" style options for multiple variables', () => {
it('npm-run-all command', async () => {
await runAll(['test-task:package-config2', '--npm-run-all-test:test=1', '--npm-run-all-test:test2=2', '--npm-run-all-test:test3=3'])
assert.equal(result(), '1\n2\n3')
})

it('run-s command', async () => {
await runSeq(['test-task:package-config2', '--npm-run-all-test:test=1', '--npm-run-all-test:test2=2', '--npm-run-all-test:test3=3'])
assert.equal(result(), '1\n2\n3')
})

it('run-p command', async () => {
await runPar(['test-task:package-config2', '--npm-run-all-test:test=1', '--npm-run-all-test:test2=2', '--npm-run-all-test:test3=3'])
assert.equal(result(), '1\n2\n3')
})
it('run-p command', async () => {
await runPar(['test-task:package-config2', '--npm-run-all-test:test=1', '--npm-run-all-test:test2=2', '--npm-run-all-test:test3=3'])
assert.equal(result(), '1\n2\n3')
})
})

describe('CLI commands should address "--a:b c" style options', () => {
it('npm-run-all command', async () => {
await runAll(['test-task:package-config', '--npm-run-all-test:test', 'OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})

it('run-s command', async () => {
await runSeq(['test-task:package-config', '--npm-run-all-test:test', 'OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})

it('run-p command', async () => {
await runPar(['test-task:package-config', '--npm-run-all-test:test', 'OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})
describe('CLI commands should address "--a:b c" style options', () => {
it('npm-run-all command', async () => {
await runAll(['test-task:package-config', '--npm-run-all-test:test', 'OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})

describe('CLI commands should transfar overriting nested commands.', () => {
it('npm-run-all command', async () => {
await runAll(['test-task:nested-package-config', '--npm-run-all-test:test', 'OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})

it('run-s command', async () => {
await runSeq(['test-task:nested-package-config', '--npm-run-all-test:test', 'OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})

it('run-p command', async () => {
await runPar(['test-task:nested-package-config', '--npm-run-all-test:test', 'OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})
it('run-s command', async () => {
await runSeq(['test-task:package-config', '--npm-run-all-test:test', 'OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})

it('run-p command', async () => {
await runPar(['test-task:package-config', '--npm-run-all-test:test', 'OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})
})

describe('CLI commands should transfar overriting nested commands.', () => {
it('npm-run-all command', async () => {
await runAll(['test-task:nested-package-config', '--npm-run-all-test:test', 'OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})

it('run-s command', async () => {
await runSeq(['test-task:nested-package-config', '--npm-run-all-test:test', 'OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})

it('run-p command', async () => {
await runPar(['test-task:nested-package-config', '--npm-run-all-test:test', 'OVERWRITTEN'])
assert.equal(result(), 'OVERWRITTEN')
})
}
})
})
Loading

0 comments on commit b2a483f

Please sign in to comment.