|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +const mockExeca = jest.fn(); |
| 11 | + |
| 12 | +jest.mock('fs', () => ({ |
| 13 | + ...jest.requireActual('fs'), |
| 14 | + existsSync: jest.fn().mockReturnValue(true), |
| 15 | +})); |
| 16 | + |
| 17 | +jest.mock('@kbn/repo-info', () => ({ |
| 18 | + REPO_ROOT: '/repo', |
| 19 | +})); |
| 20 | + |
| 21 | +jest.mock('@kbn/dev-utils', () => ({ |
| 22 | + getRemoteDefaultBranchRefs: jest.fn(), |
| 23 | + resolveNearestMergeBase: jest.fn(), |
| 24 | +})); |
| 25 | + |
| 26 | +jest.mock('execa', () => ({ |
| 27 | + __esModule: true, |
| 28 | + default: mockExeca, |
| 29 | +})); |
| 30 | + |
| 31 | +const createMoonProjectsOutput = (projects: Array<{ id: string; sourceRoot: string }>) => |
| 32 | + JSON.stringify({ |
| 33 | + projects: projects.map((project) => ({ |
| 34 | + id: project.id, |
| 35 | + source: project.sourceRoot, |
| 36 | + config: { |
| 37 | + project: { |
| 38 | + metadata: { |
| 39 | + sourceRoot: project.sourceRoot, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + })), |
| 44 | + }); |
| 45 | + |
| 46 | +describe('getAffectedMoonProjectsFromChangedFiles', () => { |
| 47 | + beforeEach(() => { |
| 48 | + jest.resetModules(); |
| 49 | + mockExeca.mockReset(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('adds the root project for repo-root TypeScript inputs', async () => { |
| 53 | + mockExeca.mockResolvedValueOnce({ |
| 54 | + stdout: createMoonProjectsOutput([]), |
| 55 | + }); |
| 56 | + |
| 57 | + const { getAffectedMoonProjectsFromChangedFiles } = await import('./query_projects'); |
| 58 | + |
| 59 | + await expect( |
| 60 | + getAffectedMoonProjectsFromChangedFiles({ |
| 61 | + changedFilesJson: JSON.stringify({ files: ['tsconfig.base.json'] }), |
| 62 | + }) |
| 63 | + ).resolves.toEqual([{ id: 'kibana', sourceRoot: '.' }]); |
| 64 | + }); |
| 65 | + |
| 66 | + it('adds the root project alongside affected package projects for typings changes', async () => { |
| 67 | + mockExeca.mockResolvedValueOnce({ |
| 68 | + stdout: createMoonProjectsOutput([{ id: 'foo', sourceRoot: 'packages/foo' }]), |
| 69 | + }); |
| 70 | + |
| 71 | + const { getAffectedMoonProjectsFromChangedFiles } = await import('./query_projects'); |
| 72 | + |
| 73 | + await expect( |
| 74 | + getAffectedMoonProjectsFromChangedFiles({ |
| 75 | + changedFilesJson: JSON.stringify({ |
| 76 | + files: ['packages/foo/src/index.ts', 'typings/something.d.ts'], |
| 77 | + }), |
| 78 | + }) |
| 79 | + ).resolves.toEqual([ |
| 80 | + { id: 'foo', sourceRoot: 'packages/foo' }, |
| 81 | + { id: 'kibana', sourceRoot: '.' }, |
| 82 | + ]); |
| 83 | + }); |
| 84 | + |
| 85 | + it('does not add the root project for package-owned files', async () => { |
| 86 | + mockExeca.mockResolvedValueOnce({ |
| 87 | + stdout: createMoonProjectsOutput([{ id: 'foo', sourceRoot: 'packages/foo' }]), |
| 88 | + }); |
| 89 | + |
| 90 | + const { getAffectedMoonProjectsFromChangedFiles } = await import('./query_projects'); |
| 91 | + |
| 92 | + await expect( |
| 93 | + getAffectedMoonProjectsFromChangedFiles({ |
| 94 | + changedFilesJson: JSON.stringify({ files: ['packages/foo/src/index.ts'] }), |
| 95 | + }) |
| 96 | + ).resolves.toEqual([{ id: 'foo', sourceRoot: 'packages/foo' }]); |
| 97 | + }); |
| 98 | + |
| 99 | + it('does not add the root project for unrelated repo-root files', async () => { |
| 100 | + mockExeca.mockResolvedValueOnce({ |
| 101 | + stdout: createMoonProjectsOutput([]), |
| 102 | + }); |
| 103 | + |
| 104 | + const { getAffectedMoonProjectsFromChangedFiles } = await import('./query_projects'); |
| 105 | + |
| 106 | + await expect( |
| 107 | + getAffectedMoonProjectsFromChangedFiles({ |
| 108 | + changedFilesJson: JSON.stringify({ files: ['.github/CODEOWNERS'] }), |
| 109 | + }) |
| 110 | + ).resolves.toEqual([]); |
| 111 | + }); |
| 112 | + |
| 113 | + it('keeps Moon-reported root project results without querying all projects again', async () => { |
| 114 | + mockExeca.mockResolvedValueOnce({ |
| 115 | + stdout: createMoonProjectsOutput([{ id: 'kibana', sourceRoot: '.' }]), |
| 116 | + }); |
| 117 | + |
| 118 | + const { getAffectedMoonProjectsFromChangedFiles } = await import('./query_projects'); |
| 119 | + |
| 120 | + await expect( |
| 121 | + getAffectedMoonProjectsFromChangedFiles({ |
| 122 | + changedFilesJson: JSON.stringify({ files: ['tsconfig.base.json'] }), |
| 123 | + }) |
| 124 | + ).resolves.toEqual([{ id: 'kibana', sourceRoot: '.' }]); |
| 125 | + |
| 126 | + expect(mockExeca).toHaveBeenCalledTimes(1); |
| 127 | + }); |
| 128 | +}); |
0 commit comments