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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
|
;;; test-util.el --- Flycheck Specs: Utility functions -*- lexical-binding: t; -*-
;; Copyright (C) 2016 Sebastian Wiesner and Flycheck contributors
;; Author: Sebastian Wiesner <swiesner@lunaryorn.com>
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Specs for Flycheck's utility functions
;;; Code:
(require 'flycheck-buttercup)
(require 'test-helpers)
(describe "Utilities"
(describe "flycheck-buffer-empty-p"
(it "considers an empty buffer as empty"
(with-temp-buffer
(expect (flycheck-buffer-empty-p) :to-be-truthy)))
(it "does not consider a buffer with content as empty"
(with-temp-buffer
(insert "foo bar")
(expect (flycheck-buffer-empty-p) :not :to-be-truthy)))
(it "detects emptiness of narrowed buffers"
(with-temp-buffer
(insert "foo\nbar")
(goto-char (point-min))
(narrow-to-region (point-min) (point-min))
(expect (buffer-string) :to-equal "")
(expect (flycheck-buffer-empty-p) :not :to-be-truthy))))
(describe "flycheck-buffer-saved-p"
(it "considers an unmodified buffer without backing file unsaved"
(with-temp-buffer
(expect (flycheck-buffer-saved-p) :not :to-be-truthy)))
(it "considers a modified buffer without backing file unsaved"
(with-temp-buffer
(set-buffer-modified-p t)
(expect (flycheck-buffer-saved-p) :not :to-be-truthy)))
(it "considers an unmodified buffer with backing file saved"
(spy-on 'file-exists-p :and-return-value t)
(spy-on 'buffer-file-name :and-return-value "test-buffer-name")
(with-temp-buffer
(expect (flycheck-buffer-saved-p) :to-be-truthy))
(expect (spy-calls-count 'file-exists-p) :to-equal 1)
(expect (spy-calls-count 'buffer-file-name) :to-equal 1))
(it "considers a modified buffer with backing file unsaved"
(spy-on 'file-exists-p :and-return-value t)
(spy-on 'buffer-file-name :and-return-value "test-buffer-name")
(with-temp-buffer
(set-buffer-modified-p t)
(expect (flycheck-buffer-saved-p) :not :to-be-truthy))
(expect (spy-calls-count 'file-exists-p) :to-equal 1)
(expect (spy-calls-count 'buffer-file-name) :to-equal 1)))
(describe "flycheck-default-executable-find"
(describe "non-existing programs"
(it "returns nil when given a non-existing program name"
(let ((result (flycheck-default-executable-find
"flycheck-nonexisting")))
(expect result :to-be nil)))
(it "returns nil when given a non-existing relative program path"
(let ((result (flycheck-default-executable-find
"dir/flycheck-nonexisting")))
(expect result :to-be nil)))
(it "returns nil when given a non-existing absolute program path"
(let ((result (flycheck-default-executable-find
"/usr/bin/flycheck-nonexisting")))
(expect result :to-be nil))))
(describe "existing programs with implied suffix"
(let (temp-dir program-path)
(before-each
(setq temp-dir (make-temp-file "flycheck-exec-find-root" 'dir-flag)
program-path (expand-file-name
(if (memq system-type '(cygwin windows-nt ms-dos))
"dir/flycheck-testprog.bat"
"dir/flycheck-testprog.program")
temp-dir))
(make-directory (expand-file-name "dir" temp-dir) t)
(write-region "" nil program-path)
(set-file-modes program-path
(logior 73 (file-modes program-path))))
(after-each
(ignore-errors (delete-directory temp-dir 'recursive)))
(it "resolves the path when given an existing program name"
(let* ((default-directory temp-dir)
(exec-path (list (expand-file-name "dir" temp-dir)))
(exec-suffixes '(".program" ".bat"))
(result (flycheck-default-executable-find
"flycheck-testprog")))
(expect result :to-equal program-path)))
(it "resolves the path when given an existing relative program path"
(let* ((default-directory temp-dir)
(exec-suffixes '(".program" ".bat"))
(result (flycheck-default-executable-find
"dir/flycheck-testprog")))
(expect result :to-equal program-path)))
(it "resolves the path when given an existing absolute program path"
(let* ((default-directory temp-dir)
(exec-suffixes '(".program" ".bat"))
(result (flycheck-default-executable-find
(expand-file-name "dir/flycheck-testprog" temp-dir))))
(expect result :to-equal program-path))))))
(describe "flycheck-string-to-number-safe"
(it "returns nil for nil"
(expect (flycheck-string-to-number-safe nil) :not :to-be-truthy))
(it "returns nil for not a string"
(expect (flycheck-string-to-number-safe [1 2 3]) :not :to-be-truthy))
(it "returns nil for already a number"
(expect (flycheck-string-to-number-safe 3) :not :to-be-truthy))
(it "returns nil for a non-numeric string"
(expect (flycheck-string-to-number-safe "123helloworld") :not :to-be-truthy))
(it "returns the number for a numeric string"
(expect (flycheck-string-to-number-safe "123") :to-equal 123))
(it "returns nil for a numeric string with leading whitespace"
(expect (flycheck-string-to-number-safe " 123") :not :to-be-truthy))
(it "returns nil for a numeric string with trailing whitespace"
(expect (flycheck-string-to-number-safe "123 ") :not :to-be-truthy)))
(describe "flycheck-string-list-p"
(it "returns nil for not a list"
(expect (flycheck-string-list-p ["foo" "bar"]) :not :to-be-truthy))
(it "returns nil for a plain string"
(expect (flycheck-string-list-p "foo") :not :to-be-truthy))
(it "returns nil for a plain integer"
(expect (flycheck-string-list-p 1) :not :to-be-truthy))
(it "returns nil for a plain symbol"
(expect (flycheck-string-list-p 'foo) :not :to-be-truthy))
(it "returns nil for a list with mixed types"
(expect (flycheck-string-list-p '("foo" 1 test)) :not :to-be-truthy))
(it "returns nil for a list of symbols"
(expect (flycheck-string-list-p '(foo bar)) :not :to-be-truthy))
(it "returns true for a list of strings"
(expect (flycheck-string-list-p '("foo" "bar")) :to-be-truthy))
(it "returns true for an empty list"
(expect (flycheck-string-list-p '()) :to-be-truthy)))
(describe "flycheck-symbol-list-p"
(it "returns nil for not a list"
(expect (flycheck-symbol-list-p ["foo" "bar"]) :not :to-be-truthy))
(it "returns nil for a plain string"
(expect (flycheck-symbol-list-p "foo") :not :to-be-truthy))
(it "returns nil for a plain integer"
(expect (flycheck-symbol-list-p 1) :not :to-be-truthy))
(it "returns nil for a plain symbol"
(expect (flycheck-symbol-list-p 'foo) :not :to-be-truthy))
(it "returns nil for a list with mixed types"
(expect (flycheck-symbol-list-p '("foo" 1 test)) :not :to-be-truthy))
(it "returns nil for a list of strings"
(expect (flycheck-symbol-list-p '("foo" "bar")) :not :to-be-truthy))
(it "returns true for a list of symbols"
(expect (flycheck-symbol-list-p '(foo bar)) :to-be-truthy))
(it "returns true for an empty list"
(expect (flycheck-symbol-list-p '()) :to-be-truthy)))
(describe "flycheck-same-files-p"
(it "returns true for same files"
(let ((default-directory flycheck-test-source-directory))
(expect (flycheck-same-files-p "flycheck.el" "flycheck.el")
:to-be-truthy)))
(it "returns nil for different files"
(let ((default-directory flycheck-test-source-directory))
(expect (flycheck-same-files-p "flycheck.el" "Makefile")
:not :to-be-truthy)))
(it "returns nil for file in non-existing directory"
(let ((default-directory flycheck-test-source-directory))
(expect (flycheck-same-files-p "flycheck.el" "foobar/flycheck.el")
:not :to-be-truthy)))
(it "returns true for non-existing files"
(let ((default-directory flycheck-test-source-directory))
(expect (flycheck-same-files-p "foobar/foobar" "foobar/foobar")
:to-be-truthy)))
(it "returns true across symlinks"
(assume (fboundp #'make-symbolic-link))
(let ((directory (make-temp-file "flycheck-test-same-files-p-" 'directory)))
(unwind-protect
(let ((link (expand-file-name "foobar.el" directory))
(flycheck (expand-file-name "flycheck.el"
flycheck-test-source-directory)))
(make-symbolic-link flycheck link)
(expect (flycheck-same-files-p flycheck link) :to-be-truthy))
(delete-directory directory 'recursive)))))
(describe "flycheck-temp-dir-system"
(it "creates a temporary directory"
(let ((dirname (flycheck-temp-dir-system)))
(unwind-protect
(expect (file-directory-p dirname) :to-be-truthy)
(flycheck-safe-delete-temporaries))
(expect (file-exists-p dirname) :not :to-be-truthy)
(expect (string-prefix-p (file-name-as-directory
(file-truename temporary-file-directory))
(file-truename dirname))
:to-be-truthy)
(expect (string-prefix-p flycheck-temp-prefix
(file-name-nondirectory
(directory-file-name dirname)))
:to-be-truthy))))
(describe "flycheck-temp-file-system"
(it "creates a temp file without file name"
(let ((filename (flycheck-temp-file-system nil)))
(unwind-protect
(expect (file-exists-p filename) :to-be-truthy)
(flycheck-safe-delete-temporaries))
(expect (file-exists-p filename) :not :to-be-truthy)
(expect (string-prefix-p (file-name-as-directory
(file-truename temporary-file-directory))
(file-truename filename))
:to-be-truthy)
(expect (string-prefix-p flycheck-temp-prefix
(file-name-nondirectory filename))
:to-be-truthy)))
(it "creates a temp file with complete path"
(let* ((filename (flycheck-temp-file-system "spam/with/eggs.el"))
(dirname (directory-file-name (file-name-directory filename))))
(unwind-protect
(progn
;; The file is not implicitly created, but the temporary
;; directory is.
(expect (file-exists-p filename) :not :to-be-truthy)
(expect (file-directory-p dirname) :to-be-truthy))
(flycheck-safe-delete-temporaries))
(expect (file-exists-p filename) :not :to-be-truthy)
(expect (file-directory-p dirname) :not :to-be-truthy)
;; The file name should be preserved. The temporary file should
;; reside in a subdirectory of the temporary directory
(expect (file-name-nondirectory filename) :to-equal "eggs.el")
(expect (string-prefix-p flycheck-temp-prefix
(file-name-nondirectory
(directory-file-name dirname)))
:to-be-truthy)
(expect (string-prefix-p flycheck-temp-prefix
(file-name-nondirectory
(directory-file-name dirname)))
:to-be-truthy))))
(describe "flycheck-temp-file-inplace"
(it "creates a temp file with just basename"
(let* ((default-directory flycheck-test-directory)
(filename (flycheck-temp-file-inplace "eggs.el")))
(unwind-protect
;; In place files should not be created early
(expect (file-exists-p filename) :not :to-be-truthy)
(flycheck-safe-delete-temporaries))
(expect filename :to-equal
(expand-file-name
(concat flycheck-temp-prefix "_eggs.el")))))
(it "creates a temp file with complete path"
(let* ((default-directory flycheck-test-directory)
(filename (flycheck-temp-file-inplace "spam/with/eggs.el")))
(unwind-protect
(expect (file-exists-p filename) :not :to-be-truthy)
(flycheck-safe-delete-temporaries))
(expect filename :to-equal
(expand-file-name (concat "spam/with/"
flycheck-temp-prefix
"_eggs.el")))))
(it "creates a temp file without file name"
(let ((filename (flycheck-temp-file-inplace nil)))
(unwind-protect
(expect (file-exists-p filename) :to-be-truthy)
(flycheck-safe-delete-temporaries))
(expect (file-name-extension filename) :not :to-be-truthy)
(expect (string-prefix-p flycheck-temp-prefix
(file-name-nondirectory filename))
:to-be-truthy))))
(describe "flycheck-save-buffer-to-file"
(it "saves buffer contents to a file"
(let ((filename (expand-file-name "tests-temp")))
(unwind-protect
(progn
(flycheck-buttercup-with-temp-buffer
(expect (file-exists-p filename) :not :to-be-truthy)
(insert "Hello world")
(flycheck-save-buffer-to-file filename))
(expect (file-exists-p filename) :to-be-truthy)
(expect (with-temp-buffer
(insert-file-contents filename)
(buffer-string))
:to-equal "Hello world"))
(ignore-errors (delete-file filename))))))
(describe "flycheck-prepend-with-option"
(it "returns nil for empty list"
(expect (flycheck-prepend-with-option "-f" nil) :not :to-be-truthy))
(it "prepends with default function"
(expect (flycheck-prepend-with-option "-L" '("foo" "bar"))
:to-equal '("-L" "foo" "-L" "bar")))
(it "prepends by string concatenation"
(expect (flycheck-prepend-with-option "-L" '("foo" "bar") #'concat)
:to-equal '("-Lfoo" "-Lbar"))))
(describe "flycheck-find-in-buffer"
(it "returns first match on success"
(with-temp-buffer
(insert "foo\nbar")
(expect (flycheck-find-in-buffer (rx (group "bar")))
:to-equal "bar")))
(it "returns nil on failure"
(with-temp-buffer
(insert "spam\nwith eggs")
(expect (flycheck-find-in-buffer (rx (group "bar")))
:not :to-be-truthy)))
(it "saves excursion"
(with-temp-buffer
(insert "spam\nwith eggs")
(goto-char (point-min))
(forward-line)
(expect (flycheck-find-in-buffer (rx (group "spam")))
:to-be-truthy)
(expect (point) :to-equal 6))))
(describe "flycheck-ephemeral-buffer-p"
(it "returns true for temporary buffer"
(flycheck-buttercup-with-temp-buffer
(expect (flycheck-ephemeral-buffer-p) :to-be-truthy)))
(it "returns true for buffer with leading whitespace"
(flycheck-buttercup-with-temp-buffer
(rename-buffer " foo")
(expect (flycheck-ephemeral-buffer-p) :to-be-truthy)))
(it "returns nil for buffer without leading whitespace"
(flycheck-buttercup-with-temp-buffer
(rename-buffer "foo")
(expect (flycheck-ephemeral-buffer-p) :not :to-be-truthy))))
(describe "flycheck-autoloads-file-p"
(it "returns nil for ephemeral buffer"
(flycheck-buttercup-with-temp-buffer
(expect (flycheck-autoloads-file-p) :not :to-be-truthy)))
(it "returns true for autoloads without backing file"
(flycheck-buttercup-with-temp-buffer
(rename-buffer "foo-autoloads.el")
(expect (flycheck-autoloads-file-p) :to-be-truthy)))
(it "returns true for autoloads with backing file"
(flycheck-buttercup-with-file-buffer (locate-library "shut-up-autoloads")
(expect (flycheck-autoloads-file-p) :to-be-truthy)))
(it "returns nil for a plain file"
(flycheck-buttercup-with-file-buffer
(expand-file-name "Eask" flycheck-test-source-directory)
(expect (flycheck-autoloads-file-p) :not :to-be-truthy))))
(describe "flycheck-in-user-emacs-directory-p"
(it "returns nil for no child of user-emacs-directory"
(let ((user-emacs-directory "/flycheck-nonexisting"))
(expect (flycheck-in-user-emacs-directory-p
(flycheck-buttercup-resource-filename
"language/emacs-lisp/warnings.el"))
:not :to-be-truthy)))
(it "returns true for direct child of user-emacs-directory"
(let ((user-emacs-directory flycheck-test-directory))
(expect (flycheck-in-user-emacs-directory-p
(expand-file-name "init.el" flycheck-test-directory))
:to-be-truthy)))
(it "returns true for indirect child of user-emacs-directory"
(let ((user-emacs-directory flycheck-test-directory))
(expect (flycheck-in-user-emacs-directory-p
(flycheck-buttercup-resource-filename
"language/emacs-lisp/warnings.el"))
:to-be-truthy))))
(describe "flycheck-safe-delete"
(it "recursively removes directory"
(let ((dirname (flycheck-temp-dir-system)))
(unwind-protect
(let ((filename (expand-file-name "foo" dirname)))
(process-lines "touch" filename)
(expect (string-prefix-p dirname filename) :to-be-truthy)
(expect (file-exists-p filename) :to-be-truthy)
(flycheck-safe-delete dirname)
(expect (file-exists-p filename) :not :to-be-truthy)
(expect (file-directory-p dirname) :not :to-be-truthy)
(expect (file-exists-p dirname) :not :to-be-truthy))
(ignore-errors (delete-directory dirname 'recurse))))))
(describe "flycheck-module-root-directory"
(it "returns default-directory for no module name and no file name"
(let ((default-directory flycheck-test-resources-directory))
(expect (flycheck-module-root-directory nil)
:to-equal flycheck-test-resources-directory)))
(it "returns file directory for no module name"
(let ((default-directory flycheck-test-resources-directory)
(file-name (flycheck-buttercup-resource-filename
"language/emacs-lisp/warnings.el")))
(expect (flycheck-module-root-directory nil file-name)
:to-equal (flycheck-buttercup-resource-filename
"language/emacs-lisp/"))))
(it "finds root for module name as string"
(let ((default-directory flycheck-test-resources-directory)
(file-name (flycheck-buttercup-resource-filename
"language/emacs-lisp/warnings.el")))
(expect (flycheck-module-root-directory
"language.emacs-lisp.warnings" file-name)
:to-equal flycheck-test-resources-directory)))
(it "finds root for module name as list"
(let ((default-directory flycheck-test-resources-directory)
(file-name (flycheck-buttercup-resource-filename
"language/emacs-lisp/warnings.el")))
(expect (flycheck-module-root-directory
'("language" "emacs-lisp" "warnings") file-name)
:to-equal flycheck-test-resources-directory)))
(it "falls back for mismatching module name"
(let ((default-directory flycheck-test-resources-directory)
(file-name (flycheck-buttercup-resource-filename
"language/emacs-lisp/warnings.el")))
(expect (flycheck-module-root-directory
'("foo" "warnings") file-name)
:to-equal (flycheck-buttercup-resource-filename
"language/emacs-lisp/"))))))
;;; test-util.el ends here
|