Ignore directories with Guard

1.1k views Asked by At

I'm using guard-minitest to automatically run my tests.

I also have a skeleton gem inside the test/fixtures directory.

The problem is, now Guard is running the tests in the skeleton gem also.

How can I tell Guard to only run my tests for my actual project and not any projects in the test/fixtures directory?

I've tried the following, but it isn't working:

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

Edit:

The docs make it seem like I can add an ignore path, this also didn't work:

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

Edit:

As recommended below I tried removing the asterisk, also doesn't work:

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
3

There are 3 answers

0
mbigras On BEST ANSWER

Make a more specific directory, add a more specific watcher regular expression, and add a test_folders: options docs

This is my working guard file and test dir:

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

dir structure:

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

4 directories, 2 files
3
dgmora On

This is happening because watch(%r{^test/(.*)\/?test_(.*)\.rb$}) uses a regular expression that matches the tests of the gem skeleton. So if you have these files:

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

/my_real_tests/my_gem/ is matched by the first (.*).

Add a more concrete regular expression so they are not matched. Something like watch(%r{^test/my_real_tests\/?test_(.*)\.rb$}). Or just remove (.*).

Here's the documentation about how watch works.

Also: Why is a gem skeleton in test/fixtures ? Seems odd :)

0
Justin On

I ran into this same issue and found that adding a decoy folder structure like the two suggested answers is a bit hacky.

A better solution IMO is to modify your regex to negate the fixtures folder:

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