blob: ace51a4dd2d66bc9def991e95bc594f4a7267fc7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
;;; test-music-config--delete-playlist-file.el --- Tests for playlist file deletion -*- coding: utf-8; lexical-binding: t; -*-
;;
;; Author: Craig Jennings <c@cjennings.net>
;;
;;; Commentary:
;; Unit tests for cj/music--delete-playlist-file function.
;; Tests the internal helper that removes a playlist's .m3u file,
;; clears the playlist buffer's file association when it pointed at
;; the deleted file, and refreshes the radio metadata cache.
;;
;; Test organization:
;; - Normal Cases: Existing file is deleted
;; - Boundary Cases: Association cleared only when it matches; cache refresh
;; - Error Cases: Nil path, nonexistent path
;;
;;; Code:
(require 'ert)
(require 'testutil-general)
;; Stub missing dependencies before loading music-config
(defvar-keymap cj/custom-keymap
:doc "Stub keymap for testing")
;; Add EMMS elpa directory to load path for batch testing
(let ((emms-dir (car (file-expand-wildcards
(expand-file-name "elpa/emms-*" user-emacs-directory)))))
(when emms-dir
(add-to-list 'load-path emms-dir)))
(require 'emms)
(require 'emms-playlist-mode)
(require 'music-config)
;;; Test helpers
(defun test-delete-playlist--setup ()
"Create test base dir and ensure playlist buffer exists."
(cj/create-test-base-dir)
(let ((buf (get-buffer-create cj/music-playlist-buffer-name)))
(with-current-buffer buf
(emms-playlist-mode)
(setq emms-playlist-buffer-p t))
(setq emms-playlist-buffer buf)
buf))
(defun test-delete-playlist--teardown ()
"Clean up test playlist buffer and temp files."
(when-let ((buf (get-buffer cj/music-playlist-buffer-name)))
(with-current-buffer buf
(setq cj/music-playlist-file nil))
(kill-buffer buf))
(cj/delete-test-base-dir))
;;; Normal Cases
(ert-deftest test-music-config--delete-playlist-file-normal-removes-file ()
"Normal: an existing playlist file is deleted from disk."
(test-delete-playlist--setup)
(unwind-protect
(let ((file (cj/create-temp-test-file-with-content
"#EXTM3U\n/tmp/song.mp3\n" "playlist.m3u")))
(cj/music--delete-playlist-file file)
(should-not (file-exists-p file)))
(test-delete-playlist--teardown)))
;;; Boundary Cases
(ert-deftest test-music-config--delete-playlist-file-boundary-clears-matching-association ()
"Boundary: deleting the associated playlist file clears the association."
(test-delete-playlist--setup)
(unwind-protect
(let ((file (cj/create-temp-test-file-with-content
"#EXTM3U\n" "playlist.m3u")))
(with-current-buffer (get-buffer cj/music-playlist-buffer-name)
(setq cj/music-playlist-file file))
(cj/music--delete-playlist-file file)
(with-current-buffer (get-buffer cj/music-playlist-buffer-name)
(should-not cj/music-playlist-file)))
(test-delete-playlist--teardown)))
(ert-deftest test-music-config--delete-playlist-file-boundary-keeps-other-association ()
"Boundary: deleting a different file leaves the association untouched."
(test-delete-playlist--setup)
(unwind-protect
(let ((doomed (cj/create-temp-test-file-with-content
"#EXTM3U\n" "doomed.m3u"))
(kept (cj/create-temp-test-file-with-content
"#EXTM3U\n" "kept.m3u")))
(with-current-buffer (get-buffer cj/music-playlist-buffer-name)
(setq cj/music-playlist-file kept))
(cj/music--delete-playlist-file doomed)
(should (file-exists-p kept))
(with-current-buffer (get-buffer cj/music-playlist-buffer-name)
(should (equal cj/music-playlist-file kept))))
(test-delete-playlist--teardown)))
(ert-deftest test-music-config--delete-playlist-file-boundary-refreshes-radio-cache ()
"Boundary: deletion clears the cached radio metadata."
(test-delete-playlist--setup)
(unwind-protect
(let ((file (cj/create-temp-test-file-with-content
"#EXTM3U\n" "playlist.m3u")))
(setq cj/music--radio-metadata-cache '(("stale" . "entry")))
(cj/music--delete-playlist-file file)
(should-not cj/music--radio-metadata-cache))
(test-delete-playlist--teardown)))
;;; Error Cases
(ert-deftest test-music-config--delete-playlist-file-error-nil-path ()
"Error: nil path signals user-error."
(test-delete-playlist--setup)
(unwind-protect
(should-error (cj/music--delete-playlist-file nil) :type 'user-error)
(test-delete-playlist--teardown)))
(ert-deftest test-music-config--delete-playlist-file-error-nonexistent-path ()
"Error: a path that does not exist signals user-error and touches nothing."
(test-delete-playlist--setup)
(unwind-protect
(let ((kept (cj/create-temp-test-file-with-content
"#EXTM3U\n" "kept.m3u")))
(with-current-buffer (get-buffer cj/music-playlist-buffer-name)
(setq cj/music-playlist-file kept))
(should-error
(cj/music--delete-playlist-file
(expand-file-name "no-such.m3u" cj/test-base-dir))
:type 'user-error)
(with-current-buffer (get-buffer cj/music-playlist-buffer-name)
(should (equal cj/music-playlist-file kept))))
(test-delete-playlist--teardown)))
(provide 'test-music-config--delete-playlist-file)
;;; test-music-config--delete-playlist-file.el ends here
|