From 594c0c88990d179923aea7f591cb1dcd6c606a5f Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Thu, 2 Jul 2020 02:04:37 +0300 Subject: [PATCH] Add test cases and fix Filter-Versions function --- .../get-new-tool-versions.Tests.ps1 | 27 +++++++++++---- .../get-new-tool-versions.ps1 | 18 +++------- get-new-tool-versions/helpers.psm1 | 34 ++++++++----------- 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/get-new-tool-versions/get-new-tool-versions.Tests.ps1 b/get-new-tool-versions/get-new-tool-versions.Tests.ps1 index b9802ce..5638fa4 100644 --- a/get-new-tool-versions/get-new-tool-versions.Tests.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.Tests.ps1 @@ -27,7 +27,7 @@ Describe "Format-Versions" { $actualOutput | Should -Be $expectedOutput } - It "Skip versions with 1 digin" { + It "Skip versions with 1 digit" { $actualOutput = Format-Versions -Versions @("14", "v2") $expectedOutput = @() $actualOutput | Should -Be $expectedOutput @@ -37,27 +37,42 @@ Describe "Format-Versions" { Describe "Filter-Versions" { $inputVersions = @("8.2.1", "9.3.3", "10.0.2", "10.0.3", "10.5.6", "12.4.3", "12.5.1", "14.2.0") - It "Include filter" { + It "Include filter only" { $includeFilters = @("8.*", "14.*") - $actualOutput = Filter-Versions -Versions $inputVersions -IncludeFilters $includeFilters + $excludeFilters = @() + $actualOutput = Filter-Versions -Versions $inputVersions -IncludeFilters $includeFilters -ExcludeFilters $excludeFilters $expectedOutput = @("8.2.1", "14.2.0") $actualOutput | Should -Be $expectedOutput } - It "Exclude filter" { + It "Include and exclude filters" { $includeFilters = @("10.*", "12.*") $excludeFilters = @("10.0.*", "12.4.3") $actualOutput = Filter-Versions -Versions $inputVersions -IncludeFilters $includeFilters -ExcludeFilters $excludeFilters $expectedOutput = @("10.5.6", "12.5.1") $actualOutput | Should -Be $expectedOutput } + + It "Exclude filter only" { + $includeFilters = @() + $excludeFilters = @("10.*", "12.*") + $actualOutput = Filter-Versions -Versions $inputVersions -IncludeFilters $includeFilters -ExcludeFilters $excludeFilters + $expectedOutput = @("8.2.1", "9.3.3", "14.2.0") + $actualOutput | Should -Be $expectedOutput + } + + It "Include and exclude filters are empty" { + $actualOutput = Filter-Versions -Versions $inputVersions + $expectedOutput = @("8.2.1", "9.3.3", "10.0.2", "10.0.3", "10.5.6", "12.4.3", "12.5.1", "14.2.0") + $actualOutput | Should -Be $expectedOutput + } } -Describe "Get-VersionsToBuild" { +Describe "Skip-ExistingVersions" { It "Substract versions correctly" { $distInput = @("14.2.0", "14.3.0", "14.4.0", "14.4.1") $manifestInput = @("12.0.0", "14.2.0", "14.4.0") - $actualOutput = Get-VersionsToBuild -VersionsFromDist $distInput -VersionsFromManifest $manifestInput + $actualOutput = Skip-ExistingVersions -VersionsFromDist $distInput -VersionsFromManifest $manifestInput $expectedOutput = @("14.3.0", "14.4.1") $actualOutput | Should -Be $expectedOutput } diff --git a/get-new-tool-versions/get-new-tool-versions.ps1 b/get-new-tool-versions/get-new-tool-versions.ps1 index 6df0727..e20094f 100644 --- a/get-new-tool-versions/get-new-tool-versions.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.ps1 @@ -6,8 +6,8 @@ Check and return list of new available tool versions param ( [Parameter(Mandatory)] [string] $DistURL, [Parameter(Mandatory)] [string] $ManifestLink, - [string] $VersionFilterToInclude, - [string] $VersionFilterToExclude, + [string[]] $VersionFilterToInclude, + [string[]] $VersionFilterToExclude, [UInt32] $RetryIntervalSec = 60, [UInt32] $RetryCount = 3 ) @@ -37,17 +37,9 @@ Write-Host "Get the packages list from $ManifestLink" [Version[]] $formattedVersions = Format-Versions -Versions $versionsFromDist -if ($VersionFilterToInclude) { - $formattedVersions = Filter-Versions -Versions $formattedVersions ` - -VersionFilter $VersionFilterToInclude ` - -IncludeVersions $true -} - -if ($VersionFilterToExclude) { - $formattedVersions = Filter-Versions -Versions $formattedVersions ` - -VersionFilter $VersionFilterToExclude ` - -IncludeVersions $false -} +$formattedVersions = Filter-Versions -Versions $formattedVersions ` + -IncludeFilters $VersionFilterToInclude ` + -ExcludeFilters $VersionFilterToExclude $versionsToBuild = Skip-ExistingVersions -VersionsFromManifest $versionsFromManifest ` -VersionsFromDist $formattedVersions diff --git a/get-new-tool-versions/helpers.psm1 b/get-new-tool-versions/helpers.psm1 index 8463fd4..2cdc330 100644 --- a/get-new-tool-versions/helpers.psm1 +++ b/get-new-tool-versions/helpers.psm1 @@ -5,9 +5,7 @@ function Format-Versions { [Version[]] $formattedVersions = @() - foreach($version in $Versions) { - $substredVersion = $null - + foreach($version in $Versions) { # Cut a string from index of first digit because initially it has invalid format (v14.4.0 or go1.14.4) if (-not ($version -match '(?\d)')) { Write-Host "Invalid version format - $version" @@ -34,23 +32,22 @@ function Format-Versions { function Filter-Versions { param ( - [Parameter(Mandatory)] [string[]] $Versions, - [Parameter(Mandatory)] [string] $VersionFilter, - [Parameter(Mandatory)] [bool] $IncludeVersions + [Parameter(Mandatory)] [version[]] $Versions, + [string[]] $IncludeFilters, + [string[]] $ExcludeFilters ) - $versionFilters = $VersionFilter.Split(',') - [Version[]] $filteredVersions = @() - - foreach($filter in $versionFilters) { - if ($IncludeVersions) { - $filteredVersions += $Versions | Where-Object { $_ -like $filter } - } else { - $filteredVersions += $Versions | Where-Object { $_ -notlike $filter } - } + if ($IncludeFilters.Length -eq 0) { + $IncludeFilters = @('*') } - return $filteredVersions + return $Versions | Where-Object { + $ver = $_ + $matchedIncludeFilters = $IncludeFilters | Where-Object { $ver -like $_ } + $matchedExcludeFilters = $ExcludeFilters | Where-Object { $ver -like $_ } + + return ($matchedIncludeFilters -ne $null) -and ($matchedExcludeFilters -eq $null) + } } function Skip-ExistingVersions { @@ -59,8 +56,5 @@ function Skip-ExistingVersions { [Parameter(Mandatory)] [string[]] $VersionsFromDist ) - $newVersions = @() - $newVersions += $VersionsFromDist | Where-Object { $VersionsFromManifest -notcontains $_ } - - return $newVersions + return $VersionsFromDist | Where-Object { $VersionsFromManifest -notcontains $_ } } \ No newline at end of file