这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
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
1 change: 1 addition & 0 deletions .vscode/cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"config",
"configs",
"custom",
"delineator",
"docsify",
"e.g.",
"env",
Expand Down
4 changes: 4 additions & 0 deletions __tests__/__mocks__/mockContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ const noConfigSolidarity = {
setSolidaritySettings: jest.fn(),
updateRequirement: jest.fn(),
updateVersions: jest.fn(() => Promise.resolve()),
getLineWithVersion: jest.fn(),
removeNonVersionCharacters: jest.fn(),
}

const mockContext = {
...realThing,
outputMode: undefined,
system: {
startTimer: jest.fn(() => jest.fn()),
run: jest.fn(() => '12'),
which: jest.fn((name) => 'usr/local/bin/${name}'),
},
template: {
generate: jest.fn(),
Expand Down
9 changes: 9 additions & 0 deletions __tests__/command_helpers/checkCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const alwaysExistCLI = {
binary: 'node',
}

const badSemver = {
binary: 'node',
semver: 'wtfbbq!!!11',
}

const outOfDateCLI = {
binary: 'node',
semver: '10.99',
Expand All @@ -24,6 +29,10 @@ test('fine on existing binary', async () => {
expect(await checkCLI(alwaysExistCLI, context)).toBe(undefined)
})

test('errors with message when an improper semver is sent', async () => {
expect(await checkCLI(badSemver, context)).toBe(`Invalid semver rule ${badSemver.semver}`)
})

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I identified a missing test, and added it as lagniappe in this PR. Sorry for the distraction, but I'm always trying to up the % coverage.

test('returns message on improper version', async () => {
solidarityExtension(context)
context.solidarity.getVersion = () => '1'
Expand Down
32 changes: 32 additions & 0 deletions __tests__/command_helpers/quirksNodeModules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const path = require('path')
const delineator = process.platform === 'win32' ? ';' : ':'
test('quirks moves node_modules to back', () => {
// key is that it has `node_modules` + path.sep
const injectedStuff = `node_modules${path.sep}testOnly`
// prepend to PATH
process.env.PATH = injectedStuff + delineator + process.env.PATH
const pathAsArray = process.env.PATH.split(delineator)
// Yup it is prepended to front
expect(pathAsArray[0]).toBe(injectedStuff)
// require mutates PATH
require('../../src/extensions/functions/quirksNodeModules')
const newPathAsArray = process.env.PATH.split(delineator)
// Not in the front
expect(newPathAsArray[0]).not.toBe(injectedStuff)
// still there though (moved to back)
expect(process.env.PATH.match(injectedStuff)).toBeTruthy()
})

test('quirks does not move just any injected path to back', () => {
const injectedStuff = `taco${path.sep}testOnly`
// prepend to PATH
process.env.PATH = injectedStuff + delineator + process.env.PATH
const pathAsArray = process.env.PATH.split(delineator)
// Yup it is prepended to front
expect(pathAsArray[0]).toBe(injectedStuff)
// require does not mutate PATH this time
require('../../src/extensions/functions/quirksNodeModules')
const newPathAsArray = process.env.PATH.split(delineator)
// Not in the front
expect(newPathAsArray[0]).toBe(injectedStuff)
})
2 changes: 2 additions & 0 deletions src/extensions/functions/checkCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { SolidarityRunContext, CLIRule } from '../../types'
module.exports = async (rule: CLIRule, context: SolidarityRunContext): Promise<string | undefined> => {
const { semver, solidarity } = context
const binaryExists = require('./binaryExists')
// Node Modules do strange things
require('./quirksNodeModules')

// First check for binary
if (!binaryExists(rule.binary, context)) {
Expand Down
11 changes: 11 additions & 0 deletions src/extensions/functions/quirksNodeModules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {
reject, contains, concat, difference
} from 'ramda'
import path from 'path'
const delineator = process.platform === 'win32' ? ';' : ':'

// Node mutates path by adding to the front, move that to the back if it exists
const originalPath = process.env.PATH || ''
const originalArray = originalPath.split(delineator)
const cleanArray = reject(contains('node_modules' + path.sep), originalArray)
process.env.PATH = concat(cleanArray, difference(originalArray, cleanArray)).join(delineator)