tests/dbatools.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
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
$Path = Split-Path -Parent $MyInvocation.MyCommand.Path
$ModulePath = (get-item $Path ).parent.FullName
$ModuleName = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -Replace ".Tests.ps1"
$ManifestPath   = "$ModulePath\$ModuleName.psd1"

Describe 'dbatools module test' -Tag 'Compliance' {
    Context 'Doing something awesome' {
        It 'It should have tests' {
            $true | Should be $true
        }
    }
}


Describe "$ModuleName Aliases" -tag Build , Aliases {
    ## Get the Aliases that should be set from the psm1 file

    $psm1 = Get-Content $ModulePath\$ModuleName.psm1 -Verbose
    $Matches = [regex]::Matches($psm1, "AliasName`"\s=\s`"(\w*-\w*)`"")
    $Aliases = $Matches.ForEach{$_.Groups[1].Value}

    foreach ($Alias in $Aliases) {
        Context "Testing $Alias Alias" {
            $Definition = (Get-Alias $Alias).Definition
            It "$Alias Alias should exist" {
                Get-Alias $Alias| Should Not BeNullOrEmpty
            }   
            It "$Alias Aliased Command $Definition Should Exist" {
                Get-Command $Definition -ErrorAction SilentlyContinue | Should Not BeNullOrEmpty
            }
        }
    }
}

# test the module manifest - exports the right functions, processes the right formats, and is generally correct
<#
Describe "Manifest" {
 
    $Manifest = $null
 
    It "has a valid manifest" {
 
        {
 
            $Script:Manifest = Test-ModuleManifest -Path $ManifestPath -ErrorAction Stop -WarningAction SilentlyContinue
 
        } | Should Not Throw
 
    }
## Should be fixed now - Until the issue with requiring full paths for required assemblies is resolved need to keep this commented out RMS 01112016
 
$Script:Manifest = Test-ModuleManifest -Path $ManifestPath -ErrorAction SilentlyContinue
    It "has a valid name" {
 
        $Script:Manifest.Name | Should Be $ModuleName
 
    }
 
 
 
 It "has a valid root module" {
 
        $Script:Manifest.RootModule | Should Be "$ModuleName.psm1"
 
    }
 
 
 
 It "has a valid Description" {
 
        $Script:Manifest.Description | Should Be 'Provides extra functionality for SQL Server Database admins and enables SQL Server instance migrations.'
 
    }
 
 It "has a valid Author" {
  $Script:Manifest.Author | Should Be 'Chrissy LeMaire'
 }
 
 It "has a valid Company Name" {
  $Script:Manifest.CompanyName | Should Be 'dbatools.io'
 }
    It "has a valid guid" {
 
        $Script:Manifest.Guid | Should Be '9d139310-ce45-41ce-8e8b-d76335aa1789'
 
    }
 It "has valid PowerShell version" {
  $Script:Manifest.PowerShellVersion | Should Be '3.0'
 }
 
 It "has valid required assemblies" {
  {$Script:Manifest.RequiredAssemblies -eq @()} | Should Be $true
 }
 
 It "has a valid copyright" {
 
  $Script:Manifest.CopyRight | Should BeLike '* Chrissy LeMaire'
 
 }
 
 
 
 # Don't want this just yet
 
 It 'exports all public functions' {
 
  $FunctionFiles = Get-ChildItem "$ModulePath\functions" -Filter *.ps1 | Select-Object -ExpandProperty BaseName
 
  $FunctionNames = $FunctionFiles
 
  $ExFunctions = $Script:Manifest.ExportedFunctions.Values.Name
  $ExFunctions
  foreach ($FunctionName in $FunctionNames)
 
  {
 
   $ExFunctions -contains $FunctionName | Should Be $true
 
  }
 
 }
}
#>