forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate-FormatData.Tests.ps1
More file actions
152 lines (139 loc) · 6.07 KB
/
Copy pathUpdate-FormatData.Tests.ps1
File metadata and controls
152 lines (139 loc) · 6.07 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe "Update-FormatData" -Tags "CI" {
BeforeEach {
$ps = [PowerShell]::Create()
}
Context "Validate Update-FormatData update correctly" {
It "Should not throw upon reloading previous formatting file" {
{ Update-FormatData } | Should -Not -throw
}
It "Should validly load formatting data" {
$path = Join-Path -Path $TestDrive -ChildPath "outputfile.ps1xml"
Get-FormatData -typename System.Diagnostics.Process | Export-FormatData -Path $path
$null = $ps.AddScript("Update-FormatData -prependPath $path")
$ps.Invoke()
$ps.HadErrors | Should -BeFalse
}
It "Update with atributes on Configuration node should be ignored" {
$xmlContent = @"
<Configuration xmlns:foo="bar">
<ViewDefinitions>
<View>
<Name>Test</Name>
<ViewSelectedBy>
<TypeName>Test</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<PropertyName>Test</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
</ViewDefinitions>
</Configuration>
"@
$path = "$testdrive\rootattribute.format.ps1xml"
Set-Content -Path $path -Value $xmlContent
$null = $ps.AddScript("Update-FormatData -prependPath $path")
$ps.Invoke()
$ps.HadErrors | Should -BeFalse
$ps.Commands.Clear()
$null = $ps.AddScript("Get-FormatData test")
$formatData = $ps.Invoke()
$formatData | Should -HaveCount 1
$formatData.TypeNames | Should -BeExactly "Test"
$formatData.FormatViewDefinition.Name | Should -BeExactly "Test"
}
}
}
Describe "Update-FormatData basic functionality" -Tags "CI" {
BeforeAll {
$testfilename = "testfile.ps1xml"
$testfile = Join-Path -Path $TestDrive -ChildPath $testfilename
$xmlContent=@"
<Types>
<Type>
<Name>AnyName</Name>
<Members>
<PropertySet>
<Name>PropertySetName</Name>
<ReferencedProperties>
<Name>FirstName</Name>
<Name>LastName</Name>
</ReferencedProperties>
</PropertySet>
</Members>
</Type>
</Types>
"@
$xmlContent > $testfile
}
It "Update-FormatData with WhatIf should work"{
{ Update-FormatData -Append $testfile -WhatIf } | Should -Not -Throw
{ Update-FormatData -Prepend $testfile -WhatIf } | Should -Not -Throw
}
It "Update with invalid format xml should fail" {
$xmlContent = @"
<Configuration>
<ViewDefinitions>
<View>
<Name>T2</Name>
</View>
</ViewDefinitions>
</Configuration>
"@
$xmlContent | Out-File -FilePath "$testdrive\invalid.format.ps1xml" -Encoding ascii
{ Update-FormatData -Path "$testdrive\invalid.format.ps1xml" -ErrorAction Stop } | Should -Throw -ErrorId "FormatXmlUpdateException,Microsoft.PowerShell.Commands.UpdateFormatDataCommand"
}
}
Describe "Update-FormatData with resources in CustomControls" -Tags "CI" {
BeforeAll {
$templatePath = Join-Path $PSScriptRoot (Join-Path 'assets' 'UpdateFormatDataTests.format.ps1xml')
$formatFilePath = Join-Path $TestDrive 'UpdateFormatDataTests.format.ps1xml'
$ps = [powershell]::Create()
$iss = [system.management.automation.runspaces.initialsessionstate]::CreateDefault2()
$rs = [system.management.automation.runspaces.runspacefactory]::CreateRunspace($iss)
$rs.Open()
$ps.Runspace = $rs
}
AfterAll {
$rs.Close()
$ps.Dispose()
}
Context "Validate Update-FormatData" {
It "Resources in WindowsPS syntax should be loaded successfully" {
$format = Get-Content -Path $templatePath -Raw
$format.Replace("%BaseName%","FileSystemProviderStrings") | Set-Content -Path $formatFilePath -Force
$null = $ps.AddScript("Update-FormatData -PrependPath $formatFilePath")
$ps.Streams.Error.Clear()
$ps.Invoke()
$ps.Streams.Error | Should -BeNullOrEmpty
}
It "Resources in CorePS syntax should be loaded successfully" {
$format = Get-Content -Path $templatePath -Raw
$format.Replace("%BaseName%","System.Management.Automation.resources.FileSystemProviderStrings") | Set-Content -Path $formatFilePath -Force
$null = $ps.AddScript("Update-FormatData -PrependPath $formatFilePath")
$ps.Streams.Error.Clear()
$ps.Invoke()
$ps.Streams.Error | Should -BeNullOrEmpty
}
It "Verify assembly path in error message when resource is Not found" {
$format = Get-Content -Path $templatePath -Raw
$format.Replace("%BaseName%","NonExistingResource") | Set-Content -Path $formatFilePath -Force
$null = $ps.AddScript("Update-FormatData -PrependPath $formatFilePath")
$ps.Streams.Error.Clear()
$ps.Invoke()
$sma = [appdomain]::CurrentDomain.GetAssemblies() | ? { if ($_.Location) {$_.Location.EndsWith("System.Management.Automation.dll")}}
$smaLocation = $sma.Location
$ps.Streams.Error | %{ $_.Exception.Message.Contains($smaLocation) | Should -BeTrue }
$ps.Streams.Error | %{ $_.FullyQualifiedErrorId | Should -Match 'FormatXmlUpdateException' }
}
}
}