Skip to content

Commit c245bb1

Browse files
authored
Separate logic related to reading settings (#1743)
_**Issue**_: AL-Go-Helper has grown significantly, and it currently isn't possible to emit telemetry from any the functions in AL-Go-Helper _**Solution**_: Separate the logic for reading settings into a module. Follow-up will be to leverage the telemetry in key functions in AL-Go-Helper. E2E test run: https://github.com/microsoft/AL-Go/actions/runs/16023887227
1 parent f16d6c5 commit c245bb1

File tree

9 files changed

+756
-786
lines changed

9 files changed

+756
-786
lines changed

Actions/.Modules/ReadSettings.psm1

Lines changed: 475 additions & 0 deletions
Large diffs are not rendered by default.
File renamed without changes.

Actions/AL-Go-Helper.ps1

Lines changed: 5 additions & 443 deletions
Large diffs are not rendered by default.

Templates/AppSource App/.AL-Go/cloudDevEnv.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@ Write-Host -ForegroundColor Yellow @'
5252
$tmpFolder = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString())"
5353
New-Item -Path $tmpFolder -ItemType Directory -Force | Out-Null
5454
$GitHubHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/Github-Helper.psm1' -folder $tmpFolder -notifyAuthenticatedAttempt
55+
$ReadSettingsModule = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/.Modules/ReadSettings.psm1' -folder $tmpFolder
5556
$ALGoHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/AL-Go-Helper.ps1' -folder $tmpFolder
5657
DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/settings.schema.json' -folder $tmpFolder | Out-Null
5758
DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/Packages.json' -folder $tmpFolder | Out-Null
5859

5960
Import-Module $GitHubHelperPath
61+
Import-Module $ReadSettingsModule
6062
. $ALGoHelperPath -local
6163

6264
$baseFolder = GetBaseFolder -folder $PSScriptRoot

Templates/AppSource App/.AL-Go/localDevEnv.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ Write-Host -ForegroundColor Yellow @'
5656
$tmpFolder = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString())"
5757
New-Item -Path $tmpFolder -ItemType Directory -Force | Out-Null
5858
$GitHubHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/Github-Helper.psm1' -folder $tmpFolder -notifyAuthenticatedAttempt
59+
$ReadSettingsModule = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/.Modules/ReadSettings.psm1' -folder $tmpFolder
5960
$ALGoHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/AL-Go-Helper.ps1' -folder $tmpFolder
6061
DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/settings.schema.json' -folder $tmpFolder | Out-Null
6162
DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/Packages.json' -folder $tmpFolder | Out-Null
6263

6364
Import-Module $GitHubHelperPath
65+
Import-Module $ReadSettingsModule
6466
. $ALGoHelperPath -local
6567

6668
$baseFolder = GetBaseFolder -folder $PSScriptRoot

Templates/Per Tenant Extension/.AL-Go/cloudDevEnv.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@ Write-Host -ForegroundColor Yellow @'
5252
$tmpFolder = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString())"
5353
New-Item -Path $tmpFolder -ItemType Directory -Force | Out-Null
5454
$GitHubHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/Github-Helper.psm1' -folder $tmpFolder -notifyAuthenticatedAttempt
55+
$ReadSettingsModule = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/.Modules/ReadSettings.psm1' -folder $tmpFolder
5556
$ALGoHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/AL-Go-Helper.ps1' -folder $tmpFolder
5657
DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/settings.schema.json' -folder $tmpFolder | Out-Null
5758
DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/Packages.json' -folder $tmpFolder | Out-Null
5859

5960
Import-Module $GitHubHelperPath
61+
Import-Module $ReadSettingsModule
6062
. $ALGoHelperPath -local
6163

6264
$baseFolder = GetBaseFolder -folder $PSScriptRoot

Templates/Per Tenant Extension/.AL-Go/localDevEnv.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ Write-Host -ForegroundColor Yellow @'
5656
$tmpFolder = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString())"
5757
New-Item -Path $tmpFolder -ItemType Directory -Force | Out-Null
5858
$GitHubHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/Github-Helper.psm1' -folder $tmpFolder -notifyAuthenticatedAttempt
59+
$ReadSettingsModule = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/.Modules/ReadSettings.psm1' -folder $tmpFolder
5960
$ALGoHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/AL-Go-Helper.ps1' -folder $tmpFolder
6061
DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/settings.schema.json' -folder $tmpFolder | Out-Null
6162
DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/Packages.json' -folder $tmpFolder | Out-Null
6263

6364
Import-Module $GitHubHelperPath
65+
Import-Module $ReadSettingsModule
6466
. $ALGoHelperPath -local
6567

6668
$baseFolder = GetBaseFolder -folder $PSScriptRoot

Tests/AL-Go-Helper.Test.ps1

Lines changed: 0 additions & 343 deletions
Large diffs are not rendered by default.

Tests/ReadSettings.Test.ps1

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
Import-Module (Join-Path $PSScriptRoot '../Actions/.Modules/ReadSettings.psm1')
2+
3+
InModuleScope ReadSettings { # Allows testing of private functions
4+
Describe "ReadSettings" {
5+
BeforeAll {
6+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'scriptPath', Justification = 'False positive.')]
7+
$schema = Get-Content -Path (Join-Path $PSScriptRoot '../Actions/.Modules/settings.schema.json') -Raw
8+
}
9+
10+
It 'Reads settings from all settings locations' {
11+
Mock Write-Host { }
12+
Mock Out-Host { }
13+
14+
Push-Location
15+
$tempName = Join-Path ([System.IO.Path]::GetTempPath()) ([Guid]::NewGuid().ToString())
16+
$githubFolder = Join-Path $tempName ".github"
17+
$ALGoFolder = Join-Path $tempName $ALGoFolderName
18+
$projectALGoFolder = Join-Path $tempName "Project/$ALGoFolderName"
19+
20+
New-Item $githubFolder -ItemType Directory | Out-Null
21+
New-Item $ALGoFolder -ItemType Directory | Out-Null
22+
New-Item $projectALGoFolder -ItemType Directory | Out-Null
23+
24+
New-Item -Path (Join-Path $tempName "projectx/$ALGoFolderName") -ItemType Directory | Out-Null
25+
New-Item -Path (Join-Path $tempName "projecty/$ALGoFolderName") -ItemType Directory | Out-Null
26+
27+
# Create settings files
28+
# Property: Repo: Project (single): Project (multi): Workflow: Workflow: User:
29+
# if(branch=dev):
30+
# Property1 repo1 single1 multi1 branch1 user1
31+
# Property2 repo2 workflow2
32+
# Property3 repo3
33+
# Arr1 @("repo1","repo2")
34+
# Property4 single4 branch4
35+
# property5 multi5
36+
# property6 user6
37+
@{ "property1" = "repo1"; "property2" = "repo2"; "property3" = "repo3"; "arr1" = @("repo1", "repo2") } | ConvertTo-Json -Depth 99 |
38+
Set-Content -Path (Join-Path $githubFolder "AL-Go-Settings.json") -encoding utf8 -Force
39+
@{ "property1" = "single1"; "property4" = "single4" } | ConvertTo-Json -Depth 99 |
40+
Set-Content -Path (Join-Path $ALGoFolder "settings.json") -encoding utf8 -Force
41+
@{ "property1" = "multi1"; "property5" = "multi5" } | ConvertTo-Json -Depth 99 |
42+
Set-Content -Path (Join-Path $projectALGoFolder "settings.json") -encoding utf8 -Force
43+
@{ "property2" = "workflow2"; "conditionalSettings" = @( @{ "branches" = @( 'dev' ); "settings" = @{ "property1" = "branch1"; "property4" = "branch4" } } ) } | ConvertTo-Json -Depth 99 |
44+
Set-Content -Path (Join-Path $githubFolder "Workflow.settings.json") -encoding utf8 -Force
45+
@{ "property1" = "user1"; "property6" = "user6" } | ConvertTo-Json -Depth 99 |
46+
Set-Content -Path (Join-Path $projectALGoFolder "user.settings.json") -encoding utf8 -Force
47+
48+
# No settings variables
49+
$ENV:ALGoOrgSettings = ''
50+
$ENV:ALGoRepoSettings = ''
51+
52+
# Repo only
53+
$repoSettings = ReadSettings -baseFolder $tempName -project '' -repoName 'repo' -workflowName '' -branchName '' -userName ''
54+
$repoSettings.property1 | Should -Be 'repo1'
55+
$repoSettings.property2 | Should -Be 'repo2'
56+
$repoSettings.property3 | Should -Be 'repo3'
57+
58+
# Repo + single project
59+
$singleProjectSettings = ReadSettings -baseFolder $tempName -project '.' -repoName 'repo' -workflowName '' -branchName '' -userName ''
60+
$singleProjectSettings.property1 | Should -Be 'single1'
61+
$singleProjectSettings.property2 | Should -Be 'repo2'
62+
$singleProjectSettings.property4 | Should -Be 'single4'
63+
64+
# Repo + multi project
65+
$multiProjectSettings = ReadSettings -baseFolder $tempName -project 'Project' -repoName 'repo' -workflowName '' -branchName '' -userName ''
66+
$multiProjectSettings.property1 | Should -Be 'multi1'
67+
$multiProjectSettings.property2 | Should -Be 'repo2'
68+
$multiProjectSettings.property5 | Should -Be 'multi5'
69+
70+
# Repo + workflow
71+
$workflowRepoSettings = ReadSettings -baseFolder $tempName -project '' -repoName 'repo' -workflowName 'Workflow' -branchName '' -userName ''
72+
$workflowRepoSettings.property1 | Should -Be 'repo1'
73+
$workflowRepoSettings.property2 | Should -Be 'workflow2'
74+
75+
# Repo + single project + workflow
76+
$workflowSingleSettings = ReadSettings -baseFolder $tempName -project '.' -repoName 'repo' -workflowName 'Workflow' -branchName '' -userName ''
77+
$workflowSingleSettings.property1 | Should -Be 'single1'
78+
$workflowSingleSettings.property2 | Should -Be 'workflow2'
79+
$workflowSingleSettings.property4 | Should -Be 'single4'
80+
$workflowSingleSettings.property3 | Should -Be 'repo3'
81+
82+
# Repo + multi project + workflow + dev branch
83+
$workflowMultiSettings = ReadSettings -baseFolder $tempName -project 'Project' -repoName 'repo' -workflowName 'Workflow' -branchName 'dev' -userName ''
84+
$workflowMultiSettings.property1 | Should -Be 'branch1'
85+
$workflowMultiSettings.property2 | Should -Be 'workflow2'
86+
$workflowMultiSettings.property3 | Should -Be 'repo3'
87+
$workflowMultiSettings.property4 | Should -Be 'branch4'
88+
$workflowMultiSettings.property5 | Should -Be 'multi5'
89+
$workflowMultiSettings.property6 | Should -BeNullOrEmpty
90+
91+
# Repo + multi project + workflow + dev branch + user
92+
$userWorkflowMultiSettings = ReadSettings -baseFolder $tempName -project 'Project' -repoName 'repo' -workflowName 'Workflow' -branchName 'dev' -userName 'user'
93+
$userWorkflowMultiSettings.property1 | Should -Be 'user1'
94+
$userWorkflowMultiSettings.property2 | Should -Be 'workflow2'
95+
$userWorkflowMultiSettings.property3 | Should -Be 'repo3'
96+
$userWorkflowMultiSettings.property4 | Should -Be 'branch4'
97+
$userWorkflowMultiSettings.property5 | Should -Be 'multi5'
98+
$userWorkflowMultiSettings.property6 | Should -Be 'user6'
99+
100+
# Org settings variable
101+
# property 2 = orgsetting2
102+
# property 7 = orgsetting7
103+
# arr1 = @(org3) - gets merged
104+
$ENV:ALGoOrgSettings = @{ "property2" = "orgsetting2"; "property7" = "orgsetting7"; "arr1" = @("org3") } | ConvertTo-Json -Depth 99
105+
106+
# Org(var) + Repo + multi project + workflow + dev branch + user
107+
$withOrgSettings = ReadSettings -baseFolder $tempName -project 'Project' -repoName 'repo' -workflowName 'Workflow' -branchName 'dev' -userName 'user'
108+
$withOrgSettings.property1 | Should -Be 'user1'
109+
$withOrgSettings.property2 | Should -Be 'workflow2'
110+
$withOrgSettings.property3 | Should -Be 'repo3'
111+
$withOrgSettings.property4 | Should -Be 'branch4'
112+
$withOrgSettings.property5 | Should -Be 'multi5'
113+
$withOrgSettings.property6 | Should -Be 'user6'
114+
$withOrgSettings.property7 | Should -Be 'orgsetting7'
115+
$withOrgSettings.arr1 | Should -Be @("org3", "repo1", "repo2")
116+
117+
# Repo settings variable
118+
# property3 = reposetting3
119+
# property8 = reposetting8
120+
$ENV:ALGoRepoSettings = @{ "property3" = "reposetting3"; "property8" = "reposetting8" } | ConvertTo-Json -Depth 99
121+
122+
# Org(var) + Repo + Repo(var) + multi project + workflow + dev branch + user
123+
$withRepoSettings = ReadSettings -baseFolder $tempName -project 'Project' -repoName 'repo' -workflowName 'Workflow' -branchName 'dev' -userName 'user'
124+
$withRepoSettings.property1 | Should -Be 'user1'
125+
$withRepoSettings.property2 | Should -Be 'workflow2'
126+
$withRepoSettings.property3 | Should -Be 'reposetting3'
127+
$withRepoSettings.property4 | Should -Be 'branch4'
128+
$withRepoSettings.property5 | Should -Be 'multi5'
129+
$withRepoSettings.property6 | Should -Be 'user6'
130+
$withRepoSettings.property7 | Should -Be 'orgsetting7'
131+
$withRepoSettings.property8 | Should -Be 'reposetting8'
132+
133+
# Add conditional settings as repo(var) settings
134+
$conditionalSettings = [ordered]@{
135+
"conditionalSettings" = @(
136+
@{
137+
"branches" = @( 'branchx', 'branchy' )
138+
"settings" = @{ "property3" = "branchxy"; "property4" = "branchxy" }
139+
}
140+
@{
141+
"repositories" = @( 'repox', 'repoy' )
142+
"settings" = @{ "property3" = "repoxy"; "property4" = "repoxy" }
143+
}
144+
@{
145+
"projects" = @( 'projectx', 'projecty' )
146+
"settings" = @{ "property3" = "projectxy"; "property4" = "projectxy" }
147+
}
148+
@{
149+
"workflows" = @( 'workflowx', 'workflowy' )
150+
"settings" = @{ "property3" = "workflowxy"; "property4" = "workflowxy" }
151+
}
152+
@{
153+
"users" = @( 'userx', 'usery' )
154+
"settings" = @{ "property3" = "userxy"; "property4" = "userxy" }
155+
}
156+
@{
157+
"branches" = @( 'branchx', 'branchy' )
158+
"projects" = @( 'projectx', 'projecty' )
159+
"settings" = @{ "property3" = "bpxy"; "property4" = "bpxy" }
160+
}
161+
)
162+
}
163+
$ENV:ALGoRepoSettings = $conditionalSettings | ConvertTo-Json -Depth 99
164+
165+
# Test that conditional settings are applied correctly
166+
$conditionalSettings = ReadSettings -baseFolder $tempName -project 'Project' -repoName 'repo' -workflowName 'Workflow' -branchName 'branchy' -userName 'user'
167+
$conditionalSettings.property3 | Should -Be 'branchxy'
168+
$conditionalSettings.property4 | Should -Be 'branchxy'
169+
170+
$conditionalSettings = ReadSettings -baseFolder $tempName -project 'Project' -repoName 'repox' -workflowName 'Workflow' -branchName 'dev' -userName 'user'
171+
$conditionalSettings.property3 | Should -Be 'repoxy'
172+
$conditionalSettings.property4 | Should -Be 'branch4'
173+
174+
$conditionalSettings = ReadSettings -baseFolder $tempName -project 'projectx' -repoName 'repo' -workflowName 'Workflow' -branchName 'branch' -userName 'user'
175+
$conditionalSettings.property3 | Should -Be 'projectxy'
176+
$conditionalSettings.property4 | Should -Be 'projectxy'
177+
178+
$conditionalSettings = ReadSettings -baseFolder $tempName -project 'projectx' -repoName 'repo' -workflowName 'Workflowx' -branchName 'branch' -userName 'user'
179+
$conditionalSettings.property3 | Should -Be 'workflowxy'
180+
$conditionalSettings.property4 | Should -Be 'workflowxy'
181+
182+
$conditionalSettings = ReadSettings -baseFolder $tempName -project 'Project' -repoName 'repo' -workflowName 'Workflow' -branchName 'branch' -userName 'usery'
183+
$conditionalSettings.property3 | Should -Be 'userxy'
184+
$conditionalSettings.property4 | Should -Be 'userxy'
185+
186+
$conditionalSettings = ReadSettings -baseFolder $tempName -project 'projecty' -repoName 'repo' -workflowName 'Workflow' -branchName 'branchx' -userName 'user'
187+
$conditionalSettings.property3 | Should -Be 'bpxy'
188+
$conditionalSettings.property4 | Should -Be 'bpxy'
189+
190+
# Invalid Org(var) setting should throw
191+
$ENV:ALGoOrgSettings = 'this is not json'
192+
{ ReadSettings -baseFolder $tempName -project 'Project' } | Should -Throw
193+
194+
$ENV:ALGoOrgSettings = ''
195+
$ENV:ALGoRepoSettings = ''
196+
197+
# Clean up
198+
Pop-Location
199+
Remove-Item -Path $tempName -Recurse -Force
200+
}
201+
202+
It 'Settings schema is valid' {
203+
Test-Json -json $schema | Should -Be $true
204+
}
205+
206+
It 'All default settings are in the schema' {
207+
$defaultSettings = GetDefaultSettings
208+
209+
$schemaObj = $schema | ConvertFrom-Json
210+
211+
$defaultSettings.Keys | ForEach-Object {
212+
$property = $_
213+
$schemaObj.properties.PSObject.Properties.Name | Should -Contain $property
214+
}
215+
}
216+
217+
It 'Default settings match schema' {
218+
$defaultSettings = GetDefaultSettings
219+
Test-Json -json (ConvertTo-Json $defaultSettings) -schema $schema | Should -Be $true
220+
}
221+
222+
It 'Shell setting can only be pwsh or powershell' {
223+
$defaultSettings = GetDefaultSettings
224+
$defaultSettings.shell = 42
225+
try {
226+
Test-Json -json (ConvertTo-Json $defaultSettings) -schema $schema
227+
}
228+
catch {
229+
$_.Exception.Message | Should -Be "The JSON is not valid with the schema: Value is `"integer`" but should be `"string`" at '/shell'"
230+
}
231+
232+
$defaultSettings.shell = "random"
233+
try {
234+
Test-Json -json (ConvertTo-Json $defaultSettings) -schema $schema
235+
}
236+
catch {
237+
$_.Exception.Message | Should -Be "The JSON is not valid with the schema: The string value is not a match for the indicated regular expression at '/shell'"
238+
}
239+
}
240+
241+
It 'Projects setting is an array of strings' {
242+
# If the projects setting is not an array, it should throw an error
243+
$defaultSettings = GetDefaultSettings
244+
$defaultSettings.projects = "not an array"
245+
try {
246+
Test-Json -json (ConvertTo-Json $defaultSettings) -schema $schema
247+
}
248+
catch {
249+
$_.Exception.Message | Should -Be "The JSON is not valid with the schema: Value is `"string`" but should be `"array`" at '/projects'"
250+
}
251+
252+
# If the projects setting is an array, but contains non-string values, it should throw an error
253+
$defaultSettings.projects = @("project1", 42)
254+
try {
255+
Test-Json -json (ConvertTo-Json $defaultSettings) -schema $schema
256+
}
257+
catch {
258+
$_.Exception.Message | Should -Be "The JSON is not valid with the schema: Value is `"integer`" but should be `"string`" at '/projects/1'"
259+
}
260+
261+
# If the projects setting is an array of strings, it should pass the schema validation
262+
$defaultSettings.projects = @("project1")
263+
Test-Json -json (ConvertTo-Json $defaultSettings) -schema $schema | Should -Be $true
264+
$defaultSettings.projects = @("project1", "project2")
265+
Test-Json -json (ConvertTo-Json $defaultSettings) -schema $schema | Should -Be $true
266+
}
267+
}
268+
}

0 commit comments

Comments
 (0)