From cebbf8b7d47522b28c849bf0262d9bf37a90110a Mon Sep 17 00:00:00 2001 From: AdmiringWorm Date: Tue, 13 Oct 2020 12:15:41 +0000 Subject: [PATCH] (GH-30) Move code for extracting meta data to its own function --- Tests/Private/Get-PackageBinaryData.Tests.ps1 | 4 ++ .../Private/Get-PackageBinaryData.ps1 | 44 +++++++++++++++++++ .../Public/Get-ChocolateyPackageDiff.ps1 | 18 ++------ 3 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 Tests/Private/Get-PackageBinaryData.Tests.ps1 create mode 100644 chocolatey-diff/Private/Get-PackageBinaryData.ps1 diff --git a/Tests/Private/Get-PackageBinaryData.Tests.ps1 b/Tests/Private/Get-PackageBinaryData.Tests.ps1 new file mode 100644 index 0000000..f49d53a --- /dev/null +++ b/Tests/Private/Get-PackageBinaryData.Tests.ps1 @@ -0,0 +1,4 @@ +#Requires -Modules chocolatey-diff + +Describe "Get-PackageBinaryData tests" { +} diff --git a/chocolatey-diff/Private/Get-PackageBinaryData.ps1 b/chocolatey-diff/Private/Get-PackageBinaryData.ps1 new file mode 100644 index 0000000..9566f64 --- /dev/null +++ b/chocolatey-diff/Private/Get-PackageBinaryData.ps1 @@ -0,0 +1,44 @@ +function Get-PackageBinaryData { + <# + .SYNOPSIS + The meta information about binaries included in package + .DESCRIPTION + The meta information about binaries included in the package, including + the relative path, the checksum of the file, the embedded file and product + version as well as the embedded company name. + .OUTPUTS + An array of hashtables with the basic information about each binary file. + .EXAMPLE + PS > Get-PackageBinaryData -packageDirectory path\to\package\dir + #> + param ( + [Parameter(Mandatory = $true)] + [string]$packageDirectory, + [ValidateSet('sha1', 'sha256', 'sha512')] + [string]$hashAlgorithm = 'sha256' + ) + + Push-Location $packageDirectory | Out-Null + + try { + Write-Verbose "Finding binary files in '$packageDirectory..." + $binaryFiles = Get-ChildItem -Path $packageDirectory -Recurse -File | ? { Test-IsBinary -Path $_.FullName } + + foreach ($file in $binaryFiles) { + Write-Verbose "Aquiring binary data from $($file.Name)..." + + $item = Get-Item $file.FullName + $result = @{ + path = Resolve-Path $file.FullName -Relative + checksum = Get-FileHash $file.FullName -Algorithm $hashAlgorithm | % Hash + fileVersion = $item.VersionInfo.FileVersion + productVersion = $item.VersionInfo.ProductVersion + CompanyName = $item.VersionInfo.CompanyName + } + $result + } + } finally { + + Pop-Location | Out-Null + } +} diff --git a/chocolatey-diff/Public/Get-ChocolateyPackageDiff.ps1 b/chocolatey-diff/Public/Get-ChocolateyPackageDiff.ps1 index 5e39a26..38f8e58 100644 --- a/chocolatey-diff/Public/Get-ChocolateyPackageDiff.ps1 +++ b/chocolatey-diff/Public/Get-ChocolateyPackageDiff.ps1 @@ -108,28 +108,16 @@ Expand-Archive -Path $newFileName -DestinationPath $newExtractPath -Force foreach ($path in @($oldExtractPath; $newExtractPath)) { - # Only because of unit tests + # Temporary workaround until mocking and unit tests + # are figured out. if (!(Test-Path $path)) { continue; } $metaDestination = Join-Path $path "binary-data.txt" - Push-Location -Path $path if (Test-Path $metaDestination) { Remove-Item $metaDestination } - Get-ChildItem -Path $path -Recurse -File | ? { Test-IsBinary -Path $_.FullName } | % { - $item = Get-Item $_.FullName - $result = @{ - path = Resolve-Path $_.FullName -Relative - checksum = Get-FileHash $_.FullName -Algorithm SHA256 | % Hash - fileVersion = $item.VersionInfo.FileVersion - productVersion = $item.VersionInfo.ProductVersion - } - - $result - } | Out-File -Encoding utf8 -FilePath $metaDestination - - Pop-Location + Get-PackageBinaryData -packageDirectory $path | Out-File -Encoding utf8 -FilePath $metaDestination } if ($compareFolder) {