Skip to content

Commit

Permalink
add check for valid version
Browse files Browse the repository at this point in the history
  • Loading branch information
motdotla committed Jun 15, 2024
1 parent 140e9aa commit e5227cc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
20 changes: 20 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ usage() {
}

# machine checks ------------------------
is_version_valid() {
if [ -z "$VERSION" ]; then
echo "[INSTALLATION_FAILED] VERSION is blank in install.sh script"
echo "? set VERSION to valid semantic semver version and try again"

return 1
fi

local semver_regex="^([0-9]+)\.([0-9]+)\.([0-9]+)$"
if [[ "$VERSION" =~ $semver_regex ]]; then
return 0
else
echo "[INSTALLATION_FAILED] VERSION is not a valid semantic version in install.sh script"
echo "? set VERSION to valid semantic semver version and try again"

return 1
fi
}

is_directory_writable() {
# check installation directory is writable
if [ ! -w "$(directory)" ]; then
Expand Down Expand Up @@ -392,6 +411,7 @@ run() {
done

# machine checks
is_version_valid
is_directory_writable
is_curl_installed
is_os_supported
Expand Down
59 changes: 48 additions & 11 deletions spec/install_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Describe 'install.sh'
Include install.sh

setup() {
VERSION="0.44.1"
VERSION="0.44.2"
DIRECTORY="./spec/tmp"
}

Expand Down Expand Up @@ -53,7 +53,7 @@ Describe 'install.sh'
Describe 'default values'
It 'checks default VERSION'
When call echo "$VERSION"
The output should equal "0.44.1"
The output should equal "0.44.2"
End

It 'checks default DIRECTORY'
Expand Down Expand Up @@ -167,7 +167,7 @@ install dotenvx – a better dotenv
Options:
--directory directory to install dotenvx to (default: \"/usr/local/bin\")
--version version of dotenvx to install (default: \"0.44.1\")
--version version of dotenvx to install (default: \"0.44.2\")
Commands:
install install dotenvx
Expand All @@ -191,6 +191,43 @@ Commands:
End
End

Describe 'is_version_valid()'
It 'is true (0)'
When call is_version_valid
The status should equal 0
End

Describe 'when VERSION blank'
mock_version_blank() {
VERSION=""
}

Before mock_version_blank

It 'is false'
When call is_version_valid
The status should equal 1
The output should equal "[INSTALLATION_FAILED] VERSION is blank in install.sh script
? set VERSION to valid semantic semver version and try again"
End
End

Describe 'when VERSION invalid'
mock_version_invalid() {
VERSION="22"
}

Before mock_version_invalid

It 'is false'
When call is_version_valid
The status should equal 1
The output should equal "[INSTALLATION_FAILED] VERSION is not a valid semantic version in install.sh script
? set VERSION to valid semantic semver version and try again"
End
End
End

Describe 'is_directory_writable()'
It 'is true (0)'
When call is_directory_writable
Expand Down Expand Up @@ -271,15 +308,15 @@ Commands:
It 'returns the combined values'
When call filename
The status should equal 0
The output should equal "dotenvx-0.44.1-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m | tr '[:upper:]' '[:lower:]').tar.gz"
The output should equal "dotenvx-0.44.2-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m | tr '[:upper:]' '[:lower:]').tar.gz"
End
End

Describe 'download_url()'
It 'returns the combined values'
When call download_url
The status should equal 0
The output should equal "https://registry.npmjs.org/@dotenvx/dotenvx-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m | tr '[:upper:]' '[:lower:]')/-/dotenvx-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m | tr '[:upper:]' '[:lower:]')-0.44.1.tgz"
The output should equal "https://registry.npmjs.org/@dotenvx/dotenvx-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m | tr '[:upper:]' '[:lower:]')/-/dotenvx-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m | tr '[:upper:]' '[:lower:]')-0.44.2.tgz"
End
End

Expand Down Expand Up @@ -319,7 +356,7 @@ Commands:
It 'returns true and outputs a message'
When call is_installed
The status should equal 0
The output should equal "[dotenvx@0.44.1] already installed (./spec/tmp/dotenvx)"
The output should equal "[dotenvx@0.44.2] already installed (./spec/tmp/dotenvx)"
End
End
End
Expand Down Expand Up @@ -380,7 +417,7 @@ Commands:
It 'installs it'
When call install_dotenvx
The status should equal 0
The output should equal "[dotenvx@0.44.1] installed successfully (./spec/tmp/dotenvx)"
The output should equal "[dotenvx@0.44.2] installed successfully (./spec/tmp/dotenvx)"
End

Describe 'when a different path'
Expand All @@ -391,7 +428,7 @@ Commands:
It 'installs it but warns'
When call install_dotenvx
The status should equal 0
The output should equal "[dotenvx@0.44.1] installed successfully (./spec/tmp/dotenvx)"
The output should equal "[dotenvx@0.44.2] installed successfully (./spec/tmp/dotenvx)"
The stderr should equal "[DOTENVX_CONFLICT] conflicting dotenvx found at /different/path
? we recommend updating your path to include ./spec/tmp"
End
Expand All @@ -406,7 +443,7 @@ Commands:
It 'installs dotenvx'
When call run
The status should equal 0
The output should equal "[dotenvx@0.44.1] installed successfully (./spec/tmp/dotenvx)"
The output should equal "[dotenvx@0.44.2] installed successfully (./spec/tmp/dotenvx)"
End

Describe 'when a different path'
Expand All @@ -417,7 +454,7 @@ Commands:
It 'installs it but warns'
When call run
The status should equal 0
The output should equal "[dotenvx@0.44.1] installed successfully (./spec/tmp/dotenvx)"
The output should equal "[dotenvx@0.44.2] installed successfully (./spec/tmp/dotenvx)"
The stderr should equal "[DOTENVX_CONFLICT] conflicting dotenvx found at /different/path
? we recommend updating your path to include ./spec/tmp"
End
Expand All @@ -429,7 +466,7 @@ Commands:
It 'says already installed'
When call run
The status should equal 0
The output should equal "[dotenvx@0.44.1] already installed (./spec/tmp/dotenvx)"
The output should equal "[dotenvx@0.44.2] already installed (./spec/tmp/dotenvx)"
End
End
End
Expand Down

0 comments on commit e5227cc

Please sign in to comment.