From 2f028bc0440d3fe98ed5bb0dac9e76ee3d85bb1e Mon Sep 17 00:00:00 2001 From: IvanZosimov Date: Thu, 18 May 2023 11:11:51 +0200 Subject: [PATCH] update unit and e2e tests --- .github/workflows/e2e-tests.yml | 67 ++++++++++++++++++++++----------- __tests__/installer.test.ts | 36 ++++++++++++++++-- __tests__/setup-dotnet.test.ts | 31 ++++++++++++++- dist/index.js | 3 +- src/installer.ts | 2 +- src/setup-dotnet.ts | 2 + 6 files changed, 113 insertions(+), 28 deletions(-) diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.yml index 6d00d5d..3933a34 100644 --- a/.github/workflows/e2e-tests.yml +++ b/.github/workflows/e2e-tests.yml @@ -133,6 +133,27 @@ jobs: shell: pwsh run: __tests__/verify-dotnet.ps1 -Patterns "^2.2", "^3.1" + test-ABCxx-syntax: + runs-on: ${{ matrix.operating-system }} + strategy: + fail-fast: false + matrix: + operating-system: [ubuntu-latest, windows-latest, macOS-latest] + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Clear toolcache + shell: pwsh + run: __tests__/clear-toolcache.ps1 ${{ runner.os }} + + - name: Setup dotnet 6.0.4xx + uses: ./ + with: + dotnet-version: '6.0.4xx' + - name: Verify dotnet + shell: pwsh + run: __tests__/verify-dotnet.ps1 -Patterns "^6\.0\.4\d{2}" + test-setup-with-wildcard: runs-on: ${{ matrix.operating-system }} strategy: @@ -183,6 +204,31 @@ jobs: shell: pwsh run: __tests__/verify-dotnet.ps1 -Patterns "^2.2", "^3.1" + test-setup-global-json-only: + runs-on: ${{ matrix.operating-system }} + strategy: + fail-fast: false + matrix: + operating-system: [ubuntu-latest, windows-latest, macOS-latest] + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Clear toolcache + shell: pwsh + run: __tests__/clear-toolcache.ps1 ${{ runner.os }} + - name: Write global.json + shell: bash + run: | + mkdir subdirectory + echo '{"sdk":{"version": "2.2.207","rollForward": "latestFeature"}}' > ./subdirectory/global.json + - name: Setup dotnet + uses: ./ + with: + global-json-file: ./subdirectory/global.json + - name: Verify dotnet + shell: pwsh + run: __tests__/verify-dotnet.ps1 -Patterns "^2.2" + test-setup-with-dotnet-quality: runs-on: ${{ matrix.operating-system }} strategy: @@ -205,27 +251,6 @@ jobs: shell: pwsh run: __tests__/verify-dotnet.ps1 -Patterns "^7\.0\.\d+-" - test-ABCxx-syntax: - runs-on: ${{ matrix.operating-system }} - strategy: - fail-fast: false - matrix: - operating-system: [ubuntu-latest, windows-latest, macOS-latest] - steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Clear toolcache - shell: pwsh - run: __tests__/clear-toolcache.ps1 ${{ runner.os }} - - - name: Setup dotnet 6.0.4xx - uses: ./ - with: - dotnet-version: '6.0.4xx' - - name: Verify dotnet - shell: pwsh - run: __tests__/verify-dotnet.ps1 -Patterns "^6\.0\.4\d{2}" - test-dotnet-version-output-during-single-version-installation: runs-on: ${{ matrix.operating-system }} strategy: diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 0d264b5..08aa8e7 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -306,7 +306,8 @@ describe('installer tests', () => { '3.1.*', '3.1.X', '3.1.2', - '3.1.0-preview1' + '3.1.0-preview1', + '6.0.2xx' ]).test( 'if valid version is supplied (%s), it should return version object with some value', async version => { @@ -358,7 +359,16 @@ describe('installer tests', () => { } ); - each(['3', '3.1', '3.1.x', '3.1.*', '3.1.X']).test( + each([ + '3', + '3.1', + '3.1.x', + '3.1.*', + '3.1.X', + '6.0.2xx', + '6.0.2XX', + '6.0.2**' + ]).test( "if version that can be resolved to 'channel' option is supplied (%s), it should set type to 'channel' in version object", async version => { const dotnetVersionResolver = new installer.DotnetVersionResolver( @@ -373,7 +383,15 @@ describe('installer tests', () => { } ); - each(['6.0', '6.0.x', '6.0.*', '6.0.X']).test( + each([ + '6.0', + '6.0.x', + '6.0.*', + '6.0.X', + '6.0.2xx', + '6.0.2XX', + '6.0.2**' + ]).test( "if version that can be resolved to 'channel' option is supplied and its major tag is >= 6 (%s), it should set type to 'channel' and qualityFlag to 'true' in version object", async version => { const dotnetVersionResolver = new installer.DotnetVersionResolver( @@ -425,6 +443,18 @@ describe('installer tests', () => { } } ); + + it(`should throw if dotnet-version is supplied in A.B.Cxx syntax with major tag lower that 5`, async () => { + const version = '3.0.1xx'; + const dotnetVersionResolver = new installer.DotnetVersionResolver( + version + ); + await expect( + async () => await dotnetVersionResolver.createDotNetVersion() + ).rejects.toThrow( + `'dotnet-version' was supplied in invalid format: ${version}! The A.B.Cxx syntax is available since the .NET 5.0 release.` + ); + }); }); }); }); diff --git a/__tests__/setup-dotnet.test.ts b/__tests__/setup-dotnet.test.ts index 831408c..45043a1 100644 --- a/__tests__/setup-dotnet.test.ts +++ b/__tests__/setup-dotnet.test.ts @@ -12,6 +12,7 @@ describe('setup-dotnet tests', () => { const getInputSpy = jest.spyOn(core, 'getInput'); const getMultilineInputSpy = jest.spyOn(core, 'getMultilineInput'); const setFailedSpy = jest.spyOn(core, 'setFailed'); + const warningSpy = jest.spyOn(core, 'warning'); const debugSpy = jest.spyOn(core, 'debug'); const infoSpy = jest.spyOn(core, 'info'); const setOutputSpy = jest.spyOn(core, 'setOutput'); @@ -133,14 +134,40 @@ describe('setup-dotnet tests', () => { ); }); - it('should call setOutput() after installation complete', async () => { + it('should call setOutput() after installation complete successfully', async () => { inputs['dotnet-version'] = ['6.0.300']; - installDotnetSpy.mockImplementation(() => Promise.resolve('')); + installDotnetSpy.mockImplementation(() => + Promise.resolve(`${inputs['dotnet-version']}`) + ); addToPathSpy.mockImplementation(() => {}); await setup.run(); expect(setOutputSpy).toHaveBeenCalledTimes(1); }); + + it(`shouldn't call setOutput() if parsing dotnet-installer logs failed`, async () => { + inputs['dotnet-version'] = ['6.0.300']; + const warningMessage = `Failed to output the installed version of .NET. The 'dotnet-version' output will not be set.`; + + installDotnetSpy.mockImplementation(() => Promise.resolve(null)); + addToPathSpy.mockImplementation(() => {}); + + await setup.run(); + expect(warningSpy).toHaveBeenCalledWith(warningMessage); + expect(setOutputSpy).not.toHaveBeenCalled(); + }); + + it(`shouldn't call setOutput() if actions didn't install .NET`, async () => { + inputs['dotnet-version'] = []; + const warningMessage = `No .NET version was installed. The 'dotnet-version' output will not be set.`; + + addToPathSpy.mockImplementation(() => {}); + + await setup.run(); + + expect(infoSpy).toHaveBeenCalledWith(warningMessage); + expect(setOutputSpy).not.toHaveBeenCalled(); + }); }); }); diff --git a/dist/index.js b/dist/index.js index 2b8903f..aca3dd0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -270,7 +270,7 @@ class DotnetVersionResolver { } isLatestPatchSyntax() { var _b, _c; - const majorTag = (_c = (_b = this.inputVersion.match(/^(?\d+)\.\d+\.\d{1}x{2}$/)) === null || _b === void 0 ? void 0 : _b.groups) === null || _c === void 0 ? void 0 : _c.majorTag; + const majorTag = (_c = (_b = this.inputVersion.match(/^(?\d+)\.\d+\.\d{1}(x|X|\*){2}$/)) === null || _b === void 0 ? void 0 : _b.groups) === null || _c === void 0 ? void 0 : _c.majorTag; if (majorTag && parseInt(majorTag) < DotnetInstallerLimits.LatestPatchSyntaxMinimalMajorTag) { @@ -606,6 +606,7 @@ function getVersionFromGlobalJson(globalJsonPath) { function outputInstalledVersion(installedVersions, globalJsonFileInput) { if (!installedVersions.length) { core.info(`No .NET version was installed. The 'dotnet-version' output will not be set.`); + return; } if (installedVersions.includes(null)) { core.warning(`Failed to output the installed version of .NET. The 'dotnet-version' output will not be set.`); diff --git a/src/installer.ts b/src/installer.ts index d31ec00..e4c3ffe 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -49,7 +49,7 @@ export class DotnetVersionResolver { private isLatestPatchSyntax() { const majorTag = this.inputVersion.match( - /^(?\d+)\.\d+\.\d{1}x{2}$/ + /^(?\d+)\.\d+\.\d{1}(x|X|\*){2}$/ )?.groups?.majorTag; if ( majorTag && diff --git a/src/setup-dotnet.ts b/src/setup-dotnet.ts index 188257b..e7e7726 100644 --- a/src/setup-dotnet.ts +++ b/src/setup-dotnet.ts @@ -112,7 +112,9 @@ function outputInstalledVersion( core.info( `No .NET version was installed. The 'dotnet-version' output will not be set.` ); + return; } + if (installedVersions.includes(null)) { core.warning( `Failed to output the installed version of .NET. The 'dotnet-version' output will not be set.`