@@ -281,3 +281,58 @@ PROPERTY should be a symbol of either 'text, 'ansi-context or
281281 (cider-repl-require-repl-utils)
282282 (expect 'nrepl--eval-request :to-have-been-called-with
283283 (cdr (assoc 'cljs cider-repl-require-repl-utils-code)) " user" )))
284+
285+ ; ;; cider-repl--history-write tests
286+ ; ; Tests for graceful degradation when history file cannot be written.
287+ ; ; Contracts: file-writable-p check, parent directory existence check,
288+ ; ; warning emission for missing directories, error for other write failures.
289+
290+ (describe " cider-repl--history-write"
291+ (describe " when file is writable"
292+ (it " writes history successfully"
293+ (let* ((temp-dir (make-temp-file " cider-test" t ))
294+ (history-file (expand-file-name " history" temp-dir))
295+ (cider-repl-input-history '(" (+ 1 2)" " (def x 1)" ))
296+ (cider-repl-history-size 100 ))
297+ (unwind-protect
298+ (progn
299+ (cider-repl--history-write history-file)
300+ (expect (file-exists-p history-file) :to-be-truthy )
301+ (with-temp-buffer
302+ (insert-file-contents history-file)
303+ (expect (buffer-string ) :to-match " CIDER REPL session" )))
304+ (delete-directory temp-dir t )))))
305+
306+ (describe " when parent directory does not exist"
307+ (it " emits a warning and returns without error"
308+ (let* ((temp-dir (make-temp-file " cider-test" t ))
309+ (history-file (expand-file-name " nonexistent/history" temp-dir))
310+ (cider-repl-input-history '(" (+ 1 2)" ))
311+ (cider-repl-history-size 100 )
312+ (warning-emitted nil ))
313+ (unwind-protect
314+ (progn
315+ (spy-on 'message :and-call-fake
316+ (lambda (&rest args )
317+ (when (string-match-p " directory does not exist"
318+ (apply #'format args))
319+ (setq warning-emitted t ))))
320+ (expect (cider-repl--history-write history-file) :not :to-throw )
321+ (expect warning-emitted :to-be-truthy ))
322+ (delete-directory temp-dir t )))))
323+
324+ (describe " when file is not writable but parent directory exists"
325+ (it " raises an error"
326+ (let* ((temp-dir (make-temp-file " cider-test" t ))
327+ (history-file (expand-file-name " history" temp-dir))
328+ (cider-repl-input-history '(" (+ 1 2)" ))
329+ (cider-repl-history-size 100 ))
330+ (unwind-protect
331+ (progn
332+ ; ; Create a read-only file
333+ (write-region " " nil history-file)
334+ (set-file-modes history-file #o444 )
335+ (expect (cider-repl--history-write history-file)
336+ :to-throw 'error ))
337+ (set-file-modes history-file #o644 )
338+ (delete-directory temp-dir t ))))))
0 commit comments