test/settings/Set-PowerBoltSetting.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
using module ..\.\TestRunnerSupportModule.psm1

Describe "Test Set-PowerBoltSetting" {
    BeforeAll {
        $TestSupportModule = [TestRunnerSupportModule]::new()
    }
    
    AfterAll {
        $TestSupportModule.Teardown()
    }
    
    Context "Setting TurnOnAutoUpdateSemVer" {

        # Mock Restore-RememberLastLocation {} -ModuleName PowerBolt

        It "Should set TurnOnAutoUpdateSemVer to '<Value>' in config file" -TestCases @(
            @{ Value = $false}
            @{ Value = $true}
        ) {
            Param($Value)

            Set-PowerBoltSetting -Name 'TurnOnAutoUpdateSemVer' -Value $Value
            
            $MKPowerShellConfig = Get-Content -Path $TestSupportModule.FixtureConfigFilePath | ConvertFrom-Json -AsHashtable
            $MKPowerShellConfig["TurnOnAutoUpdateSemVer"] -eq $true | Should -Be $Value

            # Assert-MockCalled Restore-RememberLastLocation -ModuleName PowerBolt -Times 1
        }
    } 
    
    Context "Setting TurnOnQuickRestart" {

        It "Should set TurnOnQuickRestart to '<Value>' in config file and change alias accordingly" -TestCases @(
            @{ Value = $false}
            @{ Value = $true}
        ) {
            Param($Value)

            Set-PowerBoltSetting -Name 'TurnOnQuickRestart' -Value $Value

            $MKPowerShellConfig = Get-Content -Path $TestSupportModule.FixtureConfigFilePath | ConvertFrom-Json -AsHashtable
            $MKPowerShellConfig["TurnOnQuickRestart"] -eq $true | Should -Be $Value

            $PWSHSet = Get-Alias pwsh -Scope Global -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Definition
            $PWSHASet = Get-Alias pwsha -Scope Global -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Definition

            if ($Value) {
                $PWSHSet | Should -Be 'Restart-PWSH'
                $PWSHASet | Should -Be 'Restart-PWSHAdmin'
            }
            else {
                $PWSHSet | Should -BeNullOrEmpty
                $PWSHASet | Should -BeNullOrEmpty
            } 
        } -Skip
    }

    Context "Setting Backups" {
        $Value1 = @{
            Path         = "'$PROFILE'"
            Destination  = 'C:\Google Drive\Documents\PowerShell\'
            UpdatePolicy = 'Overwrite'
        }

        It "Should set Backups to valid hashtables in config file." -TestCases @(
            @{ Value = $Value1 }
        ) {
            Param($Value)

            Set-PowerBoltSetting -Name 'Backups' -Value $Value

            $MKPowerShellConfig = Get-Content -Path $TestSupportModule.FixtureConfigFilePath | ConvertFrom-Json -AsHashtable
            $MKPowerShellConfig.Backups.Path | Should -BeLike "'$PROFILE'"
            $MKPowerShellConfig.Backups.Destination | Should -Be $Value.Destination 
        } -Skip

        It "Should remove strings that have single quotes from config file." {
            $MKPowerShellConfig = Get-Content -Path $TestSupportModule.FixtureConfigFilePath | ConvertFrom-Json -AsHashtable
            $SingleQuotedPath = $MKPowerShellConfig.Backups.Path
            Test-Path $SingleQuotedPath | Should -Be $false
            $NoQuotedPath = $SingleQuotedPath -replace "\'", ""
            Test-Path $NoQuotedPath | Should -Be $true
        } -Skip
    }
}