Skip to content

Commit 338b9d0

Browse files
committed
Added test for making sure all JSON files are valid JSON
1 parent 9b18300 commit 338b9d0

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

.github/workflows/ci-test.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ name: CI Tests
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
77
# The branches below must be a subset of the branches above
8-
branches: [ main ]
8+
branches: [main]
99
merge_group:
10-
types: [ checks_requested ]
10+
types: [checks_requested]
1111

1212
jobs:
1313
ci:
1414
name: node
1515
strategy:
1616
matrix:
17-
os: [ windows-latest, macos-latest, ubuntu-latest ]
17+
os: [windows-latest, macos-latest, ubuntu-latest]
1818
runs-on: ${{ matrix.os }}
1919
env:
2020
DOTNET_NOLOGO: true
@@ -33,6 +33,10 @@ jobs:
3333
with:
3434
path: vscode-powershell
3535

36+
- name: Make sure JSON files are valid
37+
shell: pwsh
38+
run: ./vscode-powershell/tools/testValidJson.ps1
39+
3640
- name: Install dotnet
3741
uses: actions/setup-dotnet@v4
3842
with:

tools/testValidJson.ps1

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
<#
4+
.SYNOPSIS
5+
Get all JSON files recursively and test if they are valid by trying to import them.
6+
7+
.EXAMPLE
8+
& $psEditor.GetEditorContext().CurrentFile.Path -WorkingDir '.\'
9+
#>
10+
11+
# Input and expected output
12+
[OutputType([System.Void])]
13+
Param(
14+
[Parameter()]
15+
[ValidateScript({[System.IO.Directory]::Exists($_)})]
16+
[string] $WorkingDir = '.\'
17+
)
18+
19+
# PowerShell preferences
20+
$ErrorActionPreference = 'Stop'
21+
22+
# Try to import all JSON files in the repo
23+
$TestValidJson = [PSCustomObject[]](
24+
[System.IO.Directory]::GetFiles($WorkingDir,'*.json',[System.IO.SearchOption]::AllDirectories).ForEach{
25+
[PSCustomObject]@{
26+
'Path' = [string] $_
27+
'IsValidJson' = [bool]$(
28+
Try {
29+
$null = ConvertFrom-Json -InputObject (Get-Content -Raw -Path $_) -AsHashtable
30+
$?
31+
}
32+
Catch {
33+
$false
34+
}
35+
)
36+
}
37+
}
38+
)
39+
40+
# Output results
41+
$TestValidJson | Format-Table
42+
43+
# Throw if errors were found
44+
if ($TestValidJson.Where{-not $_.'IsValidJson'}.'Count' -gt 0) {
45+
Throw ('Found {0} non-valid JSON file(s).' -f $TestValidJson.Where{-not $_.'IsValidJson'}.'Count')
46+
}

0 commit comments

Comments
 (0)