Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build: Switch to an alternative node:fs mocking system #24920

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions code/lib/core-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@
},
"devDependencies": {
"@types/fs-extra": "^11.0.1",
"@types/mock-fs": "^4.13.1",
"@types/picomatch": "^2.3.0",
"mock-fs": "^5.2.0",
"slash": "^5.0.0",
"type-fest": "~2.19",
"typescript": "~4.9.3"
Expand Down
66 changes: 66 additions & 0 deletions code/lib/core-common/src/mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import path from 'node:path';
import fs from 'node:fs';
import os from 'node:os';

const tempDir = os.tmpdir();

let savedDir: string | null = null;

interface Dir {
[key: string]: File;
}
type File = Dir | string;

function mkdirpSync(nested: string): void {
const paths = [];
let curr = nested;
for (;;) {
paths.push(curr);
const { root, dir } = path.parse(curr);
if (root === dir) {
break;
}
curr = dir;
}
for (let i = paths.length - 1; i >= 0; i -= 1) {
try {
fs.mkdirSync(paths[i]);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'EEXIST') {
throw error;
}
}
}
}

function mock(root: Dir): void {
if (savedDir !== null) {
throw new Error(`A file system is already being mocked.`);
}
savedDir = fs.mkdtempSync(path.join(tempDir, 'storybook-'));
const visit = (curr: string, dir: Dir) => {
Object.entries(dir).forEach(([name, contents]) => {
const filepath = path.join(curr, name);
if (typeof contents === 'string') {
mkdirpSync(path.dirname(filepath));
fs.writeFileSync(filepath, contents, 'utf-8');
} else {
fs.mkdirSync(filepath);
visit(filepath, contents);
}
});
};
visit(savedDir, root);
process.chdir(savedDir);
}

mock.restore = () => {
if (savedDir === null) {
throw new Error(`There is no file system to restore.`);
}
process.chdir(savedDir);
// TODO rimraf the tmp directory
savedDir = null;
};

export default mock;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mock from 'mock-fs';
import mock from '../../mock';
import { getInterpretedFile } from '../interpret-files';

describe('interpret-files', () => {
Expand Down
2 changes: 1 addition & 1 deletion code/lib/core-common/src/utils/__tests__/template.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mock from 'mock-fs';
import mock from '../../mock';
import { getPreviewHeadTemplate, getPreviewBodyTemplate } from '../template';

const HEAD_HTML_CONTENTS = '<script>console.log("custom script!");</script>';
Expand Down
18 changes: 0 additions & 18 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6386,7 +6386,6 @@ __metadata:
"@storybook/types": "workspace:*"
"@types/find-cache-dir": "npm:^3.2.1"
"@types/fs-extra": "npm:^11.0.1"
"@types/mock-fs": "npm:^4.13.1"
"@types/node": "npm:^18.0.0"
"@types/node-fetch": "npm:^2.6.4"
"@types/picomatch": "npm:^2.3.0"
Expand All @@ -6401,7 +6400,6 @@ __metadata:
glob: "npm:^10.0.0"
handlebars: "npm:^4.7.7"
lazy-universal-dotenv: "npm:^4.0.0"
mock-fs: "npm:^5.2.0"
node-fetch: "npm:^2.0.0"
picomatch: "npm:^2.3.0"
pkg-dir: "npm:^5.0.0"
Expand Down Expand Up @@ -9060,15 +9058,6 @@ __metadata:
languageName: node
linkType: hard

"@types/mock-fs@npm:^4.13.1":
version: 4.13.2
resolution: "@types/mock-fs@npm:4.13.2"
dependencies:
"@types/node": "npm:*"
checksum: c589d8ba674e9d2279c6321841ee5b4e9ba3138a8790cbe2a61ea1da1e9d254188ef9f1a2f66539b225aaebcd56f746551f4723de24afc0c430d9e7c68344a7b
languageName: node
linkType: hard

"@types/ms@npm:*":
version: 0.7.32
resolution: "@types/ms@npm:0.7.32"
Expand Down Expand Up @@ -22517,13 +22506,6 @@ __metadata:
languageName: node
linkType: hard

"mock-fs@npm:^5.2.0":
version: 5.2.0
resolution: "mock-fs@npm:5.2.0"
checksum: e917e71295ee9d805c7d9ab0214a66658db0212f35731ba0c75dc1ccbb9964e6787a6d725561f05fcf569c64cf4a1176f5ce438f98b009227520f646f8abf174
languageName: node
linkType: hard

"moo@npm:^0.5.0":
version: 0.5.2
resolution: "moo@npm:0.5.2"
Expand Down