Skip to content

feat: add more tests #282

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

Merged
merged 14 commits into from
Jul 7, 2024
73 changes: 37 additions & 36 deletions helm-git-plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -243,25 +243,10 @@ helm_inspect_name() {
[ -n "$name" ]
}

# main(raw_uri)
main() {
trace "args: $*"
helm_args="" # "$1 $2 $3"
_raw_uri=$4 # eg: git+https://git.com/user/repo@path/to/charts/index.yaml?ref=master

# If defined, use $HELM_GIT_HELM_BIN as $HELM_BIN.
if [ -n "${HELM_GIT_HELM_BIN:-}" ]; then
export HELM_BIN="${HELM_GIT_HELM_BIN}"
# If not, use $HELM_BIN after sanitizing it or default to 'helm'.
elif
[ -z "$HELM_BIN" ] ||
# terraform-provider-helm: https://github.com/aslafy-z/helm-git/issues/101
echo "$HELM_BIN" | grep -q "terraform-provider-helm" ||
# helm-diff plugin: https://github.com/aslafy-z/helm-git/issues/107
echo "$HELM_BIN" | grep -q "diff"
then
export HELM_BIN="helm"
fi
# parse_uri(raw_uri)
# sets git_repo, git_ref, helm_dir, helm_file, git_sparse, helm_depupdate, helm_package
parse_uri() {
_raw_uri=$1

# Parse URI
string_starts "$_raw_uri" "$url_prefix" ||
Expand All @@ -271,44 +256,37 @@ main() {
readonly URI_REGEX='^([^:/?#]+):(//((([^/?#]+)@)?([^/?#]+)?))?(/([^?#]*))(\?([^#]*))?(#(.*))?$'

_uri_scheme=$(echo "$_raw_uri" | sed -Ene "s'$URI_REGEX'\1'p")
readonly _uri_scheme
trace "_uri_scheme: $_uri_scheme"

_uri_authority=$(echo "$_raw_uri" | sed -Ene "s'$URI_REGEX'\3'p")
readonly _uri_authority
trace "_uri_authority: $_uri_authority"

_uri_path=$(echo "$_raw_uri" | sed -Ene "s'$URI_REGEX'\8'p")
readonly _uri_path
trace "_uri_path: $_uri_path"

_uri_query=$(echo "$_raw_uri" | sed -Ene "s'$URI_REGEX'\9'p")
readonly _uri_query
trace "_uri_query: $_uri_query"

git_scheme=$(echo "$_uri_scheme" | sed -e 's/^git+//')
readonly git_scheme="$git_scheme"
trace "git_scheme: $git_scheme"
string_contains "$allowed_protocols" "$git_scheme" ||
_git_scheme=$(echo "$_uri_scheme" | sed -e 's/^git+//')
trace "_git_scheme: $_git_scheme"
string_contains "$allowed_protocols" "$_git_scheme" ||
error "$error_invalid_protocol"

git_repo_path=$(echo "${_uri_path}" | cut -d'@' -f 1)
readonly git_repo_path
trace "git_repo_path: $git_repo_path"
_git_path=$(echo "${_uri_path}" | cut -d'@' -f 1)
trace "_git_path: $_git_path"

git_file_path=$(echo "${_uri_path}" | cut -d'@' -f 2)
readonly git_file_path
trace "git_file_path: $git_file_path"
_git_file_path=$(echo "${_uri_path}" | cut -d'@' -f 2)
trace "_git_file_path: $_git_file_path"

helm_dir=$(dirname "${git_file_path}" | sed -r '/^[\.|/]$/d')
helm_dir=$(dirname "${_git_file_path}" | sed -r '/^[\.|/]$/d')
readonly helm_dir
trace "helm_dir: $helm_dir"

helm_file=$(basename "${git_file_path}")
helm_file=$(basename "${_git_file_path}")
readonly helm_file
trace "helm_file: $helm_file"

git_repo="${git_scheme}://${_uri_authority}/${git_repo_path}"
git_repo="${_git_scheme}://${_uri_authority}/${_git_path}"
readonly git_repo
trace "git_repo: $git_repo"

Expand All @@ -335,6 +313,29 @@ main() {
[ -z "$helm_package" ] && helm_package=1
readonly helm_package
trace "helm_package: $helm_package"
}

# main(cert_file, key_file, ca_file, raw_uri)
main() {
trace "args: $*"
helm_args="" # "$1 $2 $3"
_raw_uri=$4 # eg: git+https://git.com/user/repo@path/to/charts/index.yaml?ref=master

# If defined, use $HELM_GIT_HELM_BIN as $HELM_BIN.
if [ -n "${HELM_GIT_HELM_BIN:-}" ]; then
export HELM_BIN="${HELM_GIT_HELM_BIN}"
# If not, use $HELM_BIN after sanitizing it or default to 'helm'.
elif
[ -z "$HELM_BIN" ] ||
# terraform-provider-helm: https://github.com/aslafy-z/helm-git/issues/101
echo "$HELM_BIN" | grep -q "terraform-provider-helm" ||
# helm-diff plugin: https://github.com/aslafy-z/helm-git/issues/107
echo "$HELM_BIN" | grep -q "diff"
then
export HELM_BIN="helm"
fi

parse_uri "$_raw_uri"

debug "repo: ${git_repo} ref: ${git_ref} path: ${helm_dir} file: ${helm_file} sparse: ${git_sparse} depupdate: ${helm_depupdate} package: ${helm_package}"
readonly helm_repo_uri="git+${git_repo}@${helm_dir}?ref=${git_ref}&sparse=${git_sparse}&depupdate=${helm_depupdate}&package=${helm_package}"
Expand Down
88 changes: 88 additions & 0 deletions tests/04-uri-parsing.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env bats

load 'test-helper'

@test "should parse with no options" {
parse_uri "git+https://github.com/jetstack/cert-manager@deploy/charts/index.yaml"
[ $git_repo = "https://github.com/jetstack/cert-manager" ]
[ $helm_dir = "deploy/charts" ]
}

@test "should parse with empty ref" {
parse_uri "git+https://github.com/jetstack/cert-manager@deploy/charts/index.yaml?ref="
[ $git_repo = "https://github.com/jetstack/cert-manager" ]
[ $helm_dir = "deploy/charts" ]
[ $git_ref = "master" ]
}

@test "should parse with ref" {
parse_uri "git+https://github.com/jetstack/cert-manager@deploy/charts/index.yaml?ref=foo"
[ $git_repo = "https://github.com/jetstack/cert-manager" ]
[ $helm_dir = "deploy/charts" ]
[ $git_ref = "foo" ]
}

@test "should parse with username" {
parse_uri "git+https://git@github.com/jetstack/cert-manager@deploy/charts/index.yaml?ref=master"
[ $git_repo = "https://git@github.com/jetstack/cert-manager" ]
[ $helm_dir = "deploy/charts" ]
[ $git_ref = "master" ]
}

@test "should parse with username and password" {
parse_uri "git+https://git:foo%2Fbar@github.com/jetstack/cert-manager@deploy/charts/index.yaml?ref=master"
[ $git_repo = "https://git:foo%2Fbar@github.com/jetstack/cert-manager" ]
[ $helm_dir = "deploy/charts" ]
[ $git_ref = "master" ]
}

@test "should parse with sparse disabled" {
parse_uri "git+https://github.com/jetstack/cert-manager@deploy/charts/index.yaml?ref=master&sparse=0"
[ $git_repo = "https://github.com/jetstack/cert-manager" ]
[ $helm_dir = "deploy/charts" ]
[ $git_ref = "master" ]
[ $git_sparse = 0 ]
}

@test "should parse with sparse enabled" {
parse_uri "git+https://github.com/jetstack/cert-manager@deploy/charts/index.yaml?ref=master&sparse=1"
[ $git_repo = "https://github.com/jetstack/cert-manager" ]
[ $helm_dir = "deploy/charts" ]
[ $git_ref = "master" ]
[ $git_sparse = 1 ]
}

@test "should parse with forward slash in ref" {
parse_uri "git+https://github.com/jaroslaw-osmanski/helm-git-test@test-chart/index.yaml?ref=feature/feature-test"
[ $git_repo = "https://github.com/jaroslaw-osmanski/helm-git-test" ]
[ $helm_dir = "test-chart" ]
[ $git_ref = "feature/feature-test" ]
}

@test "should parse with leading forward slash in path" {
parse_uri "git+https://github.com/jaroslaw-osmanski/helm-git-test@/test-chart/index.yaml?ref=feature/feature-test"
[ $git_repo = "https://github.com/jaroslaw-osmanski/helm-git-test" ]
[ $helm_dir = "/test-chart" ]
[ $git_ref = "feature/feature-test" ]
}

@test "should parse with empty path without slash" {
parse_uri "git+https://github.com/hashicorp/vault-helm@index.yaml?ref=v0.5.0"
[ $git_repo = "https://github.com/hashicorp/vault-helm" ]
[ -z $helm_dir ]
[ $git_ref = "v0.5.0" ]
}

@test "should parse with empty path with slash" {
parse_uri "git+https://github.com/hashicorp/vault-helm@/index.yaml?ref=v0.5.0"
[ $git_repo = "https://github.com/hashicorp/vault-helm" ]
[ -z $helm_dir ]
[ $git_ref = "v0.5.0" ]
}

@test "should parse GitLab-style multi-level repos" {
parse_uri "git+https://gitlab.com/gitlab-org/charts/gitlab@charts/gitlab/index.yaml?ref=master"
[ $git_repo = "https://gitlab.com/gitlab-org/charts/gitlab" ]
[ $helm_dir = "charts/gitlab" ]
[ $git_ref = "master" ]
}
Loading