test(setup): improve tests

This commit is contained in:
Zoltan Kochan committed 2022-05-11 03:36:33 +03:00
1 parent 59a7b06c37
commit 0bdf6bab57
10 files changed
+79 -73

No files matched your search

@@ -28,6 +28,7 @@ function pathIncludesDir (pathValue: string, dir: string): boolean {
}
export async function setupWindowsEnvironmentPath (pnpmHomeDir: string, opts: { force: boolean }): Promise<string> {
pnpmHomeDir = path.normalize(pnpmHomeDir)
// Use `chcp` to make `reg` use utf8 encoding for output.
// Otherwise, the non-ascii characters in the environment variables will become garbled characters.
@@ -1,4 +1,5 @@
import fs from 'fs'
import path from 'path'
import { homedir } from 'os'
import { tempDir } from '@pnpm/prepare'
@@ -14,45 +15,42 @@ jest.mock('os', () => {
test('PNPM_HOME is added to ~/.bashrc', async () => {
process.env.SHELL = '/bin/bash'
tempDir()
const homeDir = tempDir()
const pnpmHomeDir = path.join(homeDir, '.pnpm')
fs.writeFileSync('.bashrc', '', 'utf8')
homedir['mockReturnValue'](process.cwd())
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
homedir['mockReturnValue'](homeDir)
const output = await setup.handler({ pnpmHomeDir })
expect(output).toMatch(/^Updated /)
const bashRCContent = fs.readFileSync('.bashrc', 'utf8')
expect(bashRCContent).toEqual(`
export PNPM_HOME="${__dirname}"
export PNPM_HOME="${pnpmHomeDir}"
export PATH="$PNPM_HOME:$PATH"
`)
})
test('PNPM_HOME is added to ~/.bashrc and .bashrc file created', async () => {
process.env.SHELL = '/bin/bash'
tempDir()
homedir['mockReturnValue'](process.cwd())
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
const homeDir = tempDir()
const pnpmHomeDir = path.join(homeDir, '.pnpm')
homedir['mockReturnValue'](homeDir)
const output = await setup.handler({ pnpmHomeDir })
expect(output).toMatch(/^Created /)
const bashRCContent = fs.readFileSync('.bashrc', 'utf8')
expect(bashRCContent).toEqual(`export PNPM_HOME="${__dirname}"
expect(bashRCContent).toEqual(`export PNPM_HOME="${pnpmHomeDir}"
export PATH="$PNPM_HOME:$PATH"
`)
})
test('PNPM_HOME is not added to ~/.bashrc if already present', async () => {
process.env.SHELL = '/bin/bash'
tempDir()
const homeDir = tempDir()
const pnpmHomeDir = path.join(homeDir, '.pnpm')
fs.writeFileSync('.bashrc', `
export PNPM_HOME="pnpm_home"
export PATH="$PNPM_HOME:$PATH"
`, 'utf8')
homedir['mockReturnValue'](process.cwd())
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
homedir['mockReturnValue'](homeDir)
const output = await setup.handler({ pnpmHomeDir })
expect(output).toMatch(/^PNPM_HOME is already in /)
const bashRCContent = fs.readFileSync('.bashrc', 'utf8')
expect(bashRCContent).toEqual(`
@@ -63,31 +61,29 @@ export PATH="$PNPM_HOME:$PATH"
test('PNPM_HOME is added to ~/.zshrc', async () => {
process.env.SHELL = '/bin/zsh'
tempDir()
const homeDir = tempDir()
const pnpmHomeDir = path.join(homeDir, '.pnpm')
fs.writeFileSync('.zshrc', '', 'utf8')
homedir['mockReturnValue'](process.cwd())
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
homedir['mockReturnValue'](homeDir)
const output = await setup.handler({ pnpmHomeDir })
expect(output).toMatch(/^Updated /)
const bashRCContent = fs.readFileSync('.zshrc', 'utf8')
expect(bashRCContent).toEqual(`
export PNPM_HOME="${__dirname}"
export PNPM_HOME="${pnpmHomeDir}"
export PATH="$PNPM_HOME:$PATH"
`)
})
test('PNPM_HOME is not added to ~/.zshrc if already present', async () => {
process.env.SHELL = '/bin/zsh'
tempDir()
const homeDir = tempDir()
const pnpmHomeDir = path.join(homeDir, '.pnpm')
fs.writeFileSync('.zshrc', `
export PNPM_HOME="pnpm_home"
export PATH="$PNPM_HOME:$PATH"
`, 'utf8')
homedir['mockReturnValue'](process.cwd())
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
homedir['mockReturnValue'](homeDir)
const output = await setup.handler({ pnpmHomeDir })
expect(output).toMatch(/^PNPM_HOME is already in /)
const bashRCContent = fs.readFileSync('.zshrc', 'utf8')
expect(bashRCContent).toEqual(`
@@ -98,48 +94,45 @@ export PATH="$PNPM_HOME:$PATH"
test('PNPM_HOME is added to ~/.config/fish/config.fish', async () => {
process.env.SHELL = '/bin/fish'
tempDir()
const homeDir = tempDir()
const pnpmHomeDir = path.join(homeDir, '.pnpm')
fs.mkdirSync('.config/fish', { recursive: true })
fs.writeFileSync('.config/fish/config.fish', '', 'utf8')
homedir['mockReturnValue'](process.cwd())
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
homedir['mockReturnValue'](homeDir)
const output = await setup.handler({ pnpmHomeDir })
expect(output).toMatch(/^Updated /)
const bashRCContent = fs.readFileSync('.config/fish/config.fish', 'utf8')
expect(bashRCContent).toEqual(`
set -gx PNPM_HOME "${__dirname}"
set -gx PNPM_HOME "${pnpmHomeDir}"
set -gx PATH "$PNPM_HOME" $PATH
`)
})
test('PNPM_HOME is added to ~/.config/fish/config.fish and config.fish file created', async () => {
process.env.SHELL = '/bin/fish'
tempDir()
const homeDir = tempDir()
const pnpmHomeDir = path.join(homeDir, '.pnpm')
fs.mkdirSync('.config/fish', { recursive: true })
homedir['mockReturnValue'](process.cwd())
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
homedir['mockReturnValue'](homeDir)
const output = await setup.handler({ pnpmHomeDir })
expect(output).toMatch(/^Created /)
const bashRCContent = fs.readFileSync('.config/fish/config.fish', 'utf8')
expect(bashRCContent).toEqual(`set -gx PNPM_HOME "${__dirname}"
expect(bashRCContent).toEqual(`set -gx PNPM_HOME "${pnpmHomeDir}"
set -gx PATH "$PNPM_HOME" $PATH
`)
})
test('PNPM_HOME is not added to ~/.config/fish/config.fish if already present', async () => {
process.env.SHELL = '/bin/fish'
tempDir()
const homeDir = tempDir()
const pnpmHomeDir = path.join(homeDir, '.pnpm')
fs.mkdirSync('.config/fish', { recursive: true })
fs.writeFileSync('.config/fish/config.fish', `
set -gx PNPM_HOME "pnpm_home"
set -gx PATH "$PNPM_HOME" $PATH
`, 'utf8')
homedir['mockReturnValue'](process.cwd())
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
homedir['mockReturnValue'](homeDir)
const output = await setup.handler({ pnpmHomeDir })
expect(output).toMatch(/^PNPM_HOME is already in /)
const bashRCContent = fs.readFileSync('.config/fish/config.fish', 'utf8')
expect(bashRCContent).toEqual(`
@@ -1,5 +1,6 @@
import execa from 'execa'
import { setup } from '@pnpm/plugin-commands-setup'
import { tempDir } from '@pnpm/prepare'
jest.mock('execa')
@@ -37,7 +38,7 @@ test('Win32 registry environment values could not be retrieved', async () => {
})
const output = await setup.handler({
pnpmHomeDir: __dirname,
pnpmHomeDir: tempDir(false),
})
expect(execa).toHaveBeenNthCalledWith(3, 'reg', ['query', regKey], { windowsHide: false })
@@ -1,5 +1,6 @@
import execa from 'execa'
import { setup } from '@pnpm/plugin-commands-setup'
import { tempDir } from '@pnpm/prepare'
jest.mock('execa')
@@ -40,7 +41,7 @@ test('Environment PATH is not configured correctly', async () => {
})
const output = await setup.handler({
pnpmHomeDir: __dirname,
pnpmHomeDir: tempDir(false),
})
expect(execa).toHaveBeenNthCalledWith(3, 'reg', ['query', regKey], { windowsHide: false })
@@ -1,5 +1,6 @@
import execa from 'execa'
import { setup } from '@pnpm/plugin-commands-setup'
import { tempDir } from '@pnpm/prepare'
jest.mock('execa')
@@ -43,7 +44,7 @@ HKEY_CURRENT_USER\\Environment
})
const output = await setup.handler({
pnpmHomeDir: __dirname,
pnpmHomeDir: tempDir(false),
})
expect(execa).toHaveBeenNthCalledWith(3, 'reg', ['query', regKey], { windowsHide: false })
@@ -1,5 +1,7 @@
import { win32 as path } from 'path'
import execa from 'execa'
import { setup } from '@pnpm/plugin-commands-setup'
import { tempDir } from '@pnpm/prepare'
jest.mock('execa')
@@ -51,15 +53,15 @@ HKEY_CURRENT_USER\\Environment
stderr: 'UNEXPECTED',
})
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
const pnpmHomeDir = tempDir(false)
const pnpmHomeDirNormalized = path.normalize(pnpmHomeDir)
const output = await setup.handler({ pnpmHomeDir })
expect(execa).toHaveBeenNthCalledWith(3, 'reg', ['query', regKey], { windowsHide: false })
expect(execa).toHaveBeenNthCalledWith(4, 'reg', ['add', regKey, '/v', 'PNPM_HOME', '/t', 'REG_EXPAND_SZ', '/d', __dirname, '/f'], { windowsHide: false })
expect(execa).toHaveBeenNthCalledWith(5, 'reg', ['add', regKey, '/v', 'Path', '/t', 'REG_EXPAND_SZ', '/d', `${__dirname};${currentPathInRegistry}`, '/f'], { windowsHide: false })
expect(execa).toHaveBeenNthCalledWith(6, 'setx', ['PNPM_HOME', __dirname])
expect(output).toContain(`Setting 'PNPM_HOME' to value '${__dirname}`)
expect(execa).toHaveBeenNthCalledWith(4, 'reg', ['add', regKey, '/v', 'PNPM_HOME', '/t', 'REG_EXPAND_SZ', '/d', pnpmHomeDirNormalized, '/f'], { windowsHide: false })
expect(execa).toHaveBeenNthCalledWith(5, 'reg', ['add', regKey, '/v', 'Path', '/t', 'REG_EXPAND_SZ', '/d', `${pnpmHomeDirNormalized};${currentPathInRegistry}`, '/f'], { windowsHide: false })
expect(execa).toHaveBeenNthCalledWith(6, 'setx', ['PNPM_HOME', pnpmHomeDirNormalized])
expect(output).toContain(`Setting 'PNPM_HOME' to value '${pnpmHomeDirNormalized}`)
expect(output).toContain('Updating PATH')
expect(output).toContain('PNPM_HOME ENV VAR SET')
expect(output).toContain('PATH UPDATED')
@@ -1,5 +1,7 @@
import { win32 as path } from 'path'
import execa from 'execa'
import { setup } from '@pnpm/plugin-commands-setup'
import { tempDir } from '@pnpm/prepare'
jest.mock('execa')
@@ -27,7 +29,8 @@ const regKey = 'HKEY_CURRENT_USER\\Environment'
test('PNPM_HOME is already set, but path is updated', async () => {
const currentPathInRegistry = '%USERPROFILE%\\AppData\\Local\\Microsoft\\WindowsApps;%USERPROFILE%\\.config\\etc;'
const pnpmHomeDir = '.pnpm\\home'
const pnpmHomeDir = tempDir(false)
const pnpmHomeDirNormalized = path.normalize(pnpmHomeDir)
execa['mockResolvedValueOnce']({
failed: false,
stdout: '活动代码页: 936',
@@ -38,7 +41,7 @@ test('PNPM_HOME is already set, but path is updated', async () => {
failed: false,
stdout: `
HKEY_CURRENT_USER\\Environment
PNPM_HOME REG_EXPAND_SZ ${pnpmHomeDir}
PNPM_HOME REG_EXPAND_SZ ${pnpmHomeDirNormalized}
Path REG_EXPAND_SZ ${currentPathInRegistry}
`,
}).mockResolvedValueOnce({
@@ -52,8 +55,8 @@ HKEY_CURRENT_USER\\Environment
const output = await setup.handler({ pnpmHomeDir })
expect(execa).toHaveBeenNthCalledWith(3, 'reg', ['query', regKey], { windowsHide: false })
expect(execa).toHaveBeenNthCalledWith(4, 'reg', ['add', regKey, '/v', 'Path', '/t', 'REG_EXPAND_SZ', '/d', `${'.pnpm\\home'};${currentPathInRegistry}`, '/f'], { windowsHide: false })
expect(execa).toHaveBeenNthCalledWith(5, 'setx', ['PNPM_HOME', '.pnpm\\home'])
expect(execa).toHaveBeenNthCalledWith(4, 'reg', ['add', regKey, '/v', 'Path', '/t', 'REG_EXPAND_SZ', '/d', `${pnpmHomeDirNormalized};${currentPathInRegistry}`, '/f'], { windowsHide: false })
expect(execa).toHaveBeenNthCalledWith(5, 'setx', ['PNPM_HOME', pnpmHomeDirNormalized])
expect(output).toContain('Updating PATH')
expect(output).toContain('PATH UPDATED')
})
@@ -1,5 +1,7 @@
import { win32 as path } from 'path'
import execa from 'execa'
import { setup } from '@pnpm/plugin-commands-setup'
import { tempDir } from '@pnpm/prepare'
jest.mock('execa')
@@ -44,11 +46,10 @@ HKEY_CURRENT_USER\\Environment
stderr: 'UNEXPECTED',
})
const pnpmHomeDir = tempDir(false)
await expect(
setup.handler({
pnpmHomeDir: __dirname,
})
).rejects.toThrowError(/Currently 'PNPM_HOME' is set to '.pnpm\\home'/)
setup.handler({ pnpmHomeDir })
).rejects.toThrowError(/Currently 'PNPM_HOME' is set to/)
})
test('setup overrides PNPM_HOME, when setup is forced', async () => {
@@ -70,12 +71,13 @@ HKEY_CURRENT_USER\\Environment
stderr: 'UNEXPECTED',
})
const pnpmHomeDir = '.pnpm\\home'
const pnpmHomeDir = tempDir(false)
const pnpmHomeDirNormalized = path.normalize(pnpmHomeDir)
const output = await setup.handler({
force: true,
pnpmHomeDir,
})
expect(execa).toHaveBeenNthCalledWith(3, 'reg', ['query', regKey], { windowsHide: false })
expect(output).toContain(`Setting 'PNPM_HOME' to value '${pnpmHomeDir}'`)
expect(output).toContain(`Setting 'PNPM_HOME' to value '${pnpmHomeDirNormalized}'`)
})
@@ -1,5 +1,7 @@
import { win32 as path } from 'path'
import execa from 'execa'
import { setup } from '@pnpm/plugin-commands-setup'
import { tempDir } from '@pnpm/prepare'
jest.mock('execa')
@@ -51,14 +53,14 @@ HKEY_CURRENT_USER\\Environment
stderr: 'UNEXPECTED',
})
const output = await setup.handler({
pnpmHomeDir: __dirname,
})
const pnpmHomeDir = tempDir(false)
const pnpmHomeDirNormalized = path.normalize(pnpmHomeDir)
const output = await setup.handler({ pnpmHomeDir })
expect(execa).toHaveBeenNthCalledWith(3, 'reg', ['query', regKey], { windowsHide: false })
expect(execa).toHaveBeenNthCalledWith(4, 'reg', ['add', regKey, '/v', 'PNPM_HOME', '/t', 'REG_EXPAND_SZ', '/d', __dirname, '/f'], { windowsHide: false })
expect(execa).toHaveBeenNthCalledWith(5, 'reg', ['add', regKey, '/v', 'Path', '/t', 'REG_EXPAND_SZ', '/d', `${__dirname};${currentPathInRegistry}`, '/f'], { windowsHide: false })
expect(output).toContain(`Setting 'PNPM_HOME' to value '${__dirname}`)
expect(execa).toHaveBeenNthCalledWith(4, 'reg', ['add', regKey, '/v', 'PNPM_HOME', '/t', 'REG_EXPAND_SZ', '/d', pnpmHomeDirNormalized, '/f'], { windowsHide: false })
expect(execa).toHaveBeenNthCalledWith(5, 'reg', ['add', regKey, '/v', 'Path', '/t', 'REG_EXPAND_SZ', '/d', `${pnpmHomeDirNormalized};${currentPathInRegistry}`, '/f'], { windowsHide: false })
expect(output).toContain(`Setting 'PNPM_HOME' to value '${pnpmHomeDirNormalized}`)
expect(output).toContain('FAILED TO SET PNPM_HOME')
expect(output).toContain('Updating PATH')
expect(output).toContain('FAILED TO UPDATE PATH')
+2 -2
View File
@@ -16,13 +16,13 @@ const tmpPath = path.join(__dirname, `../../../../pnpm_tmp/${uniqueString()}`)
let dirNumber = 0
export function tempDir () {
export function tempDir (chdir: boolean = true) {
dirNumber++
const dirname = dirNumber.toString()
const tmpDir = path.join(tmpPath, dirname)
fs.mkdirSync(tmpDir, { recursive: true })
process.chdir(tmpDir)
if (chdir) process.chdir(tmpDir)
return tmpDir
}