Skip to content

Instantly share code, notes, and snippets.

@rismoney
Created September 9, 2023 19:37
Show Gist options
  • Save rismoney/ea764e2b8e127de86bc7105e27c96e8c to your computer and use it in GitHub Desktop.
Save rismoney/ea764e2b8e127de86bc7105e27c96e8c to your computer and use it in GitHub Desktop.
import-module .\Output\g1pwsh\g1pwsh.psm1
Describe "Configure-PSRepository Tests" {
BeforeAll {
Mock Install-Module -ModuleName "g1pwsh" {
param (
[string]$Name,
[string]$RequiredVersion,
[string]$Scope,
[string]$Repository,
[switch]$Force
)
$global:MockedInstallModuleArgs = $PSBoundParameters
}
Mock Get-PackageSource -Modulename "PackageManagement" {
$chocolatey = New-Object PSObject -Property @{
Name = "chocolatey"
ProviderName = "NuGet"
}
$nuget = New-Object PSObject -Property @{
Name = "nuget.org"
ProviderName = "NuGet"
}
$psGalleryVirtual = New-Object PSObject -Property @{
Name = "PSGallery"
ProviderName = "NuGet"
}
$packageSources = @($chocolatey, $nuget, $psGalleryVirtual)
}
}
Context "Installing PowerShellGet" {
It "Should install PowerShellGet module with the specified version, scope, and internal repository" {
Configure-PSRepository
# Verify specific parameter values
$global:MockedInstallModuleArgs.Name | Should -Be 'PowerShellGet'
$global:MockedInstallModuleArgs.RequiredVersion | Should -Be '2.2.5'
$global:MockedInstallModuleArgs.Scope | Should -Be 'AllUsers'
$global:MockedInstallModuleArgs.Repository | Should -Be "psgallery-virtual"
Should -Invoke -CommandName Install-Module -Modulename "g1pwsh" -Exactly 1
Should -Invoke -CommandName Get-PackageSource -Modulename "g1pwsh" -Exactly 1
}
}
}
@rismoney
Copy link
Author

rismoney commented Sep 9, 2023

this is a function in g1pwsh I am trying to test.

function Configure-PSRepository {

  # Install a specific version of PowerShellGet from the virtual repository
  Install-Module -Name PowerShellGet -Force -RequiredVersion 2.2.5 -Scope AllUsers -Repository psgallery-virtual

  # Unregister unwanted package sources
  $packageSources = Get-PackageSource -ErrorAction SilentlyContinue
  $sourcesToRemove = ("chocolatey", "nuget.org", "PSGallery")
  $packageSources | Where-Object { $_.Name -in $sourcesToRemove } | ForEach-Object {
  Unregister-PackageSource -Name $_.Name -ProviderName $_.ProviderName -ErrorAction SilentlyContinue
  }

  # Register a custom chocolatey package source
  Register-PackageSource -Name "chocolatey" -Location "https://chocolatey.example.com/chocolatey/" -ProviderName "Nuget" -ErrorAction SilentlyContinue -Force -ForceBootstrap
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment