使用 Guard 忽略目录

Ignore directories with Guard

我正在使用 guard-minitest 自动 运行 我的测试。

我在 test/fixtures 目录中也有一个框架 gem。

问题是,现在 Guard 也在 运行 骨架中进行测试 gem。

我如何告诉 Guard 仅 运行 我的实际项目测试而不是 test/fixtures 目录中的任何项目?

我尝试了以下方法,但没有用:

guard :minitest, :exclude => "test/fixtures/*" do
  # with Minitest::Unit
  watch(%r{^test/(.*)\/?test_(.*)\.rb$})
  watch(%r{^lib/(.*/)?([^/]+)\.rb$})     { |m| "test/#{m[1]}test_#{m[2]}.rb" }
  watch(%r{^test/test_helper\.rb$})      { 'test' }
end

编辑:

docs 好像我可以添加一个忽略路径,这也没有用:

ignore %r{^test/fixtures/}

guard :minitest do
  watch(%r{^test/(.*)\/?test_(.*)\.rb$})
  watch(%r{^lib/(.*/)?([^/]+)\.rb$})     { |m| "test/#{m[1]}test_#{m[2]}.rb" }
  watch(%r{^test/test_helper\.rb$})      { 'test' }
end

编辑:

按照下面的建议,我尝试删除星号,但也不起作用:

ignore %r{^test/fixtures/}

guard :minitest do
  watch(%r{^test/test_(.*)\.rb$})
  watch(%r{^lib/(.*/)?([^/]+)\.rb$})     { |m| "test/#{m[1]}test_#{m[2]}.rb" }
  watch(%r{^test/test_helper\.rb$})      { 'test' }
end

这是因为 watch(%r{^test/(.*)\/?test_(.*)\.rb$}) 使用的正则表达式与 gem 框架的测试相匹配。 所以如果你有这些文件:

test/my_real_tests/test_one.rb
test/my_real_tests/my_gem/test_two.rb

/my_real_tests/my_gem/ 匹配第一个 (.*).

添加更具体的正则表达式,使它们不匹配。类似于 watch(%r{^test/my_real_tests\/?test_(.*)\.rb$})。或者只是删除 (.*).

这是关于 watch 工作原理的 documentation

另外:为什么 gem 骨架在 test/fixtures 中?看起来很奇怪 :)

制作一个更具体的目录,添加一个更具体的watcher正则表达式,并添加一个test_folders:选项docs

这是我的工作守卫文件和测试目录:

guard :minitest, test_folders: 'test/real' do
  watch(%r{^test/real/(.*)\/?test_(.*)\.rb$})
  watch(%r{^lib/(.*/)?([^/]+)\.rb$})     { |m| "test/real/#{m[1]}test_#{m[2]}.rb" }
  watch(%r{^test/test_helper\.rb$})      { 'test/real' }
end

目录结构:

$ tree -L 2 test
test
├── fixtures
│   └── newgem
└── real
    ├── devify
    ├── test_devify.rb
    └── test_helper.rb

4 directories, 2 files

我 运行 进入了同一个问题,发现添加像两个建议答案一样的诱饵文件夹结构有点老套。

IMO 更好的解决方案是修改您的正则表达式以否定 fixtures 文件夹:

watch(%r{^test/(?!fixtures)(.*)\/?test_(.*)\.rb$})