Skip to content

Commit 8de539a

Browse files
CopilotSidnioulz
andcommitted
fix: use --with-deps on macOS/Windows and CI, skip on other Linux distros
Co-authored-by: Sidnioulz <5108577+Sidnioulz@users.noreply.github.com>
1 parent 590dffb commit 8de539a

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

code/addons/vitest/src/postinstall.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync } from 'node:fs';
22
import * as fs from 'node:fs/promises';
33
import { writeFile } from 'node:fs/promises';
4+
import os from 'node:os';
45

56
import { babelParse, generate, traverse } from 'storybook/internal/babel';
67
import { AddonVitestService } from 'storybook/internal/cli';
@@ -165,9 +166,17 @@ export default async function postInstall(options: PostinstallOptions) {
165166
useRemotePkg: !!options.skipInstall,
166167
});
167168
} else {
169+
const platform = os.platform();
170+
const useWithDeps = platform === 'darwin' || platform === 'win32';
171+
const manualCommand = useWithDeps
172+
? 'npx playwright install chromium --with-deps'
173+
: 'npx playwright install chromium';
174+
const linuxNote = !useWithDeps
175+
? '\n Note: add --with-deps to the command above if you are on Debian or Ubuntu.'
176+
: '';
168177
logger.warn(dedent`
169178
Playwright browser binaries installation skipped. Please run the following command manually later:
170-
${CLI_COLORS.cta('npx playwright install chromium')}
179+
${CLI_COLORS.cta(manualCommand)}${linuxNote}
171180
`);
172181
}
173182
}

code/core/src/cli/AddonVitestService.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as fs from 'node:fs/promises';
2+
import os from 'node:os';
23

34
import { beforeEach, describe, expect, it, vi } from 'vitest';
45

@@ -14,6 +15,7 @@ import { SupportedBuilder, SupportedFramework } from '../types';
1415
import { AddonVitestService } from './AddonVitestService';
1516

1617
vi.mock('node:fs/promises', { spy: true });
18+
vi.mock('node:os', { spy: true });
1719
vi.mock('storybook/internal/common', { spy: true });
1820
vi.mock('storybook/internal/node-logger', { spy: true });
1921
vi.mock('empathic/find', { spy: true });
@@ -418,6 +420,7 @@ describe('AddonVitestService', () => {
418420
it('should execute playwright install command', async () => {
419421
const originalCI = process.env.CI;
420422
delete process.env.CI;
423+
vi.mocked(os.platform).mockReturnValue('linux');
421424
try {
422425
type ChildProcessFactory = (signal?: AbortSignal) => ResultPromise;
423426
let commandFactory: ChildProcessFactory | ChildProcessFactory[];
@@ -447,6 +450,7 @@ describe('AddonVitestService', () => {
447450
it('should execute playwright install command with --with-deps in CI', async () => {
448451
const originalCI = process.env.CI;
449452
process.env.CI = 'true';
453+
vi.mocked(os.platform).mockReturnValue('linux');
450454
try {
451455
type ChildProcessFactory = (signal?: AbortSignal) => ResultPromise;
452456
let commandFactory: ChildProcessFactory | ChildProcessFactory[];
@@ -474,6 +478,38 @@ describe('AddonVitestService', () => {
474478
}
475479
});
476480

481+
it.each(['darwin', 'win32'] as const)(
482+
'should execute playwright install command with --with-deps on %s',
483+
async (platform) => {
484+
const originalCI = process.env.CI;
485+
delete process.env.CI;
486+
vi.mocked(os.platform).mockReturnValue(platform);
487+
try {
488+
type ChildProcessFactory = (signal?: AbortSignal) => ResultPromise;
489+
let commandFactory: ChildProcessFactory | ChildProcessFactory[];
490+
vi.mocked(prompt.confirm).mockResolvedValue(true);
491+
vi.mocked(prompt.executeTaskWithSpinner).mockImplementation(
492+
async (factory: ChildProcessFactory | ChildProcessFactory[]) => {
493+
commandFactory = Array.isArray(factory) ? factory[0] : factory;
494+
commandFactory();
495+
}
496+
);
497+
498+
await service.installPlaywright();
499+
500+
expect(mockPackageManager.runPackageCommand).toHaveBeenCalledWith({
501+
args: ['playwright', 'install', 'chromium', '--with-deps'],
502+
signal: undefined,
503+
stdio: ['inherit', 'pipe', 'pipe'],
504+
});
505+
} finally {
506+
if (originalCI !== undefined) {
507+
process.env.CI = originalCI;
508+
}
509+
}
510+
}
511+
);
512+
477513
it('should capture error stack when installation fails', async () => {
478514
const error = new Error('Installation failed');
479515
error.stack = 'Error stack trace';

code/core/src/cli/AddonVitestService.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs/promises';
2+
import os from 'node:os';
23

34
import * as babel from 'storybook/internal/babel';
45
import type { JsPackageManager } from 'storybook/internal/common';
@@ -106,8 +107,10 @@ export class AddonVitestService {
106107
/**
107108
* Install Playwright browser binaries for @storybook/addon-vitest
108109
*
109-
* Installs Chromium via `npx playwright install chromium`. In CI environments, also installs
110-
* system-level browser dependencies via `--with-deps`.
110+
* Installs Chromium via `npx playwright install chromium`. In CI environments and on
111+
* macOS/Windows (officially supported platforms), also installs system-level browser dependencies
112+
* via `--with-deps`. On other platforms (e.g. Linux), `--with-deps` is omitted to avoid requiring
113+
* `sudo` — system packages are typically managed by the distro package manager.
111114
*
112115
* @param packageManager - The package manager to use for installation
113116
* @param prompt - The prompt instance for displaying progress
@@ -124,7 +127,9 @@ export class AddonVitestService {
124127
): Promise<{ errors: string[]; result: 'installed' | 'skipped' | 'aborted' | 'failed' }> {
125128
const errors: string[] = [];
126129

127-
const playwrightCommand = process.env.CI
130+
const platform = os.platform();
131+
const useWithDeps = !!process.env.CI || platform === 'darwin' || platform === 'win32';
132+
const playwrightCommand = useWithDeps
128133
? ['playwright', 'install', 'chromium', '--with-deps']
129134
: ['playwright', 'install', 'chromium'];
130135
const playwrightCommandString = this.packageManager.getPackageCommand(playwrightCommand);

0 commit comments

Comments
 (0)