Skip to content
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

fix k8s context checking #334

Merged
merged 6 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ We run the test workflow on:
* `**.fish` files with the exception `conf.d/pure.fish`, as we have dedicated mechanism to manage versions bump ;
* and workflow (`*.yml`) files

## Bumping version
## Versioning

**required:** You MUST **follow semantic versioning** based on [conventional commits][coco].
**required:** Commits must **[follow conventional commits convention][coco]**.

Is triggered only on `master` and:
Versioning is done automatically based on commit messages and triggered only on `master` branch.

Details:

1. We compute the [project's next version][next-version] using a GitHub Action ;
2. Then update `$pure_version` value in `./conf.d/pure.fish` ;
Expand All @@ -29,3 +31,4 @@ The `add-version-tag.yml` pipeline is triggered only for `master` when `./conf.d

[next-version]: https://github.com/thenativeweb/get-next-version
[push]: https://github.com/ad-m/github-push-action
[coco]: https://www.conventionalcommits.org/en/v1.0.0/
8 changes: 7 additions & 1 deletion functions/_pure_k8s_namespace.fish
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function _pure_k8s_namespace
kubectl config view --minify --output 'jsonpath={..namespace}' 2>/dev/null
set namespace (kubectl config view --minify --output 'jsonpath={..namespace}' 2>/dev/null)

if test -z "$namespace"
set namespace default
end

echo $namespace
end
11 changes: 5 additions & 6 deletions functions/_pure_prompt_k8s.fish
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ function _pure_prompt_k8s
and _pure_check_availability pure_enable_k8s kubectl
and test -n (_pure_k8s_context) # todo: use $(cmd) syntax when Fish 3.3.1 is dropped

set --local symbol (_pure_set_color $pure_color_k8s_prefix)$pure_symbol_k8s_prefix
set --local context (_pure_set_color $pure_color_k8s_context)(_pure_k8s_context)
set --local namespace (_pure_set_color $pure_color_k8s_namespace)(_pure_k8s_namespace)

if test -n "$namespace"
set --local namespace (_pure_set_color $pure_color_k8s_namespace)default
end
Comment on lines -11 to -13
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bobsoppe I move this logic in functions/_pure_k8s_namespace.fish also changed the flag to -z as I reckon you want the default value when no namespace is set.

Fish doc Operators for text strings

 -n STRING
    Returns true if the length of STRING is non-zero.

-z STRING
    Returns true if the length of STRING is zero.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

if test -n "$context"
set --local symbol (_pure_set_color $pure_color_k8s_prefix)$pure_symbol_k8s_prefix
set --local namespace (_pure_set_color $pure_color_k8s_namespace)(_pure_k8s_namespace)

echo "$symbol $context/$namespace"
echo "$symbol $context/$namespace"
end
end
end
2 changes: 2 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ build-pure-on:
--target ${STAGE} \
--build-arg FISH_VERSION=${FISH_VERSION} \
--tag=pure-${STAGE}-${FISH_VERSION} \
--load \
./

.PHONY: dev-pure-on
Expand Down Expand Up @@ -65,6 +66,7 @@ build-pure-on-nix:
--file ./docker/${STAGE}.Dockerfile \
--build-arg FISH_VERSION=${FISH_VERSION} \
--tag=pure-${STAGE}-${FISH_VERSION} \
--load \
./

dev-pure-on-nix: STAGE?=nix
Expand Down
14 changes: 11 additions & 3 deletions tests/_pure_k8s_namespace.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ source (status dirname)/../functions/_pure_k8s_namespace.fish


function before_each
functions --erase head
_clean_all_spy_calls
_clean_all_mocks
end


Expand All @@ -19,8 +19,16 @@ before_each

before_each
@test "_pure_k8s_namespace: call `kubectl config view…`" (
function kubectl; echo (status current-function) $argv > /tmp/(status current-function).mock_calls; end # spy
_spy kubectl

_pure_k8s_namespace > /dev/null

_pure_k8s_namespace
_has_called kubectl "config view --minify --output jsonpath={..namespace}"
) $status -eq $SUCCESS

before_each
@test "_pure_k8s_namespace: return default when no namespace is set" (
_mock_response kubectl ""

_pure_k8s_namespace
) = default
32 changes: 29 additions & 3 deletions tests/_pure_prompt_k8s.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ source (status dirname)/../functions/_pure_check_availability.fish

function before_each
_purge_configs
_disable_colors
_clean_all_mocks
_disable_colors # we use mocks so cleaning them must happen before
end

function after_all
Expand All @@ -25,14 +26,39 @@ end
echo (_pure_prompt_k8s)
) = $EMPTY


before_each
@test "_pure_prompt_k8s: print kubernetes context and namespace" (
@test "_pure_prompt_k8s: print kubernetes context and namespace when present" (
set --universal pure_enable_k8s true
set --universal pure_symbol_k8s_prefix "☸"

_mock kubectl
_mock_response _pure_k8s_context "my-context"
_mock_response _pure_k8s_namespace "my-namespace"

_pure_prompt_k8s
) = '☸ my-context/my-namespace'

before_each
@test "_pure_prompt_k8s: print nothing when context is not set" (
set --universal pure_enable_k8s true
set --universal pure_symbol_k8s_prefix "☸"

_mock kubectl
_mock_response _pure_k8s_context $EMPTY

_pure_prompt_k8s
) $status -eq $SUCCESS

before_each
@test "_pure_prompt_k8s: print default when namespace is not set" (
set --universal pure_enable_k8s true
set --universal pure_symbol_k8s_prefix '☸'

_mock_response _pure_k8s_context my-context
_mock_response kubectl $EMPTY

_pure_prompt_k8s
) = '☸ my-context/default'


after_all
6 changes: 3 additions & 3 deletions tests/mocks/cleaning.fish
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ end
function _clean_all_mocks \
--description "Clean all mock function"

for mock in $__mocks
if functions --query $mock
functions --erase $mock
for function_name in $__mocks
if functions --query $function_name
functions --erase $function_name
end

if functions --query __backup_$function_name # restore original function
Expand Down
2 changes: 1 addition & 1 deletion tests/mocks/spying.fish
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function _spy \
_backup_before_mocking $function_name

function $function_name
echo (status current-function) >/tmp/(status current-function).mock_calls
echo -- (status current-function) $argv >/tmp/(status current-function).mock_calls
end # spy
end

Expand Down
Loading