diff --git a/test/powershell/ConvertFrom-Csv.Tests.ps1 b/test/powershell/ConvertFrom-Csv.Tests.ps1 index 524817f1a4..572ebafe5e 100644 --- a/test/powershell/ConvertFrom-Csv.Tests.ps1 +++ b/test/powershell/ConvertFrom-Csv.Tests.ps1 @@ -2,7 +2,7 @@ Describe "ConvertFrom-Csv" { $testObject = "a", "1" - $testcsv = "$here/assets/TestCsv2.csv" + $testcsv = Join-Path -Path (Join-Path -Path $here -ChildPath assets) -ChildPath TestCsv2.csv $testName = "Zaphod BeebleBrox" $testColumns = @" a,b,c diff --git a/test/powershell/Export-Alias.Tests.ps1 b/test/powershell/Export-Alias.Tests.ps1 index 632df20f8f..13d319a5d4 100644 --- a/test/powershell/Export-Alias.Tests.ps1 +++ b/test/powershell/Export-Alias.Tests.ps1 @@ -1,10 +1,10 @@ $here = Split-Path -Parent $MyInvocation.MyCommand.Path -. $here/Test-Common.ps1 +. (Join-Path -Path $here -ChildPath Test-Common.ps1) Describe "Export-Alias" { - $testAliasDirectory = "$(GetTempDir)/ExportAliasTestDirectory" + $testAliasDirectory = Join-Path -Path $(GetTempDir) -ChildPath ExportAliasTestDirectory $testAliases = "TestAliases" - $fulltestpath = "$($testAliasDirectory)/$($testAliases)" + $fulltestpath = Join-Path -Path $($testAliasDirectory) -ChildPath $($testAliases) BeforeEach { New-Item -Path $testAliasDirectory -ItemType Directory -Force diff --git a/test/powershell/Get-Content.Tests.ps1 b/test/powershell/Get-Content.Tests.ps1 index abcb0d0891..fe4b16e635 100644 --- a/test/powershell/Get-Content.Tests.ps1 +++ b/test/powershell/Get-Content.Tests.ps1 @@ -10,8 +10,8 @@ $testString2 = $firstline + $nl + $secondline + $nl + $thirdline + $nl + $fourthline + $nl + $fifthline - $testPath = "/tmp/testfile1" - $testPath2 = "/tmp/testfile2" + $testPath = Join-Path -Path $TestDrive -ChildPath testfile1 + $testPath2 = Join-Path -Path $TestDrive -ChildPath testfile2 BeforeEach { New-Item -Path $testPath -ItemType file -Force -Value $testString diff --git a/test/powershell/Get-FileHash.Tests.ps1 b/test/powershell/Get-FileHash.Tests.ps1 index 0325d1ff59..f4d816c1d3 100644 --- a/test/powershell/Get-FileHash.Tests.ps1 +++ b/test/powershell/Get-FileHash.Tests.ps1 @@ -1,5 +1,5 @@ Describe "Get-FileHash" { - New-Variable testDocument -Value $PSScriptRoot/assets/testablescript.ps1 -Scope Global -Force + New-Variable testDocument -Value (Join-Path -Path (Join-Path $PSScriptRoot -ChildPath assets) -ChildPath testablescript.ps1) -Scope Global -Force # The MACTripleDES and RIPEMD160 algorithms are unsupported at the moment $algorithms = @{"SHA1" ="01B865D143E07ECC875AB0EFC0A4429387FD0CF7"; "SHA256" = "4A6DA9F1C0827143BB19FC4B0F2A8057BC1DF55F6D1F62FA3B917BA458E8F570"; @@ -28,8 +28,7 @@ It "Should list the path of the file under test" { $result = Get-FileHash $testDocument - # regular expression for the forward/backslashes for linux/windows compatability - $result.Path | Should Match "assets[/|\\]testablescript.ps1" + $result.Path | Should Match (Join-Path assets -ChildPath testablescript.ps1) } } diff --git a/test/powershell/Get-Item.Tests.ps1 b/test/powershell/Get-Item.Tests.ps1 index d2b14b74a4..e4aa7fd8f6 100644 --- a/test/powershell/Get-Item.Tests.ps1 +++ b/test/powershell/Get-Item.Tests.ps1 @@ -2,8 +2,8 @@ Describe "Get-Item" { It "Should list all the items in the current working directory when asterisk is used" { - (Get-Item $here/*).GetType().BaseType | Should Be 'array' - (Get-Item $here/*).GetType().Name | Should Be 'Object[]' + (Get-Item (Join-Path -Path $here -ChildPath "*")).GetType().BaseType | Should Be 'array' + (Get-Item (Join-Path -Path $here -ChildPath "*")).GetType().Name | Should Be 'Object[]' } It "Should return the name of the current working directory when a dot is used" { @@ -13,7 +13,7 @@ Describe "Get-Item" { It "Should return the proper Name and BaseType for directory objects vs file system objects" { (Get-Item $here).GetType().Name | Should Be 'DirectoryInfo' - (Get-Item $here/Get-Item.Tests.ps1).GetType().Name | Should Be 'FileInfo' + (Get-Item (Join-Path -Path $here -ChildPath Get-Item.Tests.ps1)).GetType().Name | Should Be 'FileInfo' } It "Should have mode flags set" { diff --git a/test/powershell/Get-ItemProperty.Tests.ps1 b/test/powershell/Get-ItemProperty.Tests.ps1 index f991ef1f0f..f0c9bf0091 100644 --- a/test/powershell/Get-ItemProperty.Tests.ps1 +++ b/test/powershell/Get-ItemProperty.Tests.ps1 @@ -2,22 +2,11 @@ Describe "Get-ItemProperty" { $currentDirectory = Split-Path $here -Leaf - $parentDirectory = Split-Path $here/.. -Leaf - $isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows) + $parentDirectory = Split-Path (Join-Path -Path $here -ChildPath "..") -Leaf + $tempDirectory = $TestDrive + $testprovider = (Get-Item $tempDirectory).PSDrive.Name - if ($isWindows) - { - $tempDirectory = "~/testfolder" - $testProvider = "C" - } - else - { - $tempDirectory = "/tmp/testfolder" - $testProvider = "/" - } - - New-Item $tempDirectory -ItemType Directory -Force - $testfile = $tempDirectory + [System.IO.Path]::DirectorySeparatorChar + "testfile1" + $testfile = Join-Path -Path $tempDirectory -ChildPath testfile1 New-Item $testfile -ItemType file -Force @@ -62,6 +51,4 @@ Describe "Get-ItemProperty" { $alias.PSDrive | Should Be $cmdlet.PSDrive $alias.PSProvider.Name | Should Be $cmdlet.PSProvider.Name } - - Remove-Item $tempDirectory -Recurse -Force } diff --git a/test/powershell/Import-Alias.Tests.ps1 b/test/powershell/Import-Alias.Tests.ps1 index 9520146877..e6f1b98a42 100644 --- a/test/powershell/Import-Alias.Tests.ps1 +++ b/test/powershell/Import-Alias.Tests.ps1 @@ -1,7 +1,7 @@ $here = Split-Path -Parent $MyInvocation.MyCommand.Path Describe "Import-Alias" { - $pesteraliasfile = "$here/assets/pesteralias.txt" + $pesteraliasfile = Join-Path -Path (Join-Path $here -ChildPath assets) -ChildPath pesteralias.txt Context "Validate ability to import alias file" { diff --git a/test/powershell/Import-Csv.Tests.ps1 b/test/powershell/Import-Csv.Tests.ps1 index 154248d3e1..7f9143297b 100644 --- a/test/powershell/Import-Csv.Tests.ps1 +++ b/test/powershell/Import-Csv.Tests.ps1 @@ -1,7 +1,7 @@ $here = Split-Path -Parent $MyInvocation.MyCommand.Path Describe "Import-Csv" { - $testCsv = "$here/assets/TestCsv.csv" + $testCsv = Join-Path -Path (Join-Path $here -ChildPath assets) -ChildPath TestCsv.csv It "Should be able to call without error" { { Import-Csv $testCsv } | Should Not Throw diff --git a/test/powershell/Invoke-Expression.Tests.ps1 b/test/powershell/Invoke-Expression.Tests.ps1 index 6606eaed75..ce14bd6574 100644 --- a/test/powershell/Invoke-Expression.Tests.ps1 +++ b/test/powershell/Invoke-Expression.Tests.ps1 @@ -13,7 +13,7 @@ Describe "Invoke-Expression" { } It "Should return the echoed text from a script" { - $testfile = "$here/assets/echoscript.ps1" + $testfile = Join-Path -Path (Join-Path $here -ChildPath assets) -ChildPath echoscript.ps1 $testcommand = "echo pestertestscript" $testcommand | Add-Content -Path $testfile (Invoke-Expression $testfile) | Should Be "pestertestscript" @@ -21,7 +21,7 @@ Describe "Invoke-Expression" { } It "Should return the echoed text from a script from the alias" { - $testfile = "$here/assets/echoscript.ps1" + $testfile = Join-Path -Path (Join-Path $here -ChildPath assets) -ChildPath echoscript.ps1 $testcommand = "echo pestertestscript" $testcommand | Add-Content -Path $testfile (iex $testfile) | Should Be "pestertestscript" diff --git a/test/powershell/Json.Tests.ps1 b/test/powershell/Json.Tests.ps1 index 920d1d8ffc..2d58cc52ed 100644 --- a/test/powershell/Json.Tests.ps1 +++ b/test/powershell/Json.Tests.ps1 @@ -18,7 +18,7 @@ Describe "Json.NET LINQ Parsing" { [Microsoft.PowerShell.CoreCLR.AssemblyExtensions]::LoadFrom($path) BeforeEach { - $jsonFile = "$here/assets/TestJson.json" + $jsonFile = Join-Path -Path (Join-Path $here -ChildPath assets) -ChildPath TestJson.json $jsonData = (Get-Content $jsonFile | Out-String) $json = [Newtonsoft.Json.Linq.JObject]::Parse($jsonData) } diff --git a/test/powershell/Measure-Command.Tests.ps1 b/test/powershell/Measure-Command.Tests.ps1 index 16690a1440..0189160669 100644 --- a/test/powershell/Measure-Command.Tests.ps1 +++ b/test/powershell/Measure-Command.Tests.ps1 @@ -16,8 +16,8 @@ Describe "Measure-Command" { } It "Should return TimeSpan after executing a cmdlet" { - $pesterscript = "$here/assets/echoscript.ps1" - $testfile = "$here/assets/echoscript.ps1" + $pesterscript = Join-Path -Path (Join-Path -Path $here -ChildPath assets) -ChildPath echoscript.ps1 + $testfile = $pesterscript $testcommand = "echo pestertestscript" $testcommand | Add-Content -Path $testfile diff --git a/test/powershell/New-Item.Tests.ps1 b/test/powershell/New-Item.Tests.ps1 index 49910b1041..c5f33a4103 100644 --- a/test/powershell/New-Item.Tests.ps1 +++ b/test/powershell/New-Item.Tests.ps1 @@ -12,11 +12,11 @@ } Describe "New-Item" { - $tmpDirectory = "/tmp" + $tmpDirectory = $TestDrive $testfile = "testfile.txt" $testfolder = "newDirectory" - $FullyQualifiedFile = $tmpDirectory + "/" + $testfile - $FullyQualifiedFolder = $tmpDirectory + "/" + $testfolder + $FullyQualifiedFile = Join-Path -Path $tmpDirectory -ChildPath $testfile + $FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder BeforeEach { Clean-State @@ -74,7 +74,7 @@ Describe "New-Item" { } It "Should be able to create a multiple items in different directories" { - $FullyQualifiedFile2 = $tmpDirectory + "/" + "test2.txt" + $FullyQualifiedFile2 = Join-Path -Path $tmpDirectory -ChildPath test2.txt New-Item -ItemType file -Path $FullyQualifiedFile, $FullyQualifiedFile2 Test-Path $FullyQualifiedFile | Should Be $true @@ -84,7 +84,7 @@ Describe "New-Item" { } It "Should be able to call the whatif switch without error" { - { New-Item -Name testfile.txt -Path /tmp -ItemType file -WhatIf } | Should Not Throw + { New-Item -Name testfile.txt -Path $tmpDirectory -ItemType file -WhatIf } | Should Not Throw } It "Should not create a new file when the whatif switch is used" { diff --git a/test/powershell/Out-File.Tests.ps1 b/test/powershell/Out-File.Tests.ps1 index 64f106352c..045293aafd 100644 --- a/test/powershell/Out-File.Tests.ps1 +++ b/test/powershell/Out-File.Tests.ps1 @@ -1,7 +1,7 @@ Describe "Out-File" { $expectedContent = "some test text" $inObject = New-Object psobject -Property @{text=$expectedContent} - $testfile = "/tmp/outfileTest.txt" + $testfile = Join-Path -Path $TestDrive -ChildPath outfileTest.txt AfterEach { Remove-Item -Path $testfile -Force diff --git a/test/powershell/Remove-Item.Tests.ps1 b/test/powershell/Remove-Item.Tests.ps1 index 85b03bfb14..2dcef4c9b8 100644 --- a/test/powershell/Remove-Item.Tests.ps1 +++ b/test/powershell/Remove-Item.Tests.ps1 @@ -1,7 +1,7 @@ Describe "Remove-Item" { - $testpath = "/tmp/" + $testpath = $TestDrive $testfile = "testfile.txt" - $testfilepath = $testpath + $testfile + $testfilepath = Join-Path -Path $testpath -ChildPath $testfile Context "File removal Tests" { BeforeEach { New-Item -Name $testfile -Path $testpath -ItemType "file" -Value "lorem ipsum" -Force @@ -71,11 +71,11 @@ # Create a single file that does not match that string - already done in BeforeEach # Delete the specific string - Remove-Item /tmp/* -Include file*.txt + Remove-Item (Join-Path -Path $testpath -ChildPath "*") -Include file*.txt # validate that the string under test was deleted, and the nonmatching strings still exist - Test-path /tmp/file1.txt | Should Be $false - Test-path /tmp/file2.txt | Should Be $false - Test-path /tmp/file3.txt | Should Be $false + Test-path (Join-Path -Path $testpath -ChildPath file1.txt) | Should Be $false + Test-path (Join-Path -Path $testpath -ChildPath file2.txt) | Should Be $false + Test-path (Join-Path -Path $testpath -ChildPath file3.txt) | Should Be $false Test-Path $testfilepath | Should Be $true # Delete the non-matching strings @@ -93,27 +93,27 @@ New-Item -Name file1.txt -Path $testpath -ItemType "file" -Value "lorem ipsum" # Delete the specific string - Remove-Item /tmp/file* -Exclude *.wav -Include *.txt + Remove-Item (Join-Path -Path $testpath -ChildPath "file*") -Exclude *.wav -Include *.txt # validate that the string under test was deleted, and the nonmatching strings still exist - Test-Path /tmp/file1.wav | Should Be $true - Test-Path /tmp/file2.wav | Should Be $true - Test-Path /tmp/file1.txt | Should Be $false + Test-Path (Join-Path -Path $testpath -ChildPath file1.wav) | Should Be $true + Test-Path (Join-Path -Path $testpath -ChildPath file2.wav) | Should Be $true + Test-Path (Join-Path -Path $testpath -ChildPath file1.txt) | Should Be $false # Delete the non-matching strings - Remove-Item /tmp/file1.wav - Remove-Item /tmp/file2.wav + Remove-Item (Join-Path -Path $testpath -ChildPath file1.wav) + Remove-Item (Join-Path -Path $testpath -ChildPath file2.wav) - Test-Path /tmp/file1.wav | Should Be $false - Test-Path /tmp/file2.wav | Should Be $false + Test-Path (Join-Path -Path $testpath -ChildPath file1.wav) | Should Be $false + Test-Path (Join-Path -Path $testpath -ChildPath file2.wav) | Should Be $false } } Context "Directory Removal Tests" { - $testdirectory = "/tmp/testdir" - $testsubdirectory = $testdirectory + "/subd" + $testdirectory = Join-Path -Path $testpath -ChildPath testdir + $testsubdirectory = Join-Path -Path $testdirectory -ChildPath subd BeforeEach { - New-Item -Name "testdir" -Path "/tmp/" -ItemType "directory" -Force + New-Item -Name "testdir" -Path $testpath -ItemType "directory" -Force Test-Path $testdirectory | Should Be $true } @@ -128,7 +128,7 @@ New-Item -Name "subd" -Path $testdirectory -ItemType "directory" New-Item -Name $testfile -Path $testsubdirectory -ItemType "file" -Value "lorem ipsum" - $complexDirectory = $testsubdirectory + "/" + $testfile + $complexDirectory = Join-Path -Path $testsubdirectory -ChildPath $testfile test-path $complexDirectory | Should Be $true { Remove-Item $testdirectory -Recurse} | Should Not Throw diff --git a/test/powershell/Remove-PSBreakpoint.Tests.ps1 b/test/powershell/Remove-PSBreakpoint.Tests.ps1 index 1532193408..435dcac31d 100644 --- a/test/powershell/Remove-PSBreakpoint.Tests.ps1 +++ b/test/powershell/Remove-PSBreakpoint.Tests.ps1 @@ -2,7 +2,7 @@ Describe "Remove-PSBreakpoint" { # Set up test script - $testScript = "$here/assets/psbreakpointtestscript.ps1" + $testScript = Join-Path -Path (Join-Path -Path $here -ChildPath assets) -ChildPath psbreakpointtestscript.ps1 $script = "`$var = 1 `$var2 = Get-Process diff --git a/test/powershell/Select-Object.Tests.ps1 b/test/powershell/Select-Object.Tests.ps1 index 9d5c65c29a..bdeab7d73d 100644 --- a/test/powershell/Select-Object.Tests.ps1 +++ b/test/powershell/Select-Object.Tests.ps1 @@ -1,5 +1,5 @@ $here = Split-Path -Parent $MyInvocation.MyCommand.Path -. "$here/Test-Mocks.ps1" +. (Join-Path -Path $here -ChildPath Test-Mocks.ps1) Describe "Select-Object" { BeforeEach { diff --git a/test/powershell/Select-String.Tests.ps1 b/test/powershell/Select-String.Tests.ps1 index b6f5b9cc60..b7144b20d8 100644 --- a/test/powershell/Select-String.Tests.ps1 +++ b/test/powershell/Select-String.Tests.ps1 @@ -89,16 +89,8 @@ Context "Filesytem actions" { $isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows) - if ($isWindows) - { - $testDirectory = "C:\tmp\" - $testInputFile = "C:\tmp\testfile1.txt" - } - else - { - $testDirectory = "/tmp/" - $testInputFile = "/tmp/testfile1.txt" - } + $testDirectory = $TestDrive + $testInputFile = Join-Path -Path $testDirectory -ChildPath testfile1.txt BeforeEach { New-Item $testInputFile -Itemtype "file" -Force -Value "This is a text string, and another string${nl}This is the second line${nl}This is the third line${nl}This is the fourth line${nl}No matches" @@ -163,7 +155,11 @@ It "Should return the third line in testfile1 when a relative path is used" { $expected = "testfile1.txt:3:This is the third line" - Select-String third $testDirectory/"../tmp/testfile1.txt" | Should Match $expected + $relativePath = Join-Path -Path $testDirectory -ChildPath ".." + $relativePath = Join-Path -Path $relativePath -ChildPath ".." + $relativePath = Join-Path -Path $relativePath -ChildPath $testDirectory + $relativePath = Join-Path -Path $relativePath -ChildPath testfile1.txt + Select-String third $relativePath | Should Match $expected } It "Should return the fourth line in testfile1 when a relative path is used" { @@ -171,7 +167,7 @@ pushd $testDirectory - Select-String matches $testDirectory/testfile1.txt | Should Match $expected + Select-String matches (Join-Path -Path $testDirectory -ChildPath testfile1.txt) | Should Match $expected popd } diff --git a/test/powershell/Set-PSBreakpoint.Tests.ps1 b/test/powershell/Set-PSBreakpoint.Tests.ps1 index 1ad10e376f..18e4921573 100644 --- a/test/powershell/Set-PSBreakpoint.Tests.ps1 +++ b/test/powershell/Set-PSBreakpoint.Tests.ps1 @@ -2,7 +2,7 @@ Describe "Set-PSBreakpoint" { # Set up test script - $testScript = "$here/psbreakpointtestscript.ps1" + $testScript = Join-Path -Path $here -ChildPath psbreakpointtestscript.ps1 "`$var = 1 " > $testScript diff --git a/test/powershell/Split-Path.Tests.ps1 b/test/powershell/Split-Path.Tests.ps1 index 0d383d4e20..97a1308047 100644 --- a/test/powershell/Split-Path.Tests.ps1 +++ b/test/powershell/Split-Path.Tests.ps1 @@ -61,24 +61,21 @@ } It "Should be able to accept regular expression input and output an array for multiple objects" { - $testDir = "./testDir" + $testDir = $TestDrive $testFile1 = "testfile1.ps1" $testFile2 = "testfile2.ps1" - $testFilePath1 = $testDir + "/" + $testFile1 - $testFilePath2 = $testDir + "/" + $testFile2 + $testFilePath1 = Join-Path -Path $testDir -ChildPath $testFile1 + $testFilePath2 = Join-Path -Path $testDir -ChildPath $testFile2 New-Item -ItemType file -Path $testFilePath1, $testFilePath2 -Force Test-Path $testFilePath1 | Should Be $true Test-Path $testFilePath2 | Should Be $true - $actual = ( Split-Path $testDir/*file*.ps1 -Leaf -Resolve ) | Sort-Object + $actual = ( Split-Path (Join-Path -Path $testDir -ChildPath "*file*.ps1") -Leaf -Resolve ) | Sort-Object $actual.GetType().BaseType.Name | Should Be "Array" $actual[0] | Should Be $testFile1 $actual[1] | Should Be $testFile2 - - Remove-Item $testDir -Recurse -ErrorAction Ignore - Test-Path $testDir | Should Be $false } It "Should be able to tell if a given path is an absolute path" { diff --git a/test/powershell/Start-Process.Tests.ps1 b/test/powershell/Start-Process.Tests.ps1 index 702211dc99..c31f6ade7d 100644 --- a/test/powershell/Start-Process.Tests.ps1 +++ b/test/powershell/Start-Process.Tests.ps1 @@ -1,11 +1,11 @@ $here = Split-Path -Parent $MyInvocation.MyCommand.Path -. $here/Test-Common.ps1 +. (Join-Path -Path $here -ChildPath Test-Common.ps1) Describe "Start-Process" { $pingCommand = (Get-Command -CommandType Application ping)[0].Definition $pingDirectory = Split-Path $pingCommand -Parent - $tempFile = "$(GetTempDir)/PSTest" - $assetsFile = $here + "/assets/SortTest.txt" + $tempFile = Join-Path -Path $(GetTempDir) -ChildPath PSTest + $assetsFile = Join-Path -Path (Join-Path -Path $here -ChildPath assets) -ChildPath SortTest.txt $windows = IsWindows if ($windows) { diff --git a/test/powershell/Tee-Object.Tests.ps1 b/test/powershell/Tee-Object.Tests.ps1 index 1fd926b4ac..9cd6a13d25 100644 --- a/test/powershell/Tee-Object.Tests.ps1 +++ b/test/powershell/Tee-Object.Tests.ps1 @@ -4,29 +4,31 @@ Describe "Tee-Object" { Context "Validate Tee-Object is correctly forking output" { + $testfile = Join-Path -Path (Join-Path -Path $here -ChildPath assets) -ChildPath testfile.txt + It "Should return the output to the screen and to the variable" { - $teefile = "$here/assets/testfile.txt" + $teefile = $testfile echo teeobjecttest1 | Tee-Object -variable teeresults $teeresults | Should Be "teeobjecttest1" Remove-Item $teefile -ErrorAction SilentlyContinue } It "Should return the output to the screen and to the variable from the alias" { - $teefile = "$here/assets/testfile.txt" + $teefile = $testfile echo teeobjecttest2 | tee -variable teeresults $teeresults | Should Be "teeobjecttest2" Remove-Item $teefile -ErrorAction SilentlyContinue } It "Should tee the output to a file" { - $teefile = "$here/assets/testfile.txt" + $teefile = $testfile echo teeobjecttest3 | Tee-Object $teefile Get-Content $teefile | Should Be "teeobjecttest3" Remove-Item $teefile -ErrorAction SilentlyContinue } It "Should tee the output to a file using the alias" { - $teefile = "$here/assets/testfile.txt" + $teefile = $testfile echo teeobjecttest4 | tee $teefile Get-Content $teefile | Should Be "teeobjecttest4" Remove-item $teefile -ErrorAction SilentlyContinue diff --git a/test/powershell/Test-Path.Tests.ps1 b/test/powershell/Test-Path.Tests.ps1 index a0b9fda82e..c7dde9f560 100644 --- a/test/powershell/Test-Path.Tests.ps1 +++ b/test/powershell/Test-Path.Tests.ps1 @@ -1,17 +1,15 @@ Describe "Test-Path" { BeforeAll { - $guid = [Guid]::NewGuid().Guid - $testdirectory = New-Item -Path $guid -ItemType Directory -Force + $testdirectory = $TestDrive $testfilename = New-Item -path $testdirectory -Name testfile.txt -ItemType file -Value 1 -force - $testfile = $tmp + "/" + $testfilename # populate with additional files New-Item -Path $testdirectory -Name datestfile -value 1 -ItemType file | Out-Null New-Item -Path $testdirectory -Name gatestfile -value 1 -ItemType file | Out-Null - } + New-Item -Path $testdirectory -Name usr -value 1 -ItemType directory | Out-Null - AfterAll { - Remove-Item -Path $testdirectory -Recurse + $nonExistantDir = Join-Path -Path (Join-Path -Path $testdirectory -ChildPath usr) -ChildPath bin + $nonExistantPath = Join-Path -Path (Join-Path -Path (Join-Path -Path $testdirectory -ChildPath usr) -ChildPath bin) -ChildPath error } It "Should be called on an existing path without error" { @@ -24,15 +22,15 @@ { $testdirectory | Test-Path } | Should Not Throw $testdirectory | Test-Path | Should Be $true - "/usr/bin/totallyFakeDirectory" | Test-Path | Should Be $false + $nonExistantDir | Test-Path | Should Be $false } It "Should be called on a nonexistant path without error" { - { Test-Path -Path "aNonexistant/path/that/should/error" } | Should Not Throw + { Test-Path -Path $nonExistantPath } | Should Not Throw } It "Should return false for a nonexistant path" { - Test-Path -Path "aNonexistant/path/that/should/error" | Should Be $false + Test-Path -Path $nonExistantPath | Should Be $false } It "Should return true for an existing path" { @@ -40,16 +38,16 @@ } It "Should be able to accept a regular expression" { - { Test-Path -Path "/u*" } | Should Not Throw - { Test-Path -Path "/u[a-z]r" } | Should Not Throw + { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u*") } | Should Not Throw + { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u[a-z]r") } | Should Not Throw } It "Should be able to return the correct result when a regular expression is used" { - Test-Path -Path "/u*" | Should Be $true - Test-Path -Path "/u[a-z]*" | Should Be $true + Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u*") | Should Be $true + Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u[a-z]*") | Should Be $true - Test-Path -Path "/aoeu*" | Should Be $false - Test-Path -Path "/u[A-Z]" | Should Be $false + Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "aoeu*") | Should Be $false + Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "u[A-Z]") | Should Be $false } It "Should return false when the Leaf pathtype is used on a directory" { @@ -69,20 +67,20 @@ } It "Should be able to search multiple regular expressions using the include switch" { - { Test-Path -Path "$testdirectory/*" -Include t* } | Should Be $true + { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "*") -Include t* } | Should Be $true } It "Should be able to exclude a regular expression using the exclude switch" { - { Test-Path -Path "$testdirectory/*" -Exclude v* } | Should Be $true + { Test-Path -Path (Join-Path -Path $testdirectory -ChildPath "*") -Exclude v* } | Should Be $true } It "Should be able to exclude multiple regular expressions using the exclude switch" { - # tests whether there's any files in the /usr directory that don't start with 'd' or 'g' + # tests whether there's any files in the usr directory that don't start with 'd' or 'g' { Test-Path -Path $testfilename -Exclude d*, g* } | Should Be $true } It "Should return true if the syntax of the path is correct when using the IsValid switch" { - { Test-Path -Path /this/is/a/valid/path -IsValid } | Should Be $true + { Test-Path -Path $nonExistantPath -IsValid } | Should Be $true } It "Should return false if the syntax of the path is incorrect when using the IsValid switch" { @@ -107,13 +105,13 @@ } It "Should return false if regular expressions are used with the LiteralPath switch" { - Test-Path -LiteralPath /*sr/bin | Should Be $false - Test-Path -LiteralPath /[usth]sr/bin | Should Be $false + Test-Path -LiteralPath (Join-Path -Path $testdirectory -ChildPath "u*") | Should Be $false + Test-Path -LiteralPath (Join-Path -Path $testdirectory -ChildPath "u[a-z]r") | Should Be $false } It "Should return false if regular expressions are used with the LiteralPath alias PSPath switch" { - Test-Path -PSPath /*sr/bin | Should Be $false - Test-Path -PSPath /[aoeu]sr/bin | Should Be $false + Test-Path -PSPath (Join-Path -Path $testdirectory -ChildPath "u*") | Should Be $false + Test-Path -PSPath (Join-Path -Path $testdirectory -ChildPath "u[a-z]r") | Should Be $false } It "Should return true if used on components other than filesystem objects" {