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 | 45 +++++++++++++++++++++++++-------- __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, 102 insertions(+), 17 deletions(-) diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.yml index 6d00d5d44..3933a345f 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,7 +204,7 @@ jobs: shell: pwsh run: __tests__/verify-dotnet.ps1 -Patterns "^2.2", "^3.1" - test-setup-with-dotnet-quality: + test-setup-global-json-only: runs-on: ${{ matrix.operating-system }} strategy: fail-fast: false @@ -195,17 +216,20 @@ jobs: - name: Clear toolcache shell: pwsh run: __tests__/clear-toolcache.ps1 ${{ runner.os }} - - - name: Setup dotnet 7.0 with preview quality + - 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: - dotnet-version: '7.0' - dotnet-quality: 'preview' + global-json-file: ./subdirectory/global.json - name: Verify dotnet shell: pwsh - run: __tests__/verify-dotnet.ps1 -Patterns "^7\.0\.\d+-" + run: __tests__/verify-dotnet.ps1 -Patterns "^2.2" - test-ABCxx-syntax: + test-setup-with-dotnet-quality: runs-on: ${{ matrix.operating-system }} strategy: fail-fast: false @@ -218,13 +242,14 @@ jobs: shell: pwsh run: __tests__/clear-toolcache.ps1 ${{ runner.os }} - - name: Setup dotnet 6.0.4xx + - name: Setup dotnet 7.0 with preview quality uses: ./ with: - dotnet-version: '6.0.4xx' + dotnet-version: '7.0' + dotnet-quality: 'preview' - name: Verify dotnet shell: pwsh - run: __tests__/verify-dotnet.ps1 -Patterns "^6\.0\.4\d{2}" + run: __tests__/verify-dotnet.ps1 -Patterns "^7\.0\.\d+-" test-dotnet-version-output-during-single-version-installation: runs-on: ${{ matrix.operating-system }} diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 0d264b542..08aa8e7c6 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 831408c1e..45043a100 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 2b8903fe1..aca3dd0c9 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 d31ec0039..e4c3ffeb2 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 188257b6c..e7e7726e2 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.`