|
| 1 | +import ma = require('azure-pipelines-task-lib/mock-answer'); |
| 2 | +import tmrm = require('azure-pipelines-task-lib/mock-run'); |
| 3 | +import path = require('path'); |
| 4 | + |
| 5 | +const taskPath = path.join(__dirname, '..', 'gotool.js'); |
| 6 | +const tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath); |
| 7 | + |
| 8 | +const testCase = process.env['__case__']; |
| 9 | + |
| 10 | +// Configure inputs depending on scenario |
| 11 | +switch (testCase) { |
| 12 | + case 'useGoModSingle': |
| 13 | + tmr.setInput('useGoMod', 'true'); |
| 14 | + tmr.setInput('workingDirectory', 'work'); |
| 15 | + break; |
| 16 | + case 'useGoModMulti': |
| 17 | + tmr.setInput('useGoMod', 'true'); |
| 18 | + tmr.setInput('workingDirectory', 'repo'); |
| 19 | + break; |
| 20 | + case 'useGoModNotFound': |
| 21 | + tmr.setInput('useGoMod', 'true'); |
| 22 | + tmr.setInput('workingDirectory', 'empty'); |
| 23 | + break; |
| 24 | + case 'explicitVersion': |
| 25 | + tmr.setInput('useGoMod', 'false'); |
| 26 | + tmr.setInput('version', '1.21.0'); |
| 27 | + break; |
| 28 | + default: |
| 29 | + throw new Error('Unknown __case__ value: ' + testCase); |
| 30 | +} |
| 31 | + |
| 32 | +const answers: ma.TaskLibAnswers = <ma.TaskLibAnswers>{ |
| 33 | + 'assertAgent': { '2.115.0': true } |
| 34 | +}; |
| 35 | +tmr.setAnswers(answers); |
| 36 | + |
| 37 | +// Dynamic findMatch behavior based on test case |
| 38 | +const tl = require('azure-pipelines-task-lib/mock-task'); |
| 39 | +const tlClone = Object.assign({}, tl); |
| 40 | +tlClone.findMatch = function(root: string, pattern: string) { |
| 41 | + if (pattern !== '**/go.mod') return []; |
| 42 | + if (testCase === 'useGoModSingle' && root === 'work') { |
| 43 | + return ['work/go.mod']; |
| 44 | + } |
| 45 | + if (testCase === 'useGoModMulti' && root === 'repo') { |
| 46 | + return ['repo/a/go.mod', 'repo/b/go.mod']; |
| 47 | + } |
| 48 | + if (testCase === 'useGoModNotFound') { |
| 49 | + return []; |
| 50 | + } |
| 51 | + return []; |
| 52 | +}; |
| 53 | +tlClone.getVariable = function(v: string) { |
| 54 | + if (v.toLowerCase() === 'system.defaultworkingdirectory') { |
| 55 | + if (testCase === 'useGoModSingle') return 'work'; |
| 56 | + if (testCase === 'useGoModMulti') return 'repo'; |
| 57 | + if (testCase === 'useGoModNotFound') return 'empty'; |
| 58 | + } |
| 59 | + if (v.toLowerCase() === 'agent.tempdirectory') return 'temp'; |
| 60 | + return null; |
| 61 | +}; |
| 62 | +tlClone.assertAgent = function() { return; }; |
| 63 | +tlClone.loc = function(locString: string, ...params: any[]) { return locString + ' ' + params.join(' '); }; |
| 64 | +tlClone.prependPath = function(p: string) { /* no-op for tests */ }; |
| 65 | +tmr.registerMock('azure-pipelines-task-lib/mock-task', tlClone); |
| 66 | + |
| 67 | +// fs mock |
| 68 | +tmr.registerMock('fs', { |
| 69 | + readFileSync: function(p: string) { |
| 70 | + if (p === 'work/go.mod') return Buffer.from('module example.com/single\n\n go 1.22'); |
| 71 | + if (p === 'repo/a/go.mod') return Buffer.from('module example.com/a\n go 1.21'); |
| 72 | + if (p === 'repo/b/go.mod') return Buffer.from('module example.com/b\n go 1.22'); |
| 73 | + return Buffer.from(''); |
| 74 | + } |
| 75 | +}); |
| 76 | + |
| 77 | +// tool-lib mock |
| 78 | +tmr.registerMock('azure-pipelines-tool-lib/tool', { |
| 79 | + findLocalTool: function(tool: string, version: string) { return false; }, |
| 80 | + downloadTool: function(url: string) { return 'download'; }, |
| 81 | + extractTar: function(downloadPath: string) { return 'ext'; }, |
| 82 | + extractZip: function(downloadPath: string) { return 'ext'; }, |
| 83 | + cacheDir: function(dir: string, tool: string, version: string) { return path.join('cache', tool, version); }, |
| 84 | + prependPath: function(p: string) { /* no-op */ } |
| 85 | +}); |
| 86 | + |
| 87 | +tmr.registerMock('azure-pipelines-tasks-utility-common/telemetry', { emitTelemetry: function() {} }); |
| 88 | + |
| 89 | +tmr.run(); |
0 commit comments