File size: 16,697 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Import-Module HelpersCommon

function Clean-State
{
    if (Test-Path $FullyQualifiedLink)
    {
        Remove-Item $FullyQualifiedLink -Force
    }

    if (Test-Path $FullyQualifiedFile)
    {
        Remove-Item $FullyQualifiedFile -Force
    }

    if ($FullyQualifiedFileInFolder -and (Test-Path $FullyQualifiedFileInFolder))
    {
        Remove-Item $FullyQualifiedFileInFolder -Force
    }

    if ($FullyQualifiedSubFolder -and (Test-Path $FullyQualifiedSubFolder))
    {
        Remove-Item $FullyQualifiedSubFolder -Force
    }

    if (Test-Path $FullyQualifiedFolder)
    {
        Remove-Item $FullyQualifiedFolder -Force
    }
}

Describe "New-Item" -Tags "CI" {
    $tmpDirectory               = $TestDrive
    $testfile                   = "testfile.txt"
    $testfolder                 = "newDirectory"
    $testsubfolder              = "newSubDirectory"
    $testlink                   = "testlink"
    $FullyQualifiedFile         = Join-Path -Path $tmpDirectory -ChildPath $testfile
    $FullyQualifiedFolder       = Join-Path -Path $tmpDirectory -ChildPath $testfolder
    $FullyQualifiedLink         = Join-Path -Path $tmpDirectory -ChildPath $testlink
    $FullyQualifiedSubFolder    = Join-Path -Path $FullyQualifiedFolder -ChildPath $testsubfolder
    $FullyQualifiedFileInFolder = Join-Path -Path $FullyQualifiedFolder -ChildPath $testfile


    BeforeEach {
        Clean-State
    }

    It "should call the function without error" {
        { New-Item -Name $testfile -Path $tmpDirectory -ItemType file } | Should -Not -Throw
    }

    It "Should create a file without error" {
        New-Item -Name $testfile -Path $tmpDirectory -ItemType file

        Test-Path $FullyQualifiedFile | Should -BeTrue

        $fileInfo = Get-ChildItem $FullyQualifiedFile
        $fileInfo.Target | Should -BeNullOrEmpty
        $fileInfo.LinkType | Should -BeNullOrEmpty
    }

    It "Should create a folder without an error" {
        New-Item -Name newDirectory -Path $tmpDirectory -ItemType directory

        Test-Path $FullyQualifiedFolder | Should -BeTrue
    }

    It "Should create a file using the ni alias" {
        ni -Name $testfile -Path $tmpDirectory -ItemType file

        Test-Path $FullyQualifiedFile | Should -BeTrue
    }

    It "Should create a file using the Type alias instead of ItemType" {
        New-Item -Name $testfile -Path $tmpDirectory -Type file

        Test-Path $FullyQualifiedFile | Should -BeTrue
    }

    It "Should create a file with sample text inside the file using the Value parameter" {
        $expected = "This is test string"
        New-Item -Name $testfile -Path $tmpDirectory -ItemType file -Value $expected

        Test-Path $FullyQualifiedFile | Should -BeTrue

        Get-Content $FullyQualifiedFile | Should -Be $expected
    }

    It "Should not create a file when the Name parameter is not used and only a directory specified" {
        #errorAction used because permissions issue in Windows
        New-Item -Path $tmpDirectory -ItemType file -ErrorAction SilentlyContinue

        Test-Path $FullyQualifiedFile | Should -BeFalse

    }

    It "Should create a file when the Name parameter is not used but a fully qualified path is specified" {
        New-Item -Path $FullyQualifiedFile -ItemType file

        Test-Path $FullyQualifiedFile | Should -BeTrue
    }

    It "Should be able to create a multiple items in different directories" {
        $FullyQualifiedFile2 = Join-Path -Path $tmpDirectory -ChildPath test2.txt
        New-Item -ItemType file -Path $FullyQualifiedFile, $FullyQualifiedFile2

        Test-Path $FullyQualifiedFile  | Should -BeTrue
        Test-Path $FullyQualifiedFile2 | Should -BeTrue

        Remove-Item $FullyQualifiedFile2
    }

    It "Should be able to call the whatif switch without error" {
        { 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" {
        New-Item -Name $testfile -Path $tmpDirectory -ItemType file -WhatIf

        Test-Path $FullyQualifiedFile | Should -BeFalse
    }

    It "Should create a hard link of a file without error" {
        New-Item -Name $testfile -Path $tmpDirectory -ItemType file
        Test-Path $FullyQualifiedFile | Should -BeTrue

        New-Item -ItemType HardLink -Target $FullyQualifiedFile -Name $testlink -Path $tmpDirectory
        Test-Path $FullyQualifiedLink | Should -BeTrue

        $fileInfo = Get-ChildItem $FullyQualifiedLink
        $fileInfo.Target | Should -BeNullOrEmpty
        $fileInfo.LinkType | Should -BeExactly "HardLink"
    }

    It "Should create a file at the root of the drive while the current working directory is not the root" {
        try {
            New-Item -Name $testfolder -Path "TestDrive:\" -ItemType directory > $null
            Push-Location -Path "TestDrive:\$testfolder"
            New-Item -Name $testfile -Path "TestDrive:\" -ItemType file > $null
            $FullyQualifiedFile | Should -Exist
        }
        finally {
            Pop-Location
        }
    }

    It "Should create a folder at the root of the drive while the current working directory is not the root" {
        $testfolder2 = "newDirectory2"
        $FullyQualifiedFolder2 = Join-Path -Path $tmpDirectory -ChildPath $testfolder2

        try {
            New-Item -Name $testfolder -Path "TestDrive:\" -ItemType directory > $null
            Push-Location -Path "TestDrive:\$testfolder"
            New-Item -Name $testfolder2 -Path "TestDrive:\" -ItemType directory > $null
            $FullyQualifiedFolder2 | Should -Exist
        }
        finally {
            Pop-Location
        }
    }

    It "Should create a file in the current directory when using Drive: notation" {
        try {
            New-Item -Name $testfolder -Path "TestDrive:\" -ItemType directory > $null
            Push-Location -Path "TestDrive:\$testfolder"
            New-Item -Name $testfile -Path "TestDrive:" -ItemType file > $null
            $FullyQualifiedFileInFolder | Should -Exist
        }
        finally {
            Pop-Location
        }
    }

    It "Should create a folder in the current directory when using Drive: notation" {
        try {
            New-Item -Name $testfolder -Path "TestDrive:\" -ItemType directory > $null
            Push-Location -Path "TestDrive:\$testfolder"
            New-Item -Name $testsubfolder -Path "TestDrive:" -ItemType file > $null
            $FullyQualifiedSubFolder | Should -Exist
        }
        finally {
            Pop-Location
        }
    }

    It "Should display an error message when a symbolic link target is not specified" {
        { New-Item -Name $testfile -Path $tmpDirectory -ItemType SymbolicLink } | Should -Throw -ErrorId 'ArgumentNull,Microsoft.PowerShell.Commands.NewItemCommand'
        $Error[0].Exception.ParamName | Should -BeExactly 'content'
        { New-Item -Name $testfile -Path $tmpDirectory -ItemType:SymbolicLink -Value {} } | Should -Throw -ErrorId 'ArgumentNull,Microsoft.PowerShell.Commands.NewItemCommand'
         $Error[0].Exception.ParamName | Should -BeExactly 'content'
    }
}

# More precisely these tests require SeCreateSymbolicLinkPrivilege.
# You can see list of priveledges with `whoami /priv`.
# In the default windows setup, Admin user has this priveledge, but regular users don't.

Describe "New-Item with links" -Tags @('CI', 'RequireAdminOnWindows') {
    $tmpDirectory         = $TestDrive
    $testfile             = "testfile.txt"
    $testfolder           = "newDirectory"
    $testlink             = "testlink"
    $FullyQualifiedFile   = Join-Path -Path $tmpDirectory -ChildPath $testfile
    $FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder
    $FullyQualifiedLink   = Join-Path -Path $tmpDirectory -ChildPath $testlink
    $SymLinkMask          = [System.IO.FileAttributes]::ReparsePoint
    $DirLinkMask          = $SymLinkMask -bor [System.IO.FileAttributes]::Directory

    BeforeEach {
        Clean-State
    }

    It "Should create a symbolic link of a file without error" {
        New-Item -Name $testfile -Path $tmpDirectory -ItemType file
        Test-Path $FullyQualifiedFile | Should -BeTrue

        New-Item -ItemType SymbolicLink -Target $FullyQualifiedFile -Name $testlink -Path $tmpDirectory
        Test-Path $FullyQualifiedLink | Should -BeTrue

        $fileInfo = Get-ChildItem $FullyQualifiedLink
        $fileInfo.Target | Should -Match ([regex]::Escape($FullyQualifiedFile))
        $fileInfo.LinkType | Should -BeExactly "SymbolicLink"
        $fileInfo.Attributes -band $DirLinkMask | Should -BeExactly $SymLinkMask
    }

    It "Should create a symbolic link to a non-existing file without error" {
        $target = Join-Path $tmpDirectory "totallyBogusFile"
        New-Item -ItemType SymbolicLink -Target $target -Name $testlink -Path $tmpDirectory
        Test-Path $FullyQualifiedLink | Should -BeTrue

        $fileInfo = Get-ChildItem $FullyQualifiedLink
        $fileInfo.Target | Should -Be $target
        Test-Path $fileInfo.Target | Should -BeFalse
        $fileInfo.LinkType | Should -BeExactly "SymbolicLink"
        $fileInfo.Attributes -band $DirLinkMask | Should -BeExactly $SymLinkMask
    }

    It "Should create a symbolic link to directory without error" {
        New-Item -Name $testFolder -Path $tmpDirectory -ItemType directory
        $FullyQualifiedFolder | Should -Exist

        New-Item -ItemType SymbolicLink -Target $FullyQualifiedFolder -Name $testlink -Path $tmpDirectory
        Test-Path $FullyQualifiedLink | Should -BeTrue

        $fileInfo = Get-Item $FullyQualifiedLink
        $fileInfo.Target | Should -Match ([regex]::Escape($FullyQualifiedFolder))
        $fileInfo.LinkType | Should -BeExactly "SymbolicLink"
        $fileInfo.Attributes -band $DirLinkMask | Should -Be $DirLinkMask

        # Remove the link explicitly to avoid broken symlink issue
        Remove-Item $FullyQualifiedLink -Force
        # Test a code path removing a symbolic link (reparse point)
        Test-Path $FullyQualifiedLink | Should -BeFalse
    }

    It "New-Item -ItemType SymbolicLink should understand directory path ending with slash" {
        $folderName = [System.IO.Path]::GetRandomFileName()
        $symbolicLinkPath = New-Item -ItemType SymbolicLink -Path "$tmpDirectory/$folderName/" -Value "/bar/"
        $symbolicLinkPath | Should -Not -BeNullOrEmpty
    }

    It "New-Item -ItemType SymbolicLink should be able to create a relative link" -Skip:(!$IsWindows) {
        try {
            Push-Location $TestDrive
            $relativeFilePath = Join-Path -Path . -ChildPath "relativefile.txt"
            $file = New-Item -ItemType File -Path $relativeFilePath
            $link = New-Item -ItemType SymbolicLink -Path ./link -Target $relativeFilePath
            $link.Target | Should -BeExactly $relativeFilePath
        } finally {
            Pop-Location
        }
    }
}

Describe "New-Item: symlink with absolute/relative path test" -Tags @('CI', 'RequireAdminOnWindows') {
    BeforeAll {
        # on macOS, the /tmp directory is a symlink, so we'll resolve it here
        $TestPath = $TestDrive
        if ($IsMacOS)
        {
            $item = Get-Item $TestPath
            $dirName = $item.BaseName
            $item = Get-Item $item.PSParentPath -Force
            if ($item.LinkType -eq "SymbolicLink")
            {
                $TestPath = Join-Path $item.Target $dirName
            }
        }

        Push-Location $TestPath
        $null = New-Item -Type Directory someDir
        $null = New-Item -Type File someFile
    }

    AfterAll {
        Pop-Location
    }

    It "Symlink with absolute path to existing directory behaves like a directory" {
        New-Item -Type SymbolicLink someDirLinkAbsolute -Target (Convert-Path someDir)
        Get-Item someDirLinkAbsolute | Should -BeOfType System.IO.DirectoryInfo
    }

    It "Symlink with relative path to existing directory behaves like a directory" {
        # PowerShell should normalize '.\someDir' to './someDir' as needed.
        New-Item -Type SymbolicLink someDirLinkRelative -Target .\someDir
        Get-Item someDirLinkRelative | Should -BeOfType System.IO.DirectoryInfo
    }

    It "Symlink with absolute path to existing file behaves like a file" {
        New-Item -Type SymbolicLink someFileLinkAbsolute -Target (Convert-Path someFile)
        Get-Item someFileLinkAbsolute | Should -BeOfType System.IO.FileInfo
    }

    It "Symlink with relative path to existing file behaves like a file" {
        New-Item -Type SymbolicLink someFileLinkRelative -Target ./someFile
        Get-Item someFileLinkRelative | Should -BeOfType System.IO.FileInfo
    }
}

Describe "New-Item with links fails for non elevated user if developer mode not enabled on Windows." -Tags "CI" {
    BeforeAll {
        # on macOS, the /tmp directory is a symlink, so we'll resolve it here
        $TestPath = $TestDrive
        if ($IsMacOS)
        {
            $item = Get-Item $TestPath
            $dirName = $item.BaseName
            $item = Get-Item $item.PSParentPath -Force
            if ($item.LinkType -eq "SymbolicLink")
            {
                $TestPath = Join-Path $item.Target $dirName
            }
        }

        $testfile             = "testfile.txt"
        $testlink             = "testlink"
        $FullyQualifiedFile   = Join-Path -Path $TestPath -ChildPath $testfile
        $TestFilePath         = Join-Path -Path $TestPath -ChildPath $testlink
        $developerModeEnabled = (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock -ErrorAction SilentlyContinue).AllowDevelopmentWithoutDevLicense -eq 1
        $minBuildRequired     = [System.Environment]::OSVersion.Version -ge "10.0.14972"
        $developerMode = $developerModeEnabled -and $minBuildRequired
    }

    AfterEach {
        Remove-Item -Path $testFilePath -Force -ErrorAction SilentlyContinue
    }

    It "Should error correctly when failing to create a symbolic link and not in developer mode" -Skip:(!$IsWindows -or $developerMode -or (Test-IsElevated)) {
        { New-Item -ItemType SymbolicLink -Path $TestFilePath -Target $FullyQualifiedFile -ErrorAction Stop } |
        Should -Throw -ErrorId "NewItemSymbolicLinkElevationRequired,Microsoft.PowerShell.Commands.NewItemCommand"
        $TestFilePath | Should -Not -Exist
    }

    It "Should succeed to create a symbolic link without elevation and in developer mode" -Skip:(!$IsWindows -or !$developerMode -or (Test-IsElevated)) {
        { New-Item -ItemType SymbolicLink -Path $TestFilePath -Target $FullyQualifiedFile -ErrorAction Stop } | Should -Not -Throw
        $TestFilePath | Should -Exist
    }
}

Describe "New-Item -Force allows to create an item even if the directories in the path don't exist" -Tags "CI" {
    BeforeAll {
        $testFile             = 'testfile.txt'
        $testFolder           = 'testfolder'
        $FullyQualifiedFolder = Join-Path -Path $TestDrive -ChildPath $testFolder
        $FullyQualifiedFile   = Join-Path -Path $TestDrive -ChildPath $testFolder -AdditionalChildPath $testFile
    }

    BeforeEach {
        # Explicitly removing folder and the file before tests
        Remove-Item $FullyQualifiedFolder -ErrorAction SilentlyContinue
        Remove-Item $FullyQualifiedFile   -ErrorAction SilentlyContinue
        Test-Path -Path $FullyQualifiedFolder | Should -BeFalse
        Test-Path -Path $FullyQualifiedFile   | Should -BeFalse
    }

    It "Should error correctly when -Force is not used and folder in the path doesn't exist" {
        { New-Item $FullyQualifiedFile -ErrorAction Stop } | Should -Throw -ErrorId 'NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand'
        $FullyQualifiedFile | Should -Not -Exist
    }
    It "Should create new file correctly when -Force is used and folder in the path doesn't exist" {
        { New-Item $FullyQualifiedFile -Force -ErrorAction Stop } | Should -Not -Throw
        $FullyQualifiedFile | Should -Exist
    }
}

Describe "New-Item -Force should throw an error for invalid characters in directory path" -Tags "CI" {
    BeforeAll {
        $invalidPath = Join-Path -Path $TestDrive -ChildPath 'Invalid?'
    }

    It "Should throw an error when -Force is used with an invalid directory path" -Skip:(!$IsWindows) {
        { New-Item -Path $invalidPath -ItemType Directory -Force -ErrorAction Stop } | Should -Throw -ErrorId 'CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand'
    }
}