1+ BeforeAll {
2+ $rootPath = $PWD ;
3+ Import-Module - Name (Join-Path - Path $rootPath - ChildPath ' /src/PSRule.Rules.AzureDevOps/PSRule.Rules.AzureDevOps.psd1' ) - Force;
4+ }
5+
6+ Describe ' Azure.DevOps.RetentionSettings' {
7+ Context ' Get-AzDevOpsRetentionSettings without a connection' {
8+ It ' should throw an error' {
9+ {
10+ Disconnect-AzDevOps
11+ Get-AzDevOpsRetentionSettings - Project ' MyProject'
12+ } | Should - Throw ' Not connected to Azure DevOps. Run Connect-AzDevOps first.'
13+ }
14+ }
15+
16+ Context ' Get-AzDevOpsRetentionSettings' {
17+ BeforeAll {
18+ Connect-AzDevOps - Organization $env: ADO_ORGANIZATION - PAT $env: ADO_PAT
19+ $response = Get-AzDevOpsRetentionSettings - Project $env: ADO_PROJECT
20+ }
21+
22+ It ' should return a hashtable' {
23+ $response | Should - BeOfType [hashtable ]
24+ }
25+
26+ It ' should return a hashtable with RetentionSettings property' {
27+ $response.RetentionSettings | Should -Not - BeNullOrEmpty
28+ }
29+
30+ It ' should return a hashtable with RetentionPolicy property' {
31+ $response.RetentionPolicy | Should -Not - BeNullOrEmpty
32+ }
33+
34+ It ' should return a hashtable with ObjectType property' {
35+ $response.ObjectType | Should -Not - BeNullOrEmpty
36+ }
37+
38+ It ' should return a hashtable with ObjectName property' {
39+ $response.ObjectName | Should -Not - BeNullOrEmpty
40+ }
41+
42+ AfterAll {
43+ Disconnect-AzDevOps
44+ }
45+ }
46+
47+ Context ' Get-AzDevOpsRetentionSettings with a wrong organization or PAT' {
48+ It ' should throw an error with a wrong PAT' {
49+ {
50+ Disconnect-AzDevOps
51+ Connect-AzDevOps - Organization $env: ADO_ORGANIZATION - PAT ' wrongPAT'
52+ Get-AzDevOpsRetentionSettings - Project $env: ADO_PROJECT
53+ } | Should - Throw " Failed to get retention settings for project '$ ( $env: ADO_PROJECT ) ' from Azure DevOps"
54+ }
55+
56+ It ' should throw an error with a wrong organization' {
57+ {
58+ Disconnect-AzDevOps
59+ Connect-AzDevOps - Organization ' wrongOrganization' - PAT $env: ADO_PAT
60+ Get-AzDevOpsRetentionSettings - Project $env: ADO_PROJECT
61+ } | Should - Throw " Failed to get retention settings for project '$ ( $env: ADO_PROJECT ) ' from Azure DevOps"
62+ }
63+
64+ It ' should throw an error with a wrong project' {
65+ {
66+ Disconnect-AzDevOps
67+ Connect-AzDevOps - Organization $env: ADO_ORGANIZATION - PAT $env: ADO_PAT
68+ Get-AzDevOpsRetentionSettings - Project ' wrongProject'
69+ } | Should - Throw " Failed to get retention settings for project 'wrongProject' from Azure DevOps"
70+ }
71+ }
72+
73+ Context ' Export-AzDevOpsRetentionSettings without a connection' {
74+ It ' should throw an error' {
75+ {
76+ Disconnect-AzDevOps
77+ Export-AzDevOpsRetentionSettings - Project ' MyProject' - OutputPath $Env: ADO_EXPORT_DIR
78+ } | Should - Throw ' Not connected to Azure DevOps. Run Connect-AzDevOps first.'
79+ }
80+ }
81+
82+ Context ' Export-AzDevOpsRetentionSettings' {
83+ BeforeAll {
84+ Connect-AzDevOps - Organization $env: ADO_ORGANIZATION - PAT $env: ADO_PAT
85+ Export-AzDevOpsRetentionSettings - Project $env: ADO_PROJECT - OutputPath $Env: ADO_EXPORT_DIR
86+ }
87+
88+ It ' should export a JSON file' {
89+ $file = Get-ChildItem - Path $Env: ADO_EXPORT_DIR - Filter " $ ( $env: ADO_PROJECT ) .ret.ado.json" - Recurse - ErrorAction SilentlyContinue | Select-Object - ExpandProperty ' FullName'
90+ $file | Should -Not - BeNullOrEmpty
91+ }
92+
93+ It ' should export a JSON file with the correct name' {
94+ $file = Get-ChildItem - Path $Env: ADO_EXPORT_DIR - Filter " $ ( $env: ADO_PROJECT ) .ret.ado.json" - Recurse - ErrorAction SilentlyContinue | Select-Object - ExpandProperty ' FullName'
95+ $file | Should -Match " $ ( $env: ADO_PROJECT ) .ret.ado.json"
96+ }
97+
98+ It ' should export a JSON file with the correct content' {
99+ $file = Get-ChildItem - Path $Env: ADO_EXPORT_DIR - Filter " $ ( $env: ADO_PROJECT ) .ret.ado.json" - Recurse - ErrorAction SilentlyContinue | Select-Object - ExpandProperty ' FullName'
100+ $content = Get-Content - Path $file - Raw
101+ $content | Should -Match ' RetentionSettings'
102+ $content | Should -Match ' RetentionPolicy'
103+ $content | Should -Match ' ObjectType'
104+ $content | Should -Match ' ObjectName'
105+ }
106+
107+ AfterAll {
108+ Disconnect-AzDevOps
109+ }
110+ }
111+
112+ Context ' Export-AzDevOpsRetentionSettings with a wrong organization or PAT' {
113+ It ' should throw an error with a wrong PAT' {
114+ {
115+ Disconnect-AzDevOps
116+ Connect-AzDevOps - Organization $env: ADO_ORGANIZATION - PAT ' wrongPAT'
117+ Export-AzDevOpsRetentionSettings - Project $env: ADO_PROJECT - OutputPath $Env: ADO_EXPORT_DIR
118+ } | Should - Throw " Failed to get retention settings for project '$ ( $env: ADO_PROJECT ) ' from Azure DevOps"
119+ }
120+
121+ It ' should throw an error with a wrong organization' {
122+ {
123+ Disconnect-AzDevOps
124+ Connect-AzDevOps - Organization ' wrongOrganization' - PAT $env: ADO_PAT
125+ Export-AzDevOpsRetentionSettings - Project $env: ADO_PROJECT - OutputPath $Env: ADO_EXPORT_DIR
126+ } | Should - Throw " Failed to get retention settings for project '$ ( $env: ADO_PROJECT ) ' from Azure DevOps"
127+ }
128+
129+ It ' should throw an error with a wrong project' {
130+ {
131+ Disconnect-AzDevOps
132+ Connect-AzDevOps - Organization $env: ADO_ORGANIZATION - PAT $env: ADO_PAT
133+ Export-AzDevOpsRetentionSettings - Project ' wrongProject' - OutputPath $Env: ADO_EXPORT_DIR
134+ } | Should - Throw " Failed to get retention settings for project 'wrongProject' from Azure DevOps"
135+ }
136+ }
137+ }
138+
139+ AfterAll {
140+ Disconnect-AzDevOps
141+ Remove-Module - Name PSRule.Rules.AzureDevOps - Force
142+ }
0 commit comments