forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOut-File.Tests.ps1
More file actions
135 lines (104 loc) · 4.89 KB
/
Copy pathOut-File.Tests.ps1
File metadata and controls
135 lines (104 loc) · 4.89 KB
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
Describe "Out-File DRT Unit Tests" -Tags "CI" {
It "Should be able to write the contents into a file with -pspath" {
$tempFile = Join-Path -Path $TestDrive -ChildPath "ExposeBug928965"
{ 1 | Out-File -PSPath $tempFile } | Should Not Throw
$fileContents = Get-Content $tempFile
$fileContents | Should be 1
Remove-Item $tempFile -Force
}
It "Should be able to write the contents into a file with -pspath" {
$tempFile = Join-Path -Path $TestDrive -ChildPath "outfileAppendTest.txt"
{ 'This is first line.' | out-file $tempFile } | Should Not Throw
{ 'This is second line.' | out-file -append $tempFile } | Should Not Throw
$tempFile |Should Contain "first"
$tempFile |Should Contain "second"
Remove-Item $tempFile -Force
}
}
Describe "Out-File" -Tags "CI" {
BeforeAll {
$expectedContent = "some test text"
$inObject = New-Object psobject -Property @{text=$expectedContent}
$testfile = Join-Path -Path $TestDrive -ChildPath outfileTest.txt
}
AfterEach {
Remove-Item -Path $testfile -Force
}
It "Should be able to be called without error" {
{ Out-File -FilePath $testfile } | Should Not Throw
}
It "Should be able to accept string input via piping" {
{ $expectedContent | Out-File -FilePath $testfile } | Should Not Throw
$actual = Get-Content $testfile
$actual | Should Be $expectedContent
}
It "Should be able to accept string input via the InputObject switch" {
{ Out-File -FilePath $testfile -InputObject $expectedContent } | Should Not Throw
$actual = Get-Content $testfile
$actual | Should Be $expectedContent
}
It "Should be able to accept object input" {
{ $inObject | Out-File -FilePath $testfile } | Should Not Throw
{ Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw
}
It "Should not overwrite when the noclobber switch is used" {
Out-File -FilePath $testfile -InputObject $inObject
{ Out-File -FilePath $testfile -InputObject $inObject -NoClobber -ErrorAction SilentlyContinue } | Should Throw "already exists."
{ Out-File -FilePath $testfile -InputObject $inObject -NoOverWrite -ErrorAction SilentlyContinue } | Should Throw "already exists."
$actual = Get-Content $testfile
$actual[0] | Should Be ""
$actual[1] | Should Match "text"
$actual[2] | Should Match "----"
$actual[3] | Should Match "some test text"
}
It "Should Append a new line when the append switch is used" {
{ Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw
{ Out-File -FilePath $testfile -InputObject $inObject -Append } | Should Not Throw
$actual = Get-Content $testfile
$actual[0] | Should Be ""
$actual[1] | Should Match "text"
$actual[2] | Should Match "----"
$actual[3] | Should Match "some test text"
$actual[4] | Should Be ""
$actual[5] | Should Be ""
$actual[6] | Should Be ""
$actual[7] | Should Match "text"
$actual[8] | Should Match "----"
$actual[9] | Should Match "some test text"
$actual[10] | Should Be ""
$actual[11] | Should Be ""
}
It "Should limit each line to the specified number of characters when the width switch is used on objects" {
Out-File -FilePath $testfile -Width 10 -InputObject $inObject
$actual = Get-Content $testfile
$actual[0] | Should Be ""
$actual[1] | Should Be "text "
$actual[2] | Should Be "---- "
$actual[3] | Should Be "some te..."
}
It "Should allow the cmdlet to overwrite an existing read-only file" {
# create a read-only text file
{ Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw
Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $true
# write information to the RO file
{ Out-File -FilePath $testfile -InputObject $inObject -Append -Force } | Should Not Throw
$actual = Get-Content $testfile
$actual[0] | Should Be ""
$actual[1] | Should Match "text"
$actual[2] | Should Match "----"
$actual[3] | Should Match "some test text"
$actual[4] | Should Be ""
$actual[5] | Should Be ""
$actual[6] | Should Be ""
$actual[7] | Should Match "text"
$actual[8] | Should Match "----"
$actual[9] | Should Match "some test text"
$actual[10] | Should Be ""
$actual[11] | Should Be ""
# reset to not read only so it can be deleted
Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $false
}
It "Should be able to use the 'Path' alias for the 'FilePath' parameter" {
{ Out-File -Path $testfile } | Should Not Throw
}
}