aboutsummaryrefslogtreecommitdiff
path: root/gptel-tools
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-05-16 01:39:57 -0500
committerCraig Jennings <c@cjennings.net>2026-05-16 01:39:57 -0500
commit73e63b6c6850f8e14d8374c7bf6b127971cfbb08 (patch)
treeb446f4ac63901d75376664abfd7b8cb5f8ac436a /gptel-tools
parent2a98feaf1285b495e7d6d1eed2abf02620188e29 (diff)
downloaddotemacs-73e63b6c6850f8e14d8374c7bf6b127971cfbb08.tar.gz
dotemacs-73e63b6c6850f8e14d8374c7bf6b127971cfbb08.zip
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.
Diffstat (limited to 'gptel-tools')
-rw-r--r--gptel-tools/read_buffer.el24
-rw-r--r--gptel-tools/write_text_file.el134
2 files changed, 81 insertions, 77 deletions
diff --git a/gptel-tools/read_buffer.el b/gptel-tools/read_buffer.el
index d01cee71..1b4fc904 100644
--- a/gptel-tools/read_buffer.el
+++ b/gptel-tools/read_buffer.el
@@ -1,27 +1,31 @@
;;; read_buffer.el --- Read buffer tool for GPTel -*- coding: utf-8; lexical-binding: t; -*-
;;; Commentary:
-;;
+;; Gptel tool that returns the contents of an Emacs buffer by name.
;;; Code:
(require 'gptel)
+(defun cj/read-buffer--get-content (buffer)
+ "Return the substring of BUFFER from `point-min' to `point-max'.
+BUFFER may be a buffer object or a buffer name string. Signal an
+error when no live buffer matches."
+ (unless (buffer-live-p (get-buffer buffer))
+ (error "Buffer %s is not live" buffer))
+ (with-current-buffer buffer
+ (buffer-substring-no-properties (point-min) (point-max))))
+
(gptel-make-tool
:name "read_buffer"
- :function (lambda (buffer)
- (unless (buffer-live-p (get-buffer buffer))
- (error "Error: buffer %s is not live" buffer))
- (with-current-buffer buffer
- (buffer-substring-no-properties (point-min) (point-max))))
+ :function (lambda (buffer) (cj/read-buffer--get-content buffer))
:description "return the contents of an emacs buffer"
:args (list '(:name "buffer"
- :type string ; :type value must be a symbol
- :description "the name of the buffer whose contents are to be retrieved"))
+ :type string
+ :description "the name of the buffer whose contents are to be retrieved"))
:category "emacs")
-;; Automatically add to gptel-tools on load
(add-to-list 'gptel-tools (gptel-get-tool '("emacs" "read_buffer")))
(provide 'read_buffer)
-;;; read_buffer.el ends here.
+;;; read_buffer.el ends here
diff --git a/gptel-tools/write_text_file.el b/gptel-tools/write_text_file.el
index 03d64e57..40482c66 100644
--- a/gptel-tools/write_text_file.el
+++ b/gptel-tools/write_text_file.el
@@ -1,94 +1,94 @@
;;; write_text_file.el --- Write text files for gptel -*- lexical-binding: t; -*-
-;; Copyright (C) 2025
-
-;; Author: gptel-tool-writer
+;; Author: Craig Jennings <c@cjennings.net>
;; Keywords: convenience, tools
-;; Package-Requires: ((emacs "27.1") (gptel "0.9.0"))
;; 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.
-
;;; Commentary:
-;; This file provides a gptel tool for writing text files to the filesystem.
-;; The tool includes safety features like backup creation, size limits,
-;; and restriction to the user's home directory.
+;; Gptel tool for writing a text file under the user's home directory.
+;; Creates parent directories as needed, optionally overwrites an
+;; existing file (with a timestamped backup), and rejects writes
+;; larger than 1 GB unless the user confirms.
;;; Code:
(require 'gptel)
+(defconst cj/write-text-file--size-limit (* 1024 1024 1024)
+ "Soft cap for new-file writes (1 GB). Above this size a confirm is required.")
+
+(defun cj/write-text-file--validate-path (path)
+ "Validate PATH for write. Return the expanded path on success.
+PATH must resolve inside the user's home directory."
+ (let ((full (expand-file-name path "~")))
+ (unless (string-prefix-p (expand-file-name "~") full)
+ (error "Path must be within home directory: %s" path))
+ full))
+
+(defun cj/write-text-file--backup-name (path)
+ "Return a timestamped backup filename for PATH."
+ (format "%s-%s.bak"
+ path
+ (format-time-string "%Y-%m-%d-%H%M%S")))
+
+(defun cj/write-text-file--ensure-parent (path)
+ "Ensure the parent directory of PATH exists and is writable.
+Create missing parents. Signal on failure."
+ (let ((parent (file-name-directory path)))
+ (when parent
+ (unless (file-exists-p parent)
+ (condition-case err
+ (make-directory parent t)
+ (error (error "Cannot create directory %s: %s"
+ parent (error-message-string err)))))
+ (unless (file-writable-p parent)
+ (error "No write permission for directory %s" parent)))))
+
+(defun cj/write-text-file--run (path content &optional overwrite)
+ "Write CONTENT to PATH. Return a status string.
+PATH must be inside the user's home directory. If the file exists
+and OVERWRITE is non-nil, make a timestamped backup before writing;
+otherwise signal."
+ (let* ((full (cj/write-text-file--validate-path path))
+ (content (or content ""))
+ (size (length content)))
+ (when (> size cj/write-text-file--size-limit)
+ (unless (y-or-n-p (format "File is %s. Write anyway? "
+ (file-size-human-readable size)))
+ (error "File write cancelled: size exceeds 1GB limit")))
+ (cj/write-text-file--ensure-parent full)
+ (when (file-exists-p full)
+ (if overwrite
+ (let ((backup (cj/write-text-file--backup-name full)))
+ (copy-file full backup t)
+ (message "Backed up existing file to %s" backup))
+ (error "File %s already exists. Set overwrite to true to replace it" full)))
+ (with-temp-file full (insert content))
+ (format "Successfully wrote %d bytes to %s" size full)))
+
(with-eval-after-load 'gptel
(gptel-make-tool
:name "write_text_file"
:function (lambda (path content &optional overwrite)
- (let* ((full-path (expand-file-name path "~"))
- (content (or content ""))
- (content-size (length content))
- (size-limit (* 1024 1024 1024))) ; 1 GB
- ;; Check if path is within home directory
- (unless (string-prefix-p (expand-file-name "~") full-path)
- (error "Path must be within home directory"))
- ;; Check size limit
- (when (> content-size size-limit)
- (unless (y-or-n-p (format "File is %s. Write anyway? "
- (file-size-human-readable content-size)))
- (error "File write cancelled: size exceeds 1GB limit")))
- ;; Check write permission on parent directory
- (let ((parent-dir (file-name-directory full-path)))
- (when parent-dir
- ;; Create parent directories if needed
- (unless (file-exists-p parent-dir)
- (condition-case err
- (make-directory parent-dir t)
- (error (error "Cannot create directory %s: %s"
- parent-dir (error-message-string err)))))
- ;; Check write permission
- (unless (file-writable-p parent-dir)
- (error "No write permission for directory %s" parent-dir))))
- ;; Handle existing file
- (when (file-exists-p full-path)
- (if overwrite
- ;; Create backup with timestamp
- (let* ((backup-name
- (format "%s-%s.bak"
- full-path
- (format-time-string "%Y-%m-%d-%H%M%S"))))
- (copy-file full-path backup-name t)
- (message "Backed up existing file to %s" backup-name))
- (error "File %s already exists. Set overwrite to true to replace it" full-path)))
- ;; Write the file atomically
- (with-temp-file full-path
- (insert content))
- (format "Successfully wrote %d bytes to %s"
- content-size full-path)))
+ (cj/write-text-file--run path content overwrite))
:description "Write text content to a file within the user's home directory. Creates parent directories if needed. Backs up existing files with timestamp when overwriting."
:args (list '(:name "path"
- :type string
- :description "File path relative to home directory, e.g., 'documents/myfile.txt' or '~/documents/myfile.txt'")
+ :type string
+ :description "File path relative to home directory, e.g., 'documents/myfile.txt' or '~/documents/myfile.txt'")
'(:name "content"
- :type string
- :description "The text content to write to the file")
+ :type string
+ :description "The text content to write to the file")
'(:name "overwrite"
- :type boolean
- :description "If true, backup and overwrite existing file. If false or omitted, error if file exists"
- :optional t))
+ :type boolean
+ :description "If true, backup and overwrite existing file. If false or omitted, error if file exists"
+ :optional t))
:category "filesystem"
:confirm t
:include t)
-
- ;; Automatically add to gptel-tools on load
+
(add-to-list 'gptel-tools (gptel-get-tool '("filesystem" "write_text_file"))))
(provide 'write_text_file)
-;;; write_text_file.el ends here \ No newline at end of file
+;;; write_text_file.el ends here