test/profile/Add-ModuleToProfile.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
using module ..\.\TestRunnerSupportModule.psm1

Describe "Test Add-ModuleToProfile" {
    BeforeAll {
        $TestSupportModule = [TestRunnerSupportModule]::new('MockModuleA')
        $MockProfilePath = Join-Path -Path ($TestSupportModule.TestDrivePath) -ChildPath "\User\Bob\Documents\PowerShell\" -AdditionalChildPath "MK.PowerShell-profile.ps1"
        New-Item -Path $MockProfilePath -ItemType File -Force | Select-Object -ExpandProperty FullName
    }
    
    AfterAll {
        $TestSupportModule.Teardown()
    }
    
    Context "Appending by importing module to profile" {

        It "Should add to profile" {

            $Before = Get-Content -Path $MockProfilePath
            $Before.Count | Should -BeExactly 0

            Add-ModuleToProfile -Path 'TestDrive:\MockModuleA' -ProfilePath $MockProfilePath

            [string]$After = Get-Content -Path $MockProfilePath -Raw
            $After | Should -Match 'Import-Module.*MockModuleA'
        }
        
        It "Should append to profile" {
            Set-Content -Path $MockProfilePath -Value ("Import-Module C:\temp\non\existing\NoModule") -NoNewline
            
            [string]$Before = Get-Content -Path $MockProfilePath -Raw
            $Before | Should -Match '^Import-Module C\:\\temp\\non\\existing\\NoModule$'

            Add-ModuleToProfile -Path 'TestDrive:\MockModuleA' -ProfilePath $MockProfilePath

            [string]$After = Get-Content -Path $MockProfilePath -Raw
            $After | Should -Match 'Import-Module.*MockModuleA'
        }
    }
}