|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +# these are tests for the build module |
| 5 | + |
| 6 | +import-module -force "./build.psm1" |
| 7 | +Describe "Build Module Tests" { |
| 8 | + Context "Global.json" { |
| 9 | + BeforeAll { |
| 10 | + $globalJson = Get-Content (Join-Path $PSScriptRoot global.json) | ConvertFrom-Json |
| 11 | + $expectedVersion = $globalJson.sdk.version |
| 12 | + $result = Get-GlobalJsonSdkVersion |
| 13 | + } |
| 14 | + $propertyTestcases = @{ Name = "Major"; Type = "System.Int32" }, |
| 15 | + @{ Name = "Minor"; Type = "System.Int32" }, |
| 16 | + @{ Name = "Patch"; Type = "System.Int32" }, |
| 17 | + @{ Name = "PrereleaseLabel"; Type = "System.String" } |
| 18 | + It "Get-GlobalJsonSdkVersion returns a portable version object with property '<Name>' with type '<Type>'" -TestCases $propertyTestcases { |
| 19 | + param ( $Name, $Type ) |
| 20 | + $result.psobject.properties[$Name] | Should -BeOfType [System.Management.Automation.PSNoteProperty] |
| 21 | + $result.psobject.properties[$Name].TypeNameOfValue | Should -Be $Type |
| 22 | + } |
| 23 | + It "Can retrieve the version from global.json" { |
| 24 | + $result = Get-GlobalJsonSdkVersion |
| 25 | + $resultString = "{0}.{1}.{2}" -f $result.Major,$result.Minor,$result.Patch |
| 26 | + if ( $result.prereleasestring ) { $resultString += "-" + $result.prereleasestring } |
| 27 | + $resultString | Should -Be $expectedVersion |
| 28 | + } |
| 29 | + } |
| 30 | + Context "Test-SuiteableDotnet" { |
| 31 | + It "Test-SuitableDotnet should return true when the expected version matches the installed version" { |
| 32 | + Test-SuitableDotnet -availableVersions 2.1.2 -requiredVersion 2.1.2 | Should -Be $true |
| 33 | + } |
| 34 | + It "Test-SuitableDotnet should return true when the expected version matches the available versions" { |
| 35 | + Test-SuitableDotnet -availableVersions "2.1.1","2.1.2","2.1.3" -requiredVersion 2.1.2 | Should -Be $true |
| 36 | + } |
| 37 | + It "Test-SuitableDotnet should return false when the expected version does not match an available" { |
| 38 | + Test-SuitableDotnet -availableVersions "2.2.100","2.2.300" -requiredVersion 2.2.200 | Should -Be $false |
| 39 | + } |
| 40 | + It "Test-SuitableDotnet should return false when the expected version does not match an available" { |
| 41 | + Test-SuitableDotnet -availableVersions "2.2.100","2.2.300" -requiredVersion 2.2.105 | Should -Be $false |
| 42 | + } |
| 43 | + It "Test-SuitableDotnet should return true when the expected version matches an available" { |
| 44 | + Test-SuitableDotnet -availableVersions "2.2.150","2.2.300" -requiredVersion 2.2.105 | Should -Be $true |
| 45 | + } |
| 46 | + It "Test-SuitableDotnet should return false when the expected version does not match an available" { |
| 47 | + Test-SuitableDotnet -availableVersions "2.2.400","2.2.401","2.2.405" -requiredVersion "2.2.410" | Should -Be $false |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + Context "Test-DotnetInstallation" { |
| 52 | + BeforeAll { |
| 53 | + $availableVersions = ConvertTo-PortableVersion -strVersion "2.2.400","2.2.401","2.2.405" |
| 54 | + $foundVersion = ConvertTo-PortableVersion -strVersion 2.2.402 |
| 55 | + $missingVersion = ConvertTo-PortableVersion -strVersion 2.2.410 |
| 56 | + } |
| 57 | + |
| 58 | + It "Test-DotnetInstallation finds a good version" { |
| 59 | + Mock Get-InstalledCLIVersion { return $availableVersions } |
| 60 | + Mock Get-GlobalJSonSdkVersion { return $foundVersion } |
| 61 | + $result = Test-DotnetInstallation -requestedVersion (Get-GlobalJsonSdkVersion) -installedVersions (Get-InstalledCLIVersion) |
| 62 | + Assert-MockCalled "Get-InstalledCLIVersion" -Times 1 |
| 63 | + Assert-MockCalled "Get-GlobalJsonSdkVersion" -Times 1 |
| 64 | + $result | Should -Be $true |
| 65 | + } |
| 66 | + |
| 67 | + It "Test-DotnetInstallation cannot find a good version should return false" { |
| 68 | + Mock Get-InstalledCLIVersion { return $availableVersions } |
| 69 | + Mock Get-GlobalJSonSdkVersion { return $missingVersion } |
| 70 | + $result = Test-DotnetInstallation -requestedVersion (Get-GlobalJsonSdkVersion) -installedVersions (Get-InstalledCLIVersion) |
| 71 | + Assert-MockCalled "Get-InstalledCLIVersion" -Times 1 |
| 72 | + Assert-MockCalled "Get-GlobalJsonSdkVersion" -Times 1 |
| 73 | + $result | Should -Be $false |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + Context "Receive-DotnetInstallScript" { |
| 78 | + |
| 79 | + Mock -ModuleName Build Receive-File { new-item -type file TestDrive:/dotnet-install.sh } |
| 80 | + It "Downloads the proper non-Windows file" { |
| 81 | + try { |
| 82 | + push-location TestDrive: |
| 83 | + Receive-DotnetInstallScript -platform NonWindows |
| 84 | + "TestDrive:/dotnet-install.sh" | Should -Exist |
| 85 | + } |
| 86 | + finally { |
| 87 | + Pop-Location |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + Mock -ModuleName Build Receive-File { new-item -type file TestDrive:/dotnet-install.ps1 } |
| 92 | + It "Downloads the proper file Windows file" { |
| 93 | + try { |
| 94 | + push-location TestDrive: |
| 95 | + Receive-DotnetInstallScript -platform "Windows" |
| 96 | + "TestDrive:/dotnet-install.ps1" | Should -Exist |
| 97 | + } |
| 98 | + finally { |
| 99 | + Pop-Location |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + Context "Test result functions" { |
| 106 | + BeforeAll { |
| 107 | + $xmlFile = @' |
| 108 | +<?xml version="1.0" encoding="utf-8" standalone="no"?> |
| 109 | +<test-results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="nunit_schema_2.5.xsd" name="Pester" total="2" errors="0" failures="1" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2019-02-19" time="11:36:56"> |
| 110 | + <environment platform="Darwin" clr-version="Unknown" os-version="18.2.0" cwd="/Users/jimtru/src/github/forks/JamesWTruher/PSScriptAnalyzer" user="jimtru" user-domain="" machine-name="Jims-Mac-mini.guest.corp.microsoft.com" nunit-version="2.5.8.0" /> |
| 111 | + <culture-info current-culture="en-US" current-uiculture="en-US" /> |
| 112 | + <test-suite type="TestFixture" name="Pester" executed="True" result="Failure" success="False" time="0.0982" asserts="0" description="Pester"> |
| 113 | + <results> |
| 114 | + <test-suite type="TestFixture" name="/tmp/bad.tests.ps1" executed="True" result="Failure" success="False" time="0.0982" asserts="0" description="/tmp/bad.tests.ps1"> |
| 115 | + <results> |
| 116 | + <test-suite type="TestFixture" name="test function" executed="True" result="Failure" success="False" time="0.084" asserts="0" description="test function"> |
| 117 | + <results> |
| 118 | + <test-case description="a passing test" name="test function.a passing test" time="0.0072" asserts="0" success="True" result="Success" executed="True" /> |
| 119 | + <test-case description="a failing test" name="test function.a failing test" time="0.0268" asserts="0" success="False" result="Failure" executed="True"> |
| 120 | + <failure> |
| 121 | + <message>Expected 2, but got 1.</message> |
| 122 | + <stack-trace>at <ScriptBlock>, /tmp/bad.tests.ps1: line 3 |
| 123 | +3: It "a failing test" { 1 | Should -Be 2 }</stack-trace> |
| 124 | + </failure> |
| 125 | + </test-case> |
| 126 | + </results> |
| 127 | + </test-suite> |
| 128 | + </results> |
| 129 | + </test-suite> |
| 130 | + </results> |
| 131 | + </test-suite> |
| 132 | +</test-results> |
| 133 | +'@ |
| 134 | + |
| 135 | + $xmlFile | out-file TESTDRIVE:/results.xml |
| 136 | + $results = Get-TestResults -logfile TESTDRIVE:/results.xml |
| 137 | + $failures = Get-TestFailures -logfile TESTDRIVE:/results.xml |
| 138 | + } |
| 139 | + |
| 140 | + It "Get-TestResults finds 2 results" { |
| 141 | + $results.Count | Should -Be 2 |
| 142 | + } |
| 143 | + It "Get-TestResults finds 1 pass" { |
| 144 | + @($results | ?{ $_.result -eq "Success" }).Count |Should -Be 1 |
| 145 | + } |
| 146 | + It "Get-TestResults finds 1 failure" { |
| 147 | + @($results | ?{ $_.result -eq "Failure" }).Count |Should -Be 1 |
| 148 | + } |
| 149 | + It "Get-TestFailures finds 1 failure" { |
| 150 | + $failures.Count | Should -Be 1 |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments