/
testrun-pytest.el
73 lines (58 loc) · 2.58 KB
/
testrun-pytest.el
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
;;; testrun-pytest.el --- Pytest wrapper for testrun -*- lexical-binding: t -*-
;; Copyright (C) 2023 Alessandro Martini <martini97@protonmail.ch>
;; Author: Alessandro Martini <martini97@protonmail.ch>
;; Mantainer: Alessandro Martini <martini97@protonmail.ch>
;; Version: 0.1.1
;; Package-Requires: ((emacs "29"))
;; Keywords: tests convenience
;; Homepage: https://github.com/martini97/testrun.el
;; This file is NOT part of GNU Emacs.
;; This file 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 file 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 file. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Pytest helpers for `testrun'.
;;; Code:
(require 'rx)
(require 'seq)
(require 'testrun-core)
(require 'testrun-treesit)
(defcustom testrun-pytest-separator "::"
"Separator for the pyests test path."
:type 'string
:group 'testrun)
(defvar testrun-pytest-namespace-node-types '("class_definition")
"List of node types relevant for the \"namespace\" scope.")
(defvar testrun-pytest-nearest-node-types '("class_definition"
"function_definition")
"List of node types relevant for the \"nearest\" scope.")
(defvar testrun-pytest-node-regex (rx bol (or "test" "Test") (1+ (or word "_")) eol))
(defun testrun-pytest--get-nodes (node-types)
"Return name of nodes with NODE-TYPES."
(seq-filter (lambda (n) (string-match-p testrun-pytest-node-regex n))
(mapcar #'testrun-treesit--get-node-name
(testrun-treesit--get-nodes-by-type node-types))))
;;;###autoload
(defun testrun-pytest-get-test (scope)
"Get the pytest test specifier string for the SCOPE."
(string-join
(let ((filename (testrun-core--file-name)))
(pcase scope
("nearest" (append (list filename)
(testrun-pytest--get-nodes testrun-pytest-nearest-node-types)))
("namespace" (append (list filename)
(testrun-pytest--get-nodes testrun-pytest-namespace-node-types)))
("file" (list filename))
("all" nil)))
testrun-pytest-separator))
(provide 'testrun-pytest)
;;; testrun-pytest.el ends here