Skip to content

Commit

Permalink
Merge pull request #1720 from Microsoft/users/praga/vstestfixm100
Browse files Browse the repository at this point in the history
Fixing support for testsettings file
  • Loading branch information
prawalagarwal committed May 13, 2016
2 parents ba4fb4d + fc3eb20 commit 2027d31
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 89 deletions.
174 changes: 91 additions & 83 deletions Tasks/VsTest/Helpers.ps1
Original file line number Diff line number Diff line change
@@ -1,99 +1,107 @@
function CmdletHasMember {
[cmdletbinding()]
[OutputType([System.Boolean])]
param(
[string]$memberName
)
$publishParameters = (gcm Publish-TestResults).Parameters.Keys.Contains($memberName)
return $publishParameters
[cmdletbinding()]
[OutputType([System.Boolean])]
param(
[string]$memberName
)
$publishParameters = (gcm Publish-TestResults).Parameters.Keys.Contains($memberName)
return $publishParameters
}

function SetRegistryKeyForParallel {
[cmdletbinding()]
param(
[string]$vsTestVersion
)
$regkey = "HKCU\SOFTWARE\Microsoft\VisualStudio\" + $vsTestVersion + "_Config\FeatureFlags\TestingTools\UnitTesting\Taef"
reg add $regkey /v Value /t REG_DWORD /d 1 /f /reg:32 > $null
[cmdletbinding()]
param(
[string]$vsTestVersion
)
$regkey = "HKCU\SOFTWARE\Microsoft\VisualStudio\" + $vsTestVersion + "_Config\FeatureFlags\TestingTools\UnitTesting\Taef"
reg add $regkey /v Value /t REG_DWORD /d 1 /f /reg:32 > $null
}

function IsVisualStudio2015Update1OrHigherInstalled {
[cmdletbinding()]
[OutputType([System.Boolean])]
param(
[string]$vsTestVersion
)
if ([string]::IsNullOrWhiteSpace($vsTestVersion)){
$vsTestVersion = Locate-VSVersion
}
$version = [int]($vsTestVersion)
if($version -ge 14)
{
# checking for dll introduced in vs2015 update1
# since path of the dll will change in dev15+ using vstestversion>14 as a blanket yes
if((Test-Path -Path "$env:VS140COMNTools\..\IDE\CommonExtensions\Microsoft\TestWindow\TE.TestModes.dll") -Or ($version -gt 14))
{
# ensure the registry is set otherwise you need to launch VSIDE
SetRegistryKeyForParallel $vsTestVersion
return $true
}
}
return $false
[cmdletbinding()]
[OutputType([System.Boolean])]
param(
[string]$vsTestVersion
)
if ([string]::IsNullOrWhiteSpace($vsTestVersion)){
$vsTestVersion = Locate-VSVersion
}
$version = [int]($vsTestVersion)
if($version -ge 14)
{
# checking for dll introduced in vs2015 update1
# since path of the dll will change in dev15+ using vstestversion>14 as a blanket yes
if((Test-Path -Path "$env:VS140COMNTools\..\IDE\CommonExtensions\Microsoft\TestWindow\TE.TestModes.dll") -Or ($version -gt 14))
{
# ensure the registry is set otherwise you need to launch VSIDE
SetRegistryKeyForParallel $vsTestVersion
return $true
}
}
return $false
}

function SetupRunSettingsFileForParallel {
[cmdletbinding()]
[OutputType([System.String])]
param(
[string]$runInParallelFlag,
[string]$runSettingsFilePath,
[string]$defaultCpuCount
)
[cmdletbinding()]
[OutputType([System.String])]
param(
[string]$runInParallelFlag,
[string]$runSettingsFilePath,
[string]$defaultCpuCount
)

if($runInParallelFlag -eq "True")
{
$runSettingsForParallel = [xml]'<?xml version="1.0" encoding="utf-8"?>'
if([System.String]::IsNullOrWhiteSpace($runSettingsFilePath) -Or ([string]::Compare([io.path]::GetExtension($runSettingsFilePath), ".runsettings", $True) -ne 0) -Or (Test-Path $runSettingsFilePath -pathtype container)) # no file provided so create one and use it for the run
{
Write-Verbose "No runsettings file provided"
$runSettingsForParallel = [xml]'<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<MaxCpuCount>0</MaxCpuCount>
</RunConfiguration>
</RunSettings>
'
}
else
{
Write-Verbose "Adding maxcpucount element to runsettings file provided"
$runSettingsForParallel = [System.Xml.XmlDocument](Get-Content $runSettingsFilePath)
$runConfigurationElement = $runSettingsForParallel.SelectNodes("//RunSettings/RunConfiguration")
if($runConfigurationElement.Count -eq 0)
{
$runConfigurationElement = $runSettingsForParallel.RunSettings.AppendChild($runSettingsForParallel.CreateElement("RunConfiguration"))
}
if($runInParallelFlag -eq "True")
{
if([string]::Compare([io.path]::GetExtension($runSettingsFilePath), ".testsettings", $True) -eq 0)
{
Write-Warning "Run in Parallel is not supported with testsettings file."
}
else
{
$runSettingsForParallel = [xml]'<?xml version="1.0" encoding="utf-8"?>'
if([System.String]::IsNullOrWhiteSpace($runSettingsFilePath) -Or ([string]::Compare([io.path]::GetExtension($runSettingsFilePath), ".runsettings", $True) -ne 0) -Or (Test-Path $runSettingsFilePath -pathtype container)) # no file provided so create one and use it for the run
{
Write-Verbose "No runsettings file provided"
$runSettingsForParallel = [xml]'<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<MaxCpuCount>0</MaxCpuCount>
</RunConfiguration>
</RunSettings>
'
}
else
{
Write-Verbose "Adding maxcpucount element to runsettings file provided"
$runSettingsForParallel = [System.Xml.XmlDocument](Get-Content $runSettingsFilePath)
$runConfigurationElement = $runSettingsForParallel.SelectNodes("//RunSettings/RunConfiguration")
if($runConfigurationElement.Count -eq 0)
{
$runConfigurationElement = $runSettingsForParallel.RunSettings.AppendChild($runSettingsForParallel.CreateElement("RunConfiguration"))
}

$maxCpuCountElement = $runSettingsForParallel.SelectNodes("//RunSettings/RunConfiguration/MaxCpuCount")
if($maxCpuCountElement.Count -eq 0)
{
$newMaxCpuCountElement = $runConfigurationElement.AppendChild($runSettingsForParallel.CreateElement("MaxCpuCount"))
}
}
$maxCpuCountElement = $runSettingsForParallel.SelectNodes("//RunSettings/RunConfiguration/MaxCpuCount")
if($maxCpuCountElement.Count -eq 0)
{
$newMaxCpuCountElement = $runConfigurationElement.AppendChild($runSettingsForParallel.CreateElement("MaxCpuCount"))
}
}

$runSettingsForParallel.RunSettings.RunConfiguration.MaxCpuCount = $defaultCpuCount
$tempFile = [io.path]::GetTempFileName()
$runSettingsForParallel.Save($tempFile)
Write-Verbose "Temporary runsettings file created at $tempFile"
return $tempFile
}
return $runSettingsFilePath
$runSettingsForParallel.RunSettings.RunConfiguration.MaxCpuCount = $defaultCpuCount
$tempFile = [io.path]::GetTempFileName()
$runSettingsForParallel.Save($tempFile)
Write-Verbose "Temporary runsettings file created at $tempFile"
return $tempFile
}
}

return $runSettingsFilePath
}

function Get-SubKeysInFloatFormat($keys)
Expand Down
2 changes: 1 addition & 1 deletion Tasks/VsTest/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 36
"Patch": 37
},
"demands": [
"vstest"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/VsTest/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 36
"Patch": 37
},
"demands": [
"vstest"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Register-Mock Get-TaskVariable
. $PSScriptRoot\..\..\..\Tasks\VsTest\Helpers.ps1

$cpuCount="1"
$temptestsettingsfile = [io.path]::ChangeExtension([io.path]::GetTempFileName(),"testsettings")
$temptestsettingsfile = [io.path]::ChangeExtension([io.path]::GetTempFileName(),"xml")
$testsettings = @('<?xml version="1.0" encoding="utf-8"?>
<TestSettings name="Empty Test Settings">
<Description>Empty testsettings</Description>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[cmdletbinding()]
param()

# Arrange.
. $PSScriptRoot\..\..\lib\Initialize-Test.ps1
Register-Mock Get-LocalizedString { $OFS = " " ; "$args" }
Register-Mock Get-TaskVariable

. $PSScriptRoot\..\..\..\Tasks\VsTest\Helpers.ps1

$cpuCount="1"
$temptestsettingsfile = [io.path]::ChangeExtension([io.path]::GetTempFileName(),"testsettings")
$testsettings = @('<?xml version="1.0" encoding="utf-8"?>
<TestSettings name="Empty Test Settings">
<Description>Empty testsettings</Description>
</TestSettings>
')
Set-Content -Value $testsettings -Path $temptestsettingsfile

$returnedFilePath = SetupRunSettingsFileForParallel "true" $temptestsettingsfile $cpuCount

$fileExists = Test-Path $returnedFilePath
Assert-AreEqual $true $fileExists

Assert-AreEqual $temptestsettingsfile $returnedFilePath

#cleanup
if($fileExists){
Remove-Item $returnedFilePath
}
if(Test-Path $temptestsettingsfile){
Remove-Item $temptestsettingsfile
}
9 changes: 6 additions & 3 deletions Tests/L0/VsTest/_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@ describe('VsTest Suite', function () {
it('(RunSettingsForParallel.ReturnsNewFileIfParallelIsTrueAndFileNameIsADirectory) returns new file if parallel flag is true and runsettings input is a directory', (done) => {
psr.run(path.join(__dirname, 'RunSettingsForParallel.ReturnsNewFileIfParallelIsTrueAndFileNameIsADirectory.ps1'), done);
})
it('(RunSettingsForParallel.ReturnsNewFileIfParallelIsTrueAndFileNameIsANonRunsettingsFile) returns new file if parallel flag is true and runsettings input is not a runsettings file', (done) => {
psr.run(path.join(__dirname, 'RunSettingsForParallel.ReturnsNewFileIfParallelIsTrueAndFileNameIsANonRunsettingsFile.ps1'), done);
it('(RunSettingsForParallel.ReturnsNewFileIfParallelIsTrueAndFileNameIsANonRunsettingsNonTestSettingsFile) returns new file if parallel flag is true and runsettings input is not a runsettings or testsettings file', (done) => {
psr.run(path.join(__dirname, 'RunSettingsForParallel.ReturnsNewFileIfParallelIsTrueAndFileNameIsANonRunsettingsNonTestSettingsFile.ps1'), done);
})
it('ValidateTestAssembliesAreSplit) tests if the input test assembiles are properly passed to cmdlet', (done) => {
psr.run(path.join(__dirname, 'ValidateTestAssembliesAreSplit.ps1'), done);
})
it('ValidateTestAssembliesAreSplit) tests if the input test assembiles are properly passed to cmdlet', (done) => {
it('ValidateTestAssembliesAreSplit) tests if the input test assembiles are properly passed to cmdlet', (done) => {
psr.run(path.join(__dirname, 'ValidateTestAssembliesAreNotSplit.ps1'), done);
})
it('(RunSettingsForParallel.ReturnsSameFileIfParallelIsTrueAndFileNameIsTestSettingsFile) returns same file if parallel flag is true and runsettings input is a testsettings file', (done) => {
psr.run(path.join(__dirname, 'RunSettingsForParallel.ReturnsSameFileIfParallelIsTrueAndFileNameIsTestSettingsFile.ps1'), done);
})
}
});

0 comments on commit 2027d31

Please sign in to comment.