Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5086 - Invalid JSON in the snippets file #5087

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ name: CI Tests

on:
push:
branches: [ main ]
branches: [main]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
branches: [main]
merge_group:
types: [ checks_requested ]
types: [checks_requested]

jobs:
ci:
name: node
strategy:
matrix:
os: [ windows-latest, macos-latest, ubuntu-latest ]
os: [windows-latest, macos-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
env:
DOTNET_NOLOGO: true
Expand All @@ -33,6 +33,11 @@ jobs:
with:
path: vscode-powershell

- name: Make sure JSON files in ./vscode-powershell/ are valid
shell: pwsh
run: ./vscode-powershell/tools/testValidJson.ps1
working-directory: vscode-powershell

- name: Install dotnet
uses: actions/setup-dotnet@v4
with:
Expand Down
33 changes: 16 additions & 17 deletions snippets/PowerShell.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,23 @@
]
},
"Foreach with Progress": {
"prefix": "foreach-progress",
"prefix": "foreach-progress",
"description": "Insert a foreach loop with Write-Progress initialized",
"body": [
\\$progPercent = \"{0:n2}\" -f ([math]::round(\\$i/\\$array.count,4) * 100)",

Write-Progress -Activity \"${3:activityName}\" -Status \"\\$i of \\$array.count - \\$progPercent% Complete:\" -PercentComplete \\$progPercent",
"\\$i = 1",
"foreach ($${2:item} in $${1:array}) {",
" \\$progPercent = \"{0:n2}\" -f ([math]::round(\\$i/\\$total,4) * 100)",
" Write-Progress -Activity \"${3:activityName}\" -Status \"\\$i of \\$total - \\$progPercent% Complete:\" -PercentComplete \\$progPercent",
" # Insert Code Here",
" ${0}",
" ",
" \\$i++",
"}",
""
]
},
"body": [
"\\$progPercent = \"{0:n2}\" -f ([math]::round(\\$i/\\$array.count,4) * 100)",
"Write-Progress -Activity \"${3:activityName}\" -Status \"\\$i of \\$array.count - \\$progPercent% Complete:\" -PercentComplete \\$progPercent",
"\\$i = 1",
"foreach ($${2:item} in $${1:array}) {",
" \\$progPercent = \"{0:n2}\" -f ([math]::round(\\$i/\\$total,4) * 100)",
" Write-Progress -Activity \"${3:activityName}\" -Status \"\\$i of \\$total - \\$progPercent% Complete:\" -PercentComplete \\$progPercent",
" # Insert Code Here",
" ${0}",
" ",
" \\$i++",
"}",
""
]
},
"ForEach-Object -Parallel": {
"prefix": "foreach-parallel",
"description": "[PS 7+] Process multiple objects in parallel using runspaces. This has some limitations compared to a regular ForEach-Object. More: Get-Help ForEach-Object",
Expand Down
46 changes: 46 additions & 0 deletions tools/testValidJson.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
<#
.SYNOPSIS
Get all JSON files recursively and test if they are valid by trying to import them.

.EXAMPLE
& $psEditor.GetEditorContext().CurrentFile.Path -WorkingDir '.\'
#>

# Input and expected output
[OutputType([System.Void])]
Param(
[Parameter()]
[ValidateScript({[System.IO.Directory]::Exists($_)})]
[string] $WorkingDir = '.\'
)

# PowerShell preferences
$ErrorActionPreference = 'Stop'

# Try to import all JSON files in the repo
$TestValidJson = [PSCustomObject[]](
[System.IO.Directory]::GetFiles($WorkingDir,'*.json',[System.IO.SearchOption]::AllDirectories).ForEach{
[PSCustomObject]@{
'Path' = [string] $_
'IsValidJson' = [bool]$(
Try {
$null = ConvertFrom-Json -InputObject (Get-Content -Raw -Path $_) -AsHashtable
$?
}
Catch {
$false
}
)
}
}
)

# Output results
$TestValidJson | Format-Table

# Throw if errors were found
if ($TestValidJson.Where{-not $_.'IsValidJson'}.'Count' -gt 0) {
Throw ('Found {0} non-valid JSON file(s).' -f $TestValidJson.Where{-not $_.'IsValidJson'}.'Count')
}