Skip to content

Commit

Permalink
Merge pull request #312 from SamErde/sde-Install-Update-Tests
Browse files Browse the repository at this point in the history
Only delete known Maester tests with Update-MaesterTests
  • Loading branch information
merill authored Jul 11, 2024
2 parents dea7912 + da72604 commit 0a9c2a4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
32 changes: 22 additions & 10 deletions powershell/internal/Update-MtMaesterTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,30 @@ Function Update-MtMaesterTests {
)

$MaesterTestsPath = Get-MtMaesterTestFolderPath

if (-not (Test-Path -Path $MaesterTestsPath)) {
if (-not (Test-Path -Path $MaesterTestsPath -PathType Container)) {
Write-Error "Maester tests not found at $MaesterTestsPath"
return
}

$targetFolderExists = (Test-Path -Path $Path)
$MaesterTests = (Get-ChildItem -Path $MaesterTestsPath -Exclude 'Custom').Name

$targetFolderExists = (Test-Path -Path $Path -PathType Container)
if (-not $targetFolderExists) {
Write-Verbose "Creating directory $([System.IO.Path]::GetFullPath($Path))"
try {
New-Item -Path $Path -ItemType Directory | Out-Null
} catch {
Write-Error "Unable to create directory $([System.IO.Path]::GetFullPath($Path))"
Write-Verbose $_
return
}
}

$installOrUpdate = if ($Install) { "installed" } else { "updated" }

if ($targetFolderExists) {
# Check if the folder already exists and prompt user to confirm overwrite.
$itemsToDelete = Get-ChildItem -Path $Path -Exclude "Custom"
$itemsToDelete = Get-ChildItem -Path $Path | Where-Object {$_.Name -in $($MaesterTests)}

if ($itemsToDelete.Count -gt 0) {
$message = "`nThe following items will be deleted when installing the latest Maester tests:`n"
Expand All @@ -50,14 +62,14 @@ Function Update-MtMaesterTests {
}
}

if (-not $targetFolderExists) {
Write-Verbose "Creating directory $Path"
New-Item -Path $Path -ItemType Directory | Out-Null
try {
Copy-Item -Path $MaesterTestsPath\* -Destination $Path -Recurse -Force
} catch {
Write-Error "Unable to copy the Maester tests to $Path."
Write-Verbose $_
return
}

$MaesterTestsPath = Get-MtMaesterTestFolderPath
Copy-Item -Path $MaesterTestsPath\* -Destination $Path -Recurse -Force

$message = "Run `Connect-Maester` to sign in and then run `Invoke-Maester` to start testing."
if (Get-MgContext) {
$message = "Run Invoke-Maester to start testing."
Expand Down
2 changes: 1 addition & 1 deletion powershell/public/core/Install-MaesterTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Function Install-MaesterTests {

Write-Verbose "Installing Maester tests to $Path"

$targetFolderExists = (Test-Path -Path $Path)
$targetFolderExists = (Test-Path -Path $Path -PathType Container)


# Check if current folder is empty and prompt user to continue if it is not
Expand Down
13 changes: 8 additions & 5 deletions powershell/public/core/Update-MaesterTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,32 @@
The tests can be viewed at https://github.com/maester365/maester/tree/main/tests
.PARAMETER Path
The path to install or update the Maester tests in.
.EXAMPLE
Update-MaesterTests -Path .\maester-tests
Installs or updates the latest Maester tests in the specified directory.
.EXAMPLE
Update-MaesterTests
Update-MaesterTests -Path .\
Install the latest set of Maester tests in the current directory.
#>

Function Update-MaesterTests {
function Update-MaesterTests {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification = 'Colors are beautiful')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '', Justification = 'This command updates multiple tests')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'TODO: Implement ShouldProcess')]
[CmdletBinding()]
param(
# The path to install the Maester tests to, defaults to the current directory.
# The path to install or update Maester tests in. Defaults to the current directory.
[Parameter(Mandatory = $false)]
[string] $Path = ".\"
[string] $Path = '.\'
)
Get-IsNewMaesterVersionAvailable | Out-Null

Update-MtMaesterTests -Path $Path
}
}

0 comments on commit 0a9c2a4

Please sign in to comment.