rmu*_*unn 7 javascript vim jasmine angularjs karma-runner
我遇到了我认为是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)
Run Code Online (Sandbox Code Playgroud)
我用来触发这四次运行的唯一变化是从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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)