|
| 1 | +Import-Module .\WindowsDiskCleanup\WindowsDiskCleanup.psm1 -Force |
| 2 | + |
| 3 | +Describe 'Remove-NugetPackageVersion.Tests' { |
| 4 | + BeforeAll { |
| 5 | + $nugetRoot = Join-Path -Path $PSScriptRoot -ChildPath 'Nuget' |
| 6 | + $packageName = 'sitecore.cms.core.content' |
| 7 | + $otherPackageName = 'sitecore.cms.feature.navigation' |
| 8 | + $packagePath = Join-Path -Path $nugetRoot -ChildPath $packageName |
| 9 | + $otherPackagePath = Join-Path -Path $nugetRoot -ChildPath $otherPackageName |
| 10 | + |
| 11 | + if (-not (Test-Path $packagePath)) { |
| 12 | + New-Item -Path $packagePath -ItemType Directory -Force | Out-Null |
| 13 | + } |
| 14 | + |
| 15 | + if (-not (Test-Path $otherPackagePath)) { |
| 16 | + New-Item -Path $otherPackagePath -ItemType Directory -Force | Out-Null |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + BeforeEach { |
| 21 | + $packageVersions = @{ |
| 22 | + $packagePath = @('18.0.1580', '18.0.1581', '18.0.1582') |
| 23 | + $otherPackagePath = @('2.0.0', '2.0.1', '2.0.2') |
| 24 | + } |
| 25 | + |
| 26 | + foreach ($currentPackagePath in $packageVersions.Keys) { |
| 27 | + foreach ($version in $packageVersions[$currentPackagePath]) { |
| 28 | + $folder = New-Item -Path (Join-Path -Path $currentPackagePath -ChildPath $version) -ItemType Directory -Force |
| 29 | + $folder.CreationTimeUtc = (Get-Date).AddDays(-10) |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + AfterEach { |
| 35 | + if (Test-Path $nugetRoot) { |
| 36 | + Get-ChildItem -Path $nugetRoot -Directory | ForEach-Object { |
| 37 | + Get-ChildItem -Path $_.FullName -Directory | Remove-Item -Force -Recurse |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + AfterAll { |
| 43 | + if (Test-Path $nugetRoot) { |
| 44 | + Remove-Item -Path $nugetRoot -Recurse -Force |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + It 'should remove old package versions but keep latest version' { |
| 49 | + Remove-NugetPackageVersion -NugetFolder $nugetRoot -PackageName $packageName -Days 5 -DryRun:$false |
| 50 | + |
| 51 | + Test-Path (Join-Path -Path $packagePath -ChildPath '18.0.1580') | Should -Be $false |
| 52 | + Test-Path (Join-Path -Path $packagePath -ChildPath '18.0.1581') | Should -Be $false |
| 53 | + Test-Path (Join-Path -Path $packagePath -ChildPath '18.0.1582') | Should -Be $true |
| 54 | + } |
| 55 | + |
| 56 | + It 'should not remove package versions in dry run mode' { |
| 57 | + Remove-NugetPackageVersion -NugetFolder $nugetRoot -PackageName $packageName -Days 5 -DryRun:$true |
| 58 | + |
| 59 | + Test-Path (Join-Path -Path $packagePath -ChildPath '18.0.1580') | Should -Be $true |
| 60 | + Test-Path (Join-Path -Path $packagePath -ChildPath '18.0.1581') | Should -Be $true |
| 61 | + Test-Path (Join-Path -Path $packagePath -ChildPath '18.0.1582') | Should -Be $true |
| 62 | + } |
| 63 | + |
| 64 | + It 'should do nothing if package path does not exist' { |
| 65 | + Remove-Item -Path $packagePath -Recurse -Force |
| 66 | + |
| 67 | + { Remove-NugetPackageVersion -NugetFolder $nugetRoot -PackageName $packageName -Days 5 -DryRun:$false } | Should -Not -Throw |
| 68 | + } |
| 69 | + |
| 70 | + It 'should process all matching packages when wildcard package name is provided' { |
| 71 | + Remove-NugetPackageVersion -NugetFolder $nugetRoot -PackageName 'sitecore.cms.*' -Days 5 -DryRun:$false |
| 72 | + |
| 73 | + Test-Path (Join-Path -Path $packagePath -ChildPath '18.0.1582') | Should -Be $true |
| 74 | + Test-Path (Join-Path -Path $packagePath -ChildPath '18.0.1581') | Should -Be $false |
| 75 | + Test-Path (Join-Path -Path $otherPackagePath -ChildPath '2.0.2') | Should -Be $true |
| 76 | + Test-Path (Join-Path -Path $otherPackagePath -ChildPath '2.0.1') | Should -Be $false |
| 77 | + } |
| 78 | + |
| 79 | + It 'should process all packages when package name is omitted' { |
| 80 | + Remove-NugetPackageVersion -NugetFolder $nugetRoot -Days 5 -DryRun:$false |
| 81 | + |
| 82 | + Test-Path (Join-Path -Path $packagePath -ChildPath '18.0.1582') | Should -Be $true |
| 83 | + Test-Path (Join-Path -Path $packagePath -ChildPath '18.0.1580') | Should -Be $false |
| 84 | + Test-Path (Join-Path -Path $otherPackagePath -ChildPath '2.0.2') | Should -Be $true |
| 85 | + Test-Path (Join-Path -Path $otherPackagePath -ChildPath '2.0.0') | Should -Be $false |
| 86 | + } |
| 87 | + |
| 88 | + It 'should keep the only available version even if it is older than days threshold' { |
| 89 | + Get-ChildItem -Path $packagePath -Directory | Where-Object { $_.Name -ne '18.0.1582' } | Remove-Item -Recurse -Force |
| 90 | + |
| 91 | + Remove-NugetPackageVersion -NugetFolder $nugetRoot -PackageName $packageName -Days 5 -DryRun:$false |
| 92 | + |
| 93 | + Test-Path (Join-Path -Path $packagePath -ChildPath '18.0.1582') | Should -Be $true |
| 94 | + } |
| 95 | +} |
0 commit comments