diff --git a/docs/references/changelog.md b/docs/references/changelog.md index 007cdb1..ea7f362 100644 --- a/docs/references/changelog.md +++ b/docs/references/changelog.md @@ -11,6 +11,11 @@ versioning][semver]. ## Unreleased +### Fixed + +- Fixed a bug where `kele-mode` errors out when the kubeconfig file does not + exist yet + ### Changed - Bumped dependency `plz` from `0.3` to `0.4` diff --git a/kele.el b/kele.el index 8ece8f3..5a0e010 100644 --- a/kele.el +++ b/kele.el @@ -1198,10 +1198,23 @@ Only populated if Embark is installed.") (dolist (entry kele--embark-keymap-entries) (delete entry embark-keymap-alist))))) +(defun kele--precheck () + "Return an error if `kele-mode' is not ready to be enabled. + +Ensures various preconditions are met, +e.g. `kele-kubectl-executable' is actually present." + + (when (not (executable-find kele-kubectl-executable)) + (error "`%s' not found on PATH" kele-kubectl-executable)) + + (when (not (file-exists-p kele-kubeconfig-path)) + (error "`%s' does not exist; set up kubectl properly and try again" kele-kubeconfig-path))) + (defun kele--enable () "Enables Kele functionality. This is idempotent." + (kele--precheck) (unless kele--enabled (setq kele--enabled t) ;; FIXME: Update the watcher when `kele-kubeconfig-path' changes. diff --git a/tests/unit/test-kele.el b/tests/unit/test-kele.el index 71155c2..f2f86a2 100644 --- a/tests/unit/test-kele.el +++ b/tests/unit/test-kele.el @@ -530,7 +530,23 @@ metadata: (it "does nothing" (kele-mode 1) (expect 'kele--cache-start :not :to-have-been-called) - (expect 'kele--setup-embark-maybe :not :to-have-been-called)))) + (expect 'kele--setup-embark-maybe :not :to-have-been-called))) + (describe "when `kele-kubectl-executable' not found" + (before-each + (spy-on 'executable-find + :and-call-fake + (lambda (exec) + (not (string-equal exec kele-kubectl-executable))))) + (it "throws error" + (expect (kele-mode 1) :to-throw 'error))) + (describe "when `kele-kubeconfig-path' does not exist" + (before-each + (spy-on 'file-exists-p + :and-call-fake + (lambda (f) + (not (string-equal f kele-kubeconfig-path))))) + (it "throws error" + (expect (kele-mode 1) :to-throw 'error)))) (describe "disabling" (describe "when `kele--enabled' is nil" (before-each