-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Added Get-FileEncoding cmdlet
#20586
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
Closed
Armaan Mcleod (ArmaanMcleod)
wants to merge
8
commits into
PowerShell:master
from
ArmaanMcleod:get-file-encoding-cmdlet
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9763af4
Move GetPathEncoding method to PathUtils
ArmaanMcleod fda3b6a
Add initial Get-FileEncoding cmdlet with tests
ArmaanMcleod 9f84598
Fixing tests
ArmaanMcleod c48c1eb
Fix codefactor issues
ArmaanMcleod 1d5c776
lowercase sets in summary message
ArmaanMcleod 3e549bc
Add cmdlet to default command tests
ArmaanMcleod 8a6099d
Add output type attribute
ArmaanMcleod 643b8d6
Merge branch 'master' into get-file-encoding-cmdlet
ArmaanMcleod 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
71 changes: 71 additions & 0 deletions
71
src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetFileEncodingCommand.cs
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,71 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.IO; | ||
| using System.Management.Automation; | ||
| using System.Text; | ||
|
|
||
| namespace Microsoft.PowerShell.Commands | ||
| { | ||
| /// <summary> | ||
| /// This class implements the Get-FileEncoding command. | ||
| /// </summary> | ||
| [Cmdlet(VerbsCommon.Get, "FileEncoding", DefaultParameterSetName = PathParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=1234567")] | ||
| [OutputType(typeof(Encoding))] | ||
| public sealed class GetFileEncodingCommand : PSCmdlet | ||
| { | ||
| #region Parameter Sets | ||
|
|
||
| private const string PathParameterSet = "ByPath"; | ||
| private const string LiteralPathParameterSet = "ByLiteralPath"; | ||
|
|
||
| #endregion | ||
|
|
||
| #region Parameters | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets path from from which to get encoding. | ||
| /// </summary> | ||
| [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = PathParameterSet)] | ||
| public string Path { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets literal path from which to get encoding. | ||
| /// </summary> | ||
| [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = LiteralPathParameterSet)] | ||
| [Alias("PSPath", "LP")] | ||
| public string LiteralPath | ||
| { | ||
| get | ||
| { | ||
| return _isLiteralPath ? Path : null; | ||
| } | ||
|
|
||
| set | ||
| { | ||
| Path = value; | ||
| _isLiteralPath = true; | ||
| } | ||
| } | ||
|
|
||
| private bool _isLiteralPath; | ||
|
|
||
| #endregion | ||
|
|
||
| /// <summary> | ||
| /// Process paths to get file encoding. | ||
| /// </summary> | ||
| protected override void ProcessRecord() | ||
| { | ||
| string resolvedPath = PathUtils.ResolveFilePath(Path, this, _isLiteralPath); | ||
|
|
||
| if (!File.Exists(resolvedPath)) | ||
| { | ||
| ItemNotFoundException exception = new(Path, "PathNotFound", SessionStateStrings.PathNotFound); | ||
| ThrowTerminatingError(exception.ErrorRecord); | ||
| } | ||
|
|
||
| WriteObject(PathUtils.GetPathEncoding(resolvedPath)); | ||
| } | ||
| } | ||
| } | ||
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
45 changes: 45 additions & 0 deletions
45
test/powershell/Modules/Microsoft.PowerShell.Utility/Get-FileEncoding.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,45 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| Describe "Get-FileEncoding" -Tags "CI" { | ||
| BeforeAll { | ||
| $testFilePath = Join-Path -Path $TestDrive -ChildPath test.txt | ||
| $testFileLiteralPath = Join-Path -Path $TestDrive -ChildPath "[test].txt" | ||
| $content = 'abc' | ||
|
|
||
| $testCases = @( | ||
| @{ EncodingName = 'ansi'; ExpectedEncoding = 'utf-8' } | ||
| @{ EncodingName = 'ascii'; ExpectedEncoding = 'utf-8' } | ||
| @{ EncodingName = 'bigendianunicode'; ExpectedEncoding = 'utf-16BE' } | ||
| @{ EncodingName = 'bigendianutf32'; ExpectedEncoding = 'utf-32BE' } | ||
| @{ EncodingName = 'oem'; ExpectedEncoding = 'utf-8' } | ||
| @{ EncodingName = 'unicode'; ExpectedEncoding = 'utf-16' } | ||
| @{ EncodingName = 'utf8'; ExpectedEncoding = 'utf-8' } | ||
| @{ EncodingName = 'utf8BOM'; ExpectedEncoding = 'utf-8' } | ||
| @{ EncodingName = 'utf8NoBOM'; ExpectedEncoding = 'utf-8' } | ||
| @{ EncodingName = 'utf32'; ExpectedEncoding = 'utf-32' } | ||
| ) | ||
| } | ||
|
|
||
| It "Validate Get-FileEncoding using -Path returns file encoding for '<EncodingName>'" -TestCases $testCases { | ||
| param($EncodingName, $ExpectedEncoding) | ||
| Set-Content -Path $testFilePath -Encoding $EncodingName -Value $content -Force | ||
| (Get-FileEncoding -Path $testFilePath).BodyName | Should -Be $ExpectedEncoding | ||
| (Get-ChildItem -Path $testFilePath | Get-FileEncoding).BodyName | Should -Be $ExpectedEncoding | ||
| } | ||
|
|
||
| It "Validate Get-FileEncoding using -LiteralPath returns file encoding for '<EncodingName>'" -TestCases $testCases { | ||
| param($EncodingName, $ExpectedEncoding) | ||
| Set-Content -LiteralPath $testFileLiteralPath -Encoding $EncodingName -Value $content -Force | ||
| (Get-FileEncoding -LiteralPath $testFileLiteralPath).BodyName | Should -Be $ExpectedEncoding | ||
| (Get-ChildItem -LiteralPath $testFileLiteralPath | Get-FileEncoding).BodyName | Should -Be $ExpectedEncoding | ||
| } | ||
|
|
||
| It "Should throw exception if path is not found using -Path" { | ||
| { Get-FileEncoding -Path nonexistentpath } | Should -Throw -ErrorId 'PathNotFound,Microsoft.PowerShell.Commands.GetFileEncodingCommand' | ||
| } | ||
|
|
||
| It "Should throw exception if path is not found using -LiteralPath" { | ||
| { Get-FileEncoding -LiteralPath nonexistentpath } | Should -Throw -ErrorId 'PathNotFound,Microsoft.PowerShell.Commands.GetFileEncodingCommand' | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need new link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah put this one as a placeholder. I will also fill out documentation issue.