|
46 | 46 | (is (string? path)) |
47 | 47 | (is (fs/exists? path)) |
48 | 48 | (is (= text (slurp path))))))) |
| 49 | + |
| 50 | +(deftest workspace-dir-name-test |
| 51 | + (let [dir-name #'cache/workspace-dir-name] |
| 52 | + (testing "prefixes hash with project name from first workspace URI" |
| 53 | + (let [workspaces [{:uri "file:///home/user/my-project"}] |
| 54 | + result (dir-name workspaces identity)] |
| 55 | + (is (re-matches #"my-project_.{8}" result)))) |
| 56 | + |
| 57 | + (testing "sanitizes unsafe filesystem characters" |
| 58 | + (let [workspaces [{:uri "file:///home/user/my project@v2!"}] |
| 59 | + result (dir-name workspaces identity)] |
| 60 | + (is (re-matches #"my_project_v2__.{8}" result)) |
| 61 | + (is (nil? (re-find #"[ @!]" result))))) |
| 62 | + |
| 63 | + (testing "truncates long project names to 30 chars" |
| 64 | + (let [long-name (apply str (repeat 50 "a")) |
| 65 | + workspaces [{:uri (str "file:///home/user/" long-name)}] |
| 66 | + result (dir-name workspaces identity)] |
| 67 | + (is (<= (count result) (+ 30 1 8))))) |
| 68 | + |
| 69 | + (testing "falls back to hash-only when no workspace name available" |
| 70 | + (let [result (dir-name [] identity)] |
| 71 | + (is (re-matches #".{8}" result)))) |
| 72 | + |
| 73 | + (testing "uses first workspace name when multiple workspaces" |
| 74 | + (let [workspaces [{:uri "file:///home/user/first-project"} |
| 75 | + {:uri "file:///home/user/second-project"}] |
| 76 | + result (dir-name workspaces identity)] |
| 77 | + (is (re-matches #"first-project_.{8}" result)))))) |
| 78 | + |
| 79 | +(deftest workspace-cache-file-migration-test |
| 80 | + (testing "migrates old hash-only directory to new format" |
| 81 | + (with-temp-cache-dir |
| 82 | + (let [workspaces [{:uri "file:///home/user/my-project"}] |
| 83 | + hash-only (cache/workspaces-hash workspaces identity) |
| 84 | + old-dir (io/file (cache/global-dir) hash-only)] |
| 85 | + ;; Create old-format directory with a cache file |
| 86 | + (fs/create-dirs old-dir) |
| 87 | + (spit (io/file old-dir "db.transit.json") "{}") |
| 88 | + |
| 89 | + (let [result (cache/workspace-cache-file workspaces "db.transit.json" identity)] |
| 90 | + (is (not (fs/exists? old-dir)) "Old directory should be renamed") |
| 91 | + (is (fs/exists? (.getParentFile result)) "New directory should exist") |
| 92 | + (is (= "{}" (slurp result)) "Migrated file content should be preserved") |
| 93 | + (is (re-find #"my-project_" (str result)) "New path should contain project name"))))) |
| 94 | + |
| 95 | + (testing "does not migrate when new directory already exists" |
| 96 | + (with-temp-cache-dir |
| 97 | + (let [workspaces [{:uri "file:///home/user/my-project"}] |
| 98 | + hash-only (cache/workspaces-hash workspaces identity) |
| 99 | + old-dir (io/file (cache/global-dir) hash-only) |
| 100 | + result-before (cache/workspace-cache-file workspaces "db.transit.json" identity) |
| 101 | + new-dir (.getParentFile result-before)] |
| 102 | + ;; Create both directories |
| 103 | + (fs/create-dirs old-dir) |
| 104 | + (spit (io/file old-dir "db.transit.json") "old") |
| 105 | + (fs/create-dirs new-dir) |
| 106 | + (spit (io/file new-dir "db.transit.json") "new") |
| 107 | + |
| 108 | + (let [result (cache/workspace-cache-file workspaces "db.transit.json" identity)] |
| 109 | + (is (fs/exists? old-dir) "Old directory should remain untouched") |
| 110 | + (is (= "new" (slurp result)) "Should use new directory content")))))) |
0 commit comments