|
| 1 | +import * as core from '@actions/core'; |
| 2 | + |
| 3 | +import {configureMavenArgs} from '../src/maven-args'; |
| 4 | +import { |
| 5 | + INPUT_SHOW_DOWNLOAD_PROGRESS, |
| 6 | + MAVEN_ARGS_ENV, |
| 7 | + MAVEN_NO_TRANSFER_PROGRESS_FLAG |
| 8 | +} from '../src/constants'; |
| 9 | + |
| 10 | +describe('configureMavenArgs', () => { |
| 11 | + let inputs: Record<string, string>; |
| 12 | + let spyGetInput: jest.SpyInstance; |
| 13 | + let spyExportVariable: jest.SpyInstance; |
| 14 | + let spyInfo: jest.SpyInstance; |
| 15 | + let spyDebug: jest.SpyInstance; |
| 16 | + const originalMavenArgs = process.env[MAVEN_ARGS_ENV]; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + inputs = {}; |
| 20 | + |
| 21 | + spyGetInput = jest.spyOn(core, 'getInput'); |
| 22 | + spyGetInput.mockImplementation((name: string) => inputs[name] ?? ''); |
| 23 | + |
| 24 | + spyExportVariable = jest.spyOn(core, 'exportVariable'); |
| 25 | + spyExportVariable.mockImplementation((name: string, value: string) => { |
| 26 | + process.env[name] = value; |
| 27 | + }); |
| 28 | + |
| 29 | + spyInfo = jest.spyOn(core, 'info'); |
| 30 | + spyInfo.mockImplementation(() => undefined); |
| 31 | + |
| 32 | + spyDebug = jest.spyOn(core, 'debug'); |
| 33 | + spyDebug.mockImplementation(() => undefined); |
| 34 | + |
| 35 | + delete process.env[MAVEN_ARGS_ENV]; |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + jest.restoreAllMocks(); |
| 40 | + if (originalMavenArgs === undefined) { |
| 41 | + delete process.env[MAVEN_ARGS_ENV]; |
| 42 | + } else { |
| 43 | + process.env[MAVEN_ARGS_ENV] = originalMavenArgs; |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + it('sets MAVEN_ARGS with -ntp by default', () => { |
| 48 | + configureMavenArgs(); |
| 49 | + |
| 50 | + expect(spyExportVariable).toHaveBeenCalledWith( |
| 51 | + MAVEN_ARGS_ENV, |
| 52 | + MAVEN_NO_TRANSFER_PROGRESS_FLAG |
| 53 | + ); |
| 54 | + expect(process.env[MAVEN_ARGS_ENV]).toBe(MAVEN_NO_TRANSFER_PROGRESS_FLAG); |
| 55 | + }); |
| 56 | + |
| 57 | + it('does not modify MAVEN_ARGS when show-download-progress is true', () => { |
| 58 | + inputs[INPUT_SHOW_DOWNLOAD_PROGRESS] = 'true'; |
| 59 | + |
| 60 | + configureMavenArgs(); |
| 61 | + |
| 62 | + expect(spyExportVariable).not.toHaveBeenCalled(); |
| 63 | + expect(process.env[MAVEN_ARGS_ENV]).toBeUndefined(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('preserves an existing MAVEN_ARGS value and appends -ntp', () => { |
| 67 | + process.env[MAVEN_ARGS_ENV] = '-B -Dstyle.color=always'; |
| 68 | + |
| 69 | + configureMavenArgs(); |
| 70 | + |
| 71 | + expect(spyExportVariable).toHaveBeenCalledWith( |
| 72 | + MAVEN_ARGS_ENV, |
| 73 | + `-B -Dstyle.color=always ${MAVEN_NO_TRANSFER_PROGRESS_FLAG}` |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it('does not duplicate the flag when -ntp is already present', () => { |
| 78 | + process.env[MAVEN_ARGS_ENV] = '-B -ntp'; |
| 79 | + |
| 80 | + configureMavenArgs(); |
| 81 | + |
| 82 | + expect(spyExportVariable).not.toHaveBeenCalled(); |
| 83 | + expect(process.env[MAVEN_ARGS_ENV]).toBe('-B -ntp'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('does not duplicate the flag when --no-transfer-progress is already present', () => { |
| 87 | + process.env[MAVEN_ARGS_ENV] = '--no-transfer-progress -B'; |
| 88 | + |
| 89 | + configureMavenArgs(); |
| 90 | + |
| 91 | + expect(spyExportVariable).not.toHaveBeenCalled(); |
| 92 | + expect(process.env[MAVEN_ARGS_ENV]).toBe('--no-transfer-progress -B'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('keeps the existing MAVEN_ARGS when show-download-progress is true', () => { |
| 96 | + inputs[INPUT_SHOW_DOWNLOAD_PROGRESS] = 'true'; |
| 97 | + process.env[MAVEN_ARGS_ENV] = '-B'; |
| 98 | + |
| 99 | + configureMavenArgs(); |
| 100 | + |
| 101 | + expect(spyExportVariable).not.toHaveBeenCalled(); |
| 102 | + expect(process.env[MAVEN_ARGS_ENV]).toBe('-B'); |
| 103 | + }); |
| 104 | +}); |
0 commit comments