Skip to content

Instantly share code, notes, and snippets.

@O-I
Last active August 29, 2015 14:13
Show Gist options
  • Save O-I/0706e7f34820cc89b9be to your computer and use it in GitHub Desktop.
Save O-I/0706e7f34820cc89b9be to your computer and use it in GitHub Desktop.
[TIx 2] Don't package test files with your Ruby gem

Here's something to think about the next time you're twiddling your thumbs waiting for a bundle install or gem push command to finish running. Why did that take so long? Often we think of issues of speed solely in terms of the quality of our network connection and pay less attention to actual file sizes. I don't think anyone will argue, though, that given the same network speed, a 10MB file takes longer to download than a 100KB one. And if both did the exact same thing and you needed to download it and dozens of cousins like it, say, several times per week, wouldn't you prefer smaller?

Most of the gems we use on a daily basis weigh in at 2 - 36 times more than they need to. That's because, by default, they include a very important set of files for development that are unnecessary dead weight in a packaged gem — your tests. This issue filed by @sferik on the bundler gem does an excellent job of summing this up.

Fortunately, this is a very easy issue to rectify if you maintain (or contribute to) an existing Ruby gem. If you use bundler, the default gemspec file has two lines like this in the specification block:

# [GEMNAME].gemspec file

# snip

Gem::Specification.new do |spec|
  # .
  # .
  # .
  spec.files      = `git ls-files -z`.split("\x0")
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
  # .
  # .
  # .
end

Modifying the first line and deleting the second excludes test files from the packaged gem:

# [GEMNAME].gemspec file

# snip

Gem::Specification.new do |spec|
  # .
  # .
  # .
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
  # .
  # .
  # .
end

This might not seem like a big deal with gems that have a smaller footprint even with the tests included, but those sizes really do begin to add up, so it's a good thing to be aware of and an easy thing to fix should you decide to take the time to do so.

This behavior of excluding test files from packaged gems has been merged into the master branch of bundler as referenced in the issue above, but it doesn't appear to be included in any released version of bundler yet.

Questions I still have:

  1. Have you ever opened up a packaged gem to run the tests? How frequently do you do this?
  2. Are there any cases where test files should be bundled with the packaged gem? If so, what are they?
  3. Are there other types of files that are essentially dead weight packaged in a Ruby gem? What about documentation or other text files? (There seem to be good reasons to keep them in.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment