blob: aca0eeb94ff648bca10e6d97a41f9b0d825bd35d (
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
|
;;; test-music-config--assert-valid-playlist-file.el --- Tests for playlist file validation -*- coding: utf-8; lexical-binding: t; -*-
;;
;; Author: Craig Jennings <c@cjennings.net>
;;
;;; Commentary:
;; Unit tests for cj/music--assert-valid-playlist-file function.
;; Tests the validation guard that ensures a playlist buffer has
;; a valid, existing associated M3U file before operations proceed.
;;
;; Test organization:
;; - Normal Cases: Valid file passes without error
;; - Boundary Cases: Empty file, file exists but is empty
;; - Error Cases: Nil file, nonexistent file, deleted file
;;
;;; 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-assert--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-assert--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--assert-valid-playlist-file-normal-existing-file-passes ()
"Valid existing file does not signal error."
(test-assert--setup)
(unwind-protect
(let ((file (cj/create-temp-test-file "playlist-")))
(with-current-buffer (get-buffer cj/music-playlist-buffer-name)
(setq cj/music-playlist-file file))
;; Should not error
(cj/music--assert-valid-playlist-file))
(test-assert--teardown)))
(ert-deftest test-music-config--assert-valid-playlist-file-normal-file-with-content-passes ()
"Valid file with content does not signal error."
(test-assert--setup)
(unwind-protect
(let ((file (cj/create-temp-test-file-with-content "track1.mp3\ntrack2.mp3\n" "playlist-")))
(with-current-buffer (get-buffer cj/music-playlist-buffer-name)
(setq cj/music-playlist-file file))
(cj/music--assert-valid-playlist-file))
(test-assert--teardown)))
;;; Boundary Cases
(ert-deftest test-music-config--assert-valid-playlist-file-boundary-empty-file-passes ()
"Empty but existing file does not signal error."
(test-assert--setup)
(unwind-protect
(let ((file (cj/create-temp-test-file "playlist-")))
(with-current-buffer (get-buffer cj/music-playlist-buffer-name)
(setq cj/music-playlist-file file))
;; File exists but is empty - should still pass
(cj/music--assert-valid-playlist-file))
(test-assert--teardown)))
;;; Error Cases
(ert-deftest test-music-config--assert-valid-playlist-file-error-nil-file-signals-user-error ()
"Nil cj/music-playlist-file signals user-error."
(test-assert--setup)
(unwind-protect
(progn
(with-current-buffer (get-buffer cj/music-playlist-buffer-name)
(setq cj/music-playlist-file nil))
(should-error (cj/music--assert-valid-playlist-file)
:type 'user-error))
(test-assert--teardown)))
(ert-deftest test-music-config--assert-valid-playlist-file-error-nonexistent-file-signals-user-error ()
"Nonexistent file path signals user-error."
(test-assert--setup)
(unwind-protect
(progn
(with-current-buffer (get-buffer cj/music-playlist-buffer-name)
(setq cj/music-playlist-file "/nonexistent/path/to/playlist.m3u"))
(should-error (cj/music--assert-valid-playlist-file)
:type 'user-error))
(test-assert--teardown)))
(ert-deftest test-music-config--assert-valid-playlist-file-error-deleted-file-signals-user-error ()
"File that existed but was deleted signals user-error."
(test-assert--setup)
(unwind-protect
(let ((file (cj/create-temp-test-file "playlist-")))
(with-current-buffer (get-buffer cj/music-playlist-buffer-name)
(setq cj/music-playlist-file file))
;; Verify it passes first
(cj/music--assert-valid-playlist-file)
;; Now delete the file
(delete-file file)
;; Should now signal error
(should-error (cj/music--assert-valid-playlist-file)
:type 'user-error))
(test-assert--teardown)))
(provide 'test-music-config--assert-valid-playlist-file)
;;; test-music-config--assert-valid-playlist-file.el ends here
|