Public/New-ItemLink.Tests.ps1
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
$here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' . "$here\$sut" Describe "New-ItemLink" -Tag 'Unit' { $testDrive = Convert-Path 'TestDrive:\' # Note: All our test files will be hidden files, for the sake of Windows # Source of truth $testFileFullName = Join-Path $testDrive '.testfile' $testDirectoryFullName = Join-Path $testDrive '.testdirectory' # Link $testFileLinkFullName = Join-Path $testDrive '.testfilelink' $testDirectoryLinkFullName = Join-Path $testDrive '.testdirectorylink' AfterEach { # Powershell 5 requires a special way to remove a SymbolicLink, see: https://stackoverflow.com/a/63172492 if ($PSVersionTable.PSVersion.Major -le 5) { Get-ChildItem "$testDrive/*" -Attributes ReparsePoint | % { $_.Delete() } } Get-Item "$testDrive/*" -Force | Remove-Item -Recurse -Force } Context 'Exceptions' { It "Should throw when ItemType is invalid" { $testFile = New-Item $testFileFullName -ItemType File -Force $testFile.Attributes += 'Hidden' $params = @{ ItemType = 'foo' Path = $testFileLinkFullName Value = $testFile.FullName ErrorAction = 'Stop' } { New-ItemLink @params } | Should -Throw 'does not belong to the set' } } Context 'Error stream' { It "Should output to error stream" { $testFile = New-Item $testFileFullName -ItemType File -Force $testFile.Attributes += 'Hidden' $params = @{ ItemType = 'SymbolicLink' Path = $testFile.FullName # Deliberately simulate an error by trying to create a link in place of its file Value = $testFile.FullName ErrorVariable = 'err' ErrorAction = 'Continue' } $err = New-ItemLink @params 2>&1 $err | ? { $_ -is [System.Management.Automation.ErrorRecord] } | Should -Not -Be $null } It "Should not output to error stream" { $testFile = New-Item $testFileFullName -ItemType File -Force $testFile.Attributes += 'Hidden' $params = @{ ItemType = 'SymbolicLink' Path = $testFile.FullName # Deliberately simulate an error by trying to create a link in place of its file Value = $testFile.FullName ErrorAction = 'SilentlyContinue' } $err = New-ItemLink @params 2>&1 $err | ? { $_ -is [System.Management.Automation.ErrorRecord] } | Should -Be $null } } Context 'Behavior' { It "Should create HardLink for file" { $testFile = New-Item $testFileFullName -ItemType File -Force $testFile.Attributes += 'Hidden' $params = @{ ItemType = 'HardLink' Path = $testFileLinkFullName Value = $testFile.FullName ErrorAction = 'Stop' } $result = New-ItemLink @params $result | Should -BeOfType [System.IO.FileInfo] $result.LinkType | Should -Be $params['ItemType'] } It 'Should create a HardLink for file even if it already exists when using -Force' { $testFile = New-Item $testFileFullName -ItemType File -Force $testFile.Attributes += 'Hidden' $testFileLink = New-Item $testFileLinkFullName -Value $testFile.FullName -ItemType 'HardLink' -Force $testFileLink.Attributes += 'Hidden' $params = @{ ItemType = 'HardLink' Path = $testFileLink.FullName Value = $testFile.FullName Force = $true ErrorAction = 'Stop' } $result = New-ItemLink @params $result | Should -BeOfType [System.IO.FileInfo] $result.LinkType | Should -Be $params['ItemType'] } It "Should create SymbolicLink for file" { $testFile = New-Item $testFileFullName -ItemType File -Force $testFile.Attributes += 'Hidden' $params = @{ ItemType = 'SymbolicLink' Path = $testFileLinkFullName Value = $testFile.FullName ErrorAction = 'Stop' } $result = New-ItemLink @params $result | Should -BeOfType [System.IO.FileInfo] $result.LinkType | Should -Be $params['ItemType'] } It "Should create Junction for directory (Windows)" { if ($env:OS -eq 'Windows_NT') { $testDirectory = New-Item $testDirectoryFullName -ItemType Directory -Force $testDirectory.Attributes += 'Hidden' $params = @{ ItemType = 'Junction' Path = $testDirectoryLinkFullName Value = $testDirectory.FullName ErrorAction = 'Stop' } $result = New-ItemLink @params $result | Should -BeOfType [System.IO.DirectoryInfo] $result.LinkType | Should -Be $params['ItemType'] }else { $true } } It 'Should create a Junction for directory even if it already exists when using -Force (Windows)' { if ($env:OS -eq 'Windows_NT') { $testDirectory = New-Item $testDirectoryFullName -ItemType Directory -Force $testDirectory.Attributes += 'Hidden' $testDirectoryLink = New-Item $testDirectoryLinkFullName -Value $testDirectory.FullName -ItemType 'Junction' -Force $testDirectoryLink.Attributes += 'Hidden' $params = @{ ItemType = 'Junction' Path = $testDirectoryLink.FullName Value = $testDirectory.FullName Force = $true ErrorAction = 'Stop' } $result = New-ItemLink @params $result | Should -BeOfType [System.IO.DirectoryInfo] $result.LinkType | Should -Be $params['ItemType'] }else { $true } } It "Should create SymbolicLink for directory" { $testDirectory = New-Item $testDirectoryFullName -ItemType Directory -Force $testDirectory.Attributes += 'Hidden' $params = @{ ItemType = 'SymbolicLink' Path = $testDirectoryLinkFullName Value = $testDirectory.FullName ErrorAction = 'Stop' } $result = New-ItemLink @params $result | Should -BeOfType [System.IO.DirectoryInfo] $result.LinkType | Should -Be $params['ItemType'] } It 'Should create a SymbolicLink for directory even if it already exists when using -Force' { $testDirectory = New-Item $testDirectoryFullName -ItemType Directory -Force $testDirectory.Attributes += 'Hidden' $testDirectoryLink = New-Item $testDirectoryLinkFullName -Value $testDirectory.FullName -ItemType 'SymbolicLink' -Force $testDirectoryLink.Attributes += 'Hidden' $params = @{ ItemType = 'SymbolicLink' Path = $testDirectoryLink.FullName Value = $testDirectory.FullName Force = $true ErrorAction = 'Stop' } $result = New-ItemLink @params $result | Should -BeOfType [System.IO.DirectoryInfo] $result.LinkType | Should -Be $params['ItemType'] } } } |