diff options
| -rw-r--r-- | modules/video-audio-recording.el | 34 | ||||
| -rw-r--r-- | tests/test-video-audio-recording--normalize-recording-dir.el | 46 |
2 files changed, 66 insertions, 14 deletions
diff --git a/modules/video-audio-recording.el b/modules/video-audio-recording.el index b3c4d47b..282fdf85 100644 --- a/modules/video-audio-recording.el +++ b/modules/video-audio-recording.el @@ -774,6 +774,14 @@ Respects the user's explicit sink choice from quick-setup." ;;; Toggle Commands (User-Facing) ;;; ============================================================ +(defun cj/recording--normalize-recording-dir (location) + "Return LOCATION as an absolute directory path ending in a slash. +The recording target is always a directory ffmpeg writes a timestamped +file into, so normalizing here ensures the *selected* directory is the +one created and recorded into, not its parent (which is what +`file-name-directory' would yield for a path without a trailing slash)." + (file-name-as-directory (expand-file-name location))) + (defun cj/video-recording-toggle (arg) "Toggle video recording: start if not recording, stop if recording. On first use (or when devices not configured), runs quick setup (C-; r s). @@ -782,13 +790,12 @@ Otherwise use the default location in `video-recordings-dir'." (interactive "P") (if cj/video-recording-ffmpeg-process (cj/video-recording-stop) - (let* ((location (if arg - (read-directory-name "Enter recording location: ") - video-recordings-dir)) - (directory (file-name-directory location))) - (unless (file-directory-p directory) - (make-directory directory t)) - (cj/ffmpeg-record-video location)))) + (let ((directory (cj/recording--normalize-recording-dir + (if arg + (read-directory-name "Enter recording location: ") + video-recordings-dir)))) + (make-directory directory t) + (cj/ffmpeg-record-video directory)))) (defun cj/audio-recording-toggle (arg) "Toggle audio recording: start if not recording, stop if recording. @@ -798,13 +805,12 @@ Otherwise use the default location in `audio-recordings-dir'." (interactive "P") (if cj/audio-recording-ffmpeg-process (cj/audio-recording-stop) - (let* ((location (if arg - (read-directory-name "Enter recording location: ") - audio-recordings-dir)) - (directory (file-name-directory location))) - (unless (file-directory-p directory) - (make-directory directory t)) - (cj/ffmpeg-record-audio location)))) + (let ((directory (cj/recording--normalize-recording-dir + (if arg + (read-directory-name "Enter recording location: ") + audio-recordings-dir)))) + (make-directory directory t) + (cj/ffmpeg-record-audio directory)))) ;;; ============================================================ ;;; Start Recording diff --git a/tests/test-video-audio-recording--normalize-recording-dir.el b/tests/test-video-audio-recording--normalize-recording-dir.el new file mode 100644 index 00000000..b2997302 --- /dev/null +++ b/tests/test-video-audio-recording--normalize-recording-dir.el @@ -0,0 +1,46 @@ +;;; test-video-audio-recording--normalize-recording-dir.el --- Tests for recording-dir normalization -*- lexical-binding: t; -*- + +;;; Commentary: +;; Unit tests for cj/recording--normalize-recording-dir. +;; The recording target is always a directory that ffmpeg writes a +;; timestamped file into. Normalization must yield an absolute directory +;; path with a trailing slash so the *selected* directory (not its parent) +;; is the one created and recorded into. + +;;; Code: + +(require 'ert) + +;; Stub dependencies before loading the module +(defvar cj/custom-keymap (make-sparse-keymap) + "Stub keymap for testing.") + +(require 'video-audio-recording) + +;;; Normal Cases + +(ert-deftest test-video-audio-recording--normalize-recording-dir-normal-adds-trailing-slash () + "Normal: a path without a trailing slash becomes a directory path." + (should (equal "/tmp/foo/" + (cj/recording--normalize-recording-dir "/tmp/foo")))) + +(ert-deftest test-video-audio-recording--normalize-recording-dir-normal-idempotent () + "Normal: a path that is already a directory is returned unchanged." + (should (equal "/tmp/foo/" + (cj/recording--normalize-recording-dir "/tmp/foo/")))) + +;;; Boundary Cases + +(ert-deftest test-video-audio-recording--normalize-recording-dir-boundary-spaces-preserved () + "Boundary: spaces in the path are preserved (quoting happens at the shell)." + (should (equal "/tmp/my recordings/" + (cj/recording--normalize-recording-dir "/tmp/my recordings")))) + +(ert-deftest test-video-audio-recording--normalize-recording-dir-boundary-relative-expands-absolute () + "Boundary: a relative path expands to an absolute directory path." + (let ((result (cj/recording--normalize-recording-dir "foo"))) + (should (file-name-absolute-p result)) + (should (string-suffix-p "/foo/" result)))) + +(provide 'test-video-audio-recording--normalize-recording-dir) +;;; test-video-audio-recording--normalize-recording-dir.el ends here |
