From b72f430d3615f3631ecde8d268101c3804566f79 Mon Sep 17 00:00:00 2001 From: IvanZosimov Date: Thu, 13 Apr 2023 17:28:59 +0200 Subject: [PATCH] Update e2e tests --- .github/workflows/e2e-tests.yml | 37 +++---- __tests__/e2e-test-csproj/Test.cs | 14 +++ __tests__/e2e-test-csproj/test.csproj | 15 +++ __tests__/sample-csproj/Program.cs | 15 --- __tests__/sample-csproj/sample.csproj | 18 ---- __tests__/verify-dotnet.ps1 | 137 +++++++++++++++----------- 6 files changed, 123 insertions(+), 113 deletions(-) create mode 100644 __tests__/e2e-test-csproj/Test.cs create mode 100644 __tests__/e2e-test-csproj/test.csproj delete mode 100644 __tests__/sample-csproj/Program.cs delete mode 100644 __tests__/sample-csproj/sample.csproj diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.yml index 1be4173..fd6e85b 100644 --- a/.github/workflows/e2e-tests.yml +++ b/.github/workflows/e2e-tests.yml @@ -33,7 +33,7 @@ jobs: 3.0.x - name: Verify dotnet shell: pwsh - run: __tests__/verify-dotnet.ps1 2.2.402 3.1.404 '3.0' + run: __tests__/verify-dotnet.ps1 -Patterns 2.2.402, 3.1.404, 3.0 test-setup-full-version: runs-on: ${{ matrix.operating-system }} @@ -60,13 +60,9 @@ jobs: source-url: https://api.nuget.org/v3/index.json env: NUGET_AUTH_TOKEN: NOTATOKEN - - name: Verify nuget config file - shell: pwsh - run: | - if (-Not (Test-Path "../nuget.config")) { throw "nuget file not generated correctly" } - name: Verify dotnet shell: pwsh - run: __tests__/verify-dotnet.ps1 3.1.201 2.2.402 + run: __tests__/verify-dotnet.ps1 -Patterns 3.1.201, 2.2.402 -CheckNugetConfig test-setup-without-patch-version: runs-on: ${{ matrix.operating-system }} @@ -91,7 +87,7 @@ jobs: dotnet-version: '2.2' - name: Verify dotnet shell: pwsh - run: __tests__/verify-dotnet.ps1 3.1 2.2 + run: __tests__/verify-dotnet.ps1 -Patterns 3.1, 2.2 test-setup-prerelease-version: runs-on: ${{ matrix.operating-system }} @@ -105,17 +101,13 @@ jobs: - name: Clear toolcache shell: pwsh run: __tests__/clear-toolcache.ps1 ${{ runner.os }} - - name: Setup dotnet '2.2' - uses: ./ - with: - dotnet-version: '2.2' - name: Setup dotnet '3.1.100-preview1-014459' uses: ./ with: dotnet-version: '3.1.100-preview1-014459' - name: Verify dotnet shell: pwsh - run: __tests__/verify-dotnet.ps1 3.1.100-preview1-014459 + run: __tests__/verify-dotnet.ps1 -Patterns 3.1.100-preview1-014459 test-setup-latest-patch-version: runs-on: ${{ matrix.operating-system }} @@ -139,7 +131,7 @@ jobs: dotnet-version: 2.2.X - name: Verify dotnet shell: pwsh - run: __tests__/verify-dotnet.ps1 '2.2' '3.1' + run: __tests__/verify-dotnet.ps1 -Patterns 2.2, 3.1 test-setup-with-wildcard: runs-on: ${{ matrix.operating-system }} @@ -163,7 +155,7 @@ jobs: dotnet-version: 2.2.* - name: Verify dotnet shell: pwsh - run: __tests__/verify-dotnet.ps1 3.1 2.2 + run: __tests__/verify-dotnet.ps1 -Patterns 3.1, 2.2 test-setup-global-json-specified-and-version: runs-on: ${{ matrix.operating-system }} @@ -181,7 +173,7 @@ jobs: shell: bash run: | mkdir subdirectory - echo '{"sdk":{"version": "2.2","rollForward": "latestFeature"}}' > ./subdirectory/global.json + echo '{"sdk":{"version": "2.2.207","rollForward": "latestFeature"}}' > ./subdirectory/global.json - name: Setup dotnet uses: ./ with: @@ -189,7 +181,7 @@ jobs: global-json-file: ./subdirectory/global.json - name: Verify dotnet shell: pwsh - run: __tests__/verify-dotnet.ps1 2.2 3.1 + run: __tests__/verify-dotnet.ps1 -Patterns 2.2.207, 3.1 test-setup-with-dotnet-quality: runs-on: ${{ matrix.operating-system }} @@ -209,12 +201,9 @@ jobs: with: dotnet-version: '7.0' dotnet-quality: 'preview' - - name: Verify preview version + - name: Verify dotnet shell: pwsh - run: | - $version = & dotnet --version - Write-Host "Installed version: $version" - if (-not ($version.Contains("preview") -or $version.Contains("rc"))) { throw "Unexpected version" } + run: __tests__/verify-dotnet.ps1 -Patterns 7.0 -ContainedPattern "preview" test-dotnet-version-output-during-single-version-installation: runs-on: ${{ matrix.operating-system }} @@ -300,7 +289,8 @@ jobs: env: NUGET_AUTH_TOKEN: NOTATOKEN - name: Verify dotnet - run: __tests__/verify-dotnet.sh 3.1.201 + shell: pwsh + run: __tests__/verify-dotnet.ps1 -Patterns 3.1.201 -CheckNugetConfig test-bypass-proxy: runs-on: ubuntu-latest @@ -320,4 +310,5 @@ jobs: env: NUGET_AUTH_TOKEN: NOTATOKEN - name: Verify dotnet - run: __tests__/verify-dotnet.sh 3.1.201 + shell: pwsh + run: __tests__/verify-dotnet.ps1 -Patterns 3.1.201 -CheckNugetConfig diff --git a/__tests__/e2e-test-csproj/Test.cs b/__tests__/e2e-test-csproj/Test.cs new file mode 100644 index 0000000..533bdb8 --- /dev/null +++ b/__tests__/e2e-test-csproj/Test.cs @@ -0,0 +1,14 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace test_csproj +{ + [TestClass] + public class Test + { + [TestMethod] + public void TestMethod() + { + Assert.AreEqual((1 + 1), 2); + } + } +} diff --git a/__tests__/e2e-test-csproj/test.csproj b/__tests__/e2e-test-csproj/test.csproj new file mode 100644 index 0000000..370186a --- /dev/null +++ b/__tests__/e2e-test-csproj/test.csproj @@ -0,0 +1,15 @@ + + + + $(TEST_TARGET_FRAMEWORK) + + false + + + + + + + + + diff --git a/__tests__/sample-csproj/Program.cs b/__tests__/sample-csproj/Program.cs deleted file mode 100644 index f14c939..0000000 --- a/__tests__/sample-csproj/Program.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; - -namespace sample_csproj -{ - [TestClass] - public class Program - { - [TestMethod] - public void TestMethod1() - { - Console.WriteLine("Hello, World!"); - } - } -} diff --git a/__tests__/sample-csproj/sample.csproj b/__tests__/sample-csproj/sample.csproj deleted file mode 100644 index be6d7ea..0000000 --- a/__tests__/sample-csproj/sample.csproj +++ /dev/null @@ -1,18 +0,0 @@ - - - - netcoreapp3.1;netcoreapp3.0;netcoreapp2.2 - sample_csproj - - false - - - - - - - - - - - diff --git a/__tests__/verify-dotnet.ps1 b/__tests__/verify-dotnet.ps1 index 7068c87..53befbb 100755 --- a/__tests__/verify-dotnet.ps1 +++ b/__tests__/verify-dotnet.ps1 @@ -1,73 +1,96 @@ -if (!$args[0]) -{ - throw "Must supply dotnet version argument" -} +param( + [ValidateNotNullOrEmpty()] + [string[]]$Patterns, + [ValidateNotNullOrEmpty()] + [string]$ContainedPattern, + [switch]$CheckNugetConfig +) -$dotnet = Get-Command dotnet | Select-Object -First 1 | ForEach-Object { $_.Path } -Write-Host "Found '$dotnet'" - -if($args.count -eq 1) -{ - $version = & $dotnet --version | Out-String | ForEach-Object { $_.Trim() } - Write-Host "Version $version" - if (-not ($version.StartsWith($args[0].ToString()))) +if ($CheckNugetConfig.IsPresent) { + if (!(Test-Path "../nuget.config")) { - Write-Host "PATH='$env:PATH'" - throw "Unexpected version" + throw "The nuget.config file is not generated correctly." } } -if ($args[1]) +if (!$Patterns.Count) { - # SDKs are listed on multiple lines with the path afterwards in square brackets - $versions = & $dotnet --list-sdks | ForEach-Object { $_.SubString(0, $_.IndexOf('[')).Trim() } - Write-Host "Installed versions: $versions" - $InstalledVersionCount = 0 - foreach($arg in $args){ - foreach ($version in $versions) + throw "At least 1 dotnet-version pattern should be supplied to script." +} + +Write-Host "Those patterns were supplied to the script: $($Patterns -join ', ')." +$dotnet = Get-Command dotnet | Select-Object -First 1 | ForEach-Object { $_.Path } +Write-Host "Found: '$dotnet'" + +# SDKs are listed on multiple lines with the path afterwards in square brackets +$versions = & $dotnet --list-sdks | ForEach-Object { $_.SubString(0, $_.IndexOf('[')).Trim() } +Write-Host "Installed versions: $($versions -join ', ')." +$InstalledVersionCount = 0 +foreach($pattern in $Patterns) +{ + foreach ($version in $versions) + { + if ($ContainedPattern) { - if ($version.StartsWith($arg.ToString())) + if ($version.StartsWith($pattern.ToString()) -and $version.Contains($ContainedPattern)) { $InstalledVersionCount++ + } + } + elseif ($version.StartsWith($pattern.ToString())) + { + $InstalledVersionCount++ } - } - } - if ( $InstalledVersionCount -ne $args.Count) - { - Write-Host "PATH='$env:PATH'" - throw "Unexpected version" } } +if ( $InstalledVersionCount -ne $Patterns.Count) +{ + throw "An unexpected version of Dotnet is found on the machine, please check the script's dotnet-version patterns." +} -Write-Host "Building sample csproj" -& $dotnet build __tests__/sample-csproj/ --no-cache -if ($LASTEXITCODE -ne 0) +Write-Host "Changing directory to the ./__tests__/e2e-test-csproj" +Set-Location ./__tests__/e2e-test-csproj + +$targetFrameworkVersionMapping = @{ + "1.0" = "netcoreapp1.0"; + "1.1" = "netcoreapp1.1"; + "2.0" = "netcoreapp2.0"; + "2.1" = "netcoreapp2.1"; + "2.2" = "netcoreapp2.2"; + "3.0" = "netcoreapp3.0"; + "3.1" = "netcoreapp3.1"; + "5.0" = "net5.0"; + "6.0" = "net6.0"; + "7.0" = "net7.0"; + } + +foreach ($version in $versions) { - throw "Unexpected exit code $LASTEXITCODE" + Write-Host "Creating temporary global.json file for $version .NET version." + & $dotnet new globaljson --sdk-version $version --force + Write-Host "The global.json file for the version $version is created. Currently used .NET version is: $(& $dotnet --version)" + $version -match "^(?\d+\.\d+)" + Write-Host "Setting the TEST_TARGET_FRAMEWORK environment variable to $($targetFrameworkVersionMapping[$Matches.key])" + [Environment]::SetEnvironmentVariable('TEST_TARGET_FRAMEWORK', $($targetFrameworkVersionMapping[$Matches.key])) + + Write-Host "Building test C# project with $version .NET version." + & $dotnet build --no-cache + if ($LASTEXITCODE -ne 0) + { + throw "Building process is not successful, exit code: $LASTEXITCODE" + } + + Write-Host "Testing compiled C# project with $version .NET version." + & $dotnet test --no-build + if ($LASTEXITCODE -ne 0) + { + throw "Testing process is not successful, exit code: $LASTEXITCODE" + } + + Write-Host "Tests are completed successfully!" + + Write-Host "Removing temprary global.json file." + Remove-Item ./global.json } -Write-Host "Testing compiled app" -$sample_output = "$(dotnet test __tests__/sample-csproj/ --no-build)" -Write-Host "Sample output: $sample_output" -# For Side-by-Side installs we want to run the tests twice, for a single install the tests will run once -if ($args[1]) -{ - if ($sample_output -notlike "*Test Run Successful.*Test Run Successful.*") - { - throw "Unexpected output" - } -} -if ($args[2]) -{ - if ($sample_output -notlike "*Test Run Successful.*Test Run Successful.*Test Run Successful.*") - { - throw "Unexpected output" - } -} -else -{ - if ($sample_output -notlike "*Test Run Successful.*") - { - throw "Unexpected output" - } -} +Set-Location ../.. \ No newline at end of file