MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Get-Item" -Tags "CI" {4 BeforeAll {5 if ( $IsWindows ) {6 $skipNotWindows = $false7 }8 else {9 $skipNotWindows = $true10 }11 }12 It "Should list all the items in the current working directory when asterisk is used" {13 $items = Get-Item (Join-Path -Path $PSScriptRoot -ChildPath "*")14 ,$items | Should -BeOfType System.Object[]15 }16 17 It "Should return the name of the current working directory when a dot is used" {18 $item = Get-Item $PSScriptRoot19 $item | Should -BeOfType System.IO.DirectoryInfo20 $item.Name | Should -BeExactly (Split-Path $PSScriptRoot -Leaf)21 }22 23 It "Should return the proper Name and BaseType for directory objects vs file system objects" {24 $rootitem = Get-Item $PSScriptRoot25 $rootitem | Should -BeOfType System.IO.DirectoryInfo26 $childitem = (Get-Item (Join-Path -Path $PSScriptRoot -ChildPath Get-Item.Tests.ps1))27 $childitem | Should -BeOfType System.IO.FileInfo28 }29 30 It "Using -literalpath should find no additional files" {31 $null = New-Item -type file "$TESTDRIVE/file[abc].txt"32 $null = New-Item -type file "$TESTDRIVE/filea.txt"33 # if literalpath is not correct we would see filea.txt34 $item = Get-Item -LiteralPath "$TESTDRIVE/file[abc].txt"35 @($item).Count | Should -Be 136 $item.Name | Should -BeExactly 'file[abc].txt'37 }38 39 It "Should have mode flags set" {40 Get-ChildItem $PSScriptRoot | ForEach-Object { $_.Mode | Should -Not -BeNullOrEmpty }41 }42 43 It "Should not return the item unless force is used if hidden" {44 ${hiddenFile} = "${TESTDRIVE}/.hidden.txt"45 ${item} = New-Item -type file "${hiddenFile}"46 if ( ${IsWindows} ) {47 attrib +h "$hiddenFile"48 }49 ${result} = Get-Item "${hiddenFile}" -ErrorAction SilentlyContinue50 ${result} | Should -BeNullOrEmpty51 ${result} = Get-Item -Force "${hiddenFile}" -ErrorAction SilentlyContinue52 ${result}.FullName | Should -BeExactly ${item}.FullName53 }54 55 It "Should get properties for special reparse points" -Skip:$skipNotWindows {56 $result = Get-Item -Path $HOME/Cookies -Force57 $result.LinkType | Should -BeExactly "Junction"58 $result.Target | Should -Not -BeNullOrEmpty59 $result.Name | Should -BeExactly "Cookies"60 $result.Mode | Should -BeExactly "l--hs"61 $result.Exists | Should -BeTrue62 }63 64 It "Should return correct result for ToString() on root of drive" {65 $root = $IsWindows ? "${env:SystemDrive}\" : "/"66 (Get-Item -Path $root).ToString() | Should -BeExactly $root67 }68 69 Context "Test for Include, Exclude, and Filter" {70 BeforeAll {71 ${testBaseDir} = "${TESTDRIVE}/IncludeExclude"72 $null = New-Item -Type Directory "${testBaseDir}"73 $null = New-Item -Type File "${testBaseDir}/file1.txt"74 $null = New-Item -Type File "${testBaseDir}/file2.txt"75 }76 It "Should respect -Exclude" {77 $result = Get-Item "${testBaseDir}/*" -Exclude "file2.txt"78 ($result).Count | Should -Be 179 $result.Name | Should -BeExactly "file1.txt"80 }81 It "Should respect -Include" {82 $result = Get-Item "${testBaseDir}/*" -Include "file2.txt"83 ($result).Count | Should -Be 184 $result.Name | Should -BeExactly "file2.txt"85 }86 It "Should respect -Filter" {87 $result = Get-Item "${testBaseDir}/*" -Filter "*2*"88 ($result).Count | Should -Be 189 $result.Name | Should -BeExactly "file2.txt"90 }91 It "Should respect combinations of filter, include, and exclude" {92 $result = Get-Item "${testBaseDir}/*" -Filter *.txt -Include "file[12].txt" -Exclude file2.txt93 ($result).Count | Should -Be 194 $result.Name | Should -BeExactly "file1.txt"95 }96 }97 98 Context "Error Condition Checking" {99 It "Should return an error if the provider does not exist" {100 { Get-Item BadProvider::/BadFile -ErrorAction Stop } | Should -Throw -ErrorId "ProviderNotFound,Microsoft.PowerShell.Commands.GetItemCommand"101 }102 103 It "Should return an error if the drive does not exist" {104 { Get-Item BadDrive:/BadFile -ErrorAction Stop } | Should -Throw -ErrorId "DriveNotFound,Microsoft.PowerShell.Commands.GetItemCommand"105 }106 }107 108 Context "Alternate Stream Tests" {109 BeforeAll {110 if ( $skipNotWindows )111 {112 return113 }114 $altStreamPath = "$TESTDRIVE\altStream.txt"115 $altStreamDirectory = "$TESTDRIVE/altstreamdir"116 $noAltStreamDirectory = "$TESTDRIVE/noaltstreamdir"117 $stringData = "test data"118 $streamName = "test"119 $absentStreamName = "noExist"120 $null = New-Item -type file $altStreamPath121 Set-Content -Path $altStreamPath -Stream $streamName -Value $stringData122 $null = New-Item -type directory $altStreamDirectory123 Set-Content -Path $altStreamDirectory -Stream $streamName -Value $stringData124 $null = New-Item -type directory $noAltStreamDirectory125 }126 It "Should find an alternate stream on a file if present" -Skip:$skipNotWindows {127 $result = Get-Item $altStreamPath -Stream $streamName128 $result.Length | Should -Be ($stringData.Length + [Environment]::NewLine.Length)129 $result.Stream | Should -Be $streamName130 }131 It "Should error if it cannot find alternate stream on an existing file" -Skip:$skipNotWindows {132 { Get-Item $altStreamPath -Stream $absentStreamName -ErrorAction Stop } | Should -Throw -ErrorId "AlternateDataStreamNotFound,Microsoft.PowerShell.Commands.GetItemCommand"133 }134 It "Should find an alternate stream on a directory if present, and it should not be a container" -Skip:$skipNotWindows {135 $result = Get-Item $altStreamDirectory -Stream $streamName136 $result.Length | Should -Be ($stringData.Length + [Environment]::NewLine.Length )137 $result.Stream | Should -Be $streamName138 $result.PSIsContainer | Should -BeExactly $false139 }140 It "Should not find an alternate stream on a directory if not present" -Skip:$skipNotWindows {141 { Get-Item $noAltStreamDirectory -Stream $absentStreamName -ErrorAction Stop } | Should -Throw -ErrorId "AlternateDataStreamNotFound,Microsoft.PowerShell.Commands.GetItemCommand"142 }143 It "Should find zero alt streams and not fail on a directory with a wildcard stream name if no alt streams are present" -Skip:$skipNotWindows {144 $result = Get-Item $noAltStreamDirectory -Stream * -ErrorAction Stop145 $result | Should -BeExactly $null146 }147 It "Should return filename property correctly" -Skip:$skipNotWindows {148 $result = (Get-Item -Path $altStreamPath -Stream $streamName).FileName149 $result | Should -BeExactly $altStreamPath150 }151 }152 153 Context "Registry Provider" {154 It "Can retrieve an item from registry" -Skip:$skipNotWindows {155 ${result} = Get-Item HKLM:/Software156 ${result} | Should -BeOfType Microsoft.Win32.RegistryKey157 }158 }159 160 Context "Environment provider" -Tag "CI" {161 BeforeAll {162 $env:testvar="b"163 $env:testVar="a"164 }165 166 AfterAll {167 Clear-Item -Path env:testvar -ErrorAction SilentlyContinue168 Clear-Item -Path env:testVar -ErrorAction SilentlyContinue169 }170 171 It "get-item testVar" {172 (Get-Item env:\testVar).Value | Should -BeExactly "a"173 }174 175 It "get-item is case-sensitive/insensitive as appropriate" {176 $expectedValue = "b"177 if($IsWindows)178 {179 $expectedValue = "a"180 }181 182 (Get-Item env:\testvar).Value | Should -BeExactly $expectedValue183 }184 }185}186 187Describe "Get-Item environment provider on Windows with accidental case-variant duplicates" -Tags "Scenario" {188 BeforeAll {189 $env:testVar = 'a' # Note: Even though PSScriptAnalyzer can't detect it, this variable *is* used below, namely via Node.js.190 }191 AfterAll {192 $env:testVar = $null193 }194 It "Reports the effective value among accidental case-variant duplicates on Windows" -Skip:$skipNotWindows {195 if (-not (Get-Command -ErrorAction Ignore node.exe)) {196 Write-Warning "Test skipped, because prerequisite Node.js is not installed."197 } else {198 $fso = New-Object -ComObject Scripting.FileSystemObject199 $shortPath = $fso.GetFile("$PSHOME\pwsh.exe").ShortPath.Replace('\', '/')200 $script = @"201 env = {}202 env.testVar = process.env.testVar // include the original case variant with its original value.203 env.TESTVAR = 'b' // redefine with a case variant name and different value204 // Note: Which value will win is not deterministic(!); what matters, however, is that both205 // `$env:testvar and Get-Item env:testvar report the same value.206 // The nondeterministic behavior makes it hard to prove that the values are *always* the207 // same, however.208 require('child_process').execSync('$shortPath -noprofile -command `$env:testvar, (Get-Item env:testvar).Value', { env: env }).toString()209"@210 $valDirect, $valGetItem, $unused = node.exe -pe $script211 $LASTEXITCODE | Should -Be 0212 $valDirect | Should -Not -BeNullOrEmpty213 $valGetItem | Should -BeExactly $valDirect214 }215 }216}217 218Describe 'Formatting for FileInfo objects' -Tags 'CI' {219 BeforeAll {220 $extensionTests = [System.Collections.Generic.List[HashTable]]::new()221 foreach ($extension in @('.zip', '.tgz', '.tar', '.gz', '.nupkg', '.cab', '.7z', '.ps1', '.psd1', '.psm1', '.ps1xml')) {222 $extensionTests.Add(@{extension = $extension})223 }224 }225 226 It 'File type <extension> should have correct color' -TestCases $extensionTests {227 param($extension)228 229 $testFile = Join-Path -Path $TestDrive -ChildPath "test$extension"230 $file = New-Item -ItemType File -Path $testFile231 $file.NameString | Should -BeExactly "$($PSStyle.FileInfo.Extension[$extension] + $file.Name + $PSStyle.Reset)"232 }233 234 It 'Directory should have correct color' {235 $dirPath = Join-Path -Path $TestDrive -ChildPath 'myDir'236 $dir = New-Item -ItemType Directory -Path $dirPath237 $dir.NameString | Should -BeExactly "$($PSStyle.FileInfo.Directory + $dir.Name + $PSStyle.Reset)"238 }239 240 It 'Executable should have correct color' {241 if ($IsWindows) {242 $exePath = Join-Path -Path $TestDrive -ChildPath 'myExe.exe'243 $exe = New-Item -ItemType File -Path $exePath244 }245 else {246 $exePath = Join-Path -Path $TestDrive -ChildPath 'myExe'247 $null = New-Item -ItemType File -Path $exePath248 chmod +x $exePath249 $exe = Get-Item -Path $exePath250 }251 252 $exe.NameString | Should -BeExactly "$($PSStyle.FileInfo.Executable + $exe.Name + $PSStyle.Reset)"253 }254}255 256Describe 'Formatting for FileInfo requiring admin' -Tags 'CI','RequireAdminOnWindows' {257 It 'Symlink should have correct color' {258 $linkPath = Join-Path -Path $TestDrive -ChildPath 'link'259 $link = New-Item -ItemType SymbolicLink -Name 'link' -Value $TestDrive -Path $TestDrive260 $link.NameString | Should -BeExactly "$($PSStyle.FileInfo.SymbolicLink + $link.Name + $PSStyle.Reset) -> $TestDrive"261 }262}263 