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

Replace PATH_KEY with pathKey(), delete fixtureEnv #3

Merged
merged 2 commits into from
Dec 28, 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
26 changes: 14 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,35 @@ export async function runJsdoc(argv, env, platform) {
}

/**
* The key for the command search path within process.env.
*
* On every system other than Windows, this will be "PATH". On Windows, this
* will be "Path".
* Determines the key for the command search path within process.env.
* @param {string} platform - the process.platform string
* @returns {string} - On every platform other than 'win32', this will be "PATH"
* On 'win32', this will be "Path".
*/
export const PATH_KEY = process.platform !== 'win32' ? 'PATH' : 'Path'
export const pathKey = (platform) => platform !== 'win32' ? 'PATH' : 'Path'

/**
* Returns the full path to the specified command
* @param {string} cmdName - command to find in env[PATH_KEY]
* @param {string} cmdName - command to find in env[pathKey(platform)]
* @param {object} env - environment variables, presumably process.env
* @param {string} platform - the process.platform string
* @returns {Promise<string>} - path to the command
*/
export async function getPath(cmdName, env, platform) {
for (const p of env[PATH_KEY].split(path.delimiter)) {
// pnpm will install both the original script and versions ending with .CMD
// and .ps1. We'll just default to .CMD.
const extension = (platform === 'win32') ? '.CMD' : ''
const candidate = path.join(p, cmdName) + extension
const pk = pathKey(platform)

// pnpm will install both the original script and versions ending with .CMD
// and .ps1. We'll just default to .CMD.
if (platform === 'win32') cmdName += '.CMD'

for (const p of env[pk].split(path.delimiter)) {
try {
const candidate = path.join(p, cmdName)
await access(candidate)
return candidate
} catch { /* try next candidate */ }
}
return Promise.reject(`${cmdName} not found in ${PATH_KEY}`)
return Promise.reject(`${cmdName} not found in ${pk}`)
}

/**
Expand Down
25 changes: 0 additions & 25 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import path from 'node:path'
import { fileURLToPath } from 'node:url'

/**
Expand All @@ -15,27 +14,3 @@ import { fileURLToPath } from 'node:url'
export function fixturePath(fixtureName) {
return fileURLToPath(new URL(fixtureName, import.meta.url))
}

/**
* A fake environment variable object
* @typedef {object} FakeEnv
* @property {string} PATH - platform-correct PATH variable
*/

/**
* Returns an environment object containing PATH set for the test fixture.
*
* The PATH will always contain ['usr/local/bin', 'usr/bin', 'bin'] relative to
* the fixture root. It will be constructed using the Node.js path module to
* ensure it's native to the host platform.
* @param {string} fixtureName - test fixture name
* @returns {FakeEnv} - a fake environment for the specified test fixture
*/
export function fixtureEnv(fixtureName) {
const root = fixturePath(fixtureName)
const paths = [
path.join('usr', 'local', 'bin'), path.join('usr', 'bin'), 'bin'
]
const pathKey = process.platform !== 'win32' ? 'PATH' : 'Path'
return {[pathKey]: paths.map(p => path.join(root, p)).join(path.delimiter)}
}
21 changes: 12 additions & 9 deletions test/getPath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,30 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import { getPath, PATH_KEY } from '../lib'
import { fixturePath, fixtureEnv } from './fixtures'
import { getPath, pathKey } from '../lib'
import { fixturePath } from './fixtures'
import { describe, expect, test } from 'vitest'
import path from 'node:path'

describe('getPath', () => {
const root = fixturePath('getPath')
const env = fixtureEnv('getPath')
const envPath = ['usr/local/bin', 'usr/bin', 'bin']
.map(p => path.join(root, p))
.join(path.delimiter)
const makeEnv = (platform) => ({[pathKey(platform)]: envPath})

test('finds command on POSIX system', async() => {
await expect(getPath('testcmd', env, 'linux')).resolves
.toBe(path.join(root, 'bin', 'testcmd'))
await expect(getPath('testcmd', makeEnv('linux'), 'linux')).resolves
.toBe(path.join(root, 'bin/testcmd'))
})

test('finds command on Windows system', async() => {
await expect(getPath('testcmd', env, 'win32')).resolves
.toBe(path.join(root, 'usr', 'bin', 'testcmd.CMD'))
await expect(getPath('testcmd', makeEnv('win32'), 'win32')).resolves
.toBe(path.join(root, 'usr/bin/testcmd.CMD'))
})

test('rejects when command isn\'t found', async () => {
await expect(getPath('nonexistent', env, process.platform)).rejects
.toBe(`nonexistent not found in ${PATH_KEY}`)
await expect(getPath('nonexistent', makeEnv('linux'), 'linux')).rejects
.toBe(`nonexistent not found in ${pathKey('linux')}`)
})
})
4 changes: 3 additions & 1 deletion test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import { getPath, PATH_KEY } from '../lib'
import { getPath, pathKey } from '../lib'
import { fixturePath } from './fixtures'
import DestDirHelper from './DestDirHelper'
import { afterEach, describe, expect, test } from 'vitest'
import { spawn } from 'node:child_process'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const PATH_KEY = pathKey(process.platform)

describe('jsdoc-cli-wrapper', () => {
const root = fixturePath('jsdocStub')
const destDirHelper = new DestDirHelper()
Expand Down
4 changes: 3 additions & 1 deletion test/runJsdoc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import { runJsdoc, PATH_KEY } from '../lib'
import { runJsdoc, pathKey } from '../lib'
import { fixturePath } from './fixtures'
import DestDirHelper from './DestDirHelper'
import { afterEach, beforeEach, describe, expect, test } from 'vitest'
import path from 'node:path'

const PATH_KEY = pathKey(process.platform)

describe('runJsdoc', () => {
const root = fixturePath('jsdocStub')
const env = { [PATH_KEY]: root }
Expand Down