Skip to content

Commit

Permalink
fix(arborist): convert all sorting to string-locale-compare
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Feb 23, 2022
1 parent 63b3557 commit 41110b1
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion workspaces/arborist/bin/license.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Arborist = require('../')
const localeCompare = require('@isaacs/string-locale-compare')('en')
const options = require('./lib/options.js')
require('./lib/logging.js')
require('./lib/timers.js')
Expand All @@ -24,7 +25,7 @@ a.loadVirtual().then(tree => {
}

for (const [count, license] of set.sort((a, b) =>
a[1] && b[1] ? b[0] - a[0] || a[1].localeCompare(b[1], 'en')
a[1] && b[1] ? b[0] - a[0] || localeCompare(a[1], b[1])
: a[1] ? -1
: b[1] ? 1
: 0)) {
Expand Down
5 changes: 3 additions & 2 deletions workspaces/arborist/test/arborist/rebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { resolve, dirname } = require('path')
const fs = require('fs')
const fixtures = resolve(__dirname, '../fixtures')
const relpath = require('../../lib/relpath.js')
const localeCompare = require('@isaacs/string-locale-compare')('en')

const fixture = (t, p) => require(`${fixtures}/reify-cases/${p}`)(t)

Expand Down Expand Up @@ -181,7 +182,7 @@ t.test('verify dep flags in script environments', async t => {
// don't include path or env, because that's going to be platform-specific
const saved = [...arb.scriptsRun]
.sort(({ path: patha, event: eventa }, { path: pathb, event: eventb }) =>
patha.localeCompare(pathb, 'en') || eventa.localeCompare(eventb, 'en'))
localeCompare(patha, pathb) || localeCompare(eventa, eventb))
.map(({ pkg, event, cmd, code, signal, stdout, stderr }) =>
({ pkg, event, cmd, code, signal, stdout, stderr }))
t.matchSnapshot(saved, 'saved script results')
Expand All @@ -191,7 +192,7 @@ t.test('verify dep flags in script environments', async t => {
t.strictSame(flags, fs.readFileSync(file, 'utf8').split('\n'), pkg)
}
t.strictSame(checkLogs().sort((a, b) =>
a[2].localeCompare(b[2], 'en') || (typeof a[4] === 'string' ? -1 : 1)), [
localeCompare(a[2], b[2]) || (typeof a[4] === 'string' ? -1 : 1)), [
['info', 'run', 'devdep@1.0.0', 'postinstall', 'node_modules/devdep', 'node ../../env.js'],
['info', 'run', 'devdep@1.0.0', 'postinstall', { code: 0, signal: null }],
['info', 'run', 'devopt@1.0.0', 'postinstall', 'node_modules/devopt', 'node ../../env.js'],
Expand Down
3 changes: 2 additions & 1 deletion workspaces/arborist/test/arborist/reify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { resolve, basename } = require('path')
const t = require('tap')
const runScript = require('@npmcli/run-script')
const localeCompare = require('@isaacs/string-locale-compare')('en')

// mock rimraf so we can make it fail in rollback tests
const realRimraf = require('rimraf')
Expand Down Expand Up @@ -241,7 +242,7 @@ t.test('omit peer deps', t => {
.then(() => {
process.removeListener('time', onTime)
process.removeListener('timeEnd', onTimeEnd)
finishedTimers.sort((a, b) => a.localeCompare(b, 'en'))
finishedTimers.sort(localeCompare)
t.matchSnapshot(finishedTimers, 'finished timers')
t.strictSame(timers, {}, 'should have no timers in progress now')
})
Expand Down
7 changes: 4 additions & 3 deletions workspaces/arborist/test/audit-report.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const t = require('tap')
const localeCompare = require('@isaacs/string-locale-compare')('en')
const AuditReport = require('../lib/audit-report.js')
const { auditToBulk } = AuditReport
const Node = require('../lib/node.js')
Expand All @@ -24,14 +25,14 @@ const newArb = (path, opts = {}) =>

const sortReport = report => {
const entries = Object.entries(report.vulnerabilities)
const vulns = entries.sort(([a], [b]) => a.localeCompare(b, 'en'))
const vulns = entries.sort(([a], [b]) => localeCompare(a, b))
.map(([name, vuln]) => [
name,
{
...vuln,
via: (vuln.via || []).sort((a, b) =>
String(a.source || a).localeCompare(String(b.source || b), 'en')),
effects: (vuln.effects || []).sort((a, b) => a.localeCompare(b, 'en')),
localeCompare(String(a.source || a), String(b.source || b))),
effects: (vuln.effects || []).sort(localeCompare),
},
])
report.vulnerabilities = vulns.reduce((set, [k, v]) => {
Expand Down
3 changes: 2 additions & 1 deletion workspaces/arborist/test/diff.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const localeCompare = require('@isaacs/string-locale-compare')('en')
const Diff = require('../lib/diff.js')
const t = require('tap')
const Node = require('../lib/node.js')
Expand Down Expand Up @@ -31,7 +32,7 @@ const formatDiff = obj =>
removed: obj.removed.map(d => normalizePath(d.path).split(normalizedCWD).join('{CWD}')),
children: [...obj.children]
.map(formatDiff)
.sort((a, b) => path(a).localeCompare(path(b), 'en')),
.sort((a, b) => localeCompare(path(a), path(b))),
})

t.formatSnapshot = obj => format(formatDiff(obj), { sort: false })
Expand Down
3 changes: 2 additions & 1 deletion workspaces/arborist/test/fixtures/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mkdirp = require('mkdirp').sync
const localeCompare = require('@isaacs/string-locale-compare')('en')
const { unlinkSync, symlinkSync, readFileSync, writeFileSync } = require('fs')
const { relative, resolve, dirname } = require('path')

Expand Down Expand Up @@ -167,7 +168,7 @@ const setup = () => {
`### BEGIN IGNORED SYMLINKS ###
### this list is generated automatically, do not edit directly
### update it by running \`node test/fixtures/index.js\`
${links.sort((a,b) => a.localeCompare(b, 'en')).join('\n')}
${links.sort(localeCompare).join('\n')}
### END IGNORED SYMLINKS ###`)
writeFileSync(gifile, gitignore)
}
Expand Down
3 changes: 2 additions & 1 deletion workspaces/arborist/test/gather-dep-set.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const t = require('tap')
const localeCompare = require('@isaacs/string-locale-compare')('en')
const gatherDepSet = require('../lib/gather-dep-set.js')

const Node = require('../lib/node.js')
Expand Down Expand Up @@ -81,7 +82,7 @@ const tree = new Node({
const normalizePath = path => path.replace(/[A-Z]:/, '').replace(/\\/g, '/')

const printSet = set => [...set]
.sort((a, b) => a.name.localeCompare(b.name, 'en'))
.sort((a, b) => localeCompare(a.name, b.name))
.map(n => n.location)

const cwd = normalizePath(process.cwd())
Expand Down

0 comments on commit 41110b1

Please sign in to comment.