From 43fdbf00214294e41ba5f17587f2abc24cba2d4c Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sat, 16 May 2026 01:39:57 -0500 Subject: test(gptel-tools): cover the helpers across the five remaining tools The gptel-tools files had zero direct coverage outside of `update_text_file`, which landed with its rewrite earlier this session. This commit adds 52 tests across the five other tools. For three of the tools the helpers were already top-level defuns (`read_text_file`, `list_directory_files`, `move_to_trash`). The other two had their main bodies inlined into the `gptel-make-tool` lambda -- I extracted them so the work is testable without mocking gptel itself: read_buffer.el -> `cj/read-buffer--get-content` write_text_file.el -> `cj/write-text-file--run` plus `--validate-path`, `--backup-name`, `--ensure-parent` Test files, by tool: - read_buffer.el (5 tests): normal, empty, buffer-object, text-property-stripping, missing buffer. - write_text_file.el (10 tests): validate-path, backup-name shape, ensure-parent (creates missing / rejects unwritable), run with normal / overwrite / existing-no-overwrite / empty content / outside-home. - read_text_file.el (12 tests): validate-file-path (normal + three error shapes), metadata plist shape, size limits (no-op / hard cap / warning bypass with no-confirm), binary detection (text vs null-byte), special-type EPUB and generic-binary paths. - list_directory_files.el (15 tests): mode-to-permissions (file / dir / executable), get-file-info (file / directory), extension filter (keep / drop / always-dir / nil-extension), format-file- entry, list-directory flat / recursive / error, format-output with and without files. - move_to_trash.el (10 tests): unique-name (no conflict / conflict with timestamp / no-extension), validate-path (HOME / /tmp / outside / critical-dir / missing), perform on file and directory. Each test file uses the same load-path / gptel-stub idiom (`eval-and-compile` block, gptel stub when the real package isn't available) so the byte-compile hook is happy. --- tests/test-gptel-tools-list-directory-files.el | 162 +++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 tests/test-gptel-tools-list-directory-files.el (limited to 'tests/test-gptel-tools-list-directory-files.el') diff --git a/tests/test-gptel-tools-list-directory-files.el b/tests/test-gptel-tools-list-directory-files.el new file mode 100644 index 00000000..a91a7e79 --- /dev/null +++ b/tests/test-gptel-tools-list-directory-files.el @@ -0,0 +1,162 @@ +;;; test-gptel-tools-list-directory-files.el --- Tests for list_directory_files -*- lexical-binding: t; -*- + +;;; Commentary: +;; Tests for the helpers in list_directory_files.el. + +;;; Code: + +(require 'ert) +(require 'cl-lib) + +(eval-and-compile + (add-to-list 'load-path (expand-file-name "tests" user-emacs-directory)) + (add-to-list 'load-path (expand-file-name "gptel-tools" user-emacs-directory)) + (setq load-prefer-newer t) + (unless (featurep 'gptel) + (defvar gptel-tools nil) + (defun gptel-make-tool (&rest _args) nil) + (defun gptel-get-tool (&rest _args) nil) + (provide 'gptel))) + +(require 'list_directory_files) + +;; -------------------------- helpers + +(defun test-gptel-tools-list--with-tree (fn) + "Create a small directory tree, call FN with its root, clean up." + (let ((root (make-temp-file "test-gptel-tools-list-" t))) + (unwind-protect + (progn + (with-temp-file (expand-file-name "a.txt" root) (insert "a")) + (with-temp-file (expand-file-name "b.org" root) (insert "b")) + (make-directory (expand-file-name "sub" root)) + (with-temp-file (expand-file-name "sub/c.txt" root) (insert "c")) + (funcall fn root)) + (delete-directory root t)))) + +;; -------------------------- mode-to-permissions + +(ert-deftest test-gptel-tools-list-mode-to-permissions-regular-file () + "Mode 0644 on a regular file: -rw-r--r--." + (should (equal (list-directory-files--mode-to-permissions #o0644) + "-rw-r--r--"))) + +(ert-deftest test-gptel-tools-list-mode-to-permissions-directory () + "Mode 0755 + dir bit: drwxr-xr-x." + (should (equal (list-directory-files--mode-to-permissions + (logior #o40000 #o0755)) + "drwxr-xr-x"))) + +(ert-deftest test-gptel-tools-list-mode-to-permissions-executable () + "Mode 0700: -rwx------." + (should (equal (list-directory-files--mode-to-permissions #o0700) + "-rwx------"))) + +;; -------------------------- get-file-info + +(ert-deftest test-gptel-tools-list-get-file-info-success () + "Success: returns a plist with :success t and metadata." + (test-gptel-tools-list--with-tree + (lambda (root) + (let ((info (list-directory-files--get-file-info + (expand-file-name "a.txt" root)))) + (should (plist-get info :success)) + (should (numberp (plist-get info :size))) + (should (stringp (plist-get info :permissions))))))) + +(ert-deftest test-gptel-tools-list-get-file-info-directory () + "Directory info: :is-directory is t." + (test-gptel-tools-list--with-tree + (lambda (root) + (let ((info (list-directory-files--get-file-info + (expand-file-name "sub" root)))) + (should (plist-get info :is-directory)))))) + +;; -------------------------- filter-by-extension + +(ert-deftest test-gptel-tools-list-filter-by-extension-keeps-match () + "Filter for txt keeps txt files." + (let* ((filter (list-directory-files--filter-by-extension "txt")) + (info '(:success t :path "/x/foo.txt" :is-directory nil))) + (should (funcall filter info)))) + +(ert-deftest test-gptel-tools-list-filter-by-extension-drops-non-match () + "Filter for txt drops non-txt files." + (let* ((filter (list-directory-files--filter-by-extension "txt")) + (info '(:success t :path "/x/foo.org" :is-directory nil))) + (should-not (funcall filter info)))) + +(ert-deftest test-gptel-tools-list-filter-by-extension-always-keeps-directories () + "Filter keeps directories regardless of extension." + (let* ((filter (list-directory-files--filter-by-extension "txt")) + (info '(:success t :path "/x/sub" :is-directory t))) + (should (funcall filter info)))) + +(ert-deftest test-gptel-tools-list-filter-by-extension-no-extension-is-nil () + "No extension produces a nil filter (i.e. no filtering)." + (should-not (list-directory-files--filter-by-extension nil))) + +;; -------------------------- format-file-entry + +(ert-deftest test-gptel-tools-list-format-file-entry-shape () + "Formatted entry contains permissions, size, mtime, and relative path." + (let* ((info (list (cons :path "/home/u/foo.txt") + (cons :permissions "-rw-r--r--") + (cons :executable nil) + (cons :size 42) + (cons :last-modified (current-time)))) + ;; Build as plist by flattening the cons list. + (info-plist (cl-loop for (k . v) in info append (list k v))) + (out (list-directory-files--format-file-entry info-plist "/home/u"))) + (should (string-match-p "-rw-r--r--" out)) + (should (string-match-p "foo.txt" out)))) + +;; -------------------------- list-directory + +(ert-deftest test-gptel-tools-list-list-directory-flat () + "Non-recursive listing returns only entries in the top level." + (test-gptel-tools-list--with-tree + (lambda (root) + (let* ((result (list-directory-files--list-directory root nil nil)) + (files (plist-get result :files))) + (should files) + (let ((paths (mapcar (lambda (i) (plist-get i :path)) files))) + (should (cl-some (lambda (p) (string-match-p "/a\\.txt\\'" p)) paths)) + (should-not (cl-some (lambda (p) (string-match-p "/c\\.txt\\'" p)) paths))))))) + +(ert-deftest test-gptel-tools-list-list-directory-recursive () + "Recursive listing also returns sub-directory contents." + (test-gptel-tools-list--with-tree + (lambda (root) + (let* ((result (list-directory-files--list-directory root t nil)) + (files (plist-get result :files)) + (paths (mapcar (lambda (i) (plist-get i :path)) files))) + (should (cl-some (lambda (p) (string-match-p "/c\\.txt\\'" p)) paths)))))) + +(ert-deftest test-gptel-tools-list-list-directory-error-not-a-directory () + "Non-directory path returns errors entry." + (test-gptel-tools-list--with-tree + (lambda (root) + (let* ((result (list-directory-files--list-directory + (expand-file-name "a.txt" root) nil nil)) + (errors (plist-get result :errors))) + (should errors))))) + +;; -------------------------- format-output + +(ert-deftest test-gptel-tools-list-format-output-has-files-section () + "Format-output includes a \"Found N file(s)\" line when files present." + (test-gptel-tools-list--with-tree + (lambda (root) + (let* ((result (list-directory-files--list-directory root nil nil)) + (out (list-directory-files--format-output root result))) + (should (string-match-p "Found [0-9]+ file" out)))))) + +(ert-deftest test-gptel-tools-list-format-output-empty () + "Empty result: \"No files found\"." + (let ((out (list-directory-files--format-output + "/nowhere" '(:files nil :errors nil)))) + (should (string-match-p "No files found" out)))) + +(provide 'test-gptel-tools-list-directory-files) +;;; test-gptel-tools-list-directory-files.el ends here -- cgit v1.2.3