-
Notifications
You must be signed in to change notification settings - Fork 10
Get-FileVersion: New command
#188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3d538ac
`Get-FileVersion`: New command
johlju b8333de
Merge branch 'main' into fix/move-private-function
johlju ebcd2ea
Fix error handling in Get-FileProductVersion function to ensure consi…
johlju e19355f
Refactor Get-FileVersion function to streamline error handling by rem…
johlju 049e97c
Fix context skip condition in Get-FileVersion integration tests for W…
johlju 83b8862
Update error handling in Get-FileVersion integration test for Unix ex…
johlju aafef32
Update error message for missing DscResource.Test module dependency i…
johlju 4604d9b
Refactor Get-FileVersion tests to remove unnecessary InModuleScope us…
johlju 511f60b
Refactor Get-FileVersion tests to use a localized expected message fo…
johlju 9047c3a
Enhance Get-FileProductVersion function to validate product version f…
johlju 0c6410c
Improve error handling in Get-FileProductVersion by adding ErrorId, C…
johlju 05148c4
Remove unnecessary return statement in Get-FileVersion function for c…
johlju 2c72eca
Fix context skip conditions in Get-FileVersion integration tests for …
johlju beab9a0
Fix dependency error message and update Get-FileVersion tests to use …
johlju 57cb600
Update documentation and tests to use Get-FileVersion for product ver…
johlju b2d6548
Enhance error handling in Get-FileProductVersion and add parameter va…
johlju 035c933
Improve error handling in Get-FileProductVersion by throwing a termin…
johlju 31e2bb7
Update documentation for Get-FileProductVersion to clarify input para…
johlju e6f3289
Refactor Get-FileProductVersion to move product version parsing after…
johlju 7e31010
Refactor error handling to use New-ErrorRecord for consistency across…
johlju 83cfb66
Refactor parameter set validation in Get-FileProductVersion and Get-F…
johlju 8b4b1f0
Fix error message in BeforeDiscovery to correct build task instruction
johlju bd3e7df
Refactor mock FileInfo creation to use static method for improved cla…
johlju 1d112ba
Update error ID in Assert-RequiredCommandParameter to ARCP0002 for im…
johlju 7c1ad51
Refactor parameter set test cases in Get-FileProductVersion and Get-F…
johlju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Returns the version information for a file. | ||
|
|
||
| .DESCRIPTION | ||
| Returns the version information for a file including the product version, | ||
| file version, and other version-related metadata. | ||
|
|
||
| .PARAMETER Path | ||
| Specifies the file for which to return the version information. | ||
|
|
||
| .EXAMPLE | ||
| Get-FileVersion -Path 'E:\setup.exe' | ||
|
|
||
| Returns the version information for the file setup.exe. | ||
|
|
||
| .EXAMPLE | ||
| Get-Item -Path 'E:\setup.exe' | Get-FileVersion | ||
|
|
||
| Returns the version information for the file setup.exe using pipeline input. | ||
|
|
||
| .EXAMPLE | ||
| 'E:\setup.exe' | Get-FileVersion | ||
|
|
||
| Returns the version information for the file setup.exe using pipeline input. | ||
|
|
||
| .INPUTS | ||
| System.IO.FileInfo | ||
|
|
||
| Accepts a file path via the pipeline. | ||
|
|
||
| .INPUTS | ||
| System.String | ||
|
|
||
| Accepts a string path via the pipeline. | ||
|
|
||
| .OUTPUTS | ||
| System.Diagnostics.FileVersionInfo | ||
|
|
||
| Returns the file version information. | ||
| #> | ||
| function Get-FileVersion | ||
| { | ||
| [OutputType([System.Diagnostics.FileVersionInfo])] | ||
| [CmdletBinding()] | ||
| param | ||
| ( | ||
| [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] | ||
| [Alias('FullName')] | ||
| [System.IO.FileInfo] | ||
| $Path | ||
| ) | ||
|
|
||
| process | ||
| { | ||
| $file = Get-Item -Path $Path -ErrorAction 'Stop' | ||
|
|
||
| if ($file.PSIsContainer) | ||
| { | ||
| $PSCmdlet.ThrowTerminatingError( | ||
| (New-ErrorRecord -Exception ($script:localizedData.Get_FileVersion_PathIsNotFile -f $file.FullName) -ErrorId 'GFV0001' -ErrorCategory ([System.Management.Automation.ErrorCategory]::InvalidArgument) -TargetObject $file.FullName) # cSpell: disable-line | ||
| ) | ||
| } | ||
|
|
||
| $file.VersionInfo | ||
| } | ||
|
johlju marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
tests/Integration/Commands/Get-FileVersion.Integration.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')] | ||
| param () | ||
|
|
||
| BeforeDiscovery { | ||
| try | ||
| { | ||
| if (-not (Get-Module -Name 'DscResource.Test')) | ||
| { | ||
| # Assumes dependencies have been resolved, so if this module is not available, run 'noop' task. | ||
| if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable)) | ||
| { | ||
| # Redirect all streams to $null, except the error stream (stream 2) | ||
| & "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null | ||
| } | ||
|
|
||
| # If the dependencies have not been resolved, this will throw an error. | ||
| Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop' | ||
| } | ||
| } | ||
| catch [System.IO.FileNotFoundException] | ||
| { | ||
| throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks noop" first to set up the test environment.' | ||
| } | ||
|
johlju marked this conversation as resolved.
|
||
| } | ||
|
|
||
| BeforeAll { | ||
| $script:moduleName = 'DscResource.Common' | ||
|
|
||
| Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' | ||
| } | ||
|
|
||
| Describe 'Get-FileVersion' -Tag 'Integration' { | ||
| Context 'When running on Windows' -Skip:(-not $IsWindows) { | ||
| Context 'When getting version information from notepad.exe' { | ||
| BeforeAll { | ||
| $script:notepadPath = Join-Path -Path $env:SystemRoot -ChildPath 'System32\notepad.exe' | ||
| } | ||
|
|
||
| It 'Should return version information for notepad.exe' { | ||
| $result = Get-FileVersion -Path $script:notepadPath -ErrorAction Stop | ||
|
|
||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -BeOfType [System.Diagnostics.FileVersionInfo] | ||
| $result.ProductVersion | Should -Not -BeNullOrEmpty | ||
| $result.FileVersion | Should -Not -BeNullOrEmpty | ||
| } | ||
|
|
||
| It 'Should return version information using pipeline input' { | ||
| $result = $script:notepadPath | Get-FileVersion -ErrorAction Stop | ||
|
|
||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -BeOfType [System.Diagnostics.FileVersionInfo] | ||
| $result.ProductVersion | Should -Not -BeNullOrEmpty | ||
| } | ||
|
|
||
| It 'Should return version information using Get-Item pipeline input' { | ||
| $result = Get-Item -Path $script:notepadPath | Get-FileVersion -ErrorAction Stop | ||
|
|
||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -BeOfType [System.Diagnostics.FileVersionInfo] | ||
| $result.ProductVersion | Should -Not -BeNullOrEmpty | ||
| } | ||
| } | ||
|
|
||
| Context 'When getting version information from powershell.exe' { | ||
| BeforeAll { | ||
| $script:powershellPath = Join-Path -Path $env:SystemRoot -ChildPath 'System32\WindowsPowerShell\v1.0\powershell.exe' | ||
| } | ||
|
|
||
| It 'Should return version information for powershell.exe' { | ||
| $result = Get-FileVersion -Path $script:powershellPath -ErrorAction Stop | ||
|
|
||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -BeOfType [System.Diagnostics.FileVersionInfo] | ||
| $result.ProductVersion | Should -Not -BeNullOrEmpty | ||
| $result.FileVersion | Should -Not -BeNullOrEmpty | ||
| $result.ProductName | Should -Not -BeNullOrEmpty | ||
| } | ||
| } | ||
|
|
||
| Context 'When passing an invalid path' { | ||
| It 'Should throw an error for non-existent file' { | ||
| { Get-FileVersion -Path 'C:\NonExistent\File.exe' -ErrorAction Stop } | | ||
| Should -Throw | ||
| } | ||
| } | ||
|
|
||
| Context 'When passing a directory path' { | ||
| It 'Should throw the correct error' { | ||
| { Get-FileVersion -Path $env:SystemRoot -ErrorAction Stop } | | ||
| Should -Throw | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Context 'When running on non-Windows platforms' -Skip:($IsWindows -or $PSVersionTable.PSVersion.Major -lt 6) { | ||
| Context 'When getting version information from a shell executable' -Skip:(-not (Test-Path -Path '/bin/bash')) { | ||
| BeforeAll { | ||
| $script:bashPath = '/bin/bash' | ||
| } | ||
|
|
||
| It 'Should handle Unix executables appropriately' { | ||
| # Unix executables typically don't have version info like Windows executables | ||
| # This test verifies the command can be called without error | ||
| $result = Get-FileVersion -Path $script:bashPath -ErrorAction 'Stop' | ||
|
|
||
| # Result may be null or empty on Unix systems | ||
| $result | Should -BeOfType [System.Diagnostics.FileVersionInfo] | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.