|
1 | 1 | import { expect } from 'chai'; |
2 | 2 | import sinon from 'sinon'; |
| 3 | +import type { PathLike } from 'node:fs'; |
3 | 4 | import * as path from 'path'; |
4 | 5 | import { FsUtility, handleAndLogError, messageHandler } from '@contentstack/cli-utilities'; |
| 6 | + |
| 7 | +declare global { |
| 8 | + var __CONTENTSTACK_TEST_FS__: typeof import('node:fs'); |
| 9 | +} |
| 10 | +const fs = globalThis.__CONTENTSTACK_TEST_FS__; |
5 | 11 | import * as utilities from '@contentstack/cli-utilities'; |
6 | 12 | import EntriesExport from '../../../../src/export/modules/entries'; |
7 | 13 | import ExportConfig from '../../../../src/types/export-config'; |
@@ -146,6 +152,35 @@ describe('EntriesExport', () => { |
146 | 152 | sandbox.stub(FsUtility.prototype, 'readdir').returns([]); |
147 | 153 | sandbox.stub(FsUtility.prototype, 'readFile').returns(undefined); |
148 | 154 |
|
| 155 | + // readContentTypeSchemas() uses node:fs — mirror FsUtility stubs; only intercept content_types paths |
| 156 | + const originalExistsSync = fs.existsSync.bind(fs); |
| 157 | + const originalReaddirSync = fs.readdirSync.bind(fs); |
| 158 | + const originalReadFileSync = fs.readFileSync.bind(fs); |
| 159 | + sandbox.stub(fs, 'existsSync').callsFake((p: PathLike) => { |
| 160 | + const s = String(p); |
| 161 | + if (s.includes('content_types')) { |
| 162 | + return true; |
| 163 | + } |
| 164 | + return originalExistsSync(p); |
| 165 | + }); |
| 166 | + sandbox.stub(fs, 'readdirSync').callsFake((dirPath: PathLike) => { |
| 167 | + const s = String(dirPath); |
| 168 | + if (s.includes('content_types')) { |
| 169 | + return (FsUtility.prototype.readdir as sinon.SinonStub)(s); |
| 170 | + } |
| 171 | + return originalReaddirSync(dirPath); |
| 172 | + }); |
| 173 | + sandbox.stub(fs, 'readFileSync').callsFake((filePath: string | Buffer | URL, encoding?: Parameters<typeof fs.readFileSync>[1]) => { |
| 174 | + const p = String(filePath); |
| 175 | + if (p.includes('content_types')) { |
| 176 | + const r = (FsUtility.prototype.readFile as sinon.SinonStub)(p); |
| 177 | + if (r !== undefined) { |
| 178 | + return typeof r === 'string' ? r : JSON.stringify(r); |
| 179 | + } |
| 180 | + } |
| 181 | + return originalReadFileSync(filePath, encoding as any); |
| 182 | + }); |
| 183 | + |
149 | 184 | entriesExport = new EntriesExport({ |
150 | 185 | exportConfig: mockExportConfig, |
151 | 186 | stackAPIClient: mockStackAPIClient, |
@@ -275,7 +310,9 @@ describe('EntriesExport', () => { |
275 | 310 |
|
276 | 311 | const locales = [{ code: 'en-us' }]; |
277 | 312 | const contentTypes = [{ uid: 'ct-1', title: 'Content Type 1' }]; |
278 | | - mockFsUtil.readFile.onFirstCall().returns(locales).onSecondCall().returns(contentTypes); |
| 313 | + mockFsUtil.readFile.returns(locales); |
| 314 | + (FsUtility.prototype.readdir as sinon.SinonStub).returns(['ct-1.json']); |
| 315 | + (FsUtility.prototype.readFile as sinon.SinonStub).returns(contentTypes[0]); |
279 | 316 |
|
280 | 317 | // Mock successful entry fetch - use callsFake to preserve call tracking |
281 | 318 | const contentTypeStub = sandbox.stub().returns({ |
|
0 commit comments