|
| 1 | +import { fs } from 'memfs'; |
| 2 | +import { join } from 'path'; |
| 3 | +import { describe, expect, test, vi } from 'vitest'; |
| 4 | +import type { FsUpdates, UpdateAppOptions } from '../types'; |
| 5 | +import { mergeIntegrationDir } from './update-files'; |
| 6 | + |
| 7 | +vi.mock('node:fs', () => ({ |
| 8 | + default: fs, |
| 9 | +})); |
| 10 | + |
| 11 | +function setup() { |
| 12 | + const fakeSrcDir = 'srcDir/subSrcDir'; |
| 13 | + createFakeFiles(fakeSrcDir); |
| 14 | + |
| 15 | + const fakeDestDir = 'destDir/subDestDir'; |
| 16 | + |
| 17 | + const fakeFileUpdates: FsUpdates = { |
| 18 | + files: [], |
| 19 | + installedDeps: {}, |
| 20 | + installedScripts: [], |
| 21 | + }; |
| 22 | + |
| 23 | + const fakeOpts: UpdateAppOptions = { |
| 24 | + rootDir: fakeDestDir, |
| 25 | + integration: 'integration', |
| 26 | + }; |
| 27 | + |
| 28 | + return { |
| 29 | + fakeSrcDir, |
| 30 | + fakeDestDir, |
| 31 | + fakeFileUpdates, |
| 32 | + fakeOpts, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +function createFakeFiles(dir: string) { |
| 37 | + // Create fake src files |
| 38 | + fs.mkdirSync(join(dir, 'src'), { recursive: true }); |
| 39 | + fs.writeFileSync(join(dir, 'fake.ts'), 'fake file'); |
| 40 | + fs.writeFileSync(join(dir, 'package.json'), '{"name": "fake"}'); |
| 41 | + fs.writeFileSync(join(dir, 'src', 'global.css'), 'p{color: red}'); |
| 42 | +} |
| 43 | + |
| 44 | +describe('mergeIntegrationDir', () => { |
| 45 | + test('should merge integration directory', async () => { |
| 46 | + const { fakeSrcDir, fakeDestDir, fakeFileUpdates, fakeOpts } = setup(); |
| 47 | + |
| 48 | + await mergeIntegrationDir(fakeFileUpdates, fakeOpts, fakeSrcDir, fakeDestDir); |
| 49 | + |
| 50 | + const actualResults = fakeFileUpdates.files.map((f) => f.path); |
| 51 | + const expectedResults = [ |
| 52 | + 'destDir/subDestDir/fake.ts', |
| 53 | + 'destDir/subDestDir/package.json', |
| 54 | + 'destDir/subDestDir/src/global.css', |
| 55 | + ]; |
| 56 | + |
| 57 | + expect(actualResults).toEqual(expectedResults); |
| 58 | + }); |
| 59 | + |
| 60 | + test('should merge integration directory in a monorepo', async () => { |
| 61 | + const { fakeSrcDir, fakeDestDir, fakeFileUpdates, fakeOpts } = setup(); |
| 62 | + |
| 63 | + // Create a global file in the destination director |
| 64 | + const monorepoSubDir = join(fakeDestDir, 'apps', 'subpackage', 'src'); |
| 65 | + fs.mkdirSync(monorepoSubDir, { recursive: true }); |
| 66 | + fs.writeFileSync(join(monorepoSubDir, 'global.css'), '/* CSS */'); |
| 67 | + |
| 68 | + // Add a file that should stay in the root |
| 69 | + fs.writeFileSync(join(fakeSrcDir, 'should-stay-in-root.ts'), 'fake file'); |
| 70 | + |
| 71 | + // Creating a folder that should stay in the root |
| 72 | + fs.mkdirSync(join(fakeSrcDir, 'should-stay'), { recursive: true }); |
| 73 | + fs.writeFileSync(join(fakeSrcDir, 'should-stay', 'should-also-stay.ts'), 'fake file'); |
| 74 | + |
| 75 | + fakeOpts.projectDir = 'apps/subpackage'; |
| 76 | + fakeOpts.installDeps = true; |
| 77 | + const fakeAlwaysInRoot = ['should-stay-in-root.ts', 'should-stay']; |
| 78 | + |
| 79 | + await mergeIntegrationDir(fakeFileUpdates, fakeOpts, fakeSrcDir, fakeDestDir, fakeAlwaysInRoot); |
| 80 | + |
| 81 | + const actualResults = fakeFileUpdates.files.map((f) => f.path); |
| 82 | + const expectedResults = [ |
| 83 | + `destDir/subDestDir/apps/subpackage/fake.ts`, |
| 84 | + `destDir/subDestDir/should-stay-in-root.ts`, |
| 85 | + `destDir/subDestDir/package.json`, |
| 86 | + `destDir/subDestDir/should-stay/should-also-stay.ts`, |
| 87 | + `destDir/subDestDir/apps/subpackage/src/global.css`, |
| 88 | + ]; |
| 89 | + |
| 90 | + expect(actualResults).toEqual(expectedResults); |
| 91 | + |
| 92 | + const actualGlobalCssContent = fakeFileUpdates.files.find( |
| 93 | + (f) => f.path === `destDir/subDestDir/apps/subpackage/src/global.css` |
| 94 | + )?.content; |
| 95 | + |
| 96 | + expect(actualGlobalCssContent).toBe('p{color: red}\n\n/* CSS */\n'); |
| 97 | + }); |
| 98 | +}); |
0 commit comments