Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: keithamus/sort-package-json
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.38.2
Choose a base ref
...
head repository: keithamus/sort-package-json
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.38.3
Choose a head ref
  • 3 commits
  • 7 files changed
  • 2 contributors

Commits on Jan 10, 2020

  1. test: improve cli test (#138)

    fisker authored and keithamus committed Jan 10, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    eddumelendez Eddú Meléndez Gonzales
    Copy the full SHA
    453bccc View commit details
  2. Update semantic-release to the latest version 🚀 (#137)

    * chore(package): update semantic-release to version 16.0.0
    
    * chore(package): update lockfile package-lock.json
    greenkeeper[bot] authored and keithamus committed Jan 10, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    195a6b7 View commit details
  3. fix: missing type check on sortScripts (#139)

    * fix: missing type check on `sortScripts`
    
    * perf: skip type check inside `sortScripts`
    fisker authored and keithamus committed Jan 10, 2020
    Copy the full SHA
    869178f View commit details
Showing with 1,851 additions and 508 deletions.
  1. +3 −3 index.js
  2. +1,319 −191 package-lock.json
  3. +2 −1 package.json
  4. +43 −37 tests/_helpers.js
  5. +149 −133 tests/cli.js
  6. +335 −143 tests/snapshots/cli.js.md
  7. BIN tests/snapshots/cli.js.snap
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ const defaultNpmScripts = new Set([
'version',
])

const sortScripts = scripts => {
const sortScripts = onObject(scripts => {
const names = Object.keys(scripts)
const prefixable = new Set()

@@ -136,8 +136,8 @@ const sortScripts = scripts => {
[],
)

return sortObjectBy(order)(scripts)
}
return sortObjectKeys(scripts, order)
})

// fields marked `vscode` are for `Visual Studio Code extension manifest` only
// https://code.visualstudio.com/api/references/extension-manifest
1,510 changes: 1,319 additions & 191 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -80,9 +80,10 @@
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"husky": "^4.0.2",
"make-dir": "3.0.0",
"nyc": "^15.0.0",
"prettier": "^1.19.1",
"semantic-release": "15.14.0",
"semantic-release": "16.0.0",
"tempy": "0.3.0"
}
}
80 changes: 43 additions & 37 deletions tests/_helpers.js
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ const path = require('path')
const fs = require('fs')
const dotProp = require('dot-prop')
const tempy = require('tempy')
const makeDir = require('make-dir')
const del = require('del')
const sortPackageJson = require('..')
const { execFile } = require('child_process')
@@ -84,8 +85,8 @@ function asItIs(t, { path, options }, excludeTypes = []) {
)
}

for (const value of ['string', false, 2020]) {
const type = typeof value
for (const value of ['string', false, 2020, undefined, null]) {
const type = value === null ? 'null' : typeof value
if (!excludeTypes.includes(type)) {
t.is(
sortPackageJsonAsObject({ path, value, options }),
@@ -96,20 +97,53 @@ function asItIs(t, { path, options }, excludeTypes = []) {
}
}

async function testCLI(t, { fixtures = {}, args, cwd, message }) {
const { root } = setupFixtures(fixtures)
const actual = await runCLI({
async function testCLI(t, { fixtures = [], args, message }) {
const cwd = tempy.directory()

fixtures = fixtures.map(({ file = 'package.json', content, expect }) => {
const absolutePath = path.join(cwd, file)
makeDir.sync(path.dirname(absolutePath))

const original =
typeof content === 'string' ? content : JSON.stringify(content, null, 2)

fs.writeFileSync(absolutePath, original)

return {
file,
absolutePath,
original,
expect:
typeof expect === 'string' ? expect : JSON.stringify(expect, null, 2),
}
})

const result = await runCLI({
args,
cwd: cwd || root,
cwd,
message,
})

cleanFixtures(root)
for (const fixture of fixtures) {
fixture.actual = fs.readFileSync(fixture.absolutePath, 'utf8')
}

// clean up fixtures
del.sync(cwd, { force: true })

for (const { actual, expect, file } of fixtures) {
t.is(actual, expect, `\`${file}\` content is expected.`)
}

t.snapshot(
{
fixtures: Object.keys(fixtures).map(dir => `${dir}/packages.json`),
fixtures: fixtures.map(({ file, original, expect }) => ({
file,
original,
expect,
})),
args,
result: actual,
result,
},
message,
)
@@ -141,31 +175,6 @@ function uniqueAndSort(t, { path, options }) {
asItIs(t, { path, options }, ['array'])
}

function setupFixtures(fixtures) {
const root = tempy.directory()
const result = {
root,
}
for (const [dir, packageJson] of Object.entries(fixtures)) {
const content =
typeof packageJson === 'string'
? packageJson
: JSON.stringify(packageJson, null, 2)
const file = path.join(root, dir, 'package.json')
try {
fs.mkdirSync(path.join(root, dir), { recursive: true })
} catch (_) {}
fs.writeFileSync(file, content)
result[dir] = file
}

return result
}

function cleanFixtures(root) {
del.sync(root, { force: true })
}

module.exports = {
macro: {
sortObject,
@@ -179,7 +188,4 @@ module.exports = {
sortPackageJsonAsString,
keysToObject,
cliScript,
runCLI,
setupFixtures,
cleanFixtures,
}
282 changes: 149 additions & 133 deletions tests/cli.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import test from 'ava'
import {
runCLI,
cliScript,
cleanFixtures,
setupFixtures,
macro,
} from './_helpers'
import { cliScript, macro } from './_helpers'
import fs from 'fs'

const badJson = {
@@ -20,177 +14,199 @@ const goodJson = {
test('cli', t => {
t.notThrows(
() => fs.accessSync(cliScript, fs.constants.X_OK),
'CLI should be executable',
'CLI should be executable.',
)
})

for (const flag of ['--check', '-c']) {
test(`run \`cli ${flag}\``, async t => {
const { root: cwd, 'bad-1': fixture } = setupFixtures({
'bad-1': badJson,
})

await runCLI({
args: ['*/package.json', flag],
cwd,
})

t.is(
fs.readFileSync(fixture, 'utf8'),
JSON.stringify(badJson, null, 2),
`\`${fixture}\` should not fixed when --check is enabled.`,
)

cleanFixtures(cwd)
})
}
test('run `cli` with no patterns', macro.testCLI, {
fixtures: [
{
file: 'package.json',
content: badJson,
expect: goodJson,
},
],
args: [],
message: 'Should format package.json.',
})

test('run `cli --check` with no files', macro.testCLI, {
fixtures: {
'': badJson,
},
test('run `cli --check` with no patterns', macro.testCLI, {
fixtures: [
{
file: 'package.json',
content: badJson,
expect: badJson,
},
],
args: ['--check'],
message: 'Should package.json is not sorted',
})

test('run `cli` with no files', macro.testCLI, {
fixtures: {
'': badJson,
},
args: [],
message: 'Should format package.json',
})

test('run `cli --check` on single unsorted file', macro.testCLI, {
fixtures: {
'bad-1': badJson,
},
args: ['*/package.json', '--check'],
message: 'Should report 1 file is not sorted',
test('run `cli -c` with no patterns', macro.testCLI, {
fixtures: [
{
file: 'package.json',
content: badJson,
expect: badJson,
},
],
args: ['-c'],
message: 'Should support `-c` alias',
})

test('run `cli` on single unsorted file', macro.testCLI, {
fixtures: {
'bad-1': badJson,
},
test('run `cli` on 1 bad file', macro.testCLI, {
fixtures: [
{
file: 'bad/package.json',
content: badJson,
expect: goodJson,
},
],
args: ['*/package.json'],
message: 'Should format 1 file.',
})

test('run `cli --check` on many unsorted file', macro.testCLI, {
fixtures: {
'bad-1': badJson,
'bad-2': badJson,
},
args: ['*/package.json', '--check'],
message: 'Should report all file are not sorted',
})

test('run `cli` on many unsorted file', macro.testCLI, {
fixtures: {
'bad-1': badJson,
'bad-2': badJson,
},
args: ['*/package.json'],
message: 'Should format all files.',
})

test('run `cli --check` on single sorted file', macro.testCLI, {
fixtures: {
'good-1': goodJson,
},
test('run `cli --check` on 1 bad file', macro.testCLI, {
fixtures: [
{
file: 'bad/package.json',
content: badJson,
expect: badJson,
},
],
args: ['*/package.json', '--check'],
message: 'Should report 1 file is sorted',
message: 'Should report 1 file.',
})

test('run `cli` on single sorted file', macro.testCLI, {
fixtures: {
'good-1': goodJson,
},
test('run `cli` on 2 bad files', macro.testCLI, {
fixtures: Array.from({ length: 2 }, (_, index) => ({
file: `bad-${index + 1}/package.json`,
content: badJson,
expect: goodJson,
})),
args: ['*/package.json'],
message: 'Should not format any file.',
message: 'Should format 2 files.',
})

test('run `cli --check` on many sorted file', macro.testCLI, {
fixtures: {
'good-1': goodJson,
'good-2': goodJson,
},
test('run `cli --check` on 2 bad files', macro.testCLI, {
fixtures: Array.from({ length: 2 }, (_, index) => ({
file: `bad-${index + 1}/package.json`,
content: badJson,
expect: badJson,
})),
args: ['*/package.json', '--check'],
message: 'Should report all files are sorted',
})

test('run `cli` on single many sorted file', macro.testCLI, {
fixtures: {
'good-1': goodJson,
'good-2': goodJson,
},
message: 'Should report 2 files.',
})

test('run `cli` on 2 good files and 2 bad files', macro.testCLI, {
fixtures: [
...Array.from({ length: 2 }, (_, index) => ({
file: `bad-${index + 1}/package.json`,
content: badJson,
expect: goodJson,
})),
...Array.from({ length: 2 }, (_, index) => ({
file: `good-${index + 1}/package.json`,
content: goodJson,
expect: goodJson,
})),
],
args: ['*/package.json'],
message: 'Should not format any file.',
})

test('run `run `cli --check` on sorted and unsorted file', macro.testCLI, {
fixtures: {
'bad-1': badJson,
'bad-2': badJson,
'good-1': goodJson,
'good-2': goodJson,
},
message: 'Should format 2 files.',
})

test('run `cli --check` on 2 good files and 2 bad files', macro.testCLI, {
fixtures: [
...Array.from({ length: 2 }, (_, index) => ({
file: `bad-${index + 1}/package.json`,
content: badJson,
expect: badJson,
})),
...Array.from({ length: 2 }, (_, index) => ({
file: `good-${index + 1}/package.json`,
content: goodJson,
expect: goodJson,
})),
],
args: ['*/package.json', '--check'],
message: 'Should report some files are not sorted',
message: 'Should report 2 files.',
})

test('run `run `cli` on sorted and unsorted file', macro.testCLI, {
fixtures: {
'bad-1': badJson,
'bad-2': badJson,
'good-1': goodJson,
'good-2': goodJson,
},
args: ['*/package.json'],
message: 'Should format some files',
test('run `cli` on none exists file', macro.testCLI, {
fixtures: [
{
file: 'package.json',
content: badJson,
expect: badJson,
},
],
args: ['NONE_EXISTS_FILE'],
message: 'Should report no files matching.',
})

test('run `cli --check` on none exists file', macro.testCLI, {
fixtures: {},
fixtures: [
{
file: 'package.json',
content: badJson,
expect: badJson,
},
],
args: ['NONE_EXISTS_FILE', '--check'],
message: 'Should report no files matching.',
})

test('run `cli` on none exists file', macro.testCLI, {
fixtures: {},
args: ['NONE_EXISTS_FILE'],
message: 'Should report no files matching.',
})

test('run `cli --check` on duplicate patterns', macro.testCLI, {
fixtures: {
'bad-1': badJson,
'good-1': goodJson,
'good-2': goodJson,
},
test('run `cli` on duplicate patterns', macro.testCLI, {
fixtures: [
{
file: 'bad-1/package.json',
content: badJson,
expect: goodJson,
},
{
file: 'good-1/package.json',
content: goodJson,
expect: goodJson,
},
{
file: 'good-2/package.json',
content: goodJson,
expect: goodJson,
},
],
args: [
'bad-1/package.json',
'bad-1/package.json',
'bad-*/package.json',
'*/package.json',
'--check',
],
message: 'Should not list `bad-1/package.json` more than once',
message: 'Should not format `bad-1/package.json` more than once.',
})

test('run `cli` on duplicate patterns', macro.testCLI, {
fixtures: {
'bad-1': badJson,
'good-1': goodJson,
'good-2': goodJson,
},
test('run `cli --check` on duplicate patterns', macro.testCLI, {
fixtures: [
{
file: 'bad-1/package.json',
content: badJson,
expect: badJson,
},
{
file: 'good-1/package.json',
content: goodJson,
expect: goodJson,
},
{
file: 'good-2/package.json',
content: goodJson,
expect: goodJson,
},
],
args: [
'bad-1/package.json',
'bad-1/package.json',
'bad-*/package.json',
'*/package.json',
'--check',
],
message: 'Should not format `bad-1/package.json` more than once',
message: 'Should not list `bad-1/package.json` more than once.',
})
478 changes: 335 additions & 143 deletions tests/snapshots/cli.js.md

Large diffs are not rendered by default.

Binary file modified tests/snapshots/cli.js.snap
Binary file not shown.