Skip to content

Commit

Permalink
Consider using $PSNativeCommandUseErrorActionPreference (#57)
Browse files Browse the repository at this point in the history
* Specify -NoProfile for all pwsh execution to avoid loading profile errors in local testing

* Ensure errexit in most powershell scripts

* Skip install module if already installed to avoid blocked process in `task default`

* Personally I dont develop pwsh module with darwin anymore

* Specify  -ErrorAction SilentlyContinue for Get-InstalledModule
  • Loading branch information
kachick authored Sep 29, 2024
1 parent 48bce02 commit acb497b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 18 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci-nix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ jobs:
matrix:
os:
- ubuntu-24.04
- macos-14
runs-on: ${{ matrix.os }}
timeout-minutes: 30
steps:
Expand Down
20 changes: 12 additions & 8 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ tasks:
- task: test
- task: lint
test:
deps:
- setup
cmds:
# pwsh exit with 0 even if ReadLine made errors, the 1+ code is special for an example as parser error
# So returns 1+ with the result by yourself in each cmd
- pwsh --File ./tools/test-all.ps1
- pwsh --Command 'Test-ModuleManifest -Path ./PSFzfHistory/*.psd1'
- pwsh -NoProfile --File ./tools/test-all.ps1
- pwsh -NoProfile --Command '$PSNativeCommandUseErrorActionPreference = $true; $ErrorActionPreference = "Stop"; Test-ModuleManifest -Path ./PSFzfHistory/*.psd1'
fmt:
deps:
- setup
cmds:
- dprint fmt
- git ls-files '*.nix' | xargs nix fmt
Expand All @@ -30,16 +32,18 @@ tasks:
- git ls-files '*.nix' | xargs nixfmt --check
repl:
cmds:
- pwsh -NoExit -File ./tools/repl.ps1
- pwsh -NoProfile -NoExit -File ./tools/repl.ps1
setup:
cmds:
- pwsh -File ./tools/setup.ps1
- pwsh -NoProfile -File ./tools/setup.ps1
bench:
deps:
- setup
cmds:
- pwsh -File ./tools/benchmark.ps1
- pwsh -NoProfile -File ./tools/benchmark.ps1
deps:
cmds:
- pwsh -File ./tools/deps.ps1
- pwsh -NoProfile -File ./tools/deps.ps1
help:
cmds:
- task --list-all
5 changes: 4 additions & 1 deletion tools/benchmark.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems() cannot be used non tty? as called in CLI
$PSNativeCommandUseErrorActionPreference = $true
$ErrorActionPreference = 'Stop'

# [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems() cannot be used non tty? as called in CLI
# $lines = [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems()
$lines = Get-Content -Path ./fixtures/random-guids.txt

Expand Down
5 changes: 4 additions & 1 deletion tools/deps.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# nix --version - Excluded for Windows, nix targets onle Unix-like OS
$PSNativeCommandUseErrorActionPreference = $true
$ErrorActionPreference = 'Stop'

# nix --version - Excluded for Windows, nix targets onle Unix-like OS
nil --version
task --version
dprint --version
Expand Down
5 changes: 4 additions & 1 deletion tools/lint-fix.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Do NOT use 'Invoke-ScriptAnalyzer -Recurse -Path .' It includes dotfiles as .direnv
$PSNativeCommandUseErrorActionPreference = $true
$ErrorActionPreference = 'Stop'

# Do NOT use 'Invoke-ScriptAnalyzer -Recurse -Path .' It includes dotfiles as .direnv
# https://github.com/PowerShell/PSScriptAnalyzer/issues/561
Get-ChildItem -Recurse -Path PSFzfHistory -Include "*.ps*1" |
ForEach-Object {
Expand Down
23 changes: 17 additions & 6 deletions tools/setup.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# https://github.com/PowerShell/PSScriptAnalyzer
Install-Module -Name PSScriptAnalyzer -Force
# https://github.com/pester/pester
Install-Module -Name Pester -Force
# https://github.com/StartAutomating/Benchpress
Install-Module Benchpress -Scope CurrentUser -Force
$PSNativeCommandUseErrorActionPreference = $true
$ErrorActionPreference = 'Stop'

if (-not (Get-InstalledModule -ErrorAction SilentlyContinue PSScriptAnalyzer )) {
# https://github.com/PowerShell/PSScriptAnalyzer
Install-Module -Name PSScriptAnalyzer -Force
}

if (-not (Get-InstalledModule -ErrorAction SilentlyContinue Pester)) {
# https://github.com/pester/pester
Install-Module -Name Pester -Force
}

if (-not (Get-InstalledModule -ErrorAction SilentlyContinue Benchpress)) {
# https://github.com/StartAutomating/Benchpress
Install-Module Benchpress -Scope CurrentUser -Force
}
4 changes: 4 additions & 0 deletions tools/test-all.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# See https://github.com/kachick/dotfiles/issues/617 for background of this option
$PSNativeCommandUseErrorActionPreference = $true
$ErrorActionPreference = 'Stop'

# https://pester.dev/docs/commands/New-PesterConfiguration
$config = New-PesterConfiguration
$config.Run.Exit = $true
Expand Down

0 comments on commit acb497b

Please sign in to comment.