我遇到了我认为是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毫秒,然后运行单元测试”?虽然这将是一个蛮力的解决方案,它将解决这种特殊的竞争条件.或者,如果有人提出其他聪明的想法,我也很乐意听到.
解决方法
This GitHub issue也讨论了这个问题.可以将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