Skip to content

Commit 6b8f5a5

Browse files
committed
Merge branch 'pr89' into development
* pr89: (GH-71) Implemented cChocoFeature
2 parents c7d4976 + 5b57ef4 commit 6b8f5a5

File tree

6 files changed

+287
-2
lines changed

6 files changed

+287
-2
lines changed

.gitIgnore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.mof
2-
!*.schema.mof
2+
!*.schema.mof
3+
TestsResults.xml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Copyright (c) 2017 Chocolatey Software, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
<#
16+
.Description
17+
Returns the configuration for cChocoFeature.
18+
19+
.Example
20+
Get-TargetResource -FeatureName allowGlobalConfirmation -Ensure 'Present'
21+
#>
22+
function Get-TargetResource
23+
{
24+
[CmdletBinding()]
25+
[OutputType([System.Collections.Hashtable])]
26+
param
27+
(
28+
[parameter(Mandatory = $true)]
29+
[System.String]
30+
$FeatureName,
31+
32+
[ValidateSet('Present','Absent')]
33+
[System.String]
34+
$Ensure='Present'
35+
)
36+
37+
Write-Verbose "Starting cChocoFeature Get-TargetResource - Feature Name: $FeatureName, Ensure: $Ensure"
38+
39+
$returnValue = @{
40+
FeatureName = $FeatureName
41+
Ensure = $Ensure
42+
}
43+
44+
$returnValue
45+
46+
}
47+
48+
<#
49+
.Description
50+
Performs the set for the cChocoFeature resource.
51+
52+
.Example
53+
Get-TargetResource -FeatureName allowGlobalConfirmation -Ensure 'Present'
54+
55+
#>
56+
function Set-TargetResource
57+
{
58+
[CmdletBinding(SupportsShouldProcess=$true)]
59+
param
60+
(
61+
[parameter(Mandatory = $true)]
62+
[System.String]
63+
$FeatureName,
64+
65+
[ValidateSet('Present','Absent')]
66+
[string]
67+
$Ensure='Present'
68+
)
69+
70+
71+
Write-Verbose "Starting cChocoFeature Set-TargetResource - Feature Name: $FeatureName, Ensure: $Ensure."
72+
73+
if ($pscmdlet.ShouldProcess("Choco feature $FeatureName will be ensured $Ensure."))
74+
{
75+
if ($Ensure -eq 'Present')
76+
{
77+
Write-Verbose "Enabling choco feature $FeatureName."
78+
choco feature enable -n $FeatureName
79+
}
80+
else
81+
{
82+
Write-Verbose "Disabling choco feature $FeatureName."
83+
choco feature disable -n $FeatureName
84+
}
85+
}
86+
87+
}
88+
89+
<#
90+
.Description
91+
Performs the test for cChocoFeature.
92+
93+
.Example
94+
Test-TargetResource -FeatureName allowGlobalConfirmation -Ensure 'Present'
95+
#>
96+
function Test-TargetResource
97+
{
98+
[CmdletBinding()]
99+
[OutputType([System.Boolean])]
100+
param
101+
(
102+
[parameter(Mandatory = $true)]
103+
[System.String]
104+
$FeatureName,
105+
106+
[ValidateSet('Present','Absent')]
107+
[System.String]
108+
$Ensure='Present'
109+
)
110+
111+
Write-Verbose "Starting cChocoFeature Test-TargetResource - Feature Name: $FeatureName, Ensure: $Ensure."
112+
113+
$result = $false
114+
$feature = Get-ChocoFeature -FeatureName $FeatureName | Where-Object {$_.State -eq "Enabled"}
115+
116+
if (($Ensure -eq 'Present' -and ([bool]$feature)) -or ($Ensure -eq 'Absent' -and !([bool]$feature)))
117+
{
118+
Write-Verbose "Test-TargetResource is true, $FeatureName is $Ensure."
119+
$result = $true
120+
}
121+
else
122+
{
123+
Write-Verbose "Test-TargetResource is false, $FeatureName is not $Ensure."
124+
}
125+
126+
return $result
127+
128+
}
129+
130+
<#
131+
.Description
132+
Query chocolatey features.
133+
#>
134+
function Get-ChocoFeature
135+
{
136+
[OutputType([PSCustomObject])]
137+
param(
138+
[string]
139+
$FeatureName
140+
)
141+
choco feature -r | ConvertFrom-Csv -Delimiter "|" -Header Name, State, Description | Where-Object {$_.Name -eq $FeatureName}
142+
}
143+
144+
145+
146+
147+
Export-ModuleMember -Function *-TargetResource
148+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
[ClassVersion("1.0.0.0"), FriendlyName("cChocoFeature")]
3+
class cChocoFeature : OMI_BaseResource
4+
{
5+
[Key] String FeatureName;
6+
[Write,ValueMap{"Present", "Absent"},Values{"Present", "Absent"}] String Ensure;
7+
};
8+

Examples/cChocoFeatureExample.ps1

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) 2017 Chocolatey Software, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
configuration ChocoFeatures {
17+
18+
Import-DscResource -ModuleName cChoco
19+
20+
Node 'localhost' {
21+
22+
cChocoFeature allowGlobalConfirmation {
23+
24+
FeatureName = "allowGlobalConfirmation"
25+
Ensure = 'Present'
26+
27+
}
28+
29+
cChocoFeature powershellHost {
30+
31+
FeatureName = "powershellHost"
32+
Ensure = 'Absent'
33+
}
34+
}
35+
36+
}
37+
38+
39+
$config = ChocoFeatures
40+
41+
Start-DscConfiguration -Path $config.psparentpath -Wait -Verbose -Force

Tests/cChocoFeature_Tests.ps1

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright (c) 2017 Chocolatey Software, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
$ResourceName = ((Split-Path $MyInvocation.MyCommand.Path -Leaf) -split '_')[0]
17+
$ResourceFile = (Get-DscResource -Name $ResourceName).Path
18+
19+
$TestsPath = (split-path -path $MyInvocation.MyCommand.Path -Parent)
20+
$ResourceFile = Get-ChildItem -Recurse $TestsPath\.. -File | Where-Object {$_.name -eq "$ResourceName.psm1"}
21+
22+
Import-Module -Name $ResourceFile.FullName
23+
24+
25+
#---------------------------------#
26+
# Pester tests for cChocoInstall #
27+
#---------------------------------#
28+
Describe "Testing cChocoFeature" {
29+
30+
Context "Test-TargetResource" {
31+
32+
mock -ModuleName cChocoFeature -CommandName Get-ChocoFeature -MockWith {
33+
@([pscustomobject]@{
34+
Name = "allowGlobalConfirmation"
35+
State = "Enabled"
36+
Description = "blah"
37+
},
38+
[pscustomobject]@{
39+
Name = "powershellhost"
40+
State = "Disabled"
41+
Description = "blah"
42+
} )| Where-Object { $_.Name -eq $FeatureName }
43+
} -Verifiable
44+
45+
46+
it 'Test-TargetResource returns true when Present and Enabled.' {
47+
Test-TargetResource -FeatureName 'allowGlobalConfirmation' -Ensure 'Present' | should be $true
48+
}
49+
50+
it 'Test-TargetResource returns false when Present and Disabled' {
51+
Test-TargetResource -FeatureName 'powershellhost' -Ensure 'Present' | should be $false
52+
}
53+
54+
it 'Test-TargetResource returns false when Absent and Enabled' {
55+
Test-TargetResource -FeatureName 'allowGlobalConfirmation' -Ensure 'Absent' | Should be $false
56+
}
57+
58+
it 'Test-TargetResource returns true when Absent and Disabled' {
59+
Test-TargetResource -FeatureName 'powershellhost' -Ensure 'Absent' | should be $true
60+
}
61+
62+
}
63+
64+
Context "Set-TargetResource" {
65+
66+
InModuleScope -ModuleName cChocoFeature -ScriptBlock {
67+
function choco {}
68+
mock choco {}
69+
}
70+
71+
Set-TargetResource -FeatureName "TestFeature" -Ensure "Present"
72+
73+
it "Present - Should have called choco, with enable" {
74+
Assert-MockCalled -CommandName choco -ModuleName cChocoFeature -ParameterFilter {
75+
$args -contains "enable"
76+
}
77+
}
78+
79+
Set-TargetResource -FeatureName "TestFeature" -Ensure "Absent"
80+
81+
it "Absent - Should have called choco, with disable" {
82+
Assert-MockCalled -CommandName choco -ModuleName cChocoFeature -ParameterFilter {
83+
$args -contains "disable"
84+
}
85+
}
86+
}
87+
}

Tests/cChoco_ScriptAnalyzerTests.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
$Rules = Get-ScriptAnalyzerRule
2020

2121
#Only run on cChocoInstaller.psm1 for now as this is the only resource that has had code adjustments for PSScriptAnalyzer rules.
22-
$Modules = Get-ChildItem $PSScriptRoot\..\ -Filter *.psm1 -Recurse | Where-Object {$_.FullName -match '(cChocoInstaller|cChocoPackageInstall)\.psm1$'}
22+
$Modules = Get-ChildItem $PSScriptRoot\..\ -Filter *.psm1 -Recurse | Where-Object {$_.FullName -match '(cChocoInstaller|cChocoPackageInstall|cChocoFeature)\.psm1$'}
2323

2424
#---------------------------------#
2525
# Run Module tests (psm1) #

0 commit comments

Comments
 (0)