我可以在当前执行上下文中(应用)变量的内容,但是如果我将其保存以供以后使用,则无法

时间:2018-08-01 21:44:20

标签: elisp

背景

我在Emacs中使用的是由多个部分组成的代码库,这些代码库具有一些非标准的布局和比我真正称呼的更为合理的冗余静态分析工具,更不用说方便了。我正在尝试开发工具来解决这个问题。我发现在以前广泛使用emacs的雇主那里很方便的一件事是一对功能:

  • 选择适合当前文件(绑定到F5)的testrunner调用的按钮
  • 一个相关按钮,即使该点不再位于文件中(绑定到C-<F5>),该重复按钮也会重复执行该调用

首先,我打算逐步采用公司特定的配置,以说明在什么情况下要运行哪些测试,并附带一些方便的附加信息,例如“如果缓冲区向arbitrary.py开放,请尝试寻找tests/test_arbitrary.py”多种语言。

第二个特别有用:考虑代码使用JSON文件的情况,并且您希望在编辑JSON文件时有一种快速的方法来调用测试。

直接问题

我已经成功开发了一个函数dwim-file-test-runner,该函数可以有效地开始分配代码,并且已将其绑定到F5,如上所述。我正在研究“再次运行测试”功能。

以下代码或多或少按预期工作:

(defun dwim-python-runner ()
  "Activate tests for a Python file"
  ;; invocation is a list of 4 elements similar to that from (elpy-test-at-point)
  ;; the first is the cwd of the command, the second is the test,
  ;; the third and fourth are modules and individual tests.
  ;; elpy does something smart to find the library root (not the project root)
  ;; but our pytest configuration is unfortunately cwd-sensitive
  ;; so hardcode it to use the project root.
  (let ((invocation (cons (elpy-project-root) (cdr (elpy-test-at-point)))))
    (dwim-run-thing 'elpy-test-pytest-runner invocation)))

(defvar dwim-test-invocation ())
(defun dwim-run-thing (func args)
  "Run a thing, and save as the last thing we ran"
  (setq dwim-test-invocation (cons func args))
  (apply (car dwim-test-invocation) (cdr dwim-test-invocation)))

不起作用的是尝试重新运行测试的代码:

(defun dwim-run-again ()
  "Runs the test from last time, again"
  (interactive)
  (apply (car dwim-test-invocation) (cdr dwim-test-invocation)))

这会产生错误:

funcall: Invalid function: (elpy-test-pytest-runner "/Users/myusername/code/project/python-root/" "/Users/myusername/code/project/python-root/company/subproject/api/views/tests/test_component.py" "tests.test_component" nil)

...这非常令人惊讶,因为调用似乎是相同的。

问题:

  • 我在做什么错了?
  • 我实际上可以使用哪种惯用模式来实现存储执行信息以便以后运行的目标?

0 个答案:

没有答案