blob: e2bb0e6dee83737ac7b60bea70ec25ee533d5a75 (
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
|
;;; test-prog-shell--make-script-executable.el --- Tests for cj/make-script-executable -*- lexical-binding: t -*-
;;; Commentary:
;; Tests for the cj/make-script-executable function in prog-shell.el
;;; Code:
(require 'ert)
(require 'prog-shell)
;; Helper to create temp file with content
(defun test--create-temp-script (content)
"Create a temp file with CONTENT and return its path."
(let ((temp-file (make-temp-file "test-script-")))
(with-temp-file temp-file
(insert content))
;; Remove execute permission to start fresh
(set-file-modes temp-file #o644)
temp-file))
;; Helper to check if file is executable
(defun test--file-executable-p (file)
"Return t if FILE has any execute bit set."
(not (zerop (logand (file-modes file) #o111))))
;;; Normal Cases
(ert-deftest test-make-script-executable-normal-bash-shebang ()
"File with bash shebang becomes executable."
(let ((temp-file (test--create-temp-script "#!/bin/bash\necho hello")))
(unwind-protect
(with-current-buffer (find-file-noselect temp-file)
(should-not (test--file-executable-p temp-file))
(cj/make-script-executable)
(should (test--file-executable-p temp-file))
(kill-buffer))
(delete-file temp-file))))
(ert-deftest test-make-script-executable-normal-python-shebang ()
"File with python shebang becomes executable."
(let ((temp-file (test--create-temp-script "#!/usr/bin/env python3\nprint('hi')")))
(unwind-protect
(with-current-buffer (find-file-noselect temp-file)
(should-not (test--file-executable-p temp-file))
(cj/make-script-executable)
(should (test--file-executable-p temp-file))
(kill-buffer))
(delete-file temp-file))))
(ert-deftest test-make-script-executable-normal-already-executable ()
"Already executable file stays executable without error."
(let ((temp-file (test--create-temp-script "#!/bin/bash\necho hello")))
(unwind-protect
(progn
(set-file-modes temp-file #o755)
(with-current-buffer (find-file-noselect temp-file)
(should (test--file-executable-p temp-file))
(cj/make-script-executable) ; should not error
(should (test--file-executable-p temp-file))
(kill-buffer)))
(delete-file temp-file))))
;;; Boundary Cases
(ert-deftest test-make-script-executable-boundary-no-shebang ()
"File without shebang is NOT made executable."
(let ((temp-file (test--create-temp-script "echo hello\n# no shebang")))
(unwind-protect
(with-current-buffer (find-file-noselect temp-file)
(should-not (test--file-executable-p temp-file))
(cj/make-script-executable)
(should-not (test--file-executable-p temp-file))
(kill-buffer))
(delete-file temp-file))))
(ert-deftest test-make-script-executable-boundary-empty-file ()
"Empty file is NOT made executable."
(let ((temp-file (test--create-temp-script "")))
(unwind-protect
(with-current-buffer (find-file-noselect temp-file)
(should-not (test--file-executable-p temp-file))
(cj/make-script-executable)
(should-not (test--file-executable-p temp-file))
(kill-buffer))
(delete-file temp-file))))
(ert-deftest test-make-script-executable-boundary-shebang-line-two ()
"Shebang on line 2 (not line 1) does NOT make file executable."
(let ((temp-file (test--create-temp-script "\n#!/bin/bash\necho hello")))
(unwind-protect
(with-current-buffer (find-file-noselect temp-file)
(should-not (test--file-executable-p temp-file))
(cj/make-script-executable)
(should-not (test--file-executable-p temp-file))
(kill-buffer))
(delete-file temp-file))))
(ert-deftest test-make-script-executable-boundary-hash-but-not-shebang ()
"File starting with # but not #! is NOT made executable."
(let ((temp-file (test--create-temp-script "# This is a comment\necho hello")))
(unwind-protect
(with-current-buffer (find-file-noselect temp-file)
(should-not (test--file-executable-p temp-file))
(cj/make-script-executable)
(should-not (test--file-executable-p temp-file))
(kill-buffer))
(delete-file temp-file))))
;;; Edge Cases
(ert-deftest test-make-script-executable-edge-no-buffer-file ()
"Buffer not visiting a file does not error."
(with-temp-buffer
(insert "#!/bin/bash\necho hello")
(should-not buffer-file-name)
(cj/make-script-executable))) ; should not error
(ert-deftest test-make-script-executable-edge-shebang-with-space ()
"Shebang with space after #! still works."
(let ((temp-file (test--create-temp-script "#! /bin/bash\necho hello")))
(unwind-protect
(with-current-buffer (find-file-noselect temp-file)
(should-not (test--file-executable-p temp-file))
(cj/make-script-executable)
(should (test--file-executable-p temp-file))
(kill-buffer))
(delete-file temp-file))))
(provide 'test-prog-shell--make-script-executable)
;;; test-prog-shell--make-script-executable.el ends here
|