2016-03-04 22:48:30 +00:00
|
|
|
function Clean-State
|
2015-09-04 22:05:18 +00:00
|
|
|
{
|
2016-02-29 20:23:28 +00:00
|
|
|
if (Test-Path $FullyQualifiedLink)
|
|
|
|
{
|
2016-03-04 22:52:27 +00:00
|
|
|
Remove-Item $FullyQualifiedLink -Force
|
2016-02-29 20:23:28 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 22:05:18 +00:00
|
|
|
if (Test-Path $FullyQualifiedFile)
|
|
|
|
{
|
2016-03-04 22:52:27 +00:00
|
|
|
Remove-Item $FullyQualifiedFile -Force
|
2015-09-04 22:05:18 +00:00
|
|
|
}
|
2015-08-25 21:46:29 +00:00
|
|
|
|
2015-09-04 22:05:18 +00:00
|
|
|
if (Test-Path $FullyQualifiedFolder)
|
|
|
|
{
|
2016-03-04 22:52:27 +00:00
|
|
|
Remove-Item $FullyQualifiedFolder -Force
|
2015-09-04 22:05:18 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-25 21:46:29 +00:00
|
|
|
|
2015-10-22 23:35:31 +00:00
|
|
|
Describe "New-Item" {
|
2016-02-05 16:53:44 +00:00
|
|
|
$tmpDirectory = $TestDrive
|
2015-09-04 22:05:18 +00:00
|
|
|
$testfile = "testfile.txt"
|
|
|
|
$testfolder = "newDirectory"
|
2016-02-29 18:09:30 +00:00
|
|
|
$testlink = "testlink"
|
2016-02-05 16:53:44 +00:00
|
|
|
$FullyQualifiedFile = Join-Path -Path $tmpDirectory -ChildPath $testfile
|
|
|
|
$FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder
|
2016-02-29 18:09:30 +00:00
|
|
|
$FullyQualifiedLink = Join-Path -Path $tmpDirectory -ChildPath $testlink
|
2015-08-25 21:46:29 +00:00
|
|
|
|
2015-09-04 22:05:18 +00:00
|
|
|
BeforeEach {
|
2016-03-04 22:52:27 +00:00
|
|
|
Clean-State
|
2015-09-04 22:05:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 21:46:29 +00:00
|
|
|
It "should call the function without error" {
|
2016-03-04 22:52:27 +00:00
|
|
|
{ New-Item -Name $testfile -Path $tmpDirectory -ItemType file } | Should Not Throw
|
2015-08-25 21:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should create a file without error" {
|
2016-03-04 22:52:27 +00:00
|
|
|
New-Item -Name $testfile -Path $tmpDirectory -ItemType file
|
2015-08-25 21:46:29 +00:00
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
Test-Path $FullyQualifiedFile | Should Be $true
|
2016-03-28 21:35:34 +00:00
|
|
|
|
|
|
|
$fileInfo = Get-ChildItem $FullyQualifiedFile
|
|
|
|
$fileInfo.Target | Should Be $null
|
|
|
|
$fileInfo.LinkType | Should Be $null
|
2015-08-25 21:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should create a folder without an error" {
|
2016-03-04 22:52:27 +00:00
|
|
|
New-Item -Name newDirectory -Path $tmpDirectory -ItemType directory
|
2015-08-25 21:46:29 +00:00
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
Test-Path $FullyQualifiedFolder | Should Be $true
|
2015-08-25 21:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should create a file using the ni alias" {
|
2016-03-04 22:52:27 +00:00
|
|
|
ni -Name $testfile -Path $tmpDirectory -ItemType file
|
2015-08-25 21:46:29 +00:00
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
Test-Path $FullyQualifiedFile | Should Be $true
|
2015-08-25 21:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should create a file using the Type alias instead of ItemType" {
|
2016-03-04 22:52:27 +00:00
|
|
|
New-Item -Name $testfile -Path $tmpDirectory -Type file
|
2015-08-25 21:46:29 +00:00
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
Test-Path $FullyQualifiedFile | Should Be $true
|
2015-08-25 21:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should create a file with sample text inside the file using the Value switch" {
|
2016-03-04 22:52:27 +00:00
|
|
|
$expected = "This is test string"
|
|
|
|
New-Item -Name $testfile -Path $tmpDirectory -ItemType file -Value $expected
|
2015-08-25 21:46:29 +00:00
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
Test-Path $FullyQualifiedFile | Should Be $true
|
2015-08-25 21:46:29 +00:00
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
Get-Content $FullyQualifiedFile | Should Be $expected
|
2015-08-25 21:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should not create a file when the Name switch is not used and only a directory specified" {
|
2016-03-04 22:52:27 +00:00
|
|
|
#errorAction used because permissions issue in Windows
|
|
|
|
New-Item -Path $tmpDirectory -ItemType file -ErrorAction SilentlyContinue
|
2015-08-25 21:46:29 +00:00
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
Test-Path $FullyQualifiedFile | Should Be $false
|
2015-08-25 21:46:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Should create a file when the Name switch is not used but a fully qualified path is specified" {
|
2016-03-04 22:52:27 +00:00
|
|
|
New-Item -Path $FullyQualifiedFile -ItemType file
|
2015-08-25 21:46:29 +00:00
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
Test-Path $FullyQualifiedFile | Should Be $true
|
2015-08-25 21:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to create a multiple items in different directories" {
|
2016-03-04 22:52:27 +00:00
|
|
|
$FullyQualifiedFile2 = Join-Path -Path $tmpDirectory -ChildPath test2.txt
|
|
|
|
New-Item -ItemType file -Path $FullyQualifiedFile, $FullyQualifiedFile2
|
2015-08-25 21:46:29 +00:00
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
Test-Path $FullyQualifiedFile | Should Be $true
|
|
|
|
Test-Path $FullyQualifiedFile2 | Should Be $true
|
2015-08-25 21:46:29 +00:00
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
Remove-Item $FullyQualifiedFile2
|
2015-08-25 21:46:29 +00:00
|
|
|
}
|
2015-09-04 22:05:18 +00:00
|
|
|
|
|
|
|
It "Should be able to call the whatif switch without error" {
|
2016-03-04 22:52:27 +00:00
|
|
|
{ New-Item -Name testfile.txt -Path $tmpDirectory -ItemType file -WhatIf } | Should Not Throw
|
2015-09-04 22:05:18 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
It "Should not create a new file when the whatif switch is used" {
|
|
|
|
New-Item -Name $testfile -Path $tmpDirectory -ItemType file -WhatIf
|
2015-09-04 22:05:18 +00:00
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
Test-Path $FullyQualifiedFile | Should Be $false
|
2015-09-04 22:05:18 +00:00
|
|
|
}
|
2016-02-29 18:09:30 +00:00
|
|
|
|
2016-04-10 01:23:45 +00:00
|
|
|
It "Should create a symbolic link of a file without error" {
|
2016-03-04 22:52:27 +00:00
|
|
|
New-Item -Name $testfile -Path $tmpDirectory -ItemType file
|
|
|
|
Test-Path $FullyQualifiedFile | Should Be $true
|
2016-02-29 18:09:30 +00:00
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
New-Item -ItemType SymbolicLink -Target $FullyQualifiedFile -Name $testlink -Path $tmpDirectory
|
|
|
|
Test-Path $FullyQualifiedLink | Should Be $true
|
2016-03-28 21:35:34 +00:00
|
|
|
|
|
|
|
$fileInfo = Get-ChildItem $FullyQualifiedLink
|
|
|
|
$fileInfo.Target | Should Be $FullyQualifiedFile
|
|
|
|
$fileInfo.LinkType | Should Be "SymbolicLink"
|
2016-02-29 18:09:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 01:23:45 +00:00
|
|
|
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 Be $true
|
|
|
|
|
|
|
|
$fileInfo = Get-ChildItem $FullyQualifiedLink
|
|
|
|
$fileInfo.Target | Should Be $target
|
|
|
|
Test-Path $fileInfo.Target | Should be $false
|
|
|
|
$fileInfo.LinkType | Should Be "SymbolicLink"
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Should create a symbolic link from directory without error" {
|
2016-03-04 22:52:27 +00:00
|
|
|
New-Item -Name $testFolder -Path $tmpDirectory -ItemType directory
|
|
|
|
Test-Path $FullyQualifiedFolder | Should Be $true
|
2016-02-29 18:09:30 +00:00
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
New-Item -ItemType SymbolicLink -Target $FullyQualifiedFolder -Name $testlink -Path $tmpDirectory
|
|
|
|
Test-Path $FullyQualifiedLink | Should Be $true
|
2016-03-01 18:12:11 +00:00
|
|
|
|
2016-03-28 21:35:34 +00:00
|
|
|
$fileInfo = Get-ChildItem $FullyQualifiedLink
|
|
|
|
$fileInfo.Target | Should Be $FullyQualifiedFolder
|
|
|
|
$fileInfo.LinkType | Should Be "SymbolicLink"
|
|
|
|
|
2016-03-04 22:52:27 +00:00
|
|
|
# Remove the link explicitly to avoid broken symlink issue
|
|
|
|
Remove-Item $FullyQualifiedLink -Force
|
2016-02-29 18:09:30 +00:00
|
|
|
}
|
2016-03-05 00:01:24 +00:00
|
|
|
|
|
|
|
It "Should create a hard link of a file without error" {
|
|
|
|
New-Item -Name $testfile -Path $tmpDirectory -ItemType file
|
|
|
|
Test-Path $FullyQualifiedFile | Should Be $true
|
|
|
|
|
|
|
|
New-Item -ItemType HardLink -Target $FullyQualifiedFile -Name $testlink -Path $tmpDirectory
|
|
|
|
Test-Path $FullyQualifiedLink | Should Be $true
|
2016-03-28 21:35:34 +00:00
|
|
|
|
|
|
|
$fileInfo = Get-ChildItem $FullyQualifiedLink
|
|
|
|
$fileInfo.Target | Should Be $null
|
|
|
|
$fileInfo.LinkType | Should Be "HardLink"
|
2016-03-05 00:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-08-25 21:46:29 +00:00
|
|
|
}
|