我正在尝试在我的Exercism文件夹中设置Guard,以便自动运行所有测试(使用guard-minitest)。
文件夹结构如下所示:
.
├── exercism
└── ruby
├── bob
│ ├── bob.rb
│ ├── bob_test.rb
├── etl
│ ├── etl.rb
│ ├── etl_test.rb
...
└── binary_search_tree
├── bst.rb
├── binary_search_tree.rb
└── README.md
由于文件夹结构异常,我在Guardfile中进行了以下配置:
# tell Guard to watch all folders under ruby/
all_dirs = Dir.glob('ruby/*')
directories all_dirs
options = {
test_folders: all_dirs,
test_file_patterns: '*_test.rb',
pride: true
}
guard :minitest, options do
watch(%r{^ruby/([^/]+)/([^/]+).rb$})
end
使用此设置,我希望对.rb文件进行任何编辑后都可以运行我的所有测试。
我在 Debug模式下运行 guard 队。
所有测试均按预期在启动时运行。
但是,当我编辑.rb文件时,尽管Guard输出以下内容,但测试不会再次运行:
20:43:26 - DEBUG - Interactor was stopped or killed
20:43:26 - DEBUG - Hook :run_on_additions_begin executed for Guard::Minitest
20:43:26 - DEBUG - Hook :run_on_additions_end executed for Guard::Minitest
20:43:26 - DEBUG - Start interactor
我尝试了Guard配置的多种变体,例如:
watch(%r{^ruby/([^/]+)/([^/]+).rb$}) do |m|
Dir.glob("ruby/#{m[1]}/*_test.rb").first
end
(我希望这只能从修改文件的文件夹中运行测试。)
似乎没有任何作用。我必须转到 guard 的交互式控制台,然后按Enter键才能运行测试。
我究竟做错了什么?
编辑
阅读下面的Cezary's answer之后,我尝试了更多操作,包括在其他环境中尝试过。
事实证明这不是看守的错,但可能是我所使用的环境。
我最初将所有内容都运行在Codio框中。
我将项目移到了其他两台计算机上,即Win8和Ubuntu 14.04,并且使用此Guardfile在两个计算机上都可以正常工作:
all_dirs = Dir.glob('ruby/*')
directories all_dirs
clearing :on
options = {
test_folders: all_dirs,
test_file_patterns: '*_test.rb',
pride: true
}
guard :minitest, options do
watch(%r{^ruby/([^/]+)/([^/]+).rb$}) do |m|
file = Dir.glob("ruby/#{m[1]}/*_test.rb").first
puts " Should test #{file}"
file
end
end
guard -d
的输出如下:在Codio框(不起作用的位置)上:
<!-- language: lang-none -->
08:55:09 - DEBUG - Interactor was stopped or killed
Should test ruby/anagram/anagram_test.rb
08:55:09 - DEBUG - Hook :run_on_additions_begin executed for Guard::Minitest
08:55:09 - DEBUG - Hook :run_on_additions_end executed for Guard::Minitest
08:55:09 - DEBUG - Start interactor
[1] guard(main)>
在Win/Ubuntu上(可以正常使用):
<!-- language: lang-none -->
11:02:10 - DEBUG - Interactor was stopped or killed
Should test ruby/anagram/anagram_test.rb
11:02:10 - DEBUG - Hook :run_on_modifications_begin executed for Guard::Minitest
11:02:10 - INFO - Running: ruby/anagram/anagram_test.rb
Run options: --seed 52507
# Running tests:
..........
Finished tests in 0.001249s, 8006.0205 tests/s, 8006.0205 assertions/s.
10 tests, 10 assertions, 0 failures, 0 errors, 0 skips
11:02:10 - DEBUG - Hook :run_on_modifications_end executed for Guard::Minitest
11:02:10 - DEBUG - Start interactor
[1] guard(main)>
我不知道为什么在Codio上得到
run_on_additions
,而在另外两个上得到run_on_modifications
。无论如何,我想这是一个Codio问题。我尝试使用和不使用“手动保存”,都是一样的。
最佳答案
嗨,我是Guard的“负责人”之一,基于此,我写了一个演练:https://github.com/guard/guard/wiki/Understanding-Guard
我强烈建议您仔细阅读(感谢反馈)。
以下是要考虑的事项:
Guard::Minitest
这样的东西就可以使用了。 (有些编辑器,例如Vim和Emacs,允许您在“替代”文件之间切换,他们会自动知道在哪里寻找测试或实现文件)。 directories
选项来观看所有内容。但是,如果您的文件夹中有大量磁盘事件,并且您有大型项目,则需要选择目录。例如。在您的情况下,应为:directories ruby
。所以`Dir.glob('ruby/*')在这里没有多大意义。 test_folder: %w(ruby)
就足够了。另外,不需要test_file_patterns
,因为您的文件似乎遵循默认设置/约定(bob_test.rb
和etl_test.rb
)-除非您对bob.rb
和etl.rb
文件中包含测试套件感到有些奇怪。 bob.rb
)时,该文件将传递给Guard::Minitest
,后者会忽略非测试文件(可能使用test_file_patterns
选项,因此,由于bob.rb
与bob_test.rb
不匹配,Guard::Minitest会悄悄地这样做。 ..没什么。Guardfile
,更新您的gems(基本上是Guard::Minitest
),然后运行bundle exec guard init minitest
到Guard::Minitest
的“当前”推荐模板是什么,然后尝试对其进行调整。您会看到默认值具有:watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
其中显示了如何将更改的实现文件转换为测试文件(这是
Guard::Minitest
唯一关心的文件)。在您的情况下,您可能需要:
watch(%r{^ruby/(.*/)?([^/]+)\.rb$}) { |m| "ruby/#{m[1]}/#{m[2]}_test.rb" }
我会说regexen太糟糕了-我正计划实现glob模式支持-但这需要一段时间(对我而言,管道中已修复了许多关键的错误)。
好的,我指出了很多错误,花时间做非常非常出色的事情:
Guard
-这样做真是令人敬畏再次感谢您这样做-如果您看到其他人可以解决的问题-请这样做。如果您在Guard中看到明显的错误或问题-请在Guard中打开一个问题。如果它在另一个插件存储库中(例如
Guard::Minitest
),只需提及我,这样就不会忽略重要的问题(在GitHub上将我称为@e2
)。我确实希望使Guard更好-目前,我正在努力使其变得更简单,更直观,更灵活和更可靠。我指望报告损坏的东西的人越多,我就可以更快地处理好东西。
再次感谢,祝您有美好的一天!
关于ruby - 测试不会自动运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27324523/