Files
pnpm/packages/plugin-commands-env/test/node.test.ts
Mark Omarov ba270a9671 feat(env): add remove command to pnpm env (#5263)
* feat(env): add uninstall command to pnpm env

* chore(changeset): define minor bump

* fix(env): fixes for pnpm env uninstall

- Fix broken node path for the windows platform
- Notify user when removing default Node.JS
- Remove `await` on `fs.unlink` in `Promise.all`
- Add support for Node.JS v14.13 and earlier

* fix(env): fix failing tests on Windows platform

* fix(env): update aliases and error catching

- Add `rm, un, uninstall` aliases to the `remove` command
- Update CLI help message
- Ignore only `ENOENT` errors

* refactor(env): cr updates

- Use `remove` with `rm` alias only
- Update CLI help message
- Update tests to use `remove` command
- Replace custom support for older Node.JS versions
  with `@zkochan/rimraf`

* refactor(env): simplify catch block

* fix: env rm

* refactor: test

Co-authored-by: Mark Omarov <inkfaust@gmail.com>
Co-authored-by: Zoltan Kochan <z@kochan.io>
2022-08-30 01:53:13 +03:00

82 lines
2.3 KiB
TypeScript

import AdmZip from 'adm-zip'
import { Response } from 'node-fetch'
import path from 'path'
import { Readable } from 'stream'
import { node } from '@pnpm/plugin-commands-env'
import { tempDir } from '@pnpm/prepare'
const fetchMock = jest.fn(async (url: string) => {
if (url.endsWith('.zip')) {
// The Windows code path for pnpm's node bootstrapping expects a subdir
// within the .zip file.
const pkgName = path.basename(url, '.zip')
const zip = new AdmZip()
zip.addFile(`${pkgName}/dummy-file`, Buffer.from('test'))
return new Response(Readable.from(zip.toBuffer()))
}
return new Response(Readable.from(Buffer.alloc(0)))
})
jest.mock('@pnpm/fetch', () => ({
createFetchFromRegistry: () => fetchMock,
}))
beforeEach(() => {
fetchMock.mockClear()
})
test('check API (placeholder test)', async () => {
expect(typeof node.getNodeDir).toBe('function')
})
test('install Node uses node-mirror:release option', async () => {
tempDir()
const configDir = path.resolve('config')
const nodeMirrorRelease = 'https://pnpm-node-mirror-test.localhost/download/release'
const opts: node.NvmNodeCommandOptions = {
bin: process.cwd(),
configDir,
global: true,
pnpmHomeDir: process.cwd(),
rawConfig: {
'node-mirror:release': nodeMirrorRelease,
},
useNodeVersion: '16.4.0',
}
await node.getNodeBinDir(opts)
for (const call of fetchMock.mock.calls) {
expect(call[0]).toMatch(nodeMirrorRelease)
}
})
test('install and rc version of Node.js', async () => {
tempDir()
const configDir = path.resolve('config')
const opts: node.NvmNodeCommandOptions = {
bin: process.cwd(),
configDir,
global: true,
pnpmHomeDir: process.cwd(),
rawConfig: {},
useNodeVersion: 'rc/18.0.0-rc.3',
}
await node.getNodeBinDir(opts)
const platform = process.platform === 'win32' ? 'win' : process.platform
const extension = process.platform === 'win32' ? 'zip' : 'tar.gz'
expect(fetchMock.mock.calls[0][0]).toBe(`https://nodejs.org/download/rc/v18.0.0-rc.3/node-v18.0.0-rc.3-${platform}-x64.${extension}`)
})
test('get node version base dir', async () => {
expect(typeof node.getNodeVersionsBaseDir).toBe('function')
const versionDir = node.getNodeVersionsBaseDir(process.cwd())
expect(versionDir).toBe(path.resolve(process.cwd(), 'nodejs'))
})