|
| 1 | +(ns eca.git-test |
| 2 | + (:require |
| 3 | + [clojure.java.io :as io] |
| 4 | + [clojure.java.shell :as shell] |
| 5 | + [clojure.string :as string] |
| 6 | + [clojure.test :refer [deftest is testing]] |
| 7 | + [eca.git :as git])) |
| 8 | + |
| 9 | +(set! *warn-on-reflection* true) |
| 10 | + |
| 11 | +(deftest root-test |
| 12 | + (testing "git root returns a path when in a git repository" |
| 13 | + (let [result (git/root)] |
| 14 | + ;; Should return a non-empty string path or nil |
| 15 | + (is (or (nil? result) |
| 16 | + (and (string? result) |
| 17 | + (not (empty? result))))) |
| 18 | + |
| 19 | + ;; If we have a root, it should be an absolute path |
| 20 | + (when result |
| 21 | + (is (.isAbsolute (io/file result))))))) |
| 22 | + |
| 23 | +(deftest in-worktree-test |
| 24 | + (testing "in-worktree? returns a boolean" |
| 25 | + (let [result (git/in-worktree?)] |
| 26 | + (is (or (true? result) |
| 27 | + (false? result) |
| 28 | + (nil? result))))) |
| 29 | + |
| 30 | + (testing "correctly detects worktree by checking git-dir" |
| 31 | + ;; If we're in a worktree, git-dir should contain /worktrees/ |
| 32 | + (when (git/in-worktree?) |
| 33 | + (let [{:keys [out exit]} (shell/sh "git" "rev-parse" "--git-dir")] |
| 34 | + (when (= 0 exit) |
| 35 | + (is (string/includes? out "worktrees"))))))) |
| 36 | + |
| 37 | +(deftest main-repo-root-test |
| 38 | + (testing "main-repo-root returns a path or nil" |
| 39 | + (let [result (git/main-repo-root)] |
| 40 | + (is (or (nil? result) |
| 41 | + (and (string? result) |
| 42 | + (not (empty? result))))) |
| 43 | + |
| 44 | + ;; If we have a main repo root, it should be absolute |
| 45 | + (when result |
| 46 | + (is (.isAbsolute (io/file result))))))) |
| 47 | + |
| 48 | +(deftest integration-test |
| 49 | + (testing "git functions work together logically" |
| 50 | + (let [root (git/root)] |
| 51 | + (if root |
| 52 | + (do |
| 53 | + ;; If we have a git root, we're in a git repo |
| 54 | + (is (string? root)) |
| 55 | + (is (.exists (io/file root))) |
| 56 | + |
| 57 | + ;; If we're in a worktree, behavior should be consistent |
| 58 | + (if (git/in-worktree?) |
| 59 | + (testing "in a worktree" |
| 60 | + (let [main-root (git/main-repo-root)] |
| 61 | + ;; Main repo root should exist |
| 62 | + (is (some? main-root)) |
| 63 | + (when main-root |
| 64 | + ;; Main root and worktree root should be different |
| 65 | + (is (not= root main-root)) |
| 66 | + ;; Both should exist |
| 67 | + (is (.exists (io/file main-root)))))) |
| 68 | + |
| 69 | + (testing "in main repo (not a worktree)" |
| 70 | + ;; In main repo, main-repo-root might be nil or same as root |
| 71 | + (let [main-root (git/main-repo-root)] |
| 72 | + (is (or (nil? main-root) |
| 73 | + (= root main-root))))))) |
| 74 | + ;; If not in a git repo, verify that git/root returns nil |
| 75 | + (is (nil? root)))))) |
| 76 | + |
| 77 | +(deftest worktree-config-discovery-test |
| 78 | + (testing "git root is suitable for .eca config discovery" |
| 79 | + (let [root (git/root)] |
| 80 | + (if root |
| 81 | + ;; The root should be a directory where .eca config could exist |
| 82 | + (let [eca-dir (io/file root ".eca")] |
| 83 | + ;; We don't test if .eca exists, just that the parent dir is valid |
| 84 | + (is (.isDirectory (io/file root))) |
| 85 | + |
| 86 | + ;; If .eca exists, it should be in the worktree root (not main repo) |
| 87 | + (when (.exists eca-dir) |
| 88 | + (is (.isDirectory eca-dir)))) |
| 89 | + ;; If not in a git repo, verify that git/root returns nil |
| 90 | + (is (nil? root)))))) |
0 commit comments