Skip to content

Commit

Permalink
adding method GetFormattedLength()
Browse files Browse the repository at this point in the history
  • Loading branch information
santisq committed Feb 26, 2024
1 parent 040d1e9 commit 7538cbf
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 19 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/ci.yml
Expand Up @@ -25,7 +25,7 @@ jobs:
runs-on: windows-latest
steps:
- name: Check out repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Build module - Debug
shell: pwsh
Expand All @@ -38,7 +38,7 @@ jobs:
if: ${{ env.BUILD_CONFIGURATION == 'Release' }}

- name: Capture PowerShell Module
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: PSModule
path: output/*.nupkg
Expand All @@ -63,10 +63,10 @@ jobs:
os: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Restore Built PowerShell Module
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: PSModule
path: output
Expand Down Expand Up @@ -103,21 +103,21 @@ jobs:
- name: Upload Test Results
if: always()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: Unit Test Results (${{ matrix.info.name }})
path: ./output/TestResults/Pester.xml

- name: Upload Coverage Results
if: always() && !startsWith(github.ref, 'refs/tags/v')
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: Coverage Results (${{ matrix.info.name }})
path: ./output/TestResults/Coverage.xml

- name: Upload Coverage to codecov
if: always() && !startsWith(github.ref, 'refs/tags/v')
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
files: ./output/TestResults/Coverage.xml
flags: ${{ matrix.info.name }}
Expand All @@ -131,7 +131,7 @@ jobs:
runs-on: windows-latest
steps:
- name: Restore Built PowerShell Module
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: PSModule
path: ./
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,13 @@
# CHANGELOG

- __02/26/2024__
- Added method `.GetFormattedLength()`. Outputs the friendly `.Length` representation of `PSTreeFile` and `PSTreeDirectory` instances.

```powershell
PS ..\PSTree> (Get-PSTree D:\ -RecursiveSize -Depth 0).GetFormattedLength()
629.59 GB
```

- __10/05/2023__
- Added Parameter `-Include`. Works very similar to `-Exclude`, the patterns are evaluated against the items `.FullName` property, however this parameter targets only files (`FileInfo` instances).

Expand Down
2 changes: 1 addition & 1 deletion build.ps1
Expand Up @@ -58,7 +58,7 @@ end {
$dotnetTools = @(dotnet tool list --global) -join "`n"
if (-not $dotnetTools.Contains('coverlet.console')) {
Write-Host 'Installing dotnet tool coverlet.console'
dotnet tool install --global coverlet.console
dotnet tool install --global coverlet.console --version 6.0.0
}

$invokeBuildSplat = @{
Expand Down
2 changes: 1 addition & 1 deletion module/PSTree.Format.ps1xml
Expand Up @@ -34,7 +34,7 @@
<PropertyName>Mode</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>[PSTree.Internal._FormattingInternals]::GetFormattedLength($_.Length)</ScriptBlock>
<ScriptBlock>$_.GetFormattedLength()</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Hierarchy</PropertyName>
Expand Down
2 changes: 1 addition & 1 deletion module/PSTree.psd1
Expand Up @@ -11,7 +11,7 @@
RootModule = 'bin/netstandard2.0/PSTree.dll'

# Version number of this module.
ModuleVersion = '2.1.15'
ModuleVersion = '2.1.16'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
3 changes: 3 additions & 0 deletions src/PSTree/PSTreeFileSystemInfo.cs
Expand Up @@ -15,4 +15,7 @@ protected PSTreeFileSystemInfo(string hierarchy, string source)
Hierarchy = hierarchy;
Source = source;
}

public string GetFormattedLength() =>
Internal._FormattingInternals.GetFormattedLength(Length);
}
6 changes: 4 additions & 2 deletions src/PSTree/PathExtensions.cs
Expand Up @@ -11,7 +11,7 @@ namespace PSTree;
internal static class PathExtensions
{
[ThreadStatic]
private static readonly List<string> s_normalizedPaths = new();
private static List<string>? s_normalizedPaths;

private static readonly char[] _dirSeparator = @"\/".ToCharArray();

Expand All @@ -22,9 +22,11 @@ internal static class PathExtensions
bool throwOnInvalidPath = false,
bool throwOnInvalidProvider = false)
{
s_normalizedPaths ??= new();
s_normalizedPaths.Clear();

Collection<string> resolvedPaths;
ProviderInfo provider;
s_normalizedPaths.Clear();

foreach (string path in paths)
{
Expand Down
11 changes: 5 additions & 6 deletions tests/PSTreeDirectory.ps1 → tests/PSTreeDirectory.tests.ps1
Expand Up @@ -9,14 +9,12 @@ Import-Module ([System.IO.Path]::Combine($PSScriptRoot, 'shared.psm1'))
Describe 'PSTreeDirectory' {
It 'Can enumerate Files with .EnumerateFiles()' {
($testPath | Get-PSTree -Depth 0).EnumerateFiles() |
ForEach-Object GetType |
Should -Be ([System.IO.FileInfo])
Should -BeOfType ([System.IO.FileInfo])
}

It 'Can enumerate Directories with .EnumerateDirectories()' {
($testPath | Get-PSTree -Depth 0).EnumerateDirectories() |
ForEach-Object GetType |
Should -Be ([System.IO.DirectoryInfo])
Should -BeOfType ([System.IO.DirectoryInfo])
}

It 'Can enumerate File System Infos with .EnumerateFileSystemInfos()' {
Expand All @@ -26,9 +24,10 @@ Describe 'PSTreeDirectory' {
}

It 'Can enumerate its parent directories with .GetParents()' {
$parent = ($testPath | Get-PSTree -Depth 0).Parent
$treedir = $testPath | Get-PSTree -Depth 0
$parent = $treedir.Parent
$parent | Should -BeOfType ([System.IO.DirectoryInfo])
$paths = $parent.FullName.Split([System.IO.Path]::DirectorySeparatorChar)
$parent.GetParents() | Should -HaveCount $paths.Count
$treedir.GetParents() | Should -HaveCount $paths.Count
}
}
5 changes: 5 additions & 0 deletions tests/PSTreeFileSystemInfo_T.tests.ps1
Expand Up @@ -65,4 +65,9 @@ Describe 'PSTreeFileSystemInfo<T>' {
$instance.LastAccessTime | Should -BeOfType ([datetime])
$instance.LastWriteTimeUtc | Should -BeOfType ([datetime])
}

It 'GetFormattedLength() outputs friendly length' {
$pattern = '(?:{0}$)' -f ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' -join '|')
(Get-PSTree $testPath).GetFormattedLength() | Should -Match $pattern
}
}

0 comments on commit 7538cbf

Please sign in to comment.