1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-01-06 03:40:56 -08:00
emacs/test/lisp/progmodes/project-tests.el
Po Lu ecf08f0621 Merge from savannah/emacs-29
dc4e6b1329 ; Update copyright years in more files
64b3777631 ; Run set-copyright from admin.el
8e1c56ae46 ; Add 2024 to copyright years

# Conflicts:
#	doc/misc/modus-themes.org
#	doc/misc/texinfo.tex
#	etc/NEWS
#	etc/refcards/ru-refcard.tex
#	etc/themes/modus-operandi-theme.el
#	etc/themes/modus-themes.el
#	etc/themes/modus-vivendi-theme.el
#	lib/alloca.in.h
#	lib/binary-io.h
#	lib/c-ctype.h
#	lib/c-strcasecmp.c
#	lib/c-strncasecmp.c
#	lib/careadlinkat.c
#	lib/cloexec.c
#	lib/close-stream.c
#	lib/diffseq.h
#	lib/dup2.c
#	lib/filemode.h
#	lib/fpending.c
#	lib/fpending.h
#	lib/fsusage.c
#	lib/getgroups.c
#	lib/getloadavg.c
#	lib/gettext.h
#	lib/gettime.c
#	lib/gettimeofday.c
#	lib/group-member.c
#	lib/malloc.c
#	lib/md5-stream.c
#	lib/md5.c
#	lib/md5.h
#	lib/memmem.c
#	lib/memrchr.c
#	lib/nanosleep.c
#	lib/save-cwd.h
#	lib/sha1.c
#	lib/sig2str.c
#	lib/stdlib.in.h
#	lib/strtoimax.c
#	lib/strtol.c
#	lib/strtoll.c
#	lib/time_r.c
#	lib/xalloc-oversized.h
#	lisp/auth-source-pass.el
#	lisp/emacs-lisp/lisp-mnt.el
#	lisp/emacs-lisp/timer.el
#	lisp/info-look.el
#	lisp/jit-lock.el
#	lisp/loadhist.el
#	lisp/mail/rmail.el
#	lisp/net/ntlm.el
#	lisp/net/webjump.el
#	lisp/progmodes/asm-mode.el
#	lisp/progmodes/project.el
#	lisp/progmodes/sh-script.el
#	lisp/textmodes/flyspell.el
#	lisp/textmodes/reftex-toc.el
#	lisp/textmodes/reftex.el
#	lisp/textmodes/tex-mode.el
#	lisp/url/url-gw.el
#	m4/alloca.m4
#	m4/clock_time.m4
#	m4/d-type.m4
#	m4/dirent_h.m4
#	m4/dup2.m4
#	m4/euidaccess.m4
#	m4/fchmodat.m4
#	m4/filemode.m4
#	m4/fsusage.m4
#	m4/getgroups.m4
#	m4/getloadavg.m4
#	m4/getrandom.m4
#	m4/gettime.m4
#	m4/gettimeofday.m4
#	m4/gnulib-common.m4
#	m4/group-member.m4
#	m4/inttypes.m4
#	m4/malloc.m4
#	m4/manywarnings.m4
#	m4/mempcpy.m4
#	m4/memrchr.m4
#	m4/mkostemp.m4
#	m4/mktime.m4
#	m4/nproc.m4
#	m4/nstrftime.m4
#	m4/pathmax.m4
#	m4/pipe2.m4
#	m4/pselect.m4
#	m4/pthread_sigmask.m4
#	m4/readlink.m4
#	m4/realloc.m4
#	m4/sig2str.m4
#	m4/ssize_t.m4
#	m4/stat-time.m4
#	m4/stddef_h.m4
#	m4/stdint.m4
#	m4/stdio_h.m4
#	m4/stdlib_h.m4
#	m4/stpcpy.m4
#	m4/strnlen.m4
#	m4/strtoimax.m4
#	m4/strtoll.m4
#	m4/time_h.m4
#	m4/timegm.m4
#	m4/timer_time.m4
#	m4/timespec.m4
#	m4/unistd_h.m4
#	m4/warnings.m4
#	nt/configure.bat
#	nt/preprep.c
#	test/lisp/register-tests.el
2024-01-02 10:28:14 +08:00

166 lines
6.8 KiB
EmacsLisp

;;; project-tests.el --- tests for project.el -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2024 Free Software Foundation, Inc.
;; Keywords:
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Unit tests for progmodes/project.el.
;;; Code:
(require 'project)
(require 'cl-lib)
(require 'ert)
(require 'ert-x) ; ert-with-temp-directory
(require 'grep)
(require 'xref)
(ert-deftest project/quoted-directory ()
"Check that `project-files' and `project-find-regexp' deal with
quoted directory names (Bug#47799)."
(skip-unless (executable-find find-program))
(skip-unless (executable-find "xargs"))
(skip-unless (executable-find "grep"))
(ert-with-temp-directory directory
(let ((default-directory directory)
(project-current-directory-override t)
(project-find-functions nil)
(project-list-file
(expand-file-name "projects" directory))
(project (cons 'transient (file-name-quote directory)))
(file (expand-file-name "file" directory)))
(add-hook 'project-find-functions (lambda (_dir) project))
(should (eq (project-current) project))
(write-region "contents" nil file nil nil nil 'excl)
(should (equal (project-files project)
(list (file-name-quote file))))
(let* ((references nil)
(xref-search-program 'grep)
(xref-show-xrefs-function
(lambda (fetcher _display)
(push (funcall fetcher) references))))
(project-find-regexp "tent")
(pcase references
(`((,item))
;; FIXME: Shouldn't `xref-match-item' be a subclass of
;; `xref-item'?
(should (cl-typep item '(or xref-item xref-match-item)))
(should (file-equal-p
(xref-location-group (xref-item-location item))
file)))
(otherwise
(ert-fail (format-message "Unexpected references: %S"
otherwise))))))))
(cl-defstruct project-tests--trivial root ignores)
(cl-defmethod project-root ((project project-tests--trivial))
(project-tests--trivial-root project))
(cl-defmethod project-ignores ((project project-tests--trivial) _dir)
(project-tests--trivial-ignores project))
(ert-deftest project-ignores ()
"Check that `project-files' correctly ignores the files
returned by `project-ignores' if the root directory is a
directory name (Bug#48471)."
(skip-unless (executable-find find-program))
(ert-with-temp-directory dir
(make-empty-file (expand-file-name "some-file" dir))
(make-empty-file (expand-file-name "ignored-file" dir))
(let* ((project (make-project-tests--trivial
:root (file-name-as-directory dir)
:ignores '("./ignored-file")))
(files (project-files project))
(relative-files
(cl-loop for file in files
collect (file-relative-name file dir))))
(should (equal relative-files '("some-file"))))))
(ert-deftest project-ignores-bug-50240 ()
"Check that `project-files' does not ignore all files.
When `project-ignores' includes a name matching project dir."
(skip-unless (executable-find find-program))
(ert-with-temp-directory dir
(make-empty-file (expand-file-name "some-file" dir))
(let* ((project (make-project-tests--trivial
:root (file-name-as-directory dir)
:ignores (list (file-name-nondirectory
(directory-file-name dir)))))
(files (project-files project)))
(should (equal files
(list
(expand-file-name "some-file" dir)))))))
(defvar project-tests--this-file (or (bound-and-true-p byte-compile-current-file)
(and load-in-progress load-file-name)
buffer-file-name))
(ert-deftest project-vc-recognizes-git ()
"Check that Git repository is detected."
(skip-unless (eq (vc-responsible-backend default-directory) 'Git))
(let* ((vc-handled-backends '(Git))
(dir (file-name-directory project-tests--this-file))
(_ (vc-file-clearprops dir))
(project-vc-extra-root-markers nil)
(project (project-current nil dir)))
(should-not (null project))
(should (string-match-p
"\\`test/lisp/progmodes/project-tests\\.elc?"
(file-relative-name
project-tests--this-file
(project-root project))))))
(ert-deftest project-vc-extra-root-markers-supports-wildcards ()
"Check that one can add wildcard entries."
(skip-unless (eq (vc-responsible-backend default-directory) 'Git))
(let* ((dir (file-name-directory project-tests--this-file))
(_ (vc-file-clearprops dir))
(project-vc-extra-root-markers '("files-x-tests.*"))
(project (project-current nil dir)))
(should-not (null project))
(should (nth 1 project))
(should (string-match-p "/test/lisp/\\'" (project-root project)))))
(ert-deftest project-vc-supports-project-in-different-dir ()
"Check that it picks up dir-locals settings from somewhere else."
(skip-unless (eq (vc-responsible-backend default-directory) 'Git))
(let* ((dir (ert-resource-directory))
(_ (vc-file-clearprops dir))
(project-vc-extra-root-markers '(".dir-locals.el"))
(project (project-current nil dir)))
(should-not (null project))
(should (string-match-p "/test/lisp/progmodes/project-resources/\\'" (project-root project)))
(should (member "etc" (project-ignores project dir)))
(should (equal '(".dir-locals.el" "foo")
(mapcar #'file-name-nondirectory (project-files project))))))
(ert-deftest project-vc-nonexistent-directory-no-error ()
"Check that is doesn't error out when the current dir does not exist."
(skip-unless (eq (vc-responsible-backend default-directory) 'Git))
(let* ((dir (expand-file-name "foo-456/bar/" (ert-resource-directory)))
(_ (vc-file-clearprops dir))
(project-vc-extra-root-markers '(".dir-locals.el"))
(project (project-current nil dir)))
(should-not (null project))
(should (string-match-p "/test/lisp/progmodes/project-resources/\\'" (project-root project)))))
;;; project-tests.el ends here