Skip to content

Commit

Permalink
Break out Unit tests (#285)
Browse files Browse the repository at this point in the history
InModuleScope has been removed from all unit tests.
All unit tests are in own files.
  • Loading branch information
DarqueWarrior authored Mar 30, 2020
1 parent 8e28722 commit c2cb57d
Show file tree
Hide file tree
Showing 309 changed files with 12,615 additions and 10,530 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ coverage.xml
._.DS_Store
**/.DS_Store
**/._.DS_Store
.gdn
6 changes: 3 additions & 3 deletions Source/Classes/VSTeamPermissionInheritance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class VSTeamPermissionInheritance {

$this.Token = "repoV2/$($this.ProjectID)/$repositoryID"

$this.Version = "$([VSTeamVersions]::Git)"
$this.Version = "$(_getApiVersion Git)"
}

"BuildDefinition" {
Expand All @@ -39,7 +39,7 @@ class VSTeamPermissionInheritance {

$this.Token = "$($this.ProjectID)/$buildDefinitionID"

$this.Version = "$([VSTeamVersions]::Build)"
$this.Version = "$(_getApiVersion Build)"
}

"ReleaseDefinition" {
Expand All @@ -59,7 +59,7 @@ class VSTeamPermissionInheritance {
$this.Token = "$($this.ProjectID)" + "$($releaseDefinition.path -replace "\\","/")" + "/$($releaseDefinition.id)"
}

$this.Version = "$([VSTeamVersions]::Release)"
$this.Version = "$(_getApiVersion Release)"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Source/Classes/VSTeamVersions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ class VSTeamVersions {
static [string] $ServiceFabricEndpoint = ''
static [string] $ModuleVersion = $null
static [string] $Graph = ''
static [string] $Policy = ''
}
2 changes: 1 addition & 1 deletion Source/Private/callMembershipAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function _callMembershipAPI {
-Id $Id `
-SubDomain "vssps" `
-Method $Method `
-Version $([VSTeamVersions]::Graph) `
-Version $(_getApiVersion Graph) `
-QueryString $query

return $resp
Expand Down
123 changes: 108 additions & 15 deletions Source/Private/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function _supportsGraph {
}

function _testGraphSupport {
if (-not [VSTeamVersions]::Graph) {
if (-not $(_getApiVersion Graph)) {
return $false
}

Expand All @@ -30,7 +30,7 @@ function _supportsFeeds {
}

function _testFeedSupport {
if (-not [VSTeamVersions]::Packaging) {
if (-not $(_getApiVersion Packaging)) {
return $false
}

Expand All @@ -46,7 +46,7 @@ function _supportsSecurityNamespace {

function _supportsMemberEntitlementManagement {
_hasAccount
if (-not [VSTeamVersions]::MemberEntitlementManagement) {
if (-not $(_getApiVersion MemberEntitlementManagement)) {
throw 'This account does not support Member Entitlement.'
}
}
Expand All @@ -56,6 +56,74 @@ function _testAdministrator {
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

# When you mock this in tests be sure to add a Parameter Filter that matches
# the Service that should be used.
# Mock _getApiVersion { return '1.0-gitUnitTests' } -ParameterFilter { $Service -eq 'Git' }
# Also test in the Assert-MockCalled that the correct version was used in the URL that was
# built for the API call.
function _getApiVersion {
[CmdletBinding(DefaultParameterSetName = 'Service')]
param (
[parameter(ParameterSetName = 'Service', Mandatory = $true, Position = 0)]
[ValidateSet('Build', 'Release', 'Core', 'Git', 'DistributedTask', 'VariableGroups', 'Tfvc', 'Packaging', 'MemberEntitlementManagement', 'ExtensionsManagement', 'ServiceFabricEndpoint', 'Graph', 'TaskGroups', 'Policy')]
[string] $Service,

[parameter(ParameterSetName = 'Target')]
[switch] $Target
)

if ($Target.IsPresent) {
return [VSTeamVersions]::Version
}
else {

switch ($Service) {
'Build' {
return [VSTeamVersions]::Build
}
'Release' {
return [VSTeamVersions]::Release
}
'Core' {
return [VSTeamVersions]::Core
}
'Git' {
return [VSTeamVersions]::Git
}
'DistributedTask' {
return [VSTeamVersions]::DistributedTask
}
'VariableGroups' {
return [VSTeamVersions]::VariableGroups
}
'Tfvc' {
return [VSTeamVersions]::Tfvc
}
'Packaging' {
return [VSTeamVersions]::Packaging
}
'MemberEntitlementManagement' {
return [VSTeamVersions]::MemberEntitlementManagement
}
'ExtensionsManagement' {
return [VSTeamVersions]::ExtensionsManagement
}
'ServiceFabricEndpoint' {
return [VSTeamVersions]::ServiceFabricEndpoint
}
'Graph' {
return [VSTeamVersions]::Graph
}
'TaskGroups' {
return [VSTeamVersions]::TaskGroups
}
'Policy' {
return [VSTeamVersions]::Policy
}
}
}
}

function _getInstance {
return [VSTeamVersions]::Account
}
Expand All @@ -76,7 +144,8 @@ function _buildRequestURI {
[string]$version,
[string]$subDomain,
[object]$queryString,
[switch]$UseProjectId
[switch]$UseProjectId,
[switch]$NoProject
)
DynamicParam {
_buildProjectNameDynamicParam -Mandatory $false
Expand All @@ -92,7 +161,12 @@ function _buildRequestURI {

$sb.Append($(_addSubDomain -subDomain $subDomain -instance $(_getInstance))) | Out-Null

if ($ProjectName) {
# There are some APIs that must not have the project added to the URI.
# However, if they caller set the default project it will be passed in
# here and added to the URI by mistake. Functions that need the URI
# created without the project even if the default project is set needs
# to pass the -NoProject switch.
if ($ProjectName -and $NoProject.IsPresent -eq $false) {
if ($UseProjectId.IsPresent) {
$projectId = (Get-VSTeamProject -Name $ProjectName | Select-Object -ExpandProperty id)
$sb.Append("/$projectId") | Out-Null
Expand Down Expand Up @@ -278,7 +352,7 @@ function _getWorkItemTypes {

# Call the REST API
try {
$resp = _callAPI -ProjectName $ProjectName -area 'wit' -resource 'workitemtypes' -version $([VSTeamVersions]::Core)
$resp = _callAPI -ProjectName $ProjectName -area 'wit' -resource 'workitemtypes' -version $(_getApiVersion Core)

# This call returns JSON with "": which causes the ConvertFrom-Json to fail.
# To replace all the "": with "_end":
Expand All @@ -294,6 +368,14 @@ function _getWorkItemTypes {
}
}

# When writing unit tests mock this and return false.
# This will prevent the dynamic project name parameter
# from trying to call the getProject function.
# Mock _hasProjectCacheExpired { return $false }
function _hasProjectCacheExpired {
return $([VSTeamProjectCache]::timestamp) -ne (Get-Date).Minute
}

function _getProjects {
if (-not $(_getInstance)) {
Write-Output @()
Expand All @@ -302,7 +384,7 @@ function _getProjects {

$resource = "/projects"
$instance = $(_getInstance)
$version = [VSTeamVersions]::Core
$version = $(_getApiVersion Core)

# Build the url to list the projects
# You CANNOT use _buildRequestURI here or you will end up
Expand Down Expand Up @@ -358,7 +440,7 @@ function _buildProjectNameDynamicParam {
}

# Generate and set the ValidateSet
if ($([VSTeamProjectCache]::timestamp) -ne (Get-Date).Minute) {
if (_hasProjectCacheExpired) {
$arrSet = _getProjects
[VSTeamProjectCache]::projects = $arrSet
[VSTeamProjectCache]::timestamp = (Get-Date).Minute
Expand Down Expand Up @@ -413,10 +495,10 @@ function _getProcesses {

# Call the REST API
try {
$query = @{}
$query = @{ }
$query['stateFilter'] = 'All'
$query['$top'] = '9999'
$resp = _callAPI -area 'process' -resource 'processes' -Version $([VSTeamVersions]::Core) -QueryString $query
$resp = _callAPI -area 'process' -resource 'processes' -Version $(_getApiVersion Core) -QueryString $query

if ($resp.count -gt 0) {
Write-Output ($resp.value).name
Expand Down Expand Up @@ -620,7 +702,18 @@ function _callAPI {
[string]$Url,
[object]$QueryString,
[hashtable]$AdditionalHeaders,
[switch]$UseProjectId
# Some API calls require the Project ID and not the project name.
# However, the dynamic project name parameter only shows you names
# and not the Project IDs. Using this flag the project name provided
# will be converted to the Project ID when building the URI for the API
# call.
[switch]$UseProjectId,
# This flag makes sure that even if a default project is set that it is
# not used to build the URI for the API call. Not all API require or
# allow the project to be used. Setting a default project would cause
# that project name to be used in building the URI that would lead to
# 404 because the URI would not be correct.
[switch]$NoProject
)

# If the caller did not provide a Url build it.
Expand Down Expand Up @@ -648,7 +741,7 @@ function _callAPI {

if (_useWindowsAuthenticationOnPremise) {
$params.Add('UseDefaultCredentials', $true)
$params.Add('Headers', @{})
$params.Add('Headers', @{ })
}
elseif (_useBearerToken) {
$params.Add('Headers', @{Authorization = "Bearer $env:TEAM_TOKEN" })
Expand All @@ -664,7 +757,7 @@ function _callAPI {
}

# We have to remove any extra parameters not used by Invoke-RestMethod
$extra = 'UseProjectId', 'Area', 'Resource', 'SubDomain', 'Id', 'Version', 'JSON', 'ProjectName', 'Team', 'Url', 'QueryString', 'AdditionalHeaders'
$extra = 'NoProject', 'UseProjectId', 'Area', 'Resource', 'SubDomain', 'Id', 'Version', 'JSON', 'ProjectName', 'Team', 'Url', 'QueryString', 'AdditionalHeaders'
foreach ($e in $extra) { $params.Remove($e) | Out-Null }

try {
Expand Down Expand Up @@ -737,7 +830,7 @@ function _trackServiceEndpointProgress {
# Track status
while (-not $isReady) {
$statusTracking = _callAPI -ProjectName $projectName -Area 'distributedtask' -Resource 'serviceendpoints' -Id $resp.id `
-Version $([VSTeamVersions]::DistributedTask)
-Version $(_getApiVersion DistributedTask)

$isReady = $statusTracking.isReady;

Expand All @@ -760,7 +853,7 @@ function _trackServiceEndpointProgress {
}

function _supportsServiceFabricEndpoint {
if (-not [VSTeamVersions]::ServiceFabricEndpoint) {
if (-not $(_getApiVersion ServiceFabricEndpoint)) {
throw 'This account does not support Service Fabric endpoints.'
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Public/Add-VSTeam.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ function Add-VSTeam {
$body = '{ "name": "' + $Name + '", "description": "' + $Description + '" }'

# Call the REST API
$resp = _callAPI -Area 'projects' -Resource "$ProjectName/teams" `
-Method Post -ContentType 'application/json' -Body $body -Version $([VSTeamVersions]::Core)
$resp = _callAPI -Area 'projects' -Resource "$ProjectName/teams" -NoProject `
-Method Post -ContentType 'application/json' -Body $body -Version $(_getApiVersion Core)

$team = [VSTeamTeam]::new($resp, $ProjectName)

Expand Down
22 changes: 6 additions & 16 deletions Source/Public/Add-VSTeamAccessControlEntry.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ function Add-VSTeamAccessControlEntry {
[VSTeamSecurityNamespace] $SecurityNamespace,

[Parameter(ParameterSetName = 'ByNamespaceId', Mandatory = $true)]
[ValidateScript({
try {
[System.Guid]::Parse($_) | Out-Null
$true
} catch {
$false
}
})]
[string] $SecurityNamespaceId,
[guid] $SecurityNamespaceId,

[Parameter(ParameterSetName = 'ByNamespace', Mandatory = $true)]
[Parameter(ParameterSetName = 'ByNamespaceId', Mandatory = $true)]
Expand All @@ -35,13 +27,12 @@ function Add-VSTeamAccessControlEntry {
)

process {
if ($SecurityNamespace)
{
if ($SecurityNamespace) {
$SecurityNamespaceId = $SecurityNamespace.ID
}

$body =
@"
$body =
@"
{
"token": "$Token",
"merge": true,
Expand All @@ -57,11 +48,10 @@ $body =
"@
# Call the REST API
$resp = _callAPI -Area 'accesscontrolentries' -id $SecurityNamespaceId -method POST -body $body `
-Version $([VSTeamVersions]::Core) `
-Version $(_getApiVersion Core) -NoProject `
-ContentType "application/json"

if ($resp.count -ne 1)
{
if ($resp.count -ne 1) {
throw "Expected 1 result, but got $($rep.count)"
}

Expand Down
8 changes: 4 additions & 4 deletions Source/Public/Add-VSTeamBuild.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ function Add-VSTeamBuild {
else {
# Find the BuildDefinition id from the name
$id = Get-VSTeamBuildDefinition -ProjectName "$ProjectName" -Type All |
Where-Object { $_.name -eq $BuildDefinition } |
Select-Object -ExpandProperty id
Where-Object { $_.name -eq $BuildDefinition } |
Select-Object -ExpandProperty id
}

$body = @{
Expand All @@ -73,7 +73,7 @@ function Add-VSTeamBuild {

if ($QueueName) {
$queueId = Get-VSTeamQueue -ProjectName "$ProjectName" -queueName "$QueueName" |
Select-Object -ExpandProperty Id
Select-Object -ExpandProperty Id

$body.Add('queue', @{ id = $queueId })
}
Expand All @@ -89,7 +89,7 @@ function Add-VSTeamBuild {
# Call the REST API
$resp = _callAPI -ProjectName $ProjectName -Area 'build' -Resource 'builds' `
-Method Post -ContentType 'application/json' -Body ($body | ConvertTo-Json) `
-Version $([VSTeamVersions]::Build)
-Version $(_getApiVersion Build)

_applyTypesToBuild -item $resp

Expand Down
2 changes: 1 addition & 1 deletion Source/Public/Add-VSTeamBuildDefinition.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function Add-VSTeamBuildDefinition {
# Bind the parameter to a friendly variable
$ProjectName = $PSBoundParameters["ProjectName"]

$resp = _callAPI -Method Post -ProjectName $ProjectName -Area build -Resource definitions -Version $([VSTeamVersions]::Build) -infile $InFile -ContentType 'application/json'
$resp = _callAPI -Method Post -ProjectName $ProjectName -Area build -Resource definitions -Version $(_getApiVersion Build) -infile $InFile -ContentType 'application/json'

return $resp
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Public/Add-VSTeamBuildTag.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function Add-VSTeamBuildTag {
foreach ($tag in $tags) {
# Call the REST API
_callAPI -ProjectName $projectName -Area 'build' -Resource "builds/$Id/tags" `
-Method Put -Querystring @{tag = $tag} -Version $([VSTeamVersions]::Build) | Out-Null
-Method Put -Querystring @{tag = $tag} -Version $(_getApiVersion Build) | Out-Null
}
}
}
Expand Down
Loading

0 comments on commit c2cb57d

Please sign in to comment.