我遇到了Vim保存文件和Karma重新运行Jasmine单元测试之间的竞争条件。以下是四个测试运行的序列,展示了症状(我截断了错误日志中超长的路径):
$ karma start karma.conf.js --auto-watch
[... snip a lot of coding and test running ...]
PhantomJS 1.6 (Linux) LOG: 'Running tests at 2013-08-14T08:19:57.252Z'
PhantomJS 1.6 (Linux): Executed 4 of 4 SUCCESS (0.307 secs / 0.013 secs)
PhantomJS 1.6 (Linux) LOG: 'Running tests at 2013-08-14T08:20:09.866Z'
PhantomJS 1.6 (Linux): Executed 4 of 4 SUCCESS (0.288 secs / 0.012 secs)
PhantomJS 1.6 (Linux) LOG: 'Running tests at 2013-08-14T08:20:14.366Z'
PhantomJS 1.6 (Linux) controller should have a breadcrumb after $routeChangeSuccess FAILED
Error: No module: myapp.question
at /home/rmunn/.../angular.js:1124
at ensure (/home/rmunn/.../angular.js:1065)
at module (/home/rmunn/.../angular.js:1296)
at /home/rmunn/.../angular.js:2806
TypeError: 'undefined' is not an object (evaluating 'rootScope.$broadcast')
at /home/rmunn/.../unit/controllersSpec.js:35
PhantomJS 1.6 (Linux): Executed 4 of 4 (1 FAILED) (0.312 secs / 0.014 secs)
PhantomJS 1.6 (Linux) LOG: 'Running tests at 2013-08-14T08:20:28.588Z'
PhantomJS 1.6 (Linux): Executed 4 of 4 SUCCESS (0.287 secs / 0.013 secs)
我为触发这四次运行所做的唯一更改是在question.js
文件中添加和删除空白;没有实质性的代码更改,但运行#3失败,其中运行1、2和4成功。
我的Vim配置设置为保留备份文件,默认情况下,Vim通过重命名原始文件并写入新文件来保留备份文件。(某些情况除外,此处不适用)。我认为我正在触发一个竞争条件:Vim重命名question.js
(或我刚刚编辑的任何文件),Karma检测到这一变化并开始运行测试,因为我现在的测试太少了,所有测试都在Linux进程调度程序将控制权交还给Vim之前运行。一旦Vim再次被调度,它就会写入question.js
的新内容,但到那时,对于依赖于现有且非空的question.js
的Jasmine测试来说已经太晚了。
我可以通过配置Vim不保留备份文件来解决这个问题,但我真的不想冒这个风险。毕竟,我使用备份文件是有原因的。那么,有没有一种方法可以告诉Karma"当你检测到文件更改时,暂停N毫秒,然后运行单元测试"?虽然这将是一个蛮力解决方案,但将解决这个特定的竞赛条件。或者,如果有人有其他聪明的想法可以提供,我也很乐意听到。
本期GitHub也讨论了这个问题。Vim可以配置为通过复制然后覆盖原始文件来进行备份,而不是移动原始文件并写入新文件。虽然从技术上讲,这不是你问题的答案,但它应该足以解决你的问题:
set backupcopy=yes
Vim现在将在适当的位置编辑文件,Karma将检测到这是"已更改"而不是"已删除"。
详见:help 'backupcopy'
:
*'backupcopy'* *'bkc'*
'backupcopy' 'bkc' string (Vi default for Unix: "yes", otherwise: "auto")
global
{not in Vi}
When writing a file and a backup is made, this option tells how it's
done. This is a comma separated list of words.
The main values are:
"yes" make a copy of the file and overwrite the original one
"no" rename the file and write a new one
"auto" one of the previous, what works best
从karma文档中,似乎有一个"exclude"选项可以删除一些文件模式。如果您修改选项以使用不同的备份名称并忽略Vim备份格式,它应该可以正常工作。
但它会以某种方式改变您的工作流程,您可能也不得不忽略.gitignore
中的备份。
除此之外,我觉得对于vim persistent undo(版本>7.3),备份文件似乎有点多余。所以也许使用
set nobackup
set undodir=~/.vim/undodir
if (v:version >= 703)
set undofile
set undolevels=1000 "maximum number of changes that can be undone
set undoreload=10000 "maximum number lines to save for undo on a buffer reload
endif