1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
- Describe " Export-FormatData DRT Unit Tests " - Tags " CI" {
4
- It " Test basic functionality " {
3
+ Describe " Export-FormatData" - Tags " CI" {
4
+ BeforeAll {
5
5
$fd = Get-FormatData
6
- $tempFile = Join-Path $TestDrive - ChildPath " exportFormatTest.txt"
7
- $results = Export-FormatData - InputObject $fd [0 ] - Path $tempFile
8
- $content = Get-Content $tempFile
6
+ $testOutput = Join-Path - Path $TestDrive - ChildPath " outputfile"
7
+ }
8
+
9
+ AfterEach {
10
+ Remove-Item $testOutput - Force - ErrorAction SilentlyContinue
11
+ }
12
+
13
+ It " Can export all types" {
14
+ try
15
+ {
16
+ $fd | Export-FormatData - path $TESTDRIVE \allformat.ps1xml - IncludeScriptBlock
17
+
18
+ $sessionState = [System.Management.Automation.Runspaces.InitialSessionState ]::CreateDefault()
19
+ $sessionState.Formats.Clear ()
20
+ $sessionState.Types.Clear ()
21
+
22
+ $runspace = [System.Management.Automation.Runspaces.RunspaceFactory ]::CreateRunspace($sessionState )
23
+ $runspace.Open ()
24
+
25
+ $runspace.CreatePipeline (" Update-FormatData -AppendPath $TESTDRIVE \allformat.ps1xml" ).Invoke()
26
+ $actualAllFormat = $runspace.CreatePipeline (" Get-FormatData -TypeName *" ).Invoke()
27
+
28
+ $fd.Count | Should - Be $actualAllFormat.Count
29
+ Compare-Object $fd $actualAllFormat | Should - Be $null
30
+ }
31
+ finally
32
+ {
33
+ $runspace.Close ()
34
+ Remove-Item - Path $TESTDRIVE \allformat.ps1xml - Force - ErrorAction SilentlyContinue
35
+ }
36
+ }
37
+
38
+ It " Works with literal path" {
39
+ $filename = ' TestDrive:\[formats.ps1xml'
40
+ $fd | Export-FormatData - LiteralPath $filename
41
+ (Test-Path - LiteralPath $filename ) | Should - BeTrue
42
+ }
43
+
44
+ It " Should overwrite the destination file" {
45
+ $filename = ' TestDrive:\ExportFormatDataWithForce.ps1xml'
46
+ $unexpected = " SHOULD BE OVERWRITTEN"
47
+ $unexpected | Out-File - FilePath $filename - Force
48
+ $file = Get-Item $filename
49
+ $file.IsReadOnly = $true
50
+ $fd | Export-FormatData - Path $filename - Force
51
+
52
+ $actual = @ (Get-Content $filename )[0 ]
53
+ $actual | Should -Not - Be $unexpected
54
+ }
55
+
56
+ It " should not overwrite the destination file with NoClobber" {
57
+ $filename = " TestDrive:\ExportFormatDataWithNoClobber.ps1xml"
58
+ $fd | Export-FormatData - LiteralPath $filename
59
+
60
+ { $fd | Export-FormatData - LiteralPath $filename - NoClobber } | Should - Throw - ErrorId ' NoClobber,Microsoft.PowerShell.Commands.ExportFormatDataCommand'
61
+ }
62
+
63
+ It " Test basic functionality" {
64
+ Export-FormatData - InputObject $fd [0 ] - Path $testOutput
65
+ $content = Get-Content $testOutput - Raw
9
66
$formatViewDefinition = $fd [0 ].FormatViewDefinition
10
67
$typeName = $fd [0 ].TypeName
11
68
$content.Contains ($typeName ) | Should - BeTrue
@@ -14,33 +71,86 @@ Describe "Export-FormatData DRT Unit Tests" -Tags "CI" {
14
71
$content.Contains ($formatViewDefinition [$i ].Name) | Should - BeTrue
15
72
}
16
73
}
17
- }
18
74
19
- Describe " Export-FormatData" - Tags " CI" {
75
+ It " Should have a valid xml tag at the start of the file" {
76
+ $fd | Export-FormatData - Path $testOutput
77
+ $piped = Get-Content $testOutput - Raw
78
+ $piped [0 ] | Should - BeExactly " <"
79
+ }
20
80
21
- $testOutput = Join-Path - Path $TestDrive - ChildPath " outputfile"
81
+ It " Should pretty print xml output" {
82
+ $xmlContent = @"
83
+ <Configuration>
84
+ <ViewDefinitions>
85
+ <View>
86
+ <Name>ExportFormatDataName</Name>
87
+ <ViewSelectedBy>
88
+ <TypeName>ExportFormatDataTypeName</TypeName>
89
+ </ViewSelectedBy>
90
+ <TableControl>
91
+ <TableHeaders />
92
+ <TableRowEntries>
93
+ <TableRowEntry>
94
+ <TableColumnItems>
95
+ <TableColumnItem>
96
+ <PropertyName>Guid</PropertyName>
97
+ </TableColumnItem>
98
+ </TableColumnItems>
99
+ </TableRowEntry>
100
+ </TableRowEntries>
101
+ </TableControl>
102
+ </View>
103
+ </ViewDefinitions>
104
+ </Configuration>
<
F438
/code>
105
+ "@
106
+ $expected = @"
107
+ <?xml version="1.0" encoding="utf-8"?>
108
+ <Configuration>
109
+ <ViewDefinitions>
110
+ <View>
111
+ <Name>ExportFormatDataName</Name>
112
+ <ViewSelectedBy>
113
+ <TypeName>ExportFormatDataTypeName</TypeName>
114
+ </ViewSelectedBy>
115
+ <TableControl>
116
+ <TableHeaders />
117
+ <TableRowEntries>
118
+ <TableRowEntry>
119
+ <TableColumnItems>
120
+ <TableColumnItem>
121
+ <PropertyName>Guid</PropertyName>
122
+ </TableColumnItem>
123
+ </TableColumnItems>
124
+ </TableRowEntry>
125
+ </TableRowEntries>
126
+ </TableControl>
127
+ </View>
128
+ </ViewDefinitions>
129
+ </Configuration>
130
+ "@ -replace " `r`n ?|`n " , " "
131
+ try
132
+ {
133
+ $testfilename = [guid ]::NewGuid().ToString(' N' )
134
+ $testfile = Join-Path - Path $TestDrive - ChildPath " $testfilename .ps1xml"
135
+ Set-Content - Path $testfile - Value $xmlContent
22
136
23
- AfterEach {
24
- Remove-Item $testOutput - Force - ErrorAction SilentlyContinue
25
- }
137
+ $sessionState = [ System.Management.Automation.Runspaces.InitialSessionState ]::CreateDefault()
138
+ $sessionState .Formats.Clear ()
139
+ $sessionState .Types.Clear ()
26
140
27
- Context " Check Export-FormatData can be called validly." {
28
- It " Should be able to be called without error" {
29
- { Get-FormatData | Export-FormatData - Path $testOutput } | Should -Not - Throw
30
- }
31
- }
141
+ $runspace = [System.Management.Automation.Runspaces.RunspaceFactory ]::CreateRunspace($sessionState )
142
+ $runspace.Open ()
32
143
33
- Context " Check that the output is in the correct format" {
34
- It " Should not return an empty xml file" {
35
- Get-FormatData | Export-FormatData - Path $testOutput
36
- $piped = Get-Content $testOutput
37
- $piped | Should -Not - BeNullOrEmpty
38
- }
144
+ $runspace.CreatePipeline (" Update-FormatData -prependPath $testfile " ).Invoke()
145
+ $runspace.CreatePipeline (" Get-FormatData -TypeName 'ExportFormatDataTypeName' | Export-FormatData -Path $testOutput " ).Invoke()
39
146
40
- It " Should have a valid xml tag at the start of the file" {
41
- Get-FormatData | Export-FormatData - Path $testOutput
42
- $piped = Get-Content $testOutput
43
- $piped [0 ] | Should - BeExactly " <"
44
- }
147
+ $content = (Get-Content $testOutput - Raw) -replace " `r`n ?|`n " , " "
148
+
149
+ $content | Should - BeExactly $expected
150
+ }
151
+ finally
152
+ {
153
+ $runspace.Close ()
154
+ }
45
155
}
46
156
}
0 commit comments