From 0bdf6bab576071a4af4d4e4e15fddfa78ae48523 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Wed, 11 May 2022 03:36:33 +0300 Subject: [PATCH] test(setup): improve tests --- .../src/setupOnWindows.ts | 1 + .../plugin-commands-setup/test/setup.test.ts | 83 +++++++++---------- .../test/setupOnWindows/01.test.ts | 3 +- .../test/setupOnWindows/02.test.ts | 3 +- .../test/setupOnWindows/03.test.ts | 3 +- .../test/setupOnWindows/04.test.ts | 16 ++-- .../test/setupOnWindows/05.test.ts | 11 ++- .../test/setupOnWindows/06.test.ts | 14 ++-- .../test/setupOnWindows/07.test.ts | 14 ++-- privatePackages/prepare/src/index.ts | 4 +- 10 files changed, 79 insertions(+), 73 deletions(-) diff --git a/packages/plugin-commands-setup/src/setupOnWindows.ts b/packages/plugin-commands-setup/src/setupOnWindows.ts index 5eae05b0c3..12b2ffb073 100644 --- a/packages/plugin-commands-setup/src/setupOnWindows.ts +++ b/packages/plugin-commands-setup/src/setupOnWindows.ts @@ -28,6 +28,7 @@ function pathIncludesDir (pathValue: string, dir: string): boolean { } export async function setupWindowsEnvironmentPath (pnpmHomeDir: string, opts: { force: boolean }): Promise { + 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. diff --git a/packages/plugin-commands-setup/test/setup.test.ts b/packages/plugin-commands-setup/test/setup.test.ts index a3bc21cefa..38d4085982 100644 --- a/packages/plugin-commands-setup/test/setup.test.ts +++ b/packages/plugin-commands-setup/test/setup.test.ts @@ -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(` diff --git a/packages/plugin-commands-setup/test/setupOnWindows/01.test.ts b/packages/plugin-commands-setup/test/setupOnWindows/01.test.ts index f24e31d22b..6dccee8eee 100644 --- a/packages/plugin-commands-setup/test/setupOnWindows/01.test.ts +++ b/packages/plugin-commands-setup/test/setupOnWindows/01.test.ts @@ -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 }) diff --git a/packages/plugin-commands-setup/test/setupOnWindows/02.test.ts b/packages/plugin-commands-setup/test/setupOnWindows/02.test.ts index 8b45a2c07c..3a06b77482 100644 --- a/packages/plugin-commands-setup/test/setupOnWindows/02.test.ts +++ b/packages/plugin-commands-setup/test/setupOnWindows/02.test.ts @@ -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 }) diff --git a/packages/plugin-commands-setup/test/setupOnWindows/03.test.ts b/packages/plugin-commands-setup/test/setupOnWindows/03.test.ts index f7f7b26e96..2edacd4551 100644 --- a/packages/plugin-commands-setup/test/setupOnWindows/03.test.ts +++ b/packages/plugin-commands-setup/test/setupOnWindows/03.test.ts @@ -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 }) diff --git a/packages/plugin-commands-setup/test/setupOnWindows/04.test.ts b/packages/plugin-commands-setup/test/setupOnWindows/04.test.ts index af2bf81860..ff175ce1b4 100644 --- a/packages/plugin-commands-setup/test/setupOnWindows/04.test.ts +++ b/packages/plugin-commands-setup/test/setupOnWindows/04.test.ts @@ -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') diff --git a/packages/plugin-commands-setup/test/setupOnWindows/05.test.ts b/packages/plugin-commands-setup/test/setupOnWindows/05.test.ts index 6e0b137fec..e120b258ec 100644 --- a/packages/plugin-commands-setup/test/setupOnWindows/05.test.ts +++ b/packages/plugin-commands-setup/test/setupOnWindows/05.test.ts @@ -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') }) diff --git a/packages/plugin-commands-setup/test/setupOnWindows/06.test.ts b/packages/plugin-commands-setup/test/setupOnWindows/06.test.ts index 27b8d719ff..3c74784edd 100644 --- a/packages/plugin-commands-setup/test/setupOnWindows/06.test.ts +++ b/packages/plugin-commands-setup/test/setupOnWindows/06.test.ts @@ -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}'`) }) diff --git a/packages/plugin-commands-setup/test/setupOnWindows/07.test.ts b/packages/plugin-commands-setup/test/setupOnWindows/07.test.ts index dbfb27cdd4..e38f23456d 100644 --- a/packages/plugin-commands-setup/test/setupOnWindows/07.test.ts +++ b/packages/plugin-commands-setup/test/setupOnWindows/07.test.ts @@ -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') diff --git a/privatePackages/prepare/src/index.ts b/privatePackages/prepare/src/index.ts index b8d7e4619a..1ccdbb8b42 100644 --- a/privatePackages/prepare/src/index.ts +++ b/privatePackages/prepare/src/index.ts @@ -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 }