Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Clear-Content.Tests.ps1171 linesDownload Raw Back to Microsoft.PowerShell.Management
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4# get a random string of characters a-z and A-Z5function Get-RandomString6{7    param ( [int]$Length = 8 )8    $chars = .{ ([int][char]'a')..([int][char]'z');([int][char]'A')..([int][char]'Z') }9    ([char[]]($chars | Get-Random -Count $Length)) -join ""10}11 12# get a random string which is not the name of an existing provider13function Get-NonExistantProviderName14{15   param ( [int]$Length = 8 )16   do {17       $providerName = Get-RandomString -Length $Length18   } until ( $null -eq (Get-PSProvider -PSProvider $providername -ErrorAction SilentlyContinue) )19   $providerName20}21 22# get a random string which is not the name of an existing drive23function Get-NonExistantDriveName24{25    param ( [int]$Length = 8 )26    do {27        $driveName = Get-RandomString -Length $Length28    } until ( $null -eq (Get-PSDrive $driveName -ErrorAction SilentlyContinue) )29    $drivename30}31 32# get a random string which is not the name of an existing function33function Get-NonExistantFunctionName34{35    param ( [int]$Length = 8 )36    do {37        $functionName = Get-RandomString -Length $Length38    } until ( (Test-Path -Path function:$functionName) -eq $false )39    $functionName40}41 42Describe "Clear-Content cmdlet tests" -Tags "CI" {43  BeforeAll {44    $file1 = "file1.txt"45    $file2 = "file2.txt"46    $file3 = "file3.txt"47    $content1 = "This is content"48    $content2 = "This is content for alternate stream tests"49    Setup -File "$file1"50    Setup -File "$file2" -Content $content151    Setup -File "$file3" -Content $content252    $streamContent = "content for alternate stream"53    $streamName = "altStream1"54    $dirName = "clearcontent"55    Setup -Directory "$dirName"56  }57 58  Context "Clear-Content should actually clear content" {59    It "should clear-Content of TestDrive:\$file1" {60      Set-Content -Path TestDrive:\$file1 -Value "ExpectedContent" -PassThru | Should -BeExactly "ExpectedContent"61      Clear-Content -Path TestDrive:\$file162    }63 64    It "shouldn't get any content from TestDrive:\$file1" {65      $result = Get-Content -Path TestDrive:\$file166      $result | Should -BeNullOrEmpty67    }68 69    # we could suppress the WhatIf output here if we use the testhost, but it's not necessary70    It "The filesystem provider supports should process" -Skip:(!$IsWindows) {71      Clear-Content -Path TestDrive:\$file2 -WhatIf72      "TestDrive:\$file2" | Should -FileContentMatch "This is content"73    }74 75    It "The filesystem provider should support ShouldProcess (reference ProviderSupportsShouldProcess member)" {76      $cci = ((Get-Command -Name Clear-Content).ImplementingType)::new()77      $cci.SupportsShouldProcess | Should -BeTrue78    }79 80    Context "Clear-Content should work with alternate data streams on Windows" {81      It "Alternate streams should be cleared with Clear-Content on a file" -Skip:(!$IsWindows) {82 83        Set-Content           -Path "TestDrive:/$file3" -Stream $streamName -Value $streamContent84        Get-Content           -Path "TestDrive:/$file3" -Stream $streamName | Should -BeExactly $streamContent85 86        Clear-Content         -Path "TestDrive:/$file3" -Stream $streamName -ErrorAction Stop87 88        $result = Get-Item -Path "TestDrive:/$file3" -Stream $streamName89        $result | Should -BeOfType System.Management.Automation.Internal.AlternateStreamData90        $result.length | Should -Be 091      }92 93      It "Alternate streams should be cleared with Clear-Content on a directory" -Skip:(!$IsWindows) {94        Set-Content           -Path "TestDrive:/$dirName" -Stream $streamName -Value $streamContent95 96        Get-Content           -Path "TestDrive:/$dirName" -Stream $streamName | Should -BeExactly $streamContent97        Clear-Content         -Path "TestDrive:/$dirName" -Stream $streamName -ErrorAction Stop98 99        $result = Get-Item -Path "TestDrive:/$dirName" -Stream $streamName100        $result | Should -BeOfType System.Management.Automation.Internal.AlternateStreamData101        $result.length | Should -Be 0102      }103 104      It "the '-Stream' dynamic parameter is visible to get-command in the filesystem" -Skip:(!$IsWindows) {105        try {106          Push-Location -Path TestDrive:107          (Get-Command Clear-Content -Stream foo).parameters.keys -eq "Stream" | Should -BeExactly "Stream"108        }109        finally {110          Pop-Location111        }112      }113 114      It "the '-Stream' dynamic parameter should not be visible to get-command in the function provider" -Skip:(!$IsWindows) {115        try {116        Push-Location -Path function:117          { Get-Command Clear-Content -Stream $streamName } |118            Should -Throw -ErrorId "NamedParameterNotFound,Microsoft.PowerShell.Commands.GetCommandCommand"119        }120        finally {121          Pop-Location122        }123      }124    }125  }126 127  Context "Proper errors should be delivered when bad locations are specified" {128    It "should throw when targetting a directory." {129      { Clear-Content -Path . -ErrorAction Stop } | Should -Throw -ErrorId "ClearDirectoryContent"130    }131 132    It "should throw `"Cannot bind argument to parameter 'Path'`" when -Path is `$null" {133      { Clear-Content -Path $null -ErrorAction Stop } |134        Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ClearContentCommand"135    }136 137    #[BugId(BugDatabase.WindowsOutOfBandReleases, 903880)]138    It "should throw `"Cannot bind argument to parameter 'Path'`" when -Path is `$()" {139      { Clear-Content -Path $() -ErrorAction Stop } |140        Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ClearContentCommand"141    }142 143    #[DRT][BugId(BugDatabase.WindowsOutOfBandReleases, 906022)]144    It "should throw 'PSNotSupportedException' when you clear-content to an unsupported provider" {145      $functionName = Get-NonExistantFunctionName146      $null = New-Item -Path function:$functionName -Value { 1 }147      { Clear-Content -Path function:$functionName -ErrorAction Stop } |148        Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.ClearContentCommand"149    }150 151    It "should throw FileNotFound error when referencing a non-existant file" {152      $badFile = "TestDrive:/badfilename.txt"153      { Clear-Content -Path $badFile -ErrorAction Stop } |154        Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ClearContentCommand"155    }156 157    It "should throw DriveNotFound error when referencing a non-existant drive" {158      $badDrive = "{0}:/file.txt" -f (Get-NonExistantDriveName)159      { Clear-Content -Path $badDrive -ErrorAction Stop } |160        Should -Throw -ErrorId "DriveNotFound,Microsoft.PowerShell.Commands.ClearContentCommand"161    }162 163    # we'll use a provider qualified path to produce this error164    It "should throw ProviderNotFound error when referencing a non-existant provider" {165      $badProviderPath = "{0}::C:/file.txt" -f (Get-NonExistantProviderName)166      { Clear-Content -Path $badProviderPath -ErrorAction Stop } |167        Should -Throw -ErrorId "ProviderNotFound,Microsoft.PowerShell.Commands.ClearContentCommand"168    }169  }170}171