Pentia.Publish-WebProject.Tests.ps1

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
# Requires https://github.com/pester/Pester: Install-Module Pester -Force -SkipPublisherCheck
Import-Module "$PSScriptRoot\..\Pentia.UserSettings\Pentia.UserSettings.psm1" -Force
Import-Module "$PSScriptRoot\..\Pentia.Invoke-MSBuild\Pentia.Invoke-MSBuild.psm1" -Force
Import-Module "$PSScriptRoot\..\Pentia.Invoke-ConfigurationTransform\Pentia.Invoke-ConfigurationTransform.psm1" -Force
Import-Module "$PSScriptRoot\Pentia.Publish-WebProject.psm1" -Force
Import-Module "$PSScriptRoot\..\TestContent\TestSolution\New-TestSolution.psm1" -Force

Describe "Publish-WebProject" {

    $ProjectLayerWebProjectFilePath = "\src\Project\WebProject\Code\Project.WebProject.csproj"
    $FeatureLayerWebProjectFilePath = "\src\Feature\WebProject\Code\Feature.WebProject.csproj"
    $FoundationLayerWebProjectFilePath = "\src\Foundation\WebProject\Code\Foundation.WebProject.csproj"
    $PublishWebsitePath = "$TestDrive\Website"
    
    It "should create the output directory if it doesn't exist" {
        # Arrange
        $solutionRootPath = New-TestSolution -TempPath "$TestDrive"
        $projectFilePath = $solutionRootPath + $ProjectLayerWebProjectFilePath

        # Act
        Publish-WebProject -WebProjectFilePath $projectFilePath -OutputPath $PublishWebsitePath

        # Assert
        Test-Path $PublishWebsitePath | Should Be $true
    }

    It "should publish a web project to the target directory" {
        # Arrange
        $PublishUnConfigeredWebsitePath = "$TestDrive\UnConfigeredWebsite"
        $solutionRootPath = New-TestSolution -TempPath "$TestDrive"
        $projectFilePath = $solutionRootPath + $ProjectLayerWebProjectFilePath

        # Act
        Publish-WebProject -WebProjectFilePath $projectFilePath -OutputPath $PublishUnConfigeredWebsitePath

        # Assert
        $publishedFiles = Get-ChildItem $PublishUnConfigeredWebsitePath -Recurse -File | Select-Object -ExpandProperty Name
        $publishedFiles -contains "Project.WebProject.dll" | Should Be $true
        $publishedFiles -contains "Project.WebProject.pdb" | Should Be $true
    }

    It "should publish a web project to the target directory using the alias Publish-UnconfiguredWebProject" {
        # Arrange
        $solutionRootPath = New-TestSolution -TempPath "$TestDrive"
        $projectFilePath = $solutionRootPath + $ProjectLayerWebProjectFilePath

        # Act
        Publish-UnconfiguredWebProject -WebProjectFilePath $projectFilePath -OutputPath $PublishWebsitePath

        # Assert
        $publishedFiles = Get-ChildItem $PublishWebsitePath -Recurse -File | Select-Object -ExpandProperty Name
        $publishedFiles -contains "Project.WebProject.dll" | Should Be $true
        $publishedFiles -contains "Project.WebProject.pdb" | Should Be $true
    }

    It "should respect Build Actions of XDT files" {
        # Arrange
        $solutionRootPath = New-TestSolution -TempPath "$TestDrive"
        $projectFilePath = $solutionRootPath + $ProjectLayerWebProjectFilePath
        [xml]$projectFileXml = Get-Content -Path $projectFilePath
        $contentFiles = $projectFileXml.SelectNodes("//*[local-name()='Content']/@Include") | Select-Object -ExpandProperty "Value"
        
        # Act
        Publish-WebProject -WebProjectFilePath $projectFilePath -OutputPath $PublishWebsitePath
        
        # Assert
        $contentFiles.Count | Should BeGreaterThan 0 "Didn't find any files with Build Action 'Content'."
        $publishedFiles = Get-ChildItem $PublishWebsitePath -Recurse -File | Select-Object -ExpandProperty Name
        foreach ($contentFile in $contentFiles) {
            $publishedFiles -contains $contentFile | Should Be $true "Didn't find file '$contentFile' in publish output."
        }
    }
    
    It "should throw an exception when a project fails to publish" {
        # Arrange
        $solutionRootPath = New-TestSolution -TempPath "$TestDrive"
        $projectFilePath = $solutionRootPath + $FoundationLayerWebProjectFilePath
        Remove-Item -Path ($solutionRootPath + "\src\Foundation\WebProject\Code\Web.Foundation.WebProject.Debug.config") -ErrorAction Stop
        
        # Act
        $publishWebProject = { Publish-WebProject -WebProjectFilePath $projectFilePath -OutputPath $PublishWebsitePath }

        # Assert
        $publishWebProject | Should Throw
    }

    It "should not apply any XDTs during publish" {
        # Arrange
        $solutionRootPath = New-TestSolution -TempPath "$TestDrive"
        $projectFilePath = $solutionRootPath + $FeatureLayerWebProjectFilePath

        # Act
        Publish-WebProject -WebProjectFilePath $projectFilePath -OutputPath $PublishWebsitePath

        # Assert
        $webConfigContent = Get-Content "$PublishWebsitePath\App_Config\Include\Feature.WebProject.Pipelines.config" -ErrorAction Stop | Out-String
        $webConfigContent | Should Not Match "Feature\.WebProject\.Pipelines\.Debug"
        $webConfigContent | Should Not Match "Feature\.WebProject\.Pipelines\.Release"
    }

    It "should publish projects relative to current working directory" {
        # Arrange
        $solutionRootPath = New-TestSolution -TempPath "$TestDrive"
        $projectFilePath = $solutionRootPath + $FeatureLayerWebProjectFilePath

        # Act
        Push-Location $TestDrive
        try {
            Publish-WebProject -WebProjectFilePath $projectFilePath -OutputPath ".\output"
        }
        finally {
            Pop-Location
        }

        # Assert
        Get-ChildItem "$TestDrive\output\" -Recurse | Measure-Object | Select-Object -ExpandProperty Count | Should BeGreaterThan 0
    }
}

InModuleScope "Pentia.Publish-WebProject" {

    Describe "Find-SolutionRootPath" {
        It "should not create an infinite loop" {
            # Arrange
            $searchStartPath = $TestDrive

            # Act
            $solutionRootPath = Find-SolutionRootPath -SearchStartPath $searchStartPath

            # Assert
            $solutionRootPath | Should Be $null
        }

        It "should find the solution root path when starting in the solution root folder" {
            # Arrange
            New-Item "$TestDrive/.pentia" -ItemType Directory -Force
            Set-Content "$TestDrive/.pentia/user-settings.json" -Value "{""buildConfiguration"":""Debug""}" -Force
            $searchStartPath = "$TestDrive"

            # Act
            $solutionRootPath = Find-SolutionRootPath -SearchStartPath $searchStartPath

            # Assert
            $solutionRootPath | Should Be "$TestDrive"
        }

        It "should find the solution root path when starting in a solution subfolder" {
            # Arrange
            New-Item "$TestDrive/subfolder" -ItemType Directory -Force
            New-Item "$TestDrive/.pentia" -ItemType Directory -Force
            Set-Content "$TestDrive/.pentia/user-settings.json" -Value "{""buildConfiguration"":""Debug""}" -Force
            $searchStartPath = "$TestDrive/subfolder"

            # Act
            $solutionRootPath = Find-SolutionRootPath -SearchStartPath $searchStartPath

            # Assert
            $solutionRootPath | Should Be "$TestDrive"
        }

        It "should find the solution root path when the search start path is a relative path" {
            # Arrange
            New-Item "$TestDrive/subfolder" -ItemType Directory -Force
            New-Item "$TestDrive/.pentia" -ItemType Directory -Force
            Set-Content "$TestDrive/.pentia/user-settings.json" -Value "{""buildConfiguration"":""Debug""}" -Force
            $searchStartPath = "$TestDrive/subfolder"

            # Act
            Push-Location $searchStartPath
            try {
                $solutionRootPath = Find-SolutionRootPath -SearchStartPath "."
            }
            finally {
                Pop-Location                
            }

            # Assert
            $solutionRootPath | Should Be "$TestDrive"
        }
    }

}

Describe "Publish-ConfiguredWebProject" {
    $FeatureLayerWebProjectFilePath = "\src\Feature\WebProject\Code\Feature.WebProject.csproj"
    $PublishWebsitePath = "$TestDrive\Website"

    function New-UserSettings {
        param (
            [Parameter(Mandatory = $true)]
            [string]$SolutionRootPath,

            [Parameter(Mandatory = $false)]
            [string]$BuildConfiguration = "Debug"            
        )
        New-Item -Path "$SolutionRootPath\.pentia" -ItemType Directory -Force | Out-Null
        $settings = @{
            "webrootOutputPath"  = "$PublishWebsitePath";
            "dataOutputPath"     = "$PublishWebsitePath\Data";
            "buildConfiguration" = "$BuildConfiguration";
        }
        $settings | ConvertTo-Json -Depth 100 | Out-File -FilePath "$SolutionRootPath\.pentia\user-settings.json"
    }

    function New-Solution {
        $solutionRootPath = New-TestSolution -TempPath "$TestDrive"
        New-UserSettings -SolutionRootPath $solutionRootPath
        New-Item -Path "$TestDrive\Website\Web.config" -Force | Set-Content -Value "<?xml version=""1.0"" encoding=""utf-8""?><configuration></configuration>" -Force
        $solutionRootPath
    }

    It "should apply XDTs" {
        # Arrange
        $solutionRootPath = New-Solution
        $projectFilePath = $solutionRootPath + $FeatureLayerWebProjectFilePath

        # Act
        Publish-ConfiguredWebProject -WebProjectFilePath $projectFilePath -WebrootOutputPath $PublishWebsitePath -BuildConfiguration "Debug"

        # Assert
        $webConfigContent = Get-Content "$PublishWebsitePath\App_Config\Include\Feature.WebProject.Pipelines.config" -ErrorAction Stop | Out-String
        $webConfigContent | Should Match "Feature\.WebProject\.Pipelines\.Debug"
        $webConfigContent | Should Not Match "Feature\.WebProject\.Pipelines\.Release"
    }

    It "should delete XDTs after applying them" {
        # Arrange
        $solutionRootPath = New-Solution
        $projectFilePath = $solutionRootPath + $FeatureLayerWebProjectFilePath

        # Act
        Publish-ConfiguredWebProject -WebProjectFilePath $projectFilePath -WebrootOutputPath $PublishWebsitePath -BuildConfiguration "Debug"

        # Assert
        "$TestDrive\App_Config\Include\Feature.WebProject.Pipelines.Debug.config" | Should Not Exist
        "$TestDrive\App_Config\Include\Feature.WebProject.Pipelines.Release.config" | Should Not Exist
    }

    It "should use user settings as fallback parameters when available" {
        # Arrange
        $solutionRootPath = New-Solution
        New-UserSettings -SolutionRootPath $solutionRootPath -BuildConfiguration "Release"
        $projectFilePath = $solutionRootPath + $FeatureLayerWebProjectFilePath

        # Act
        Publish-ConfiguredWebProject -WebProjectFilePath $projectFilePath

        # Assert
        $webConfigContent = Get-Content "$PublishWebsitePath\App_Config\Include\Feature.WebProject.Pipelines.config" -ErrorAction Stop | Out-String
        $webConfigContent | Should Not Match "Feature\.WebProject\.Pipelines\.Debug"
        $webConfigContent | Should Match "Feature\.WebProject\.Pipelines\.Release"
    }
}