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

fix(test): added npm init parity test #223

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ jobs:
node-version: ${{ fromJson(needs.setup.outputs.nodes) }}
steps:
- uses: actions/checkout@v4
- name: Setup git user
run: |
git config --global user.name "$(git --no-pager log --format=format:'%an' -n 1)"
git config --global user.email "$(git --no-pager log --format=format:'%ae' -n 1)"
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
Expand Down
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,8 @@ async function format (opts, pkg = {}) {
pkg.main = opts.main
pkg.type = opts.type || 'commonjs'

if (opts.keywords && opts.keywords.length) {
// TODO: extra parsing going on here due to wesleytodd/opta#1
pkg.keywords = uniquify([...(pkg.keywords || []), ...parseList(opts.keywords)])
}
// TODO: extra parsing going on here due to wesleytodd/opta#1
pkg.keywords = uniquify([...(pkg.keywords || []), ...parseList(opts.keywords || [])])

// Scripts
if (Object.keys(opts.scripts).length) {
Expand Down
40 changes: 37 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict'
const path = require('path')
const assert = require('assert')
const path = require('node:path')
const assert = require('node:assert')
const fs = require('node:fs/promises')
const { promisify } = require('node:util')
const execFile = promisify(require('node:child_process').execFile)
const { suite, test, before } = require('mocha')
const fixtures = require('fs-test-fixtures')
const createPkgJson = require('..')
Expand Down Expand Up @@ -79,7 +82,7 @@ suite('create-package-json', () => {
assert.strictEqual(pkg.description, '')
assert.strictEqual(pkg.author, 'Test User <[email protected]>')
assert.strictEqual(pkg.repository, undefined)
assert.strictEqual(pkg.keywords, undefined)
assert.deepStrictEqual(pkg.keywords, [])
assert.strictEqual(pkg.license, 'ISC')
assert.strictEqual(pkg.type, 'commonjs')
assert.strictEqual(pkg.main, 'index.js')
Expand Down Expand Up @@ -248,4 +251,35 @@ suite('create-package-json', () => {
assert.deepStrictEqual(pkg.man, './man/foo.1')
})
})

suite('npm init', () => {
test('parity', async () => {
await fix.setup()
try {
await execFile('npm', ['init', '-y'], {
cwd: fix.TMP
})
} catch (e) {
console.error(e)
throw e
}
const npmInitPkg = JSON.parse(await fs.readFile(path.join(fix.TMP, 'package.json'), 'utf8'))

await fix.setup()
const pkg = await createPackageJson()

// Should be the same
assert.strictEqual(pkg.name, npmInitPkg.name)
assert.strictEqual(pkg.version, npmInitPkg.version)
assert.strictEqual(pkg.description, npmInitPkg.description)
assert.strictEqual(pkg.main, npmInitPkg.main)
assert.deepStrictEqual(pkg.scripts, npmInitPkg.scripts)
assert.deepStrictEqual(pkg.keywords, npmInitPkg.keywords)
assert.strictEqual(pkg.license, npmInitPkg.license)

// Should be different
assert.notStrictEqual(pkg.author, npmInitPkg.author, JSON.stringify([pkg.author, npmInitPkg.author]))
assert.notStrictEqual(pkg.type, npmInitPkg.type, JSON.stringify([pkg.type, npmInitPkg.type]))
})
})
})