# Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. # these are tests for the build module import-module -force "./build.psm1" Describe "Build Module Tests" { Context "Global.json" { BeforeAll { $globalJson = Get-Content (Join-Path $PSScriptRoot global.json) | ConvertFrom-Json $expectedVersion = $globalJson.sdk.version $result = Get-GlobalJsonSdkVersion } $propertyTestcases = @{ Name = "Major"; Type = "System.Int32" }, @{ Name = "Minor"; Type = "System.Int32" }, @{ Name = "Patch"; Type = "System.Int32" }, @{ Name = "PrereleaseLabel"; Type = "System.String" } It "Get-GlobalJsonSdkVersion returns a portable version object with property '' with type ''" -TestCases $propertyTestcases { param ( $Name, $Type ) $result.psobject.properties[$Name] | Should -BeOfType [System.Management.Automation.PSNoteProperty] $result.psobject.properties[$Name].TypeNameOfValue | Should -Be $Type } It "Can retrieve the version from global.json" { $result = Get-GlobalJsonSdkVersion $resultString = "{0}.{1}.{2}" -f $result.Major,$result.Minor,$result.Patch if ( $result.prereleasestring ) { $resultString += "-" + $result.prereleasestring } $resultString | Should -Be $expectedVersion } } Context "Test-SuiteableDotnet" { It "Test-SuitableDotnet should return true when the expected version matches the installed version" { Test-SuitableDotnet -availableVersions 2.1.2 -requiredVersion 2.1.2 | Should -Be $true } It "Test-SuitableDotnet should return true when the expected version matches the available versions" { Test-SuitableDotnet -availableVersions "2.1.1","2.1.2","2.1.3" -requiredVersion 2.1.2 | Should -Be $true } It "Test-SuitableDotnet should return false when the expected version does not match an available" { Test-SuitableDotnet -availableVersions "2.2.100","2.2.300" -requiredVersion 2.2.200 | Should -Be $false } It "Test-SuitableDotnet should return false when the expected version does not match an available" { Test-SuitableDotnet -availableVersions "2.2.100","2.2.300" -requiredVersion 2.2.105 | Should -Be $false } It "Test-SuitableDotnet should return true when the expected version matches an available" { Test-SuitableDotnet -availableVersions "2.2.150","2.2.300" -requiredVersion 2.2.105 | Should -Be $true } It "Test-SuitableDotnet should return false when the expected version does not match an available" { Test-SuitableDotnet -availableVersions "2.2.400","2.2.401","2.2.405" -requiredVersion "2.2.410" | Should -Be $false } } Context "Test-DotnetInstallation" { BeforeAll { $availableVersions = ConvertTo-PortableVersion -strVersion "2.2.400","2.2.401","2.2.405" $foundVersion = ConvertTo-PortableVersion -strVersion 2.2.402 $missingVersion = ConvertTo-PortableVersion -strVersion 2.2.410 } It "Test-DotnetInstallation finds a good version" { Mock Get-InstalledCLIVersion { return $availableVersions } Mock Get-GlobalJSonSdkVersion { return $foundVersion } $result = Test-DotnetInstallation -requestedVersion (Get-GlobalJsonSdkVersion) -installedVersions (Get-InstalledCLIVersion) Assert-MockCalled "Get-InstalledCLIVersion" -Times 1 Assert-MockCalled "Get-GlobalJsonSdkVersion" -Times 1 $result | Should -Be $true } It "Test-DotnetInstallation cannot find a good version should return false" { Mock Get-InstalledCLIVersion { return $availableVersions } Mock Get-GlobalJSonSdkVersion { return $missingVersion } $result = Test-DotnetInstallation -requestedVersion (Get-GlobalJsonSdkVersion) -installedVersions (Get-InstalledCLIVersion) Assert-MockCalled "Get-InstalledCLIVersion" -Times 1 Assert-MockCalled "Get-GlobalJsonSdkVersion" -Times 1 $result | Should -Be $false } } Context "Receive-DotnetInstallScript" { Mock -ModuleName Build Receive-File { new-item -type file TestDrive:/dotnet-install.sh } It "Downloads the proper non-Windows file" { try { push-location TestDrive: Receive-DotnetInstallScript -platform NonWindows "TestDrive:/dotnet-install.sh" | Should -Exist } finally { Pop-Location } } Mock -ModuleName Build Receive-File { new-item -type file TestDrive:/dotnet-install.ps1 } It "Downloads the proper file Windows file" { try { push-location TestDrive: Receive-DotnetInstallScript -platform "Windows" "TestDrive:/dotnet-install.ps1" | Should -Exist } finally { Pop-Location } } } Context "Test result functions" { BeforeAll { $xmlFile = @'  Expected 2, but got 1. at <ScriptBlock>, /tmp/bad.tests.ps1: line 3 3: It "a failing test" { 1 | Should -Be 2 } '@ $xmlFile | out-file TESTDRIVE:/results.xml $results = Get-TestResults -logfile TESTDRIVE:/results.xml $failures = Get-TestFailures -logfile TESTDRIVE:/results.xml } It "Get-TestResults finds 2 results" { $results.Count | Should -Be 2 } It "Get-TestResults finds 1 pass" { @($results | Where-Object -FilterScript { $_.result -eq "Success" }).Count | Should -Be 1 } It "Get-TestResults finds 1 failure" { @($results | Where-Object -FilterScript { $_.result -eq "Failure" }).Count | Should -Be 1 } It "Get-TestFailures finds 1 failure" { $failures.Count | Should -Be 1 } } }