Skip to content

Commit

Permalink
Define exit codes as constants
Browse files Browse the repository at this point in the history
  • Loading branch information
tak-sakumoto committed Feb 10, 2024
1 parent 0bb9bab commit 89ff303
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
14 changes: 9 additions & 5 deletions src/Main.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ Import-Module Pester

Describe "Main.ps1" {
BeforeAll {
# Constants
"$PSScriptRoot\Set-ConstsForMain.ps1"

# Target script
$targetScript = "$PSScriptRoot\..\src\Main.ps1"

Expand All @@ -25,7 +28,7 @@ Describe "Main.ps1" {
& $targetScript -listPath $listPath

# Assert
$LASTEXITCODE | Should -Be 1
$LASTEXITCODE | Should -Be $EXIT_PARAM_CSV_EMPTY
}
}

Expand All @@ -40,21 +43,22 @@ Describe "Main.ps1" {
& $targetScript -listPath $listPath

# Assert
$LASTEXITCODE | Should -Be 1
$LASTEXITCODE | Should -Be $EXIT_PARAM_CSV_INVALID
}
}

Context "When the default parent path does not exist" {
It "Should exit with a non-zero code" {
# Arrange
$listPath = "$listDirPath\list.csv"
New-Item -Path $listPath
$defaultParent = "$testDirPath\absent"

# Act
& $targetScript -listPath $listPath -defaultParent $defaultParent

# Assert
$LASTEXITCODE | Should -Be 1
$LASTEXITCODE | Should -Be $EXIT_PARAM_PARENT_INVALID
}
}

Expand All @@ -78,7 +82,7 @@ Describe "Main.ps1" {
& $targetScript -listPath $listPath -defaultParent $defaultParent

# Assert
$LASTEXITCODE | Should -Be 0
$LASTEXITCODE | Should -Be $EXIT_SUCCESS
$shortcutPath = "$parent\$name.lnk"
$shortcutPath | Should -Not -Exist
}
Expand All @@ -105,7 +109,7 @@ Describe "Main.ps1" {
& $targetScript -listPath $listPath -defaultParent $defaultParent

# Assert
$LASTEXITCODE | Should -Be 0
$LASTEXITCODE | Should -Be $EXIT_SUCCESS
$shortcutPath = "$parent\$name.lnk"
$shortcutPath | Should -Exist
}
Expand Down
11 changes: 7 additions & 4 deletions src/Main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ param (
[string]$defaultParent = ".\"
)

# Constants
"$PSScriptRoot\Set-ConstsForMain.ps1"

# Dot sourcing
. "$PSScriptRoot\New-Shortcut.ps1"
. "$PSScriptRoot\Set-RegexEnvVars.ps1"
Expand All @@ -13,21 +16,21 @@ param (
if ([string]::IsNullOrEmpty($listPath)) {
# Display an error message and exit with a non-zero code
Write-Host "Error: CSV file parameter is empty"
exit 1
exit $EXIT_PARAM_CSV_EMPTY
}

# Check if the list path is invalid
if (!(Test-Path $listPath)) {
# Display an error message and exit with a non-zero code
Write-Host "Error: $listPath does not exist"
exit 1
exit $EXIT_PARAM_CSV_INVALID
}

# Check if the default parent path is invalid
if (!(Test-Path $defaultParent)) {
# Display an error message and exit with a non-zero code
Write-Host "Error: $defaultParent does not exist"
exit 1
exit $EXIT_PARAM_PARENT_INVALID
}

# Get listed paths
Expand Down Expand Up @@ -78,4 +81,4 @@ foreach ($line in $data) {
}

# Exit with a success code
exit 0
exit $EXIT_SUCCESS
5 changes: 5 additions & 0 deletions src/Set-ConstsForMain.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Define exit codes
Set-Variable -Name EXIT_SUCCESS -Value 0 -Option constant
Set-Variable -Name EXIT_PARAM_CSV_EMPTY -Value 1 -Option constant
Set-Variable -Name EXIT_PARAM_CSV_INVALID -Value 2 -Option constant
Set-Variable -Name EXIT_PARAM_PARENT_INVALID -Value 3 -Option Constant

0 comments on commit 89ff303

Please sign in to comment.