|
| 1 | +import { createPhpBuilder } from '../pipeline.builder'; |
| 2 | +import { |
| 3 | + createBuilderInput, |
| 4 | + createBuilderOutput, |
| 5 | + createMinimalIr, |
| 6 | + createPipelineContext, |
| 7 | +} from '../test-support/php-builder.test-support'; |
| 8 | + |
| 9 | +const codemodApplyMock = jest.fn(async (_options, next) => { |
| 10 | + await next?.(); |
| 11 | +}); |
| 12 | +const createCodemodHelperImpl = jest.fn(() => ({ |
| 13 | + key: 'builder.generate.php.codemod-ingestion', |
| 14 | + kind: 'builder' as const, |
| 15 | + apply: codemodApplyMock, |
| 16 | +})); |
| 17 | + |
| 18 | +const writerApplyMock = jest.fn(async (_options, next) => { |
| 19 | + await next?.(); |
| 20 | +}); |
| 21 | +const createWriterHelperImpl = jest.fn(() => ({ |
| 22 | + key: 'builder.generate.php.writer', |
| 23 | + kind: 'builder' as const, |
| 24 | + apply: writerApplyMock, |
| 25 | +})); |
| 26 | + |
| 27 | +jest.mock('../pipeline.codemods', () => ({ |
| 28 | + createPhpCodemodIngestionHelper: jest.fn((options) => |
| 29 | + createCodemodHelperImpl(options) |
| 30 | + ), |
| 31 | +})); |
| 32 | + |
| 33 | +jest.mock('@wpkernel/php-json-ast', () => { |
| 34 | + const actual = jest.requireActual('@wpkernel/php-json-ast'); |
| 35 | + return { |
| 36 | + ...actual, |
| 37 | + createPhpProgramWriterHelper: jest.fn((options) => |
| 38 | + createWriterHelperImpl(options) |
| 39 | + ), |
| 40 | + }; |
| 41 | +}); |
| 42 | + |
| 43 | +const { createPhpCodemodIngestionHelper } = jest.requireMock( |
| 44 | + '../pipeline.codemods' |
| 45 | +) as { |
| 46 | + createPhpCodemodIngestionHelper: jest.Mock; |
| 47 | +}; |
| 48 | +const { createPhpProgramWriterHelper } = jest.requireMock( |
| 49 | + '@wpkernel/php-json-ast' |
| 50 | +) as { |
| 51 | + createPhpProgramWriterHelper: jest.Mock; |
| 52 | +}; |
| 53 | + |
| 54 | +describe('createPhpBuilder - adapter codemods', () => { |
| 55 | + beforeEach(() => { |
| 56 | + jest.clearAllMocks(); |
| 57 | + codemodApplyMock.mockImplementation(async (_options, next) => { |
| 58 | + await next?.(); |
| 59 | + }); |
| 60 | + writerApplyMock.mockImplementation(async (_options, next) => { |
| 61 | + await next?.(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('threads adapter codemods through the helper pipeline', async () => { |
| 66 | + const builder = createPhpBuilder({ |
| 67 | + driver: { |
| 68 | + binary: '/base/php', |
| 69 | + scriptPath: '/base/program-writer.php', |
| 70 | + importMetaUrl: 'file:///base/dist/index.js', |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + const ir = createMinimalIr({ |
| 75 | + config: { |
| 76 | + adapters: { |
| 77 | + php() { |
| 78 | + return { |
| 79 | + driver: { |
| 80 | + scriptPath: '/adapter/program-writer.php', |
| 81 | + }, |
| 82 | + codemods: { |
| 83 | + files: ['plugin.php', ''], |
| 84 | + configurationPath: 'codemods/baseline.json', |
| 85 | + diagnostics: { nodeDumps: true }, |
| 86 | + driver: { |
| 87 | + binary: '/adapter/php', |
| 88 | + scriptPath: '/adapter/codemod.php', |
| 89 | + importMetaUrl: |
| 90 | + 'file:///adapter/dist/index.js', |
| 91 | + }, |
| 92 | + }, |
| 93 | + }; |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + const context = createPipelineContext(); |
| 100 | + const input = createBuilderInput({ |
| 101 | + ir, |
| 102 | + options: { |
| 103 | + config: ir.config, |
| 104 | + namespace: ir.config.namespace, |
| 105 | + }, |
| 106 | + }); |
| 107 | + const output = createBuilderOutput(); |
| 108 | + |
| 109 | + await builder.apply( |
| 110 | + { context, input, output, reporter: context.reporter }, |
| 111 | + undefined |
| 112 | + ); |
| 113 | + |
| 114 | + expect(createPhpCodemodIngestionHelper).toHaveBeenCalledTimes(1); |
| 115 | + expect(createCodemodHelperImpl).toHaveBeenCalledWith({ |
| 116 | + files: ['plugin.php', ''], |
| 117 | + configurationPath: 'codemods/baseline.json', |
| 118 | + enableDiagnostics: true, |
| 119 | + phpBinary: '/adapter/php', |
| 120 | + scriptPath: '/adapter/codemod.php', |
| 121 | + importMetaUrl: 'file:///adapter/dist/index.js', |
| 122 | + }); |
| 123 | + expect(createPhpProgramWriterHelper).toHaveBeenCalledTimes(1); |
| 124 | + expect(createWriterHelperImpl).toHaveBeenCalledWith({ |
| 125 | + driver: { |
| 126 | + binary: '/base/php', |
| 127 | + scriptPath: '/adapter/program-writer.php', |
| 128 | + importMetaUrl: 'file:///base/dist/index.js', |
| 129 | + }, |
| 130 | + }); |
| 131 | + expect(codemodApplyMock).toHaveBeenCalled(); |
| 132 | + expect(writerApplyMock).toHaveBeenCalled(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('skips codemod helper when no valid target files are declared', async () => { |
| 136 | + const builder = createPhpBuilder(); |
| 137 | + |
| 138 | + const ir = createMinimalIr({ |
| 139 | + config: { |
| 140 | + adapters: { |
| 141 | + php() { |
| 142 | + return { |
| 143 | + codemods: { |
| 144 | + files: [123 as unknown as string], |
| 145 | + }, |
| 146 | + }; |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }); |
| 151 | + |
| 152 | + const context = createPipelineContext(); |
| 153 | + const input = createBuilderInput({ |
| 154 | + ir, |
| 155 | + options: { |
| 156 | + config: ir.config, |
| 157 | + namespace: ir.config.namespace, |
| 158 | + }, |
| 159 | + }); |
| 160 | + const output = createBuilderOutput(); |
| 161 | + |
| 162 | + await builder.apply( |
| 163 | + { context, input, output, reporter: context.reporter }, |
| 164 | + undefined |
| 165 | + ); |
| 166 | + |
| 167 | + expect(createPhpCodemodIngestionHelper).not.toHaveBeenCalled(); |
| 168 | + expect(createPhpProgramWriterHelper).toHaveBeenCalledTimes(1); |
| 169 | + }); |
| 170 | + |
| 171 | + it('defaults codemod driver overrides to the merged PHP driver options', async () => { |
| 172 | + const builder = createPhpBuilder({ |
| 173 | + driver: { |
| 174 | + binary: '/base/php', |
| 175 | + scriptPath: '/base/codemod.php', |
| 176 | + importMetaUrl: 'file:///base/dist/index.js', |
| 177 | + }, |
| 178 | + }); |
| 179 | + |
| 180 | + const ir = createMinimalIr({ |
| 181 | + config: { |
| 182 | + adapters: { |
| 183 | + php() { |
| 184 | + return { |
| 185 | + codemods: { |
| 186 | + files: ['plugin.php'], |
| 187 | + driver: { |
| 188 | + // no overrides provided |
| 189 | + }, |
| 190 | + }, |
| 191 | + }; |
| 192 | + }, |
| 193 | + }, |
| 194 | + }, |
| 195 | + }); |
| 196 | + |
| 197 | + const context = createPipelineContext(); |
| 198 | + const input = createBuilderInput({ |
| 199 | + ir, |
| 200 | + options: { |
| 201 | + config: ir.config, |
| 202 | + namespace: ir.config.namespace, |
| 203 | + }, |
| 204 | + }); |
| 205 | + const output = createBuilderOutput(); |
| 206 | + |
| 207 | + await builder.apply( |
| 208 | + { context, input, output, reporter: context.reporter }, |
| 209 | + undefined |
| 210 | + ); |
| 211 | + |
| 212 | + expect(createPhpCodemodIngestionHelper).toHaveBeenCalledTimes(1); |
| 213 | + expect(createCodemodHelperImpl).toHaveBeenCalledWith( |
| 214 | + expect.objectContaining({ |
| 215 | + phpBinary: '/base/php', |
| 216 | + scriptPath: '/base/codemod.php', |
| 217 | + importMetaUrl: 'file:///base/dist/index.js', |
| 218 | + }) |
| 219 | + ); |
| 220 | + }); |
| 221 | +}); |
0 commit comments