Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to GitHubRepositories.tests.ps1 #176

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ Thank you to all of our contributors, no matter how big or small the contributio
- **[Matt Boren (@mtboren)](http://github.com/mtboren)**
- **[Shannon Deminick (@Shazwazza)](http://github.com/Shazwazza)**
- **[Jess Pomfret (@jpomfret)](https://github.com/jpomfret)**
- **[Giuseppe Campanelli (@themilanfan)](https://github.com/themilanfan)**

----------

Expand Down
195 changes: 166 additions & 29 deletions Tests/GitHubRepositories.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<#
.Synopsis
Tests for GitHubRepositories.ps1 module
.Description
Many cmdlets are indirectly tested in the course of other tests (New-GitHubRepository, Remove-GitHubRepository), and may not have explicit tests here
#>

# This is common test code setup logic for all Pester test files
Expand All @@ -14,6 +12,15 @@ $moduleRootPath = Split-Path -Path $PSScriptRoot -Parent

try
{
# Define Script-scoped, readonly, hidden variables.
@{
defaultRepoDesc = "This is a description."
defaultRepoHomePage = "https://www.microsoft.com/"
defaultRepoTopic = "microsoft"
}.GetEnumerator() | ForEach-Object {
Set-Variable -Force -Scope Script -Option ReadOnly -Visibility Private -Name $_.Key -Value $_.Value
}

Describe 'Getting repositories' {
Context 'For authenticated user' {
BeforeAll -Scriptblock {
Expand All @@ -25,22 +32,23 @@ try
$privateRepo = $privateRepo
}

$publicRepos = @(Get-GitHubRepository -Visibility Public)
$privateRepos = @(Get-GitHubRepository -Visibility Private)

It "Should have the public repo" {
$publicRepo.Name | Should BeIn $publicRepos.Name
$publicRepo.Name | Should Not BeIn $privateRepos.Name
$publicRepos = @(Get-GitHubRepository -Visibility Public)
$privateRepos = @(Get-GitHubRepository -Visibility Private)
$publicRepo.name | Should -BeIn $publicRepos.name
$publicRepo.name | Should -Not -BeIn $privateRepos.name
}

It "Should have the private repo" {
$privateRepo.Name | Should BeIn $privateRepos.Name
$privateRepo.Name | Should Not BeIn $publicRepos.Name
$publicRepos = @(Get-GitHubRepository -Visibility Public)
$privateRepos = @(Get-GitHubRepository -Visibility Private)
$privateRepo.name | Should -BeIn $privateRepos.name
$privateRepo.name | Should -Not -BeIn $publicRepos.name
}

It 'Should not permit bad combination of parameters' {
{ Get-GitHubRepository -Type All -Visibility All } | Should Throw
{ Get-GitHubRepository -Type All -Affiliation Owner } | Should Throw
{ Get-GitHubRepository -Type All -Visibility All } | Should -Throw
{ Get-GitHubRepository -Type All -Affiliation Owner } | Should -Throw
}

AfterAll -ScriptBlock {
Expand All @@ -50,11 +58,10 @@ try
}

Context 'For any user' {
$repos = @(Get-GitHubRepository -OwnerName 'octocat' -Type Public)

It "Should have results for The Octocat" {
$repos = @(Get-GitHubRepository -OwnerName 'octocat' -Type Public)
$repos.Count | Should -BeGreaterThan 0
$repos[0].owner.login | Should Be 'octocat'
$repos[0].owner.login | Should -Be 'octocat'
}
}

Expand All @@ -66,9 +73,9 @@ try
$repo = $repo
}

$repos = @(Get-GitHubRepository -OrganizationName $script:organizationName -Type All)
It "Should have results for the organization" {
$repo.name | Should BeIn $repos.name
$repos = @(Get-GitHubRepository -OrganizationName $script:organizationName -Type All)
$repo.name | Should -BeIn $repos.name
}

AfterAll -ScriptBlock {
Expand All @@ -82,30 +89,30 @@ try
}

Context 'For a specific repo' {
BeforeAll -Scriptblock {
BeforeAll -ScriptBlock {
$repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit

# Avoid PSScriptAnalyzer PSUseDeclaredVarsMoreThanAssignments
$repo = $repo
}

$result = Get-GitHubRepository -Uri $repo.svn_url
It "Should be a single result using Uri ParameterSet" {
$result = Get-GitHubRepository -Uri $repo.svn_url
$result | Should -BeOfType PSCustomObject
}

$result = Get-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.Name
It "Should be a single result using Elements ParameterSet" {
$result = Get-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.name
$result | Should -BeOfType PSCustomObject
}

It 'Should not permit additional parameters' {
{ Get-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.Name -Type All } | Should Throw
{ Get-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.name -Type All } | Should -Throw
}

It 'Should require both OwnerName and RepositoryName' {
{ Get-GitHubRepository -RepositoryName $repo.Name } | Should Throw
{ Get-GitHubRepository -Uri "https://github.com/$script:ownerName" } | Should Throw
{ Get-GitHubRepository -RepositoryName $repo.name } | Should -Throw
{ Get-GitHubRepository -Uri "https://github.com/$script:ownerName" } | Should -Throw
}

AfterAll -ScriptBlock {
Expand All @@ -114,30 +121,160 @@ try
}
}

Describe 'Modifying repositories' {
Describe 'Creating repositories' {

Context -Name 'For creating a repository' -Fixture {
BeforeAll -ScriptBlock {
$repoName = ([Guid]::NewGuid().Guid)
$repo = New-GitHubRepository -RepositoryName $repoName -Description $defaultRepoDesc -AutoInit
}

It 'Should get repository' {
$repo | Should -Not -BeNullOrEmpty
}

It 'Name is correct' {
$repo.name | Should -Be $repoName
}

It 'Description is correct' {
$repo.description | Should -Be $defaultRepoDesc
}

AfterAll -ScriptBlock {
Remove-GitHubRepository -Uri $repo.svn_url-Confirm:$false
}
}
}

Describe 'Deleting repositories' {

Context -Name 'For deleting a repository' -Fixture {
BeforeAll -ScriptBlock {
$repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -Description $defaultRepoDesc -AutoInit
}

It 'Should get no content' {
$delete = Remove-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.name -Confirm:$false
{ Get-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.name } | Should -Throw
}
}
}

Describe 'Renaming repositories' {

Context -Name 'For renaming a repository' -Fixture {
BeforeEach -Scriptblock {
$repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit
$suffixToAddToRepo = "_renamed"
$newRepoName = "$($repo.Name)$suffixToAddToRepo"
Write-Verbose "New repo name shall be: '$newRepoName'"
$newRepoName = "$($repo.name)$suffixToAddToRepo"
}

It "Should have the expected new repository name - by URI" {
$renamedRepo = $repo | Rename-GitHubRepository -NewName $newRepoName -Confirm:$false
$renamedRepo.Name | Should be $newRepoName
$renamedRepo.name | Should -Be $newRepoName
}

It "Should have the expected new repository name - by Elements" {
$renamedRepo = Rename-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.name -NewName $newRepoName -Confirm:$false
$renamedRepo.Name | Should be $newRepoName
$renamedRepo.name | Should -Be $newRepoName
}
## cleanup temp testing repository

AfterEach -Scriptblock {
## variables from BeforeEach scriptblock are accessible here, but not variables from It scriptblocks, so need to make URI (instead of being able to use $renamedRepo variable from It scriptblock)
Remove-GitHubRepository -Uri "$($repo.svn_url)$suffixToAddToRepo" -Confirm:$false
}
}
}

Describe 'Updating repositories' {

Context -Name 'For creating a repository' -Fixture {
BeforeAll -ScriptBlock {
$repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -Description $defaultRepoDesc -AutoInit
}

It 'Should have the new updated description' {
$modifiedRepoDesc = $defaultRepoDesc + "_modified"
$updatedRepo = Update-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.name -Description $modifiedRepoDesc
$updatedRepo.description | Should -Be $modifiedRepoDesc
}

It 'Should have the new updated homepage url' {
$updatedRepo = Update-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.name -Homepage $defaultRepoHomePage
$updatedRepo.homepage | Should -Be $defaultRepoHomePage
}

AfterAll -ScriptBlock {
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
}
}

Describe 'Get/set repository topic' {

Context -Name 'For creating and getting a repository topic' -Fixture {
BeforeAll -ScriptBlock {
$repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit
}

It 'Should have the expected topic' {
Set-GitHubRepositoryTopic -OwnerName $repo.owner.login -RepositoryName $repo.name -Name $defaultRepoTopic
$topic = Get-GitHubRepositoryTopic -OwnerName $repo.owner.login -RepositoryName $repo.name
$topic.names | Should -Be $defaultRepoTopic
}

It 'Should have no topics' {
Set-GitHubRepositoryTopic -OwnerName $repo.owner.login -RepositoryName $repo.name -Clear
$topic = Get-GitHubRepositoryTopic -OwnerName $repo.owner.login -RepositoryName $repo.name
$topic.names | Should -BeNullOrEmpty
}

AfterAll -ScriptBlock {
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
}
}

Describe 'Get repository languages' {

Context -Name 'For getting repository languages' -Fixture {
BeforeAll -ScriptBlock {
$repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit
}

It 'Should be empty' {
$languages = Get-GitHubRepositoryLanguage -OwnerName $repo.owner.login -RepositoryName $repo.name
$languages | Should -BeNullOrEmpty
}

It 'Should be contain PowerShell' {
$languages = Get-GitHubRepositoryLanguage -OwnerName "microsoft" -RepositoryName "PowerShellForGitHub"
$languages.PowerShell | Should -Not -BeNullOrEmpty
}

AfterAll -ScriptBlock {
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
}
}

Describe 'Get repository tags' {

Context -Name 'For getting repository tags' -Fixture {
BeforeAll -ScriptBlock {
$repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit
}

It 'Should be empty' {
$tags = Get-GitHubRepositoryTag -OwnerName $repo.owner.login -RepositoryName $repo.name
$tags | Should -BeNullOrEmpty
}

AfterAll -ScriptBlock {
Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false
}
}
}
}
finally
{
Expand Down