| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {PathLike} from 'node:fs'; |
| b69ab31 | | | 9 | import type {FileHandle} from 'node:fs/promises'; |
| b69ab31 | | | 10 | import type {Repository} from '../Repository'; |
| b69ab31 | | | 11 | import type {ServerPlatform} from '../serverPlatform'; |
| b69ab31 | | | 12 | import type {RepositoryContext} from '../serverTypes'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | import {GeneratedStatus} from 'isl/src/types'; |
| b69ab31 | | | 15 | import {promises} from 'node:fs'; |
| b69ab31 | | | 16 | import {mockLogger} from 'shared/testUtils'; |
| b69ab31 | | | 17 | import {GeneratedFilesDetector} from '../GeneratedFiles'; |
| b69ab31 | | | 18 | import {makeServerSideTracker} from '../analytics/serverSideTracker'; |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | /* eslint-disable require-await */ |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | const mockTracker = makeServerSideTracker( |
| b69ab31 | | | 23 | mockLogger, |
| b69ab31 | | | 24 | {platformName: 'test'} as ServerPlatform, |
| b69ab31 | | | 25 | '0.1', |
| b69ab31 | | | 26 | jest.fn(), |
| b69ab31 | | | 27 | ); |
| b69ab31 | | | 28 | |
| b69ab31 | | | 29 | const mockCtx: RepositoryContext = { |
| b69ab31 | | | 30 | cwd: 'cwd', |
| b69ab31 | | | 31 | cmd: 'sl', |
| b69ab31 | | | 32 | logger: mockLogger, |
| b69ab31 | | | 33 | tracker: mockTracker, |
| b69ab31 | | | 34 | }; |
| b69ab31 | | | 35 | |
| b69ab31 | | | 36 | describe('GeneratedFiles', () => { |
| b69ab31 | | | 37 | describe('getGeneratedFilePathRegex', () => { |
| b69ab31 | | | 38 | it('can take configured custom regex', async () => { |
| b69ab31 | | | 39 | jest.spyOn(promises, 'open').mockImplementation(async () => { |
| b69ab31 | | | 40 | throw new Error('skipping in tests'); |
| b69ab31 | | | 41 | }); |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | const mockRepo = { |
| b69ab31 | | | 44 | getConfig: async () => Promise.resolve('foobar'), |
| b69ab31 | | | 45 | logger: mockLogger, |
| b69ab31 | | | 46 | } as unknown as Repository; |
| b69ab31 | | | 47 | const detector = new GeneratedFilesDetector(); |
| b69ab31 | | | 48 | const result = await detector.queryFilesGenerated(mockRepo, mockCtx, '/', [ |
| b69ab31 | | | 49 | 'src/myFile.js', |
| b69ab31 | | | 50 | 'foobar', |
| b69ab31 | | | 51 | ]); |
| b69ab31 | | | 52 | expect(result).toEqual({ |
| b69ab31 | | | 53 | 'src/myFile.js': GeneratedStatus.Manual, |
| b69ab31 | | | 54 | foobar: GeneratedStatus.Generated, |
| b69ab31 | | | 55 | }); |
| b69ab31 | | | 56 | }); |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | it('detects yarn.lock as generated', async () => { |
| b69ab31 | | | 59 | jest.spyOn(promises, 'open').mockImplementation(async () => { |
| b69ab31 | | | 60 | throw new Error('skipping in tests'); |
| b69ab31 | | | 61 | }); |
| b69ab31 | | | 62 | |
| b69ab31 | | | 63 | const mockRepo = { |
| b69ab31 | | | 64 | getConfig: async () => Promise.resolve(undefined), |
| b69ab31 | | | 65 | logger: mockLogger, |
| b69ab31 | | | 66 | } as unknown as Repository; |
| b69ab31 | | | 67 | const detector = new GeneratedFilesDetector(); |
| b69ab31 | | | 68 | const result = await detector.queryFilesGenerated(mockRepo, mockCtx, '/', [ |
| b69ab31 | | | 69 | 'src/myFile.js', |
| b69ab31 | | | 70 | 'yarn.lock', |
| b69ab31 | | | 71 | 'subproject/yarn.lock', |
| b69ab31 | | | 72 | ]); |
| b69ab31 | | | 73 | expect(result).toEqual({ |
| b69ab31 | | | 74 | 'src/myFile.js': GeneratedStatus.Manual, |
| b69ab31 | | | 75 | 'yarn.lock': GeneratedStatus.Generated, |
| b69ab31 | | | 76 | 'subproject/yarn.lock': GeneratedStatus.Generated, |
| b69ab31 | | | 77 | }); |
| b69ab31 | | | 78 | }); |
| b69ab31 | | | 79 | }); |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | describe('readFilesLookingForGeneratedTag', () => { |
| b69ab31 | | | 82 | it('detects generate tag in file content', async () => { |
| b69ab31 | | | 83 | jest.spyOn(promises, 'open').mockImplementation(async (filePath: PathLike, _flags, _mod) => { |
| b69ab31 | | | 84 | return { |
| b69ab31 | | | 85 | read: jest.fn(async () => ({ |
| b69ab31 | | | 86 | buffer: |
| b69ab31 | | | 87 | filePath === '/myGeneratedFile.js' |
| b69ab31 | | | 88 | ? `/* this file is ${'@'}generated */` |
| b69ab31 | | | 89 | : filePath === '/myPartiallyGeneratedFile.js' |
| b69ab31 | | | 90 | ? `/* this file is ${'@'}partially-generated */` |
| b69ab31 | | | 91 | : '// Normal file content', |
| b69ab31 | | | 92 | })), |
| b69ab31 | | | 93 | close: jest.fn(), |
| b69ab31 | | | 94 | } as unknown as FileHandle; |
| b69ab31 | | | 95 | }); |
| b69ab31 | | | 96 | |
| b69ab31 | | | 97 | const mockRepo = { |
| b69ab31 | | | 98 | getConfig: async () => Promise.resolve(undefined), |
| b69ab31 | | | 99 | logger: mockLogger, |
| b69ab31 | | | 100 | } as unknown as Repository; |
| b69ab31 | | | 101 | const detector = new GeneratedFilesDetector(); |
| b69ab31 | | | 102 | const result = await detector.queryFilesGenerated(mockRepo, mockCtx, '/', [ |
| b69ab31 | | | 103 | 'myFile.js', |
| b69ab31 | | | 104 | 'myPartiallyGeneratedFile.js', |
| b69ab31 | | | 105 | 'myGeneratedFile.js', |
| b69ab31 | | | 106 | ]); |
| b69ab31 | | | 107 | expect(result).toEqual({ |
| b69ab31 | | | 108 | 'myFile.js': GeneratedStatus.Manual, |
| b69ab31 | | | 109 | 'myPartiallyGeneratedFile.js': GeneratedStatus.PartiallyGenerated, |
| b69ab31 | | | 110 | 'myGeneratedFile.js': GeneratedStatus.Generated, |
| b69ab31 | | | 111 | }); |
| b69ab31 | | | 112 | }); |
| b69ab31 | | | 113 | }); |
| b69ab31 | | | 114 | }); |