From 27f5e98aaee329219d4ffdd52f4da876c5e84f22 Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Wed, 11 Dec 2019 14:02:33 +0900 Subject: bundler gem: Support test-unit as a testing framework --- lib/bundler/cli.rb | 2 +- lib/bundler/cli/gem.rb | 21 ++++--- lib/bundler/templates/newgem/Rakefile.tt | 7 ++- .../newgem/test/minitest/newgem_test.rb.tt | 11 ++++ .../newgem/test/minitest/test_helper.rb.tt | 4 ++ .../templates/newgem/test/newgem_test.rb.tt | 11 ---- .../newgem/test/test-unit/newgem_test.rb.tt | 13 +++++ .../newgem/test/test-unit/test_helper.rb.tt | 4 ++ .../templates/newgem/test/test_helper.rb.tt | 4 -- man/bundle-gem.1 | 4 +- man/bundle-gem.1.txt | 16 +++--- man/bundle-gem.ronn | 12 ++-- spec/commands/newgem_spec.rb | 65 ++++++++++++++++++++-- 13 files changed, 128 insertions(+), 46 deletions(-) create mode 100644 lib/bundler/templates/newgem/test/minitest/newgem_test.rb.tt create mode 100644 lib/bundler/templates/newgem/test/minitest/test_helper.rb.tt delete mode 100644 lib/bundler/templates/newgem/test/newgem_test.rb.tt create mode 100644 lib/bundler/templates/newgem/test/test-unit/newgem_test.rb.tt create mode 100644 lib/bundler/templates/newgem/test/test-unit/test_helper.rb.tt delete mode 100644 lib/bundler/templates/newgem/test/test_helper.rb.tt diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index 86c9da6b99..523eb77168 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -569,7 +569,7 @@ module Bundler method_option :mit, :type => :boolean, :desc => "Generate an MIT license file. Set a default with `bundle config set gem.mit true`." method_option :rubocop, :type => :boolean, :desc => "Add rubocop to the generated Rakefile and gemspec. Set a default with `bundle config set gem.rubocop true`." method_option :test, :type => :string, :lazy_default => "rspec", :aliases => "-t", :banner => "rspec", - :desc => "Generate a test directory for your library, either rspec or minitest. Set a default with `bundle config set gem.test rspec`." + :desc => "Generate a test directory for your library, either rspec, minitest or test-unit. Set a default with `bundle config set gem.test rspec`." def gem(name) end diff --git a/lib/bundler/cli/gem.rb b/lib/bundler/cli/gem.rb index 403005a6bd..00b3bc55a6 100644 --- a/lib/bundler/cli/gem.rb +++ b/lib/bundler/cli/gem.rb @@ -12,6 +12,7 @@ module Bundler TEST_FRAMEWORK_VERSIONS = { "rspec" => "3.0", "minitest" => "5.0", + "test-unit" => "3.0", }.freeze attr_reader :options, :gem_name, :thor, :name, :target @@ -92,16 +93,22 @@ module Bundler "spec/spec_helper.rb.tt" => "spec/spec_helper.rb", "spec/newgem_spec.rb.tt" => "spec/#{namespaced_path}_spec.rb" ) + config[:test_task] = :spec when "minitest" templates.merge!( - "test/test_helper.rb.tt" => "test/test_helper.rb", - "test/newgem_test.rb.tt" => "test/#{namespaced_path}_test.rb" + "test/minitest/test_helper.rb.tt" => "test/test_helper.rb", + "test/minitest/newgem_test.rb.tt" => "test/#{namespaced_path}_test.rb" ) + config[:test_task] = :test + when "test-unit" + templates.merge!( + "test/test-unit/test_helper.rb.tt" => "test/test_helper.rb", + "test/test-unit/newgem_test.rb.tt" => "test/#{namespaced_path}_test.rb" + ) + config[:test_task] = :test end end - config[:test_task] = config[:test] == "minitest" ? :test : :spec - if ask_and_set(:mit, "Do you want to license your code permissively under the MIT license?", "This means that any other developer or company will be legally allowed to use your code " \ "for free as long as they admit you created it. You can read more about the MIT license " \ @@ -208,9 +215,9 @@ module Bundler if test_framework.nil? Bundler.ui.confirm "Do you want to generate tests with your gem?" - result = Bundler.ui.ask "Type 'rspec' or 'minitest' to generate those test files now and " \ - "in the future. rspec/minitest/(none):" - if result =~ /rspec|minitest/ + result = Bundler.ui.ask "Type 'rspec', 'minitest' or 'test-unit' to generate those test files now and " \ + "in the future. rspec/minitest/test-unit/(none):" + if result =~ /rspec|minitest|test-unit/ test_framework = result else test_framework = false diff --git a/lib/bundler/templates/newgem/Rakefile.tt b/lib/bundler/templates/newgem/Rakefile.tt index 1262457c72..af7729c04e 100644 --- a/lib/bundler/templates/newgem/Rakefile.tt +++ b/lib/bundler/templates/newgem/Rakefile.tt @@ -1,6 +1,7 @@ require "bundler/gem_tasks" -<% default_task_names = [config[:test_task]] -%> -<% if config[:test] == "minitest" -%> +<% default_task_names = [config[:test_task]].compact -%> +<% case config[:test] -%> +<% when "minitest", "test-unit" -%> require "rake/testtask" Rake::TestTask.new(:test) do |t| @@ -9,7 +10,7 @@ Rake::TestTask.new(:test) do |t| t.test_files = FileList["test/**/*_test.rb"] end -<% elsif config[:test] == "rspec" -%> +<% when "rspec" -%> require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) diff --git a/lib/bundler/templates/newgem/test/minitest/newgem_test.rb.tt b/lib/bundler/templates/newgem/test/minitest/newgem_test.rb.tt new file mode 100644 index 0000000000..f2af9f90e0 --- /dev/null +++ b/lib/bundler/templates/newgem/test/minitest/newgem_test.rb.tt @@ -0,0 +1,11 @@ +require "test_helper" + +class <%= config[:constant_name] %>Test < Minitest::Test + def test_that_it_has_a_version_number + refute_nil ::<%= config[:constant_name] %>::VERSION + end + + def test_it_does_something_useful + assert false + end +end diff --git a/lib/bundler/templates/newgem/test/minitest/test_helper.rb.tt b/lib/bundler/templates/newgem/test/minitest/test_helper.rb.tt new file mode 100644 index 0000000000..7d7db165ec --- /dev/null +++ b/lib/bundler/templates/newgem/test/minitest/test_helper.rb.tt @@ -0,0 +1,4 @@ +$LOAD_PATH.unshift File.expand_path("../lib", __dir__) +require "<%= config[:namespaced_path] %>" + +require "minitest/autorun" diff --git a/lib/bundler/templates/newgem/test/newgem_test.rb.tt b/lib/bundler/templates/newgem/test/newgem_test.rb.tt deleted file mode 100644 index f2af9f90e0..0000000000 --- a/lib/bundler/templates/newgem/test/newgem_test.rb.tt +++ /dev/null @@ -1,11 +0,0 @@ -require "test_helper" - -class <%= config[:constant_name] %>Test < Minitest::Test - def test_that_it_has_a_version_number - refute_nil ::<%= config[:constant_name] %>::VERSION - end - - def test_it_does_something_useful - assert false - end -end diff --git a/lib/bundler/templates/newgem/test/test-unit/newgem_test.rb.tt b/lib/bundler/templates/newgem/test/test-unit/newgem_test.rb.tt new file mode 100644 index 0000000000..e653993006 --- /dev/null +++ b/lib/bundler/templates/newgem/test/test-unit/newgem_test.rb.tt @@ -0,0 +1,13 @@ +require "test_helper" + +class <%= config[:constant_name] %>Test < Test::Unit::TestCase + test "VERSION" do + assert do + ::<%= config[:constant_name] %>.const_defined?(:VERSION) + end + end + + test "something useful" do + assert_equal("expected", "actual") + end +end diff --git a/lib/bundler/templates/newgem/test/test-unit/test_helper.rb.tt b/lib/bundler/templates/newgem/test/test-unit/test_helper.rb.tt new file mode 100644 index 0000000000..461ee391a5 --- /dev/null +++ b/lib/bundler/templates/newgem/test/test-unit/test_helper.rb.tt @@ -0,0 +1,4 @@ +$LOAD_PATH.unshift File.expand_path("../lib", __dir__) +require "<%= config[:namespaced_path] %>" + +require "test-unit" diff --git a/lib/bundler/templates/newgem/test/test_helper.rb.tt b/lib/bundler/templates/newgem/test/test_helper.rb.tt deleted file mode 100644 index 7d7db165ec..0000000000 --- a/lib/bundler/templates/newgem/test/test_helper.rb.tt +++ /dev/null @@ -1,4 +0,0 @@ -$LOAD_PATH.unshift File.expand_path("../lib", __dir__) -require "<%= config[:namespaced_path] %>" - -require "minitest/autorun" diff --git a/man/bundle-gem.1 b/man/bundle-gem.1 index 9971e3528c..9a7654a8b4 100644 --- a/man/bundle-gem.1 +++ b/man/bundle-gem.1 @@ -64,8 +64,8 @@ Add an MIT license to a \fBLICENSE\.txt\fR file in the root of the generated pro Do not create a \fBLICENSE\.txt\fR (overrides \fB\-\-mit\fR specified in the global config)\. . .TP -\fB\-t\fR, \fB\-\-test=minitest\fR, \fB\-\-test=rspec\fR -Specify the test framework that Bundler should use when generating the project\. Acceptable values are \fBminitest\fR and \fBrspec\fR\. The \fBGEM_NAME\.gemspec\fR will be configured and a skeleton test/spec directory will be created based on this option\. If this option is unspecified, an interactive prompt will be displayed and the answer will be saved in Bundler\'s global config for future \fBbundle gem\fR use\. If no option is specified, the default testing framework is RSpec\. +\fB\-t\fR, \fB\-\-test=minitest\fR, \fB\-\-test=rspec\fR, \fB\-\-test=test\-unit\fR +Specify the test framework that Bundler should use when generating the project\. Acceptable values are \fBminitest\fR, \fBrspec\fR and \fBtest\-unit\fR\. The \fBGEM_NAME\.gemspec\fR will be configured and a skeleton test/spec directory will be created based on this option\. If this option is unspecified, an interactive prompt will be displayed and the answer will be saved in Bundler\'s global config for future \fBbundle gem\fR use\. If no option is specified, the default testing framework is RSpec\. . .TP \fB\-e\fR, \fB\-\-edit[=EDITOR]\fR diff --git a/man/bundle-gem.1.txt b/man/bundle-gem.1.txt index c95c409c65..2b075e6e20 100644 --- a/man/bundle-gem.1.txt +++ b/man/bundle-gem.1.txt @@ -65,15 +65,15 @@ OPTIONS Do not create a LICENSE.txt (overrides --mit specified in the global config). - -t, --test=minitest, --test=rspec + -t, --test=minitest, --test=rspec, --test=test-unit Specify the test framework that Bundler should use when generat- - ing the project. Acceptable values are minitest and rspec. The - GEM_NAME.gemspec will be configured and a skeleton test/spec - directory will be created based on this option. If this option - is unspecified, an interactive prompt will be displayed and the - answer will be saved in Bundler's global config for future bun- - dle gem use. If no option is specified, the default testing - framework is RSpec. + ing the project. Acceptable values are minitest, rspec and + test-unit. The GEM_NAME.gemspec will be configured and a skele- + ton test/spec directory will be created based on this option. If + this option is unspecified, an interactive prompt will be dis- + played and the answer will be saved in Bundler's global config + for future bundle gem use. If no option is specified, the + default testing framework is RSpec. -e, --edit[=EDITOR] Open the resulting GEM_NAME.gemspec in EDITOR, or the default diff --git a/man/bundle-gem.ronn b/man/bundle-gem.ronn index cf3d037df2..1dba9ed607 100644 --- a/man/bundle-gem.ronn +++ b/man/bundle-gem.ronn @@ -60,13 +60,13 @@ configuration file using the following names: Do not create a `LICENSE.txt` (overrides `--mit` specified in the global config). -* `-t`, `--test=minitest`, `--test=rspec`: +* `-t`, `--test=minitest`, `--test=rspec`, `--test=test-unit`: Specify the test framework that Bundler should use when generating the - project. Acceptable values are `minitest` and `rspec`. The `GEM_NAME.gemspec` - will be configured and a skeleton test/spec directory will be created based - on this option. If this option is unspecified, an interactive prompt will be - displayed and the answer will be saved in Bundler's global config for future - `bundle gem` use. + project. Acceptable values are `minitest`, `rspec` and `test-unit`. The + `GEM_NAME.gemspec` will be configured and a skeleton test/spec directory will + be created based on this option. If this option is unspecified, an interactive + prompt will be displayed and the answer will be saved in Bundler's global + config for future `bundle gem` use. If no option is specified, the default testing framework is RSpec. * `-e`, `--edit[=EDITOR]`: diff --git a/spec/commands/newgem_spec.rb b/spec/commands/newgem_spec.rb index 2c8ab4b5ef..6c2fcb6e92 100644 --- a/spec/commands/newgem_spec.rb +++ b/spec/commands/newgem_spec.rb @@ -420,7 +420,7 @@ RSpec.describe "bundle gem" do expect(bundled_app("#{gem_name}/spec/#{require_path}_spec.rb")).to_not exist expect(bundled_app("#{gem_name}/spec/spec_helper.rb")).to_not exist expect(bundled_app("#{gem_name}/test/#{require_path}.rb")).to_not exist - expect(bundled_app("#{gem_name}/test/minitest_helper.rb")).to_not exist + expect(bundled_app("#{gem_name}/test/test_helper.rb")).to_not exist end end @@ -503,7 +503,7 @@ RSpec.describe "bundle gem" do expect(bundled_app("#{gem_name}/test/test_helper.rb").read).to include(%(require "#{require_path}")) end - it "requires 'minitest_helper'" do + it "requires 'test_helper'" do expect(bundled_app("#{gem_name}/test/#{require_path}_test.rb").read).to include(%(require "test_helper")) end @@ -536,6 +536,63 @@ RSpec.describe "bundle gem" do end end + context "--test parameter set to test-unit" do + before do + bundle "gem #{gem_name} --test=test-unit" + end + + it "depends on a specific version of test-unit" do + Dir.chdir(bundled_app(gem_name)) do + builder = Bundler::Dsl.new + builder.eval_gemfile(bundled_app("#{gem_name}/Gemfile")) + builder.dependencies + test_unit_dep = builder.dependencies.find {|d| d.name == "test-unit" } + expect(test_unit_dep).to be_specific + end + end + + it "builds spec skeleton" do + expect(bundled_app("#{gem_name}/test/#{require_path}_test.rb")).to exist + expect(bundled_app("#{gem_name}/test/test_helper.rb")).to exist + end + + it "requires the main file" do + expect(bundled_app("#{gem_name}/test/test_helper.rb").read).to include(%(require "#{require_path}")) + end + + it "requires 'test_helper'" do + expect(bundled_app("#{gem_name}/test/#{require_path}_test.rb").read).to include(%(require "test_helper")) + end + + it "creates a default test which fails" do + expect(bundled_app("#{gem_name}/test/#{require_path}_test.rb").read).to include("assert_equal(\"expected\", \"actual\")") + end + end + + context "gem.test setting set to test-unit" do + before do + bundle "config set gem.test test-unit" + bundle "gem #{gem_name}" + end + + it "creates a default rake task to run the test suite" do + rakefile = strip_whitespace <<-RAKEFILE + require "bundler/gem_tasks" + require "rake/testtask" + + Rake::TestTask.new(:test) do |t| + t.libs << "test" + t.libs << "lib" + t.test_files = FileList["test/**/*_test.rb"] + end + + task :default => :test + RAKEFILE + + expect(bundled_app("#{gem_name}/Rakefile").read).to eq(rakefile) + end + end + context "--test with no arguments" do before do bundle "gem #{gem_name} --test" @@ -543,7 +600,7 @@ RSpec.describe "bundle gem" do it "defaults to rspec" do expect(bundled_app("#{gem_name}/spec/spec_helper.rb")).to exist - expect(bundled_app("#{gem_name}/test/minitest_helper.rb")).to_not exist + expect(bundled_app("#{gem_name}/test/test_helper.rb")).to_not exist end it "creates a .travis.yml file to test the library against the current Ruby version on Travis CI" do @@ -647,7 +704,7 @@ RSpec.describe "bundle gem" do ext.lib_dir = "lib/#{gem_name}" end - task :default => [:clobber, :compile, :spec] + task :default => [:clobber, :compile] RAKEFILE expect(bundled_app("#{gem_name}/Rakefile").read).to eq(rakefile) -- cgit v1.2.1