test/scaffolds/Install-Template.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
using module ..\.\TestRunnerSupportModule.psm1

Describe "Test Install-Template" {
    BeforeAll {
        $TestSupportModule = [TestRunnerSupportModule]::new('MockModuleB')

        $AlicesTemplates = New-Item -Path (Join-Path $TestSupportModule.TestDrivePath "AlicesTemplates") -ItemType Directory

        Copy-Item -Path (Join-Path -Path $TestSupportModule.FixtureDirectoryPath -ChildPath 'resources\templates\NewModuleProject') -Destination $AlicesTemplates -Recurse
        $MockNewModuleProjectPath = Join-Path -Path $AlicesTemplates -ChildPath 'NewModuleProject\plasterManifest_en-US.xml'
   
        Copy-Item -Path (Join-Path -Path $TestSupportModule.FixtureDirectoryPath -ChildPath 'resources\templates\NewScript') -Destination $AlicesTemplates -Recurse
        $MockNewScriptTemplatePath = Join-Path -Path $AlicesTemplates -ChildPath 'NewScript\plasterManifest_en-US.xml'

        Push-Location -StackName 'InstallTemplateTest'
    }
    
    AfterAll {
        Pop-Location -StackName 'InstallTemplateTest'

        $TestSupportModule.Teardown()
    }
    
    Context "Call Install-Template with built-in template 'NewModuleProject' and then with 'NewScript'" {

        Set-Location $TestSupportModule.TestDrivePath

        Mock Add-ModuleToProfile {} -ModuleName PowerBolt

        Install-Template -TemplateName 'NewModuleProject' '.' 'MockModuleC' 'Alice' 

        $MockModuleCPath = (Join-Path -Path '.' -ChildPath 'MockModuleC' -Resolve)

        Assert-MockCalled Add-ModuleToProfile -ModuleName PowerBolt -Times 1 -ParameterFilter {
            $Path -eq $MockModuleCPath
        }

        $ScaffoldModuleFolder = Join-Path -Path $TestSupportModule.TestDrivePath -ChildPath 'MockModuleC'
        $ScaffoldManifestPath = Join-Path $ScaffoldModuleFolder -ChildPath 'MockModuleC.psd1'
        $ScaffoldRootModulePath = Join-Path $ScaffoldModuleFolder -ChildPath 'MockModuleC.psm1'

        It "Should scaffold module folder" {
            $ScaffoldModuleFolder | Should -Exist 
        }

        It "Should scaffold manifest and root module" {
            $ScaffoldManifestPath | Should -FileContentMatch ([regex]::Escape('Alice'))
            $ScaffoldManifestPath | Should -FileContentMatch ([regex]::Escape('MockModuleC'))
            $ScaffoldManifestPath | Should -FileContentMatch ([regex]::Escape('0.0.1'))

            $ScaffoldRootModulePath | Should -Exist 
        }

        Set-Location $ScaffoldModuleFolder

        Install-Template -TemplateName 'NewScript' 'utils/io' 'Get-FileExtension'

        $GetFileExtensionSrcPath = Join-Path -Path '.' -ChildPath 'src/utils/io/Get-FileExtension.ps1'
        $GetFileExtensionTestPath = Join-Path -Path '.' -ChildPath 'test/utils/io/Get-FileExtension.Tests.ps1'
        
        It "Should scaffold files to correct locations" {
            $GetFileExtensionSrcPath | Should -Exist 
            $GetFileExtensionTestPath | Should -Exist 
        }

        It "Should modify the scaffold src file as expected" {
            $GetFileExtensionSrcPath | Should -FileContentMatch ([regex]::Escape('function Get-FileExtension'))
        }

        It "Should modify the scaffold test file as expected" {
            $GetFileExtensionTestPath | Should -FileContentMatch ([regex]::Escape("Describe 'Test Get-FileExtension'"))
        }
    }
    
    Context "Call Install-Template with explict paths to templates" {

        Set-Location $TestSupportModule.TestDrivePath
        Install-Template -TemplatePath $MockNewModuleProjectPath  '.' 'MockModuleC' 'Alice' 

        $ScaffoldModuleFolder = Join-Path -Path $TestSupportModule.TestDrivePath -ChildPath 'MockModuleC'
        $ScaffoldManifestPath = Join-Path $ScaffoldModuleFolder -ChildPath 'MockModuleC.psd1'
        $ScaffoldRootModulePath = Join-Path $ScaffoldModuleFolder -ChildPath 'MockModuleC.psm1'

        It "Should scaffold module folder" {
            $ScaffoldModuleFolder | Should -Exist 
        }

        It "Should scaffold manifest and root module" {
            $ScaffoldManifestPath | Should -FileContentMatch ([regex]::Escape('Alice'))
            $ScaffoldManifestPath | Should -FileContentMatch ([regex]::Escape('MockModuleC'))
            $ScaffoldManifestPath | Should -FileContentMatch ([regex]::Escape('0.0.1'))

            $ScaffoldRootModulePath | Should -Exist 
        }

        Set-Location $ScaffoldModuleFolder
        Install-Template -TemplatePath $MockNewScriptTemplatePath 'utils/io' 'Get-FileExtension'

        $GetFileExtensionSrcPath = Join-Path -Path '.' -ChildPath 'src/utils/io/Get-FileExtension.ps1'
        $GetFileExtensionTestPath = Join-Path -Path '.' -ChildPath 'test/utils/io/Get-FileExtension.Tests.ps1'
        
        It "Should scaffold files to correct locations" {
            $GetFileExtensionSrcPath | Should -Exist 
            $GetFileExtensionTestPath | Should -Exist 
        }

        It "Should modify the scaffold src file as expected" {
            $GetFileExtensionSrcPath | Should -FileContentMatch ([regex]::Escape('function Get-FileExtension'))
        }

        It "Should modify the scaffold test file as expected" {
            $GetFileExtensionTestPath | Should -FileContentMatch ([regex]::Escape("Describe 'Test Get-FileExtension'"))
        }
    }
}