Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added RSpec test skeleton to gem command #2108

Merged
merged 4 commits into from

2 participants

@MafcoCinco

Description

Added an option to bundle gem called --test which creates skeleton code for unit testing. Currently the only value --test accepts is rspec but it should be easy to add others if this is a feature that people want. RSpec was a selfish selection as it is the testing framework that I use.

I Included corresponding unit tests (all of which pass) in spec/other/newgem_spec.rb. I also added unit tests for the --bin option which seemed to be missing.

Usage

bundle gem my_gem --test=rspec

@indirect
Owner

I'm a bit of two minds about this... I personally would use this, but this level of complication seems better provided by a Bundler plugin than by Bundler itself. Maybe we should pull this in, and then extract the gem command into a plugin as soon as plugins are a going thing.

@MafcoCinco

That sounds reasonable to me. I've thought that the gem command belongs out on it's own. It seems like having it in Bundler is breaking the rule of "one object/gem/class/etc., one job".

In my opinion, it is the best skeleton generator but there are some enhancements, like this one, that I would like to see it have. Having it as a plugin might allow it to evolve a bit more quickly as check-ins would not be directly to Bundler core functionality.

Is there an ETA on plugins? Also, is there a dev build? I assume plugin functionality is under active development and I would be happy to help out with moving the gem command if there is a dev branch of the repo that supports plugins.

@indirect
Owner

I don't have a dev build yet -- I haven't had too much time to work on it yet, so it's still in the planning stages. I wrote a blog post about my ideas for plugins if you'd like to check it out and give some feedback before I actually get going on the coding. Thanks!

@indirect
Owner

To help motivate me to get plugins up and working...

@indirect indirect merged commit 8f634b3 into from
@kcurtin kcurtin referenced this pull request
Merged

add minitest skeleton #2179

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
7 lib/bundler/cli.rb
@@ -583,6 +583,7 @@ def viz
desc "gem GEM", "Creates a skeleton for creating a rubygem"
method_option :bin, :type => :boolean, :default => false, :aliases => '-b', :banner => "Generate a binary for your library."
+ method_option :test, :type => :string, :default => nil, :alias => '-t', :banner => "Generate test directory for your library."
def gem(name)
name = name.chomp("/") # remove trailing slash if present
target = File.join(Dir.pwd, name)
@@ -610,6 +611,12 @@ def gem(name)
if options[:bin]
template(File.join("newgem/bin/newgem.tt"), File.join(target, 'bin', name), opts)
end
+ case options[:test]
+ when 'rspec'
+ template(File.join("newgem/rspec.tt"), File.join(target, ".rspec"), opts)
+ template(File.join("newgem/spec/spec_helper.rb.tt"), File.join(target, "spec/spec_helper.rb"), opts)
+ template(File.join("newgem/spec/newgem_spec.rb.tt"), File.join(target, "spec/#{name}_spec.rb"), opts)
+ end
Bundler.ui.info "Initializating git repo in #{target}"
Dir.chdir(target) { `git init`; `git add .` }
end
View
4 lib/bundler/templates/newgem/rspec.tt
@@ -0,0 +1,4 @@
+# ./.rspec
+--format nested
+--color
+--require spec_helper
View
9 lib/bundler/templates/newgem/spec/newgem_spec.rb.tt
@@ -0,0 +1,9 @@
+describe <%= config[:constant_name] %> do
+ it 'should have a version number' do
+ <%= config[:constant_name] %>::VERSION.should_not be_nil
+ end
+
+ it 'should do something useful' do
+ false.should be_true
+ end
+end
View
1  lib/bundler/templates/newgem/spec/spec_helper.rb.tt
@@ -0,0 +1 @@
+require_relative '../lib/<%= config[:name] %>'
View
38 spec/other/newgem_spec.rb
@@ -85,4 +85,42 @@
out.should include("SUCCESS")
end
end
+
+ context "--bin parameter set" do
+ before :each do
+ reset!
+ in_app_root
+ bundle 'gem test-gem --bin'
+ end
+
+ it "builds bin skeleton" do
+ bundled_app("test-gem/bin/test-gem").should exist
+ end
+
+ it "requires 'test-gem'" do
+ bundled_app("test-gem/bin/test-gem").read.should =~ /require 'test-gem'/
+ end
+ end
+
+ context "--test parameter set to rspec" do
+ before :each do
+ reset!
+ in_app_root
+ bundle "gem test-gem --test=rspec"
+ end
+
+ it "builds spec skeleton" do
+ bundled_app("test-gem/.rspec").should exist
+ bundled_app("test-gem/spec/test-gem_spec.rb").should exist
+ bundled_app("test-gem/spec/spec_helper.rb").should exist
+ end
+
+ it "requires 'test-gem'" do
+ bundled_app("test-gem/spec/spec_helper.rb").read.should =~ /require_relative '..\/lib\/test-gem'/
+ end
+
+ it "creates a default test which fails" do
+ bundled_app("test-gem/spec/test-gem_spec.rb").read.should =~ /false.should be_true/
+ end
+ end
end
Something went wrong with that request. Please try again.