Skip to content

Commit

Permalink
Merge pull request #974 from chromaui/fix-storybook-base-dir-check
Browse files Browse the repository at this point in the history
Properly check `storybookBaseDir` against repository root rather than CWD
  • Loading branch information
ghengeveld committed May 7, 2024
2 parents a82fa62 + 10ac294 commit c066f80
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
29 changes: 21 additions & 8 deletions node-src/lib/checkStorybookBaseDir.test.ts
@@ -1,8 +1,21 @@
import { describe, expect, it } from 'vitest';
import { checkStorybookBaseDir } from './checkStorybookBaseDir';
import path from 'path';
import TestLogger from './testLogger';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import * as git from '../git/git';
import { checkStorybookBaseDir } from './checkStorybookBaseDir';
import { exitCodes } from './setExitCode';
import TestLogger from './testLogger';

vi.mock('../git/git');

const getRepositoryRoot = vi.mocked(git.getRepositoryRoot);

beforeEach(() => {
getRepositoryRoot.mockResolvedValue(path.join(__dirname, '../__mocks__'));
});
afterEach(() => {
getRepositoryRoot.mockReset();
});

const getContext: any = (storybookBaseDir?: string) => ({
log: new TestLogger(),
Expand All @@ -11,7 +24,7 @@ const getContext: any = (storybookBaseDir?: string) => ({

describe('checkStorybookBaseDir', () => {
it('should return if a js(x)/ts(x) module in stats exists at the path prepended by the storybookBaseDir', async () => {
const ctx = getContext(path.join(__dirname, '../__mocks__/storybookBaseDir'));
const ctx = getContext('storybookBaseDir');

const statsWithJsModule = {
modules: [
Expand Down Expand Up @@ -64,7 +77,7 @@ describe('checkStorybookBaseDir', () => {
],
};

const ctxWithBaseDir = getContext(path.join(__dirname, '../__mocks__/wrong'));
const ctxWithBaseDir = getContext('wrong');
await expect(() => checkStorybookBaseDir(ctxWithBaseDir, stats)).rejects.toThrow();
expect(ctxWithBaseDir.exitCode).toBe(exitCodes.INVALID_OPTIONS);

Expand All @@ -74,12 +87,12 @@ describe('checkStorybookBaseDir', () => {
});

it('should not consider modules in node_modules as valid files to match', async () => {
const ctx = getContext(path.join(__dirname, '../../'));
const ctx = getContext('../..');
const stats = {
modules: [
{
id: './node_modules/@storybook/core-client/dist/esm/globals/polyfills.js',
name: './node_modules/@storybook/core-client/dist/esm/globals/polyfills.js',
id: './node_modules/env-ci/index.js',
name: './node_modules/env-ci/index.js',
},
],
};
Expand Down
6 changes: 4 additions & 2 deletions node-src/lib/checkStorybookBaseDir.ts
Expand Up @@ -4,10 +4,12 @@ import { invalidStorybookBaseDir } from '../ui/messages/errors/invalidStorybookB
import { Context, Stats } from '../types';
import pLimit from 'p-limit';
import { exitCodes, setExitCode } from './setExitCode';
import { getRepositoryRoot } from '../git/git';

export async function checkStorybookBaseDir(ctx: Context, stats: Stats) {
const repositoryRoot = await getRepositoryRoot();
const { storybookBaseDir = '' } = ctx.options;
ctx.log.debug('Storybook base directory:', storybookBaseDir);
ctx.log.debug('Storybook base directory:', path.join(repositoryRoot, storybookBaseDir));

// Find all js(x)/ts(x) files in stats that are not in node_modules
const sourceModuleFiles = stats.modules.filter(
Expand All @@ -22,7 +24,7 @@ export async function checkStorybookBaseDir(ctx: Context, stats: Stats) {
await Promise.any(
sourceModuleFiles.map((file) => {
return limitConcurrency(() => {
const absolutePath = path.join(storybookBaseDir, file.name);
const absolutePath = path.join(repositoryRoot, storybookBaseDir, file.name);
return new Promise((resolve, reject) =>
fs.access(absolutePath, (err) => {
if (err) {
Expand Down

0 comments on commit c066f80

Please sign in to comment.