File size: 7,136 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# get a random string of characters a-z and A-Z
function Get-RandomString
{
    param ( [int]$Length = 8 )
    $chars = .{ ([int][char]'a')..([int][char]'z');([int][char]'A')..([int][char]'Z') }
    ([char[]]($chars | Get-Random -Count $Length)) -join ""
}

# get a random string which is not the name of an existing provider
function Get-NonExistantProviderName
{
   param ( [int]$Length = 8 )
   do {
       $providerName = Get-RandomString -Length $Length
   } until ( $null -eq (Get-PSProvider -PSProvider $providername -ErrorAction SilentlyContinue) )
   $providerName
}

# get a random string which is not the name of an existing drive
function Get-NonExistantDriveName
{
    param ( [int]$Length = 8 )
    do {
        $driveName = Get-RandomString -Length $Length
    } until ( $null -eq (Get-PSDrive $driveName -ErrorAction SilentlyContinue) )
    $drivename
}

# get a random string which is not the name of an existing function
function Get-NonExistantFunctionName
{
    param ( [int]$Length = 8 )
    do {
        $functionName = Get-RandomString -Length $Length
    } until ( (Test-Path -Path function:$functionName) -eq $false )
    $functionName
}

Describe "Clear-Content cmdlet tests" -Tags "CI" {
  BeforeAll {
    $file1 = "file1.txt"
    $file2 = "file2.txt"
    $file3 = "file3.txt"
    $content1 = "This is content"
    $content2 = "This is content for alternate stream tests"
    Setup -File "$file1"
    Setup -File "$file2" -Content $content1
    Setup -File "$file3" -Content $content2
    $streamContent = "content for alternate stream"
    $streamName = "altStream1"
    $dirName = "clearcontent"
    Setup -Directory "$dirName"
  }

  Context "Clear-Content should actually clear content" {
    It "should clear-Content of TestDrive:\$file1" {
      Set-Content -Path TestDrive:\$file1 -Value "ExpectedContent" -PassThru | Should -BeExactly "ExpectedContent"
      Clear-Content -Path TestDrive:\$file1
    }

    It "shouldn't get any content from TestDrive:\$file1" {
      $result = Get-Content -Path TestDrive:\$file1
      $result | Should -BeNullOrEmpty
    }

    # we could suppress the WhatIf output here if we use the testhost, but it's not necessary
    It "The filesystem provider supports should process" -Skip:(!$IsWindows) {
      Clear-Content -Path TestDrive:\$file2 -WhatIf
      "TestDrive:\$file2" | Should -FileContentMatch "This is content"
    }

    It "The filesystem provider should support ShouldProcess (reference ProviderSupportsShouldProcess member)" {
      $cci = ((Get-Command -Name Clear-Content).ImplementingType)::new()
      $cci.SupportsShouldProcess | Should -BeTrue
    }

    Context "Clear-Content should work with alternate data streams on Windows" {
      It "Alternate streams should be cleared with Clear-Content on a file" -Skip:(!$IsWindows) {

        Set-Content           -Path "TestDrive:/$file3" -Stream $streamName -Value $streamContent
        Get-Content           -Path "TestDrive:/$file3" -Stream $streamName | Should -BeExactly $streamContent

        Clear-Content         -Path "TestDrive:/$file3" -Stream $streamName -ErrorAction Stop

        $result = Get-Item -Path "TestDrive:/$file3" -Stream $streamName
        $result | Should -BeOfType System.Management.Automation.Internal.AlternateStreamData
        $result.length | Should -Be 0
      }

      It "Alternate streams should be cleared with Clear-Content on a directory" -Skip:(!$IsWindows) {
        Set-Content           -Path "TestDrive:/$dirName" -Stream $streamName -Value $streamContent

        Get-Content           -Path "TestDrive:/$dirName" -Stream $streamName | Should -BeExactly $streamContent
        Clear-Content         -Path "TestDrive:/$dirName" -Stream $streamName -ErrorAction Stop

        $result = Get-Item -Path "TestDrive:/$dirName" -Stream $streamName
        $result | Should -BeOfType System.Management.Automation.Internal.AlternateStreamData
        $result.length | Should -Be 0
      }

      It "the '-Stream' dynamic parameter is visible to get-command in the filesystem" -Skip:(!$IsWindows) {
        try {
          Push-Location -Path TestDrive:
          (Get-Command Clear-Content -Stream foo).parameters.keys -eq "Stream" | Should -BeExactly "Stream"
        }
        finally {
          Pop-Location
        }
      }

      It "the '-Stream' dynamic parameter should not be visible to get-command in the function provider" -Skip:(!$IsWindows) {
        try {
        Push-Location -Path function:
          { Get-Command Clear-Content -Stream $streamName } |
            Should -Throw -ErrorId "NamedParameterNotFound,Microsoft.PowerShell.Commands.GetCommandCommand"
        }
        finally {
          Pop-Location
        }
      }
    }
  }

  Context "Proper errors should be delivered when bad locations are specified" {
    It "should throw when targetting a directory." {
      { Clear-Content -Path . -ErrorAction Stop } | Should -Throw -ErrorId "ClearDirectoryContent"
    }

    It "should throw `"Cannot bind argument to parameter 'Path'`" when -Path is `$null" {
      { Clear-Content -Path $null -ErrorAction Stop } |
        Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ClearContentCommand"
    }

    #[BugId(BugDatabase.WindowsOutOfBandReleases, 903880)]
    It "should throw `"Cannot bind argument to parameter 'Path'`" when -Path is `$()" {
      { Clear-Content -Path $() -ErrorAction Stop } |
        Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ClearContentCommand"
    }

    #[DRT][BugId(BugDatabase.WindowsOutOfBandReleases, 906022)]
    It "should throw 'PSNotSupportedException' when you clear-content to an unsupported provider" {
      $functionName = Get-NonExistantFunctionName
      $null = New-Item -Path function:$functionName -Value { 1 }
      { Clear-Content -Path function:$functionName -ErrorAction Stop } |
        Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.ClearContentCommand"
    }

    It "should throw FileNotFound error when referencing a non-existant file" {
      $badFile = "TestDrive:/badfilename.txt"
      { Clear-Content -Path $badFile -ErrorAction Stop } |
        Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ClearContentCommand"
    }

    It "should throw DriveNotFound error when referencing a non-existant drive" {
      $badDrive = "{0}:/file.txt" -f (Get-NonExistantDriveName)
      { Clear-Content -Path $badDrive -ErrorAction Stop } |
        Should -Throw -ErrorId "DriveNotFound,Microsoft.PowerShell.Commands.ClearContentCommand"
    }

    # we'll use a provider qualified path to produce this error
    It "should throw ProviderNotFound error when referencing a non-existant provider" {
      $badProviderPath = "{0}::C:/file.txt" -f (Get-NonExistantProviderName)
      { Clear-Content -Path $badProviderPath -ErrorAction Stop } |
        Should -Throw -ErrorId "ProviderNotFound,Microsoft.PowerShell.Commands.ClearContentCommand"
    }
  }
}