From 5be35b08a9d06c2353d4b2f3877ecf368e84634e Mon Sep 17 00:00:00 2001 From: Driss Date: Thu, 9 Jul 2026 15:25:09 +0100 Subject: [PATCH 1/5] Implement run command (wip) --- .github/workflows/ci.yml | 2 +- readme.md | 42 +++++++++++++++++++++++++++++++++ src/core/handlers.ps1 | 51 ++++++++++++++++++++++++++++++++++++++++ src/core/router.ps1 | 19 +++++++++++++++ src/helpers/config.ps1 | 14 ++++++++++- 5 files changed, 126 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 95fd004..a91bde1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -81,7 +81,7 @@ jobs: Import-Module Pester # Run pvm.ps1 directly and capture exit code - .\src\pvm.ps1 test --coverage=85 --sort=coverage --group=folder + .\src\pvm.ps1 run test:ci $exitCode = $LASTEXITCODE Write-Host "Test execution completed with exit code: $exitCode" diff --git a/readme.md b/readme.md index 587ac37..bd155a7 100644 --- a/readme.md +++ b/readme.md @@ -20,6 +20,7 @@ PVM (PHP Version Manager) is a lightweight PowerShell tool for Windows that make - [Aliases](#command-aliases) - [Build Types](#build-types) - [Namespaced Commands](#namespaced-commands) + - [Scripts](#scripts) - [Data Storage](#data-storage) - [Running Tests](#running-tests) - [Requirements](#requirements-1) @@ -378,6 +379,47 @@ pvm cache pvm cache: ``` +### Scripts + +PVM includes predefined scripts for common test scenarios, similar to npm scripts in package.json or composer scripts in composer.json. These scripts provide shortcuts for running tests with specific configurations without typing all the options each time. + +```sh +# List all available scripts +pvm run + +# Run a specific script +pvm run + +# Examples: +pvm run test ....................... Runs all tests with default settings +pvm run test:coverage ............... Runs tests with 80% coverage target +pvm run test:ci ...................... Runs tests with coverage for CI environments +pvm run test:full ................... Runs tests with coverage and detailed output +pvm run test:unit ................... Runs only tests tagged as 'unit' +pvm run test:integration ............ Runs only tests tagged as 'integration' +``` + +**Available Scripts:** + +| Script Name | Command | Description | +|-------------|---------|-------------| +| `test` | `pvm test` | Runs all tests with default settings | +| `test:minimal` | `pvm test --verbosity=None` | Runs tests with minimal output | +| `test:quiet` | `pvm test --verbosity=None` | Alias for test:minimal | +| `test:coverage` | `pvm test --coverage=75` | Runs tests with 75% coverage target | +| `test:coverage80` | `pvm test --coverage=80` | Runs tests with 80% coverage target | +| `test:coverage85` | `pvm test --coverage=85` | Runs tests with 85% coverage target | +| `test:ci` | `pvm test --coverage=85 --sort=coverage --group=folder` | CI-optimized test run (matches GitHub Actions) | +| `test:full` | `pvm test --coverage=85 --verbosity=Detailed` | Runs tests with coverage and detailed output | +| `test:unit` | `pvm test --tag=unit` | Runs only tests tagged as 'unit' | +| `test:integration` | `pvm test --tag=integration` | Runs only tests tagged as 'integration' | +| `test:sorted` | `pvm test --sort=coverage` | Runs tests and sorts results by coverage | +| `test:grouped` | `pvm test --group=folder` | Runs tests and groups results by folder | + +**Customizing Scripts:** + +You can customize scripts by editing the `scripts` section in `src/helpers/config.ps1`. The scripts are defined in the `$PVMConfig.defaults.scripts` hashtable. Add or modify scripts to suit your workflow. + ## Data Storage | Item | Location | diff --git a/src/core/handlers.ps1 b/src/core/handlers.ps1 index 4f25ec8..ec3cee7 100644 --- a/src/core/handlers.ps1 +++ b/src/core/handlers.ps1 @@ -497,3 +497,54 @@ function Invoke-Update { Display-Msg-By-ExitCode -result $result return $result.code } + +function Invoke-Run { + param ($arguments) + + $scriptName = $arguments[0] + + if ([string]::IsNullOrWhiteSpace($scriptName)) { + Write-Host -Object "`nPlease provide a script name to run: pvm run " -ForegroundColor Yellow + Write-Host -Object "`nAvailable scripts:`n" -ForegroundColor Cyan + $scripts = $PVMConfig.defaults.scripts + $maxNameLength = ($scripts.Keys | Measure-Object -Maximum Length).Maximum + ($PVMConfig.env.MIN_PAD_RIGHT_LENGTH * 2) + $scripts.Keys | ForEach-Object { + $name = "$_ ".PadRight($maxNameLength, '.') + $command = $scripts[$_] + Write-Host -Object " $name $command" + } + return -1 + } + + $scripts = $PVMConfig.defaults.scripts + + if (-not $scripts.Contains($scriptName)) { + Write-Host -Object "`nScript '$scriptName' not found." -ForegroundColor Yellow + Write-Host -Object "`nAvailable scripts:`n" -ForegroundColor Cyan + $maxNameLength = ($scripts.Keys | Measure-Object -Maximum Length).Maximum + ($PVMConfig.env.MIN_PAD_RIGHT_LENGTH * 2) + $scripts.Keys | ForEach-Object { + $name = "$_ ".PadRight($maxNameLength, '.') + $command = $scripts[$_] + Write-Host -Object " $name $command" + } + return -1 + } + + $scriptCommand = $scripts[$scriptName] + Write-Host -Object "`nRunning script: $scriptName" -ForegroundColor Cyan + Write-Host -Object "Command: pvm $scriptCommand`n" -ForegroundColor Gray + + $parts = $scriptCommand -split ' ' + $command = $parts[0] + $scriptArgs = if ($parts.Count -gt 1) { $parts[1..($parts.Count - 1)] } else { @() } + + $actions = Get-Actions -arguments $scriptArgs + + if (-not $actions.Contains($command)) { + Write-Host -Object "`nInvalid command in script: $command" -ForegroundColor Yellow + return -1 + } + + $result = $($actions[$command].action.Invoke()) + return $result +} diff --git a/src/core/router.ps1 b/src/core/router.ps1 index e3137d0..295e1f8 100644 --- a/src/core/router.ps1 +++ b/src/core/router.ps1 @@ -344,5 +344,24 @@ function Get-Actions { } action = { return Invoke-Update -arguments $script:arguments } } + 'run' = @{ + command = 'pvm run '; + description = 'Run a predefined script from the scripts configuration.'; + usage = [ordered]@{ + USAGE = 'pvm run ' + DESCRIPTION = @( + 'Runs a predefined script from the scripts configuration.', + 'Scripts are shortcuts for common commands with predefined options.' + ) + EXAMPLES = @( + 'pvm run test ............... Runs all tests with default settings' + 'pvm run test:coverage ..... Runs tests with 80% coverage target' + 'pvm run test:ci ............ Runs tests with coverage for CI environments' + 'pvm run test:fast .......... Runs tests excluding slow tests' + 'pvm run test:full .......... Runs tests with coverage and detailed output' + ) + } + action = { return Invoke-Run -arguments $script:arguments } + } } } diff --git a/src/helpers/config.ps1 b/src/helpers/config.ps1 index 8160d5a..596f861 100644 --- a/src/helpers/config.ps1 +++ b/src/helpers/config.ps1 @@ -147,7 +147,6 @@ function Get-Config { 'a' = 'add'; '+' = 'add'; 'rm' = 'remove'; '-' = 'remove' 'del' = 'delete'; 'cls' = 'clear' 'logs' = 'log'; 'upgrade' = 'update' - 'run' = 'test' 'fix' = 'repair'; } flags = [ordered]@{ @@ -156,6 +155,19 @@ function Get-Config { '--help' = 'help' '-h' = 'help' } + scripts = [ordered]@{ + 'test' = 'test' + 'test:quiet' = 'test --verbosity=None' + 'test:coverage' = 'test --coverage=75' + 'test:coverage80' = 'test --coverage=80' + 'test:coverage85' = 'test --coverage=85' + 'test:ci' = 'test --coverage=85 --sort=coverage --group=folder' + 'test:full' = 'test --coverage=85 --verbosity=Detailed' + 'test:unit' = 'test --tag=unit' + 'test:integration' = 'test --tag=integration' + 'test:sorted' = 'test --sort=coverage' + 'test:grouped' = 'test --group=folder' + } } } } From 75c8b17b583db26b20703d330ee446cd73f471f7 Mon Sep 17 00:00:00 2001 From: Driss Date: Fri, 10 Jul 2026 14:21:12 +0100 Subject: [PATCH 2/5] Add Get-Scripts helper --- src/core/handlers.ps1 | 5 ++--- src/helpers/config.ps1 | 4 ++++ tests/helpers/config.tests.ps1 | 7 +++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/core/handlers.ps1 b/src/core/handlers.ps1 index ec3cee7..8b0e65d 100644 --- a/src/core/handlers.ps1 +++ b/src/core/handlers.ps1 @@ -502,11 +502,12 @@ function Invoke-Run { param ($arguments) $scriptName = $arguments[0] + $scripts = Get-Scripts if ([string]::IsNullOrWhiteSpace($scriptName)) { Write-Host -Object "`nPlease provide a script name to run: pvm run " -ForegroundColor Yellow Write-Host -Object "`nAvailable scripts:`n" -ForegroundColor Cyan - $scripts = $PVMConfig.defaults.scripts + $maxNameLength = ($scripts.Keys | Measure-Object -Maximum Length).Maximum + ($PVMConfig.env.MIN_PAD_RIGHT_LENGTH * 2) $scripts.Keys | ForEach-Object { $name = "$_ ".PadRight($maxNameLength, '.') @@ -516,8 +517,6 @@ function Invoke-Run { return -1 } - $scripts = $PVMConfig.defaults.scripts - if (-not $scripts.Contains($scriptName)) { Write-Host -Object "`nScript '$scriptName' not found." -ForegroundColor Yellow Write-Host -Object "`nAvailable scripts:`n" -ForegroundColor Cyan diff --git a/src/helpers/config.ps1 b/src/helpers/config.ps1 index 596f861..dcfb5f2 100644 --- a/src/helpers/config.ps1 +++ b/src/helpers/config.ps1 @@ -69,6 +69,10 @@ function Get-FlagMap { return $PVMConfig.defaults.flags } +function Get-Scripts { + return $PVMConfig.defaults.scripts +} + function Get-Config { param([string] $rootPath) diff --git a/tests/helpers/config.tests.ps1 b/tests/helpers/config.tests.ps1 index 51247ea..33b9a5f 100644 --- a/tests/helpers/config.tests.ps1 +++ b/tests/helpers/config.tests.ps1 @@ -219,6 +219,13 @@ Describe "Get-FlagMap" { } } +Describe "Get-Scripts" { + It "Returns PVMConfig.defaults.scripts" { + $result = Get-Scripts + $result.Count | Should -Be $PVMConfig.defaults.scripts.Count + } +} + Describe "Get-Config" { Context "When .env file exists" { BeforeAll { From c1ae215d5bf51c660395a7c3c59a8868ad432117 Mon Sep 17 00:00:00 2001 From: Driss Date: Fri, 10 Jul 2026 15:47:04 +0100 Subject: [PATCH 3/5] Add tests and refactor scripts list --- readme.md | 27 ++++++++--------------- src/core/router.ps1 | 7 +++--- src/helpers/config.ps1 | 13 ++++-------- tests/core/handlers.tests.ps1 | 40 +++++++++++++++++++++++++++++++++++ tests/core/router.tests.ps1 | 9 ++++++++ 5 files changed, 66 insertions(+), 30 deletions(-) diff --git a/readme.md b/readme.md index 8f944ef..ea6b789 100644 --- a/readme.md +++ b/readme.md @@ -384,19 +384,16 @@ pvm cache: PVM includes predefined scripts for common test scenarios, similar to npm scripts in package.json or composer scripts in composer.json. These scripts provide shortcuts for running tests with specific configurations without typing all the options each time. ```sh -# List all available scripts -pvm run - # Run a specific script pvm run # Examples: -pvm run test ....................... Runs all tests with default settings -pvm run test:coverage ............... Runs tests with 80% coverage target -pvm run test:ci ...................... Runs tests with coverage for CI environments -pvm run test:full ................... Runs tests with coverage and detailed output -pvm run test:unit ................... Runs only tests tagged as 'unit' -pvm run test:integration ............ Runs only tests tagged as 'integration' +pvm run test # ............... Runs all tests with default settings +pvm run test:quiet # ......... Runs tests with verbosity set to None +pvm run test:cov # ........... Runs tests with 80% coverage target +pvm run test:cov80 # ......... Runs tests with 80% coverage target +pvm run test:cov90 # ......... Runs tests with 90% coverage target +pvm run test:full # .......... Runs tests with coverage and detailed output ``` **Available Scripts:** @@ -404,17 +401,11 @@ pvm run test:integration ............ Runs only tests tagged as 'integration' | Script Name | Command | Description | |-------------|---------|-------------| | `test` | `pvm test` | Runs all tests with default settings | -| `test:minimal` | `pvm test --verbosity=None` | Runs tests with minimal output | | `test:quiet` | `pvm test --verbosity=None` | Alias for test:minimal | -| `test:coverage` | `pvm test --coverage=75` | Runs tests with 75% coverage target | -| `test:coverage80` | `pvm test --coverage=80` | Runs tests with 80% coverage target | -| `test:coverage85` | `pvm test --coverage=85` | Runs tests with 85% coverage target | -| `test:ci` | `pvm test --coverage=85 --sort=coverage --group=folder` | CI-optimized test run (matches GitHub Actions) | +| `test:cov` | `pvm test --coverage=75` | Runs tests with 75% coverage target | +| `test:cov80` | `pvm test --coverage=80` | Runs tests with 80% coverage target | +| `test:cov90` | `pvm test --coverage=90` | Runs tests with 90% coverage target | | `test:full` | `pvm test --coverage=85 --verbosity=Detailed` | Runs tests with coverage and detailed output | -| `test:unit` | `pvm test --tag=unit` | Runs only tests tagged as 'unit' | -| `test:integration` | `pvm test --tag=integration` | Runs only tests tagged as 'integration' | -| `test:sorted` | `pvm test --sort=coverage` | Runs tests and sorts results by coverage | -| `test:grouped` | `pvm test --group=folder` | Runs tests and groups results by folder | **Customizing Scripts:** diff --git a/src/core/router.ps1 b/src/core/router.ps1 index adc7283..cc8d702 100644 --- a/src/core/router.ps1 +++ b/src/core/router.ps1 @@ -355,9 +355,10 @@ function Get-Actions { ) EXAMPLES = @( 'pvm run test ............... Runs all tests with default settings' - 'pvm run test:coverage ..... Runs tests with 80% coverage target' - 'pvm run test:ci ............ Runs tests with coverage for CI environments' - 'pvm run test:fast .......... Runs tests excluding slow tests' + 'pvm run test:quiet ......... Runs tests with 80% coverage target' + 'pvm run test:cov ........... Runs tests with 75% coverage target' + 'pvm run test:cov80 ......... Runs tests with 80% coverage target' + 'pvm run test:cov90 ......... Runs tests with 90% coverage target' 'pvm run test:full .......... Runs tests with coverage and detailed output' ) } diff --git a/src/helpers/config.ps1 b/src/helpers/config.ps1 index dcfb5f2..431fcba 100644 --- a/src/helpers/config.ps1 +++ b/src/helpers/config.ps1 @@ -162,15 +162,10 @@ function Get-Config { scripts = [ordered]@{ 'test' = 'test' 'test:quiet' = 'test --verbosity=None' - 'test:coverage' = 'test --coverage=75' - 'test:coverage80' = 'test --coverage=80' - 'test:coverage85' = 'test --coverage=85' - 'test:ci' = 'test --coverage=85 --sort=coverage --group=folder' - 'test:full' = 'test --coverage=85 --verbosity=Detailed' - 'test:unit' = 'test --tag=unit' - 'test:integration' = 'test --tag=integration' - 'test:sorted' = 'test --sort=coverage' - 'test:grouped' = 'test --group=folder' + 'test:cov' = 'test --coverage=75' + 'test:cov80' = 'test --coverage=80' + 'test:cov90' = 'test --coverage=90' + 'test:full' = 'test --coverage=85 --verbosity=Detailed --sort=coverage --group=folder' } } } diff --git a/tests/core/handlers.tests.ps1 b/tests/core/handlers.tests.ps1 index b5252dd..2d5cc99 100644 --- a/tests/core/handlers.tests.ps1 +++ b/tests/core/handlers.tests.ps1 @@ -1422,3 +1422,43 @@ Describe "Invoke-Update Tests" { Should -Invoke Update-PVM -Times 1 } } + +Describe "Invoke-Run Tests" { + BeforeAll { + Mock Get-Actions { + [ordered]@{ + 'install' = @{ action = { return 0 } } + 'list' = @{ action = { return 0 } } + 'ini' = @{ action = { return 0 } } + 'cache' = @{ action = { return 0 } } + 'test' = @{ action = { return 0 } } + } + } + } + + It "Should return -1 when no arguments are provided" { + $result = Invoke-Run -arguments @() + $result | Should -Be -1 + } + + It "Should return -1 when an unknown script is provided" { + Mock Get-Scripts { return @{ 'script1' = 'command1'; 'script2' = 'command2' } } + + $result = Invoke-Run -arguments @('unknown') + $result | Should -Be -1 + } + + It "Should return -1 when an unknown command is provided" { + Mock Get-Scripts { return @{ 'cmd1:arg1' = 'command1'; 'cmd2:arg2' = 'command2' } } + + $result = Invoke-Run -arguments @('cmd1:arg1') + $result | Should -Be -1 + } + + It "Should return 0 when a valid script is provided" { + Mock Get-Scripts { return @{ 'test:cov' = 'test --coverage'; 'test:quiet' = 'test --verbosity=None' } } + + $result = Invoke-Run -arguments @('test:cov') + $result | Should -Be 0 + } +} diff --git a/tests/core/router.tests.ps1 b/tests/core/router.tests.ps1 index b928a9b..c7bfea1 100644 --- a/tests/core/router.tests.ps1 +++ b/tests/core/router.tests.ps1 @@ -22,6 +22,7 @@ Describe "Get-Actions Tests" { Mock Invoke-Cache { } Mock Invoke-Aliases { } Mock Invoke-Update { } + Mock Invoke-Run { } } It "Should return ordered hashtable with all actions" { @@ -44,6 +45,7 @@ Describe "Get-Actions Tests" { $actions.Keys | Should -Contain 'test' $actions.Keys | Should -Contain 'log' $actions.Keys | Should -Contain 'update' + $actions.Keys | Should -Contain 'run' } It "Should set script-level arguments variable" { @@ -174,6 +176,13 @@ Describe "Get-Actions Tests" { Should -Invoke Invoke-Update -Times 1 } + + It "Should execute run action" { + $actions = Get-Actions -arguments @() + $actions['run'].action.Invoke() + + Should -Invoke Invoke-Run -Times 1 + } } } From 7d7ce66d2549044c2121fcbb904d78cebdbc6504 Mon Sep 17 00:00:00 2001 From: Driss Date: Fri, 10 Jul 2026 20:39:40 +0100 Subject: [PATCH 4/5] Remove useless item --- readme.md | 3 +-- src/core/router.ps1 | 3 +-- src/helpers/config.ps1 | 1 - 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index ea6b789..f12e549 100644 --- a/readme.md +++ b/readme.md @@ -400,8 +400,7 @@ pvm run test:full # .......... Runs tests with coverage and detailed output | Script Name | Command | Description | |-------------|---------|-------------| -| `test` | `pvm test` | Runs all tests with default settings | -| `test:quiet` | `pvm test --verbosity=None` | Alias for test:minimal | +| `test:quiet` | `pvm test --verbosity=None` | Runs tests with verbosity set to None | | `test:cov` | `pvm test --coverage=75` | Runs tests with 75% coverage target | | `test:cov80` | `pvm test --coverage=80` | Runs tests with 80% coverage target | | `test:cov90` | `pvm test --coverage=90` | Runs tests with 90% coverage target | diff --git a/src/core/router.ps1 b/src/core/router.ps1 index cc8d702..fb41dbe 100644 --- a/src/core/router.ps1 +++ b/src/core/router.ps1 @@ -354,8 +354,7 @@ function Get-Actions { 'Scripts are shortcuts for common commands with predefined options.' ) EXAMPLES = @( - 'pvm run test ............... Runs all tests with default settings' - 'pvm run test:quiet ......... Runs tests with 80% coverage target' + 'pvm run test:quiet ......... Runs tests with verbosity set to None' 'pvm run test:cov ........... Runs tests with 75% coverage target' 'pvm run test:cov80 ......... Runs tests with 80% coverage target' 'pvm run test:cov90 ......... Runs tests with 90% coverage target' diff --git a/src/helpers/config.ps1 b/src/helpers/config.ps1 index 431fcba..30be387 100644 --- a/src/helpers/config.ps1 +++ b/src/helpers/config.ps1 @@ -160,7 +160,6 @@ function Get-Config { '-h' = 'help' } scripts = [ordered]@{ - 'test' = 'test' 'test:quiet' = 'test --verbosity=None' 'test:cov' = 'test --coverage=75' 'test:cov80' = 'test --coverage=80' From 86a255a2763b85e7fb9091aa5e603380c0e85269 Mon Sep 17 00:00:00 2001 From: Driss Date: Fri, 10 Jul 2026 21:09:10 +0100 Subject: [PATCH 5/5] Store scripts in json file --- src/actions/setup.ps1 | 9 ++++++ src/helpers/config.ps1 | 26 ++++++++++++++++ tests/actions/setup.tests.ps1 | 7 +++++ tests/helpers/config.tests.ps1 | 55 +++++++++++++++++++++++++++++++--- 4 files changed, 93 insertions(+), 4 deletions(-) diff --git a/src/actions/setup.ps1 b/src/actions/setup.ps1 index dc64f26..80d4df6 100644 --- a/src/actions/setup.ps1 +++ b/src/actions/setup.ps1 @@ -100,6 +100,15 @@ function Initialize-PVMFiles { Write-Host -Object "`nFailed to create aliases list." -ForegroundColor DarkYellow } + $codes += $code = Set-Scripts-List + if ($code -eq 0) { + Write-Host -Object "`nScripts list created successfully at '$($PVMConfig.paths.scriptsList)'." -ForegroundColor DarkGreen + Write-Host -Object "- Use 'pvm scripts' to see available scripts." -ForegroundColor Gray + Write-Host -Object "- Feel free to modify it." -ForegroundColor Gray + } else { + Write-Host -Object "`nFailed to create scripts list." -ForegroundColor DarkYellow + } + return $codes } diff --git a/src/helpers/config.ps1 b/src/helpers/config.ps1 index 30be387..d65bcc3 100644 --- a/src/helpers/config.ps1 +++ b/src/helpers/config.ps1 @@ -69,7 +69,32 @@ function Get-FlagMap { return $PVMConfig.defaults.flags } +function Set-Scripts-List { + try { + $jsonContent = $PVMConfig.defaults.scripts | ConvertTo-Json -Depth 10 + Set-Content -Path $PVMConfig.paths.scriptsList -Value $jsonContent -Encoding UTF8 + + return 0 + } catch { + $null = Log-Data -data @{ header = "$($MyInvocation.MyCommand.Name) - Failed to create scripts list"; exception = $_ } + return -1 + } +} + function Get-Scripts { + try { + if (Is-File-Exists -path $PVMConfig.paths.scriptsList) { + $data = (Get-Content -Path $PVMConfig.paths.scriptsList -Raw | ConvertFrom-Json) + if ($null -ne $data) { + $ordered = [ordered]@{} + $data.PSObject.Properties | ForEach-Object { $ordered[$_.Name] = $_.Value } + if ($ordered.Count -gt 0) { return $ordered } + } + } + } catch { + $null = Log-Data -data @{ header = "$($MyInvocation.MyCommand.Name) - Failed to get scripts list"; exception = $_ } + } + return $PVMConfig.defaults.scripts } @@ -98,6 +123,7 @@ function Get-Config { profileTemplate = "$templates\profile-template.json" zendExtensionsList = "$templates\zend_extensions.json" aliasesList = "$templates\aliases.json" + scriptsList = "$templates\scripts.json" log = $logs logError = "$logs\error.log" pathVarBackup = "$logs\path.bak.log" diff --git a/tests/actions/setup.tests.ps1 b/tests/actions/setup.tests.ps1 index f47e5c0..18f9a47 100644 --- a/tests/actions/setup.tests.ps1 +++ b/tests/actions/setup.tests.ps1 @@ -221,6 +221,7 @@ Describe "Initialize-PVMFiles" { Mock Create-Profile-Template { return 0 } Mock Set-Zend-Extensions-List { return 0 } Mock Set-Aliases-List { return 0 } + Mock Set-Scripts-List { return 0 } } It "Returns -1 when the example profile creation fails" { @@ -246,6 +247,12 @@ Describe "Initialize-PVMFiles" { $result = Setup-Environment-Directories-And-Files $result | Should -Be -1 } + + It "Returns -1 when the scripts file creation fails" { + Mock Set-Scripts-List { return -1 } + $result = Setup-Environment-Directories-And-Files + $result | Should -Be -1 + } } Describe "Setup-Environment-Directories-And-Files" { diff --git a/tests/helpers/config.tests.ps1 b/tests/helpers/config.tests.ps1 index 33b9a5f..454f41b 100644 --- a/tests/helpers/config.tests.ps1 +++ b/tests/helpers/config.tests.ps1 @@ -1,4 +1,4 @@ - + BeforeAll { Mock Write-Host {} $script:PVMRootBackup = $PVMRoot @@ -185,7 +185,7 @@ Describe "Get-Aliases" { $script:TEMPLATES_PATH = $PVMConfig.paths.templates = 'TestDrive:\\storage\data\templates' $script:ALIASES_LIST_PATH = $PVMConfig.paths.aliasesList = "$TEMPLATES_PATH\aliases.json" New-Item -ItemType Directory -Path $script:TEMPLATES_PATH | Out-Null - $testContent = [ordered]@{'?' = 'help'; 'i' = 'install'; 'init' = 'setup'} + $testContent = [ordered]@{'?' = 'help'; 'i' = 'install'; 'init' = 'setup'} $testContent | ConvertTo-Json -Depth 10 | Set-Content -Path $ALIASES_LIST_PATH $script:DEFAULT_ALIASES = $PVMConfig.defaults.aliases } @@ -219,10 +219,57 @@ Describe "Get-FlagMap" { } } +Describe "Set-Scripts-List" { + BeforeAll { + $script:TEMPLATES_PATH = $PVMConfig.paths.templates = 'TestDrive:\\storage\data\templates' + $PVMConfig.paths.scriptsList = "$TEMPLATES_PATH\scripts.json" + New-Item -ItemType Directory -Force -Path $script:TEMPLATES_PATH | Out-Null + $script:DEFAULT_SCRIPTS = $PVMConfig.defaults.scripts + } + + It "Creates scripts.json" { + $result = Set-Scripts-List + $result | Should -Be 0 + + $result = Get-Scripts + $result.Count | Should -Be $DEFAULT_SCRIPTS.Count + } + + It "Returns -1 when exception is thrown" { + Mock Set-Content { throw 'Test exception' } + $result = Set-Scripts-List + $result | Should -Be -1 + } +} + Describe "Get-Scripts" { - It "Returns PVMConfig.defaults.scripts" { + BeforeAll { + $script:TEMPLATES_PATH = $PVMConfig.paths.templates = 'TestDrive:\\storage\data\templates' + $script:SCRIPTS_LIST_PATH = $PVMConfig.paths.scriptsList = "$TEMPLATES_PATH\scripts.json" + New-Item -ItemType Directory -Path $script:TEMPLATES_PATH | Out-Null + $testContent = [ordered]@{'test:quiet' = 'test --verbosity=None'; 'test:cov' = 'test --coverage=75'} + $testContent | ConvertTo-Json -Depth 10 | Set-Content -Path $SCRIPTS_LIST_PATH + $script:DEFAULT_SCRIPTS = $PVMConfig.defaults.scripts + } + + It "Returns scripts from scripts.json or PVMConfig.defaults.scripts" { + $result = Get-Scripts + $result.Count | Should -Be 2 + $result['test:quiet'] | Should -Be 'test --verbosity=None' + $result['test:cov'] | Should -Be 'test --coverage=75' + } + + It "Falls back to DEFAULT_SCRIPTS value" { + Remove-Item -Path "$script:TEMPLATES_PATH\scripts.json" + $result = Get-Scripts + $result.Count | Should -Be $DEFAULT_SCRIPTS.Count + } + + It "Returns default value when exception is thrown" { + Mock Is-File-Exists { return $true } + Mock Get-Content { throw 'Test exception' } $result = Get-Scripts - $result.Count | Should -Be $PVMConfig.defaults.scripts.Count + $result.Count | Should -Be $DEFAULT_SCRIPTS.Count } }