MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Import-Module HelpersCommon5 6function Clean-State7{8 if (Test-Path $FullyQualifiedLink)9 {10 Remove-Item $FullyQualifiedLink -Force11 }12 13 if (Test-Path $FullyQualifiedFile)14 {15 Remove-Item $FullyQualifiedFile -Force16 }17 18 if ($FullyQualifiedFileInFolder -and (Test-Path $FullyQualifiedFileInFolder))19 {20 Remove-Item $FullyQualifiedFileInFolder -Force21 }22 23 if ($FullyQualifiedSubFolder -and (Test-Path $FullyQualifiedSubFolder))24 {25 Remove-Item $FullyQualifiedSubFolder -Force26 }27 28 if (Test-Path $FullyQualifiedFolder)29 {30 Remove-Item $FullyQualifiedFolder -Force31 }32}33 34Describe "New-Item" -Tags "CI" {35 $tmpDirectory = $TestDrive36 $testfile = "testfile.txt"37 $testfolder = "newDirectory"38 $testsubfolder = "newSubDirectory"39 $testlink = "testlink"40 $FullyQualifiedFile = Join-Path -Path $tmpDirectory -ChildPath $testfile41 $FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder42 $FullyQualifiedLink = Join-Path -Path $tmpDirectory -ChildPath $testlink43 $FullyQualifiedSubFolder = Join-Path -Path $FullyQualifiedFolder -ChildPath $testsubfolder44 $FullyQualifiedFileInFolder = Join-Path -Path $FullyQualifiedFolder -ChildPath $testfile45 46 47 BeforeEach {48 Clean-State49 }50 51 It "should call the function without error" {52 { New-Item -Name $testfile -Path $tmpDirectory -ItemType file } | Should -Not -Throw53 }54 55 It "Should create a file without error" {56 New-Item -Name $testfile -Path $tmpDirectory -ItemType file57 58 Test-Path $FullyQualifiedFile | Should -BeTrue59 60 $fileInfo = Get-ChildItem $FullyQualifiedFile61 $fileInfo.Target | Should -BeNullOrEmpty62 $fileInfo.LinkType | Should -BeNullOrEmpty63 }64 65 It "Should create a folder without an error" {66 New-Item -Name newDirectory -Path $tmpDirectory -ItemType directory67 68 Test-Path $FullyQualifiedFolder | Should -BeTrue69 }70 71 It "Should create a file using the ni alias" {72 ni -Name $testfile -Path $tmpDirectory -ItemType file73 74 Test-Path $FullyQualifiedFile | Should -BeTrue75 }76 77 It "Should create a file using the Type alias instead of ItemType" {78 New-Item -Name $testfile -Path $tmpDirectory -Type file79 80 Test-Path $FullyQualifiedFile | Should -BeTrue81 }82 83 It "Should create a file with sample text inside the file using the Value parameter" {84 $expected = "This is test string"85 New-Item -Name $testfile -Path $tmpDirectory -ItemType file -Value $expected86 87 Test-Path $FullyQualifiedFile | Should -BeTrue88 89 Get-Content $FullyQualifiedFile | Should -Be $expected90 }91 92 It "Should not create a file when the Name parameter is not used and only a directory specified" {93 #errorAction used because permissions issue in Windows94 New-Item -Path $tmpDirectory -ItemType file -ErrorAction SilentlyContinue95 96 Test-Path $FullyQualifiedFile | Should -BeFalse97 98 }99 100 It "Should create a file when the Name parameter is not used but a fully qualified path is specified" {101 New-Item -Path $FullyQualifiedFile -ItemType file102 103 Test-Path $FullyQualifiedFile | Should -BeTrue104 }105 106 It "Should be able to create a multiple items in different directories" {107 $FullyQualifiedFile2 = Join-Path -Path $tmpDirectory -ChildPath test2.txt108 New-Item -ItemType file -Path $FullyQualifiedFile, $FullyQualifiedFile2109 110 Test-Path $FullyQualifiedFile | Should -BeTrue111 Test-Path $FullyQualifiedFile2 | Should -BeTrue112 113 Remove-Item $FullyQualifiedFile2114 }115 116 It "Should be able to call the whatif switch without error" {117 { New-Item -Name testfile.txt -Path $tmpDirectory -ItemType file -WhatIf } | Should -Not -Throw118 }119 120 It "Should not create a new file when the whatif switch is used" {121 New-Item -Name $testfile -Path $tmpDirectory -ItemType file -WhatIf122 123 Test-Path $FullyQualifiedFile | Should -BeFalse124 }125 126 It "Should create a hard link of a file without error" {127 New-Item -Name $testfile -Path $tmpDirectory -ItemType file128 Test-Path $FullyQualifiedFile | Should -BeTrue129 130 New-Item -ItemType HardLink -Target $FullyQualifiedFile -Name $testlink -Path $tmpDirectory131 Test-Path $FullyQualifiedLink | Should -BeTrue132 133 $fileInfo = Get-ChildItem $FullyQualifiedLink134 $fileInfo.Target | Should -BeNullOrEmpty135 $fileInfo.LinkType | Should -BeExactly "HardLink"136 }137 138 It "Should create a file at the root of the drive while the current working directory is not the root" {139 try {140 New-Item -Name $testfolder -Path "TestDrive:\" -ItemType directory > $null141 Push-Location -Path "TestDrive:\$testfolder"142 New-Item -Name $testfile -Path "TestDrive:\" -ItemType file > $null143 $FullyQualifiedFile | Should -Exist144 }145 finally {146 Pop-Location147 }148 }149 150 It "Should create a folder at the root of the drive while the current working directory is not the root" {151 $testfolder2 = "newDirectory2"152 $FullyQualifiedFolder2 = Join-Path -Path $tmpDirectory -ChildPath $testfolder2153 154 try {155 New-Item -Name $testfolder -Path "TestDrive:\" -ItemType directory > $null156 Push-Location -Path "TestDrive:\$testfolder"157 New-Item -Name $testfolder2 -Path "TestDrive:\" -ItemType directory > $null158 $FullyQualifiedFolder2 | Should -Exist159 }160 finally {161 Pop-Location162 }163 }164 165 It "Should create a file in the current directory when using Drive: notation" {166 try {167 New-Item -Name $testfolder -Path "TestDrive:\" -ItemType directory > $null168 Push-Location -Path "TestDrive:\$testfolder"169 New-Item -Name $testfile -Path "TestDrive:" -ItemType file > $null170 $FullyQualifiedFileInFolder | Should -Exist171 }172 finally {173 Pop-Location174 }175 }176 177 It "Should create a folder in the current directory when using Drive: notation" {178 try {179 New-Item -Name $testfolder -Path "TestDrive:\" -ItemType directory > $null180 Push-Location -Path "TestDrive:\$testfolder"181 New-Item -Name $testsubfolder -Path "TestDrive:" -ItemType file > $null182 $FullyQualifiedSubFolder | Should -Exist183 }184 finally {185 Pop-Location186 }187 }188 189 It "Should display an error message when a symbolic link target is not specified" {190 { New-Item -Name $testfile -Path $tmpDirectory -ItemType SymbolicLink } | Should -Throw -ErrorId 'ArgumentNull,Microsoft.PowerShell.Commands.NewItemCommand'191 $Error[0].Exception.ParamName | Should -BeExactly 'content'192 { New-Item -Name $testfile -Path $tmpDirectory -ItemType:SymbolicLink -Value {} } | Should -Throw -ErrorId 'ArgumentNull,Microsoft.PowerShell.Commands.NewItemCommand'193 $Error[0].Exception.ParamName | Should -BeExactly 'content'194 }195}196 197# More precisely these tests require SeCreateSymbolicLinkPrivilege.198# You can see list of priveledges with `whoami /priv`.199# In the default windows setup, Admin user has this priveledge, but regular users don't.200 201Describe "New-Item with links" -Tags @('CI', 'RequireAdminOnWindows') {202 $tmpDirectory = $TestDrive203 $testfile = "testfile.txt"204 $testfolder = "newDirectory"205 $testlink = "testlink"206 $FullyQualifiedFile = Join-Path -Path $tmpDirectory -ChildPath $testfile207 $FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder208 $FullyQualifiedLink = Join-Path -Path $tmpDirectory -ChildPath $testlink209 $SymLinkMask = [System.IO.FileAttributes]::ReparsePoint210 $DirLinkMask = $SymLinkMask -bor [System.IO.FileAttributes]::Directory211 212 BeforeEach {213 Clean-State214 }215 216 It "Should create a symbolic link of a file without error" {217 New-Item -Name $testfile -Path $tmpDirectory -ItemType file218 Test-Path $FullyQualifiedFile | Should -BeTrue219 220 New-Item -ItemType SymbolicLink -Target $FullyQualifiedFile -Name $testlink -Path $tmpDirectory221 Test-Path $FullyQualifiedLink | Should -BeTrue222 223 $fileInfo = Get-ChildItem $FullyQualifiedLink224 $fileInfo.Target | Should -Match ([regex]::Escape($FullyQualifiedFile))225 $fileInfo.LinkType | Should -BeExactly "SymbolicLink"226 $fileInfo.Attributes -band $DirLinkMask | Should -BeExactly $SymLinkMask227 }228 229 It "Should create a symbolic link to a non-existing file without error" {230 $target = Join-Path $tmpDirectory "totallyBogusFile"231 New-Item -ItemType SymbolicLink -Target $target -Name $testlink -Path $tmpDirectory232 Test-Path $FullyQualifiedLink | Should -BeTrue233 234 $fileInfo = Get-ChildItem $FullyQualifiedLink235 $fileInfo.Target | Should -Be $target236 Test-Path $fileInfo.Target | Should -BeFalse237 $fileInfo.LinkType | Should -BeExactly "SymbolicLink"238 $fileInfo.Attributes -band $DirLinkMask | Should -BeExactly $SymLinkMask239 }240 241 It "Should create a symbolic link to directory without error" {242 New-Item -Name $testFolder -Path $tmpDirectory -ItemType directory243 $FullyQualifiedFolder | Should -Exist244 245 New-Item -ItemType SymbolicLink -Target $FullyQualifiedFolder -Name $testlink -Path $tmpDirectory246 Test-Path $FullyQualifiedLink | Should -BeTrue247 248 $fileInfo = Get-Item $FullyQualifiedLink249 $fileInfo.Target | Should -Match ([regex]::Escape($FullyQualifiedFolder))250 $fileInfo.LinkType | Should -BeExactly "SymbolicLink"251 $fileInfo.Attributes -band $DirLinkMask | Should -Be $DirLinkMask252 253 # Remove the link explicitly to avoid broken symlink issue254 Remove-Item $FullyQualifiedLink -Force255 # Test a code path removing a symbolic link (reparse point)256 Test-Path $FullyQualifiedLink | Should -BeFalse257 }258 259 It "New-Item -ItemType SymbolicLink should understand directory path ending with slash" {260 $folderName = [System.IO.Path]::GetRandomFileName()261 $symbolicLinkPath = New-Item -ItemType SymbolicLink -Path "$tmpDirectory/$folderName/" -Value "/bar/"262 $symbolicLinkPath | Should -Not -BeNullOrEmpty263 }264 265 It "New-Item -ItemType SymbolicLink should be able to create a relative link" -Skip:(!$IsWindows) {266 try {267 Push-Location $TestDrive268 $relativeFilePath = Join-Path -Path . -ChildPath "relativefile.txt"269 $file = New-Item -ItemType File -Path $relativeFilePath270 $link = New-Item -ItemType SymbolicLink -Path ./link -Target $relativeFilePath271 $link.Target | Should -BeExactly $relativeFilePath272 } finally {273 Pop-Location274 }275 }276}277 278Describe "New-Item: symlink with absolute/relative path test" -Tags @('CI', 'RequireAdminOnWindows') {279 BeforeAll {280 # on macOS, the /tmp directory is a symlink, so we'll resolve it here281 $TestPath = $TestDrive282 if ($IsMacOS)283 {284 $item = Get-Item $TestPath285 $dirName = $item.BaseName286 $item = Get-Item $item.PSParentPath -Force287 if ($item.LinkType -eq "SymbolicLink")288 {289 $TestPath = Join-Path $item.Target $dirName290 }291 }292 293 Push-Location $TestPath294 $null = New-Item -Type Directory someDir295 $null = New-Item -Type File someFile296 }297 298 AfterAll {299 Pop-Location300 }301 302 It "Symlink with absolute path to existing directory behaves like a directory" {303 New-Item -Type SymbolicLink someDirLinkAbsolute -Target (Convert-Path someDir)304 Get-Item someDirLinkAbsolute | Should -BeOfType System.IO.DirectoryInfo305 }306 307 It "Symlink with relative path to existing directory behaves like a directory" {308 # PowerShell should normalize '.\someDir' to './someDir' as needed.309 New-Item -Type SymbolicLink someDirLinkRelative -Target .\someDir310 Get-Item someDirLinkRelative | Should -BeOfType System.IO.DirectoryInfo311 }312 313 It "Symlink with absolute path to existing file behaves like a file" {314 New-Item -Type SymbolicLink someFileLinkAbsolute -Target (Convert-Path someFile)315 Get-Item someFileLinkAbsolute | Should -BeOfType System.IO.FileInfo316 }317 318 It "Symlink with relative path to existing file behaves like a file" {319 New-Item -Type SymbolicLink someFileLinkRelative -Target ./someFile320 Get-Item someFileLinkRelative | Should -BeOfType System.IO.FileInfo321 }322}323 324Describe "New-Item with links fails for non elevated user if developer mode not enabled on Windows." -Tags "CI" {325 BeforeAll {326 # on macOS, the /tmp directory is a symlink, so we'll resolve it here327 $TestPath = $TestDrive328 if ($IsMacOS)329 {330 $item = Get-Item $TestPath331 $dirName = $item.BaseName332 $item = Get-Item $item.PSParentPath -Force333 if ($item.LinkType -eq "SymbolicLink")334 {335 $TestPath = Join-Path $item.Target $dirName336 }337 }338 339 $testfile = "testfile.txt"340 $testlink = "testlink"341 $FullyQualifiedFile = Join-Path -Path $TestPath -ChildPath $testfile342 $TestFilePath = Join-Path -Path $TestPath -ChildPath $testlink343 $developerModeEnabled = (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock -ErrorAction SilentlyContinue).AllowDevelopmentWithoutDevLicense -eq 1344 $minBuildRequired = [System.Environment]::OSVersion.Version -ge "10.0.14972"345 $developerMode = $developerModeEnabled -and $minBuildRequired346 }347 348 AfterEach {349 Remove-Item -Path $testFilePath -Force -ErrorAction SilentlyContinue350 }351 352 It "Should error correctly when failing to create a symbolic link and not in developer mode" -Skip:(!$IsWindows -or $developerMode -or (Test-IsElevated)) {353 { New-Item -ItemType SymbolicLink -Path $TestFilePath -Target $FullyQualifiedFile -ErrorAction Stop } |354 Should -Throw -ErrorId "NewItemSymbolicLinkElevationRequired,Microsoft.PowerShell.Commands.NewItemCommand"355 $TestFilePath | Should -Not -Exist356 }357 358 It "Should succeed to create a symbolic link without elevation and in developer mode" -Skip:(!$IsWindows -or !$developerMode -or (Test-IsElevated)) {359 { New-Item -ItemType SymbolicLink -Path $TestFilePath -Target $FullyQualifiedFile -ErrorAction Stop } | Should -Not -Throw360 $TestFilePath | Should -Exist361 }362}363 364Describe "New-Item -Force allows to create an item even if the directories in the path don't exist" -Tags "CI" {365 BeforeAll {366 $testFile = 'testfile.txt'367 $testFolder = 'testfolder'368 $FullyQualifiedFolder = Join-Path -Path $TestDrive -ChildPath $testFolder369 $FullyQualifiedFile = Join-Path -Path $TestDrive -ChildPath $testFolder -AdditionalChildPath $testFile370 }371 372 BeforeEach {373 # Explicitly removing folder and the file before tests374 Remove-Item $FullyQualifiedFolder -ErrorAction SilentlyContinue375 Remove-Item $FullyQualifiedFile -ErrorAction SilentlyContinue376 Test-Path -Path $FullyQualifiedFolder | Should -BeFalse377 Test-Path -Path $FullyQualifiedFile | Should -BeFalse378 }379 380 It "Should error correctly when -Force is not used and folder in the path doesn't exist" {381 { New-Item $FullyQualifiedFile -ErrorAction Stop } | Should -Throw -ErrorId 'NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand'382 $FullyQualifiedFile | Should -Not -Exist383 }384 It "Should create new file correctly when -Force is used and folder in the path doesn't exist" {385 { New-Item $FullyQualifiedFile -Force -ErrorAction Stop } | Should -Not -Throw386 $FullyQualifiedFile | Should -Exist387 }388}389 390Describe "New-Item -Force should throw an error for invalid characters in directory path" -Tags "CI" {391 BeforeAll {392 $invalidPath = Join-Path -Path $TestDrive -ChildPath 'Invalid?'393 }394 395 It "Should throw an error when -Force is used with an invalid directory path" -Skip:(!$IsWindows) {396 { New-Item -Path $invalidPath -ItemType Directory -Force -ErrorAction Stop } | Should -Throw -ErrorId 'CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand'397 }398}399 