files.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ;;; files.el --- tests for file handling.
  2. ;; Copyright (C) 2012 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is free software: you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; GNU Emacs is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (require 'ert)
  16. ;; Set to t if the local variable was set, `query' if the query was
  17. ;; triggered.
  18. (defvar files-test-result)
  19. (defvar files-test-safe-result)
  20. (put 'files-test-safe-result 'safe-local-variable 'booleanp)
  21. (defun files-test-fun1 ()
  22. (setq files-test-result t))
  23. ;; Test combinations:
  24. ;; `enable-local-variables' t, nil, :safe, :all, or something else.
  25. ;; `enable-local-eval' t, nil, or something else.
  26. (defvar files-test-local-variable-data
  27. ;; Unsafe eval form
  28. '((("eval: (files-test-fun1)")
  29. (t t (eq files-test-result t))
  30. (t nil (eq files-test-result nil))
  31. (t maybe (eq files-test-result 'query))
  32. (nil t (eq files-test-result nil))
  33. (nil nil (eq files-test-result nil))
  34. (nil maybe (eq files-test-result nil))
  35. (:safe t (eq files-test-result nil))
  36. (:safe nil (eq files-test-result nil))
  37. (:safe maybe (eq files-test-result nil))
  38. (:all t (eq files-test-result t))
  39. (:all nil (eq files-test-result nil))
  40. (:all maybe (eq files-test-result t)) ; This combination is ambiguous.
  41. (maybe t (eq files-test-result 'query))
  42. (maybe nil (eq files-test-result 'query))
  43. (maybe maybe (eq files-test-result 'query)))
  44. ;; Unsafe local variable value
  45. (("files-test-result: t")
  46. (t t (eq files-test-result 'query))
  47. (t nil (eq files-test-result 'query))
  48. (t maybe (eq files-test-result 'query))
  49. (nil t (eq files-test-result nil))
  50. (nil nil (eq files-test-result nil))
  51. (nil maybe (eq files-test-result nil))
  52. (:safe t (eq files-test-result nil))
  53. (:safe nil (eq files-test-result nil))
  54. (:safe maybe (eq files-test-result nil))
  55. (:all t (eq files-test-result t))
  56. (:all nil (eq files-test-result t))
  57. (:all maybe (eq files-test-result t))
  58. (maybe t (eq files-test-result 'query))
  59. (maybe nil (eq files-test-result 'query))
  60. (maybe maybe (eq files-test-result 'query)))
  61. ;; Safe local variable
  62. (("files-test-safe-result: t")
  63. (t t (eq files-test-safe-result t))
  64. (t nil (eq files-test-safe-result t))
  65. (t maybe (eq files-test-safe-result t))
  66. (nil t (eq files-test-safe-result nil))
  67. (nil nil (eq files-test-safe-result nil))
  68. (nil maybe (eq files-test-safe-result nil))
  69. (:safe t (eq files-test-safe-result t))
  70. (:safe nil (eq files-test-safe-result t))
  71. (:safe maybe (eq files-test-safe-result t))
  72. (:all t (eq files-test-safe-result t))
  73. (:all nil (eq files-test-safe-result t))
  74. (:all maybe (eq files-test-safe-result t))
  75. (maybe t (eq files-test-result 'query))
  76. (maybe nil (eq files-test-result 'query))
  77. (maybe maybe (eq files-test-result 'query)))
  78. ;; Safe local variable with unsafe value
  79. (("files-test-safe-result: 1")
  80. (t t (eq files-test-result 'query))
  81. (t nil (eq files-test-result 'query))
  82. (t maybe (eq files-test-result 'query))
  83. (nil t (eq files-test-safe-result nil))
  84. (nil nil (eq files-test-safe-result nil))
  85. (nil maybe (eq files-test-safe-result nil))
  86. (:safe t (eq files-test-safe-result nil))
  87. (:safe nil (eq files-test-safe-result nil))
  88. (:safe maybe (eq files-test-safe-result nil))
  89. (:all t (eq files-test-safe-result 1))
  90. (:all nil (eq files-test-safe-result 1))
  91. (:all maybe (eq files-test-safe-result 1))
  92. (maybe t (eq files-test-result 'query))
  93. (maybe nil (eq files-test-result 'query))
  94. (maybe maybe (eq files-test-result 'query))))
  95. "List of file-local variable tests.
  96. Each list element should have the form
  97. (LOCAL-VARS-LIST . TEST-LIST)
  98. where LOCAL-VARS-LISTS should be a list of local variable
  99. definitions (strings) and TEST-LIST is a list of tests to
  100. perform. Each entry of TEST-LIST should have the form
  101. (ENABLE-LOCAL-VARIABLES ENABLE-LOCAL-EVAL FORM)
  102. where ENABLE-LOCAL-VARIABLES is the value to assign to
  103. `enable-local-variables', ENABLE-LOCAL-EVAL is the value to
  104. assign to `enable-local-eval', and FORM is a desired `should'
  105. form.")
  106. (defun file-test--do-local-variables-test (str test-settings)
  107. (with-temp-buffer
  108. (insert str)
  109. (let ((enable-local-variables (nth 0 test-settings))
  110. (enable-local-eval (nth 1 test-settings))
  111. (files-test-result nil)
  112. (files-test-queried nil)
  113. (files-test-safe-result nil))
  114. (hack-local-variables)
  115. (eval (nth 2 test-settings)))))
  116. (ert-deftest files-test-local-variables ()
  117. "Test the file-local variables implementation."
  118. (unwind-protect
  119. (progn
  120. (defadvice hack-local-variables-confirm (around files-test activate)
  121. (setq files-test-result 'query)
  122. nil)
  123. (dolist (test files-test-local-variable-data)
  124. (let ((str (concat "text\n\n;; Local Variables:\n;; "
  125. (mapconcat 'identity (car test) "\n;; ")
  126. "\n;; End:\n")))
  127. (dolist (subtest (cdr test))
  128. (should (file-test--do-local-variables-test str subtest))))))
  129. (ad-disable-advice 'hack-local-variables-confirm 'around 'files-test)))
  130. ;;; files.el ends here