ScriptCop.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
#requires -Module Pester, ScriptCop

describe ScriptCop {
    context 'Static Analysis' {
        it 'Can enforce rules' {
            function foo {

            }

            Get-command foo |
                Test-Command -Rule Test-CommandNamingConvention |
                Select-Object -ExpandProperty Problem |
                should -BeLike *StandardVerb-CustomNoun*
        }

        it 'Can exlcude rules' {
            function foo {

            }

            Get-command foo |
                Test-Command -Rule Test-CommandNamingConvention -ExcludedRule Test-CommandNamingConvention |
                should -be $null
        }

        it 'Can run groups of rules, or Patrols' {
            function foo {
            }

            Get-Command foo |
                Test-Command -Patrol Test-Documentation | # Test foo
                    Select-Object -ExpandProperty Problem | # extract the problems
                        Where-Object { $_ -like '*example*' } |
                            should -BeLike *example* # some problems should complain about examples.
        }
    }

    context 'Auto-Repair' {
        it 'Will automatically fix some problems' {
            $tempModulePsd1 = @'
@{
    ModuleVersion = '0.1'
    Description = 'A Module'
}
'@


            $tempModuleName = "TempModule$(Get-Random)"

            $tempModuleRoot =
                if ($PSVersionTable.OS -and $PSVersionTable.OS -notlike '*Windows*') {
                    Join-Path '/tmp' $tempModuleName
                } else {
                    Join-Path $env:Temp $tempModuleName
                }

            $tempModulePath = Join-Path $tempModuleRoot "$tempModuleName.psd1"
            $null = New-item -ItemType File -Force -Path $tempModulePath
            $tempModulePsd1 | Set-Content $tempModulePath

            $importedModule = Import-Module $tempModulePath -Force -PassThru
            $importedModule|
                Test-Command |
                Repair-Command -WarningAction SilentlyContinue

            $newText = [IO.File]::ReadAllText($tempModulePath) # | should -Not -Be $tempModulePsd1
            if ($newText -eq $tempModulePath) {
                throw "Expected Repair-Command to change things. $tempModulePath was unchanged."
            }
            $importedModule | Remove-Module
            Remove-Item $tempModulePath -Recurse -Force
        }
    }


    context 'Dynamic Registration' {
        it 'Can remove a registered rule' {
            Unregister-ScriptCopRule -Name Test-DocumentationQuality
            $(Get-ScriptCopRule |
                Where-Object Name -EQ Test-DocumentationQuality) | should -Be $null
        }

        it 'Can remove a registered fixer' {
            Unregister-ScriptCopFixer -Name Repair-ModuleManifest
            $(Get-ScriptCopFixer |
                Where-Object Name -EQ Repair-ModuleManifest) | should -Be $null

        }

        it 'Can remove a registered patrol' {
            Unregister-ScriptCopPatrol -Name Test-Documentation
            $(Get-ScriptCopPatrol |
                Where-Object Name -EQ Test-Documentation) | should -Be $null
        }
    }

    context 'Fault tolerance' {
        it 'Will not let just any script be a ScriptCop Rule' {
            {
                function foo {
                }

                Get-Command foo | Register-ScriptCopRule -ErrorAction Stop
            } | should -Throw
        }

        it 'Will not let just any script be a ScriptCop Fixer' {
            {
                function foo {
                }

                Get-Command foo | Register-ScriptCopFixer -ErrorAction Stop
            } | should -Throw
        }
    }


    context 'Self-Testing' {
        it 'Can reload and test itself' {
            $theModule = Get-Module ScriptCop
            $theModuleRoot = $theModule.Path.Substring(0, $theModule.Path.LastIndexOf([IO.Path]::DirectorySeparatorChar))
            $theModuleRoot = $theModuleRoot + [IO.Path]::DirectorySeparatorChar + 'ScriptCop.psd1'
            Import-Module $theModuleRoot -Global -Force -PassThru |
                   Test-Module -GetCommandCoverage
        }
    }

}