Use Minitest::Test

`Test::Unit` is deprecated, and we can switch to `Minitest::Test` with
almost no side effects.

This commit does all of the work required to make Minitest tests run
with Gollum's existing test helpers.
This commit is contained in:
benjamin wil
2021-12-31 11:29:56 -08:00
parent 4b2dc8e5c0
commit 0364d6e1be
5 changed files with 98 additions and 76 deletions
+27 -9
View File
@@ -1,11 +1,11 @@
require 'rubygems' require 'rubygems'
require 'rack/test' require 'rack/test'
require 'test/unit'
require 'shoulda' require 'shoulda'
require 'mocha/setup' require 'minitest/autorun'
require 'fileutils'
require 'minitest/reporters' require 'minitest/reporters'
require 'minitest/spec' require 'minitest/spec'
require 'mocha/setup'
require 'fileutils'
require 'tmpdir' require 'tmpdir'
# Silence locale validation warning # Silence locale validation warning
@@ -62,14 +62,29 @@ def normal(text)
text text
end end
# test/spec/mini 3 # The following configuration originates from this gist:
# http://gist.github.com/25455 # http://gist.github.com/25455
# chris@ozmm.org #
# file:lib/test/spec/mini.rb # But it has been modified since it was first committed. It allows you to
# write tests with an RSpec-like DSL:
#
# context "my test context" do
# setup do
# # My test setup
# end
#
# teardown do
# # My test teardown
# end
#
# test "some functionality" do
# assert true
# end
# end
def context(*args, &block) def context(*args, &block)
return super unless (name = args.first) && block return super unless (name = args.first) && block
require 'test/unit' klass = Class.new(Minitest::Test) do
klass = Class.new(defined?(ActiveSupport::TestCase) ? ActiveSupport::TestCase : Test::Unit::TestCase) do
def self.test(name, &block) def self.test(name, &block)
define_method("test_#{name.gsub(/\W/, '_')}", &block) if block define_method("test_#{name.gsub(/\W/, '_')}", &block) if block
end end
@@ -85,10 +100,13 @@ def context(*args, &block)
define_method(:teardown, &block) define_method(:teardown, &block)
end end
end end
( (
class << klass; class << klass;
self self
end).send(:define_method, :name) { name.gsub(/\W/, '_') } end
).send(:define_method, :name) { name.gsub(/\W/, '_') }
$contexts << klass $contexts << klass
klass.class_eval &block klass.class_eval &block
end end
+11 -11
View File
@@ -51,11 +51,11 @@ context "Precious::Views::Editing" do
get '/gollum/history/A' get '/gollum/history/A'
assert_no_match /Edit/, last_response.body, "'Edit' link is not blocked in history template" refute_match /Edit/, last_response.body, "'Edit' link is not blocked in history template"
get '/gollum/compare/A/fc66539528eb96f21b2bbdbf557788fe8a1196ac..b26b791cb7917c4f37dd9cb4d1e0efb24ac4d26f' get '/gollum/compare/A/fc66539528eb96f21b2bbdbf557788fe8a1196ac..b26b791cb7917c4f37dd9cb4d1e0efb24ac4d26f'
assert_no_match /Edit Page/, last_response.body, "'Edit Page' link is not blocked in compare template" refute_match /Edit Page/, last_response.body, "'Edit Page' link is not blocked in compare template"
assert_match /Revert Changes/, last_response.body, "'Revert Changes' link is blocked in compare template" assert_match /Revert Changes/, last_response.body, "'Revert Changes' link is blocked in compare template"
end end
@@ -63,24 +63,24 @@ context "Precious::Views::Editing" do
Precious::App.set(:wiki_options, { allow_editing: false }) Precious::App.set(:wiki_options, { allow_editing: false })
get '/A' get '/A'
assert_no_match /Delete this Page/, last_response.body, "'Delete this Page' link not blocked in page template" refute_match /Delete this Page/, last_response.body, "'Delete this Page' link not blocked in page template"
assert_no_match /New/, last_response.body, "'New' button not blocked in page template" refute_match /New/, last_response.body, "'New' button not blocked in page template"
assert_no_match /Upload\b/, last_response.body, "'Upload' link not blocked in page template" refute_match /Upload\b/, last_response.body, "'Upload' link not blocked in page template"
assert_no_match /Rename/, last_response.body, "'Rename' link not blocked in page template" refute_match /Rename/, last_response.body, "'Rename' link not blocked in page template"
assert_no_match /Edit/, last_response.body, "'Edit' link not blocked in page template" refute_match /Edit/, last_response.body, "'Edit' link not blocked in page template"
get '/gollum/overview' get '/gollum/overview'
assert_no_match /New/, last_response.body, "'New' link not blocked in pages template" refute_match /New/, last_response.body, "'New' link not blocked in pages template"
get '/gollum/history/A' get '/gollum/history/A'
assert_no_match /Edit/, last_response.body, "'Edit' link not blocked in history template" refute_match /Edit/, last_response.body, "'Edit' link not blocked in history template"
get '/gollum/compare/A/fc66539528eb96f21b2bbdbf557788fe8a1196ac..b26b791cb7917c4f37dd9cb4d1e0efb24ac4d26f' get '/gollum/compare/A/fc66539528eb96f21b2bbdbf557788fe8a1196ac..b26b791cb7917c4f37dd9cb4d1e0efb24ac4d26f'
assert_no_match /Edit Page/, last_response.body, "'Edit Page' link not blocked in compare template" refute_match /Edit Page/, last_response.body, "'Edit Page' link not blocked in compare template"
assert_no_match /Revert Changes/, last_response.body, "'Revert Changes' link not blocked in compare template" refute_match /Revert Changes/, last_response.body, "'Revert Changes' link not blocked in compare template"
end end
def app def app
+21 -21
View File
@@ -104,7 +104,7 @@ EOF
page_2 = @wiki.page(page_1.name) page_2 = @wiki.page(page_1.name)
assert_equal 'abc', page_2.raw_data assert_equal 'abc', page_2.raw_data
assert_equal 'def', page_2.version.message assert_equal 'def', page_2.version.message
assert_not_equal page_1.version.sha, page_2.version.sha refute_equal page_1.version.sha, page_2.version.sha
end end
test "edit page fails when page is outdated (edit collision)" do test "edit page fails when page is outdated (edit collision)" do
@@ -117,7 +117,7 @@ EOF
@wiki.clear_cache @wiki.clear_cache
page = @wiki.page('A') page = @wiki.page('A')
new_sha = page.sha new_sha = page.sha
assert_not_equal old_sha, new_sha refute_equal old_sha, new_sha
post "/gollum/edit/A", :content => 'def', :page => 'A', post "/gollum/edit/A", :content => 'def', :page => 'A',
:format => page.format, :message => 'def', :etag => old_sha :format => page.format, :message => 'def', :etag => old_sha
@@ -134,7 +134,7 @@ EOF
page_2 = @wiki.page(page_1.name) page_2 = @wiki.page(page_1.name)
assert_equal 'abc', page_2.raw_data assert_equal 'abc', page_2.raw_data
assert_equal '[no message]', page_2.version.message assert_equal '[no message]', page_2.version.message
assert_not_equal page_1.version.sha, page_2.version.sha refute_equal page_1.version.sha, page_2.version.sha
end end
test "edit page with slash" do test "edit page with slash" do
@@ -165,12 +165,12 @@ EOF
assert_equal 'header', header_2.raw_data assert_equal 'header', header_2.raw_data
assert_equal 'footer', foot_2.raw_data assert_equal 'footer', foot_2.raw_data
assert_equal 'def', foot_2.version.message assert_equal 'def', foot_2.version.message
assert_not_equal foot_1.version.sha, foot_2.version.sha refute_equal foot_1.version.sha, foot_2.version.sha
assert_not_equal header_1.version.sha, header_2.version.sha refute_equal header_1.version.sha, header_2.version.sha
assert_equal 'sidebar', side_2.raw_data assert_equal 'sidebar', side_2.raw_data
assert_equal 'def', side_2.version.message assert_equal 'def', side_2.version.message
assert_not_equal side_1.version.sha, side_2.version.sha refute_equal side_1.version.sha, side_2.version.sha
assert_equal commits, @wiki.repo.commits('master').size assert_equal commits, @wiki.repo.commits('master').size
end end
@@ -187,7 +187,7 @@ EOF
page_2 = @wiki.page('C') page_2 = @wiki.page('C')
assert_equal "INITIAL\n\nSPAM2\n", page_2.raw_data assert_equal "INITIAL\n\nSPAM2\n", page_2.raw_data
assert_equal 'def', page_2.last_version.message assert_equal 'def', page_2.last_version.message
assert_not_equal page_1.version.sha, page_2.version.sha refute_equal page_1.version.sha, page_2.version.sha
end end
test "rename preserves format" do test "rename preserves format" do
@@ -222,7 +222,7 @@ EOF
test "renames page in subdirectory" do test "renames page in subdirectory" do
page_1 = @wiki.page("G/H") page_1 = @wiki.page("G/H")
assert_not_equal page_1, nil refute_equal page_1, nil
post "/gollum/rename/G/H", :rename => "/I/C", :message => 'def' post "/gollum/rename/G/H", :rename => "/I/C", :message => 'def'
follow_redirect! follow_redirect!
@@ -234,12 +234,12 @@ EOF
page_2 = @wiki.page('I/C') page_2 = @wiki.page('I/C')
assert_equal "INITIAL\n\nSPAM2\n", page_2.raw_data assert_equal "INITIAL\n\nSPAM2\n", page_2.raw_data
assert_equal 'def', page_2.last_version.message assert_equal 'def', page_2.last_version.message
assert_not_equal page_1.version.sha, page_2.version.sha refute_equal page_1.version.sha, page_2.version.sha
end end
test "renames page relative in subdirectory" do test "renames page relative in subdirectory" do
page_1 = @wiki.page("G/H") page_1 = @wiki.page("G/H")
assert_not_equal page_1, nil refute_equal page_1, nil
post "/gollum/rename/G/H", :rename => "K/C", :message => 'def' post "/gollum/rename/G/H", :rename => "K/C", :message => 'def'
follow_redirect! follow_redirect!
@@ -251,7 +251,7 @@ EOF
page_2 = @wiki.page('G/K/C') page_2 = @wiki.page('G/K/C')
assert_equal "INITIAL\n\nSPAM2\n", page_2.raw_data assert_equal "INITIAL\n\nSPAM2\n", page_2.raw_data
assert_equal 'def', page_2.last_version.message assert_equal 'def', page_2.last_version.message
assert_not_equal page_1.version.sha, page_2.version.sha refute_equal page_1.version.sha, page_2.version.sha
end end
test "creates page" do test "creates page" do
@@ -320,7 +320,7 @@ EOF
name = "#{dir}/bar" name = "#{dir}/bar"
get "/gollum/create/#{name}" get "/gollum/create/#{name}"
assert_match(/\/#{dir}/, last_response.body) assert_match(/\/#{dir}/, last_response.body)
assert_no_match(/[^\/]#{dir}/, last_response.body) refute_match(/[^\/]#{dir}/, last_response.body)
end end
test "create with template succeed if template exists" do test "create with template succeed if template exists" do
@@ -351,7 +351,7 @@ EOF
post '/gollum/edit/', :content => 'edit_msg', post '/gollum/edit/', :content => 'edit_msg',
:page => page, :path => path, :message => '' :page => page, :path => path, :message => ''
page_e = @wiki.page(::File.join(path,page)) page_e = @wiki.page(::File.join(path,page))
assert_equal nil, page_e assert_nil page_e
end end
test "edit allows changing format" do test "edit allows changing format" do
@@ -407,7 +407,7 @@ EOF
@wiki.clear_cache @wiki.clear_cache
page = @wiki.page(name) page = @wiki.page(name)
assert_not_equal 'abc', page.raw_data refute_equal 'abc', page.raw_data
end end
test "uploading is not allowed unless explicitly enabled" do test "uploading is not allowed unless explicitly enabled" do
@@ -483,7 +483,7 @@ EOF
@wiki.clear_cache @wiki.clear_cache
page = @wiki.page(name) page = @wiki.page(name)
assert_equal nil, page assert_nil page
end end
test "previews content" do test "previews content" do
@@ -507,7 +507,7 @@ EOF
@wiki.clear_cache @wiki.clear_cache
page2 = @wiki.page('B') page2 = @wiki.page('B')
assert_not_equal page1.version.sha, page2.version.sha refute_equal page1.version.sha, page2.version.sha
assert_equal "INITIAL", page2.raw_data.strip assert_equal "INITIAL", page2.raw_data.strip
assert_equal "Revert commit 7c45b5f", page2.version.message assert_equal "Revert commit 7c45b5f", page2.version.message
end end
@@ -521,7 +521,7 @@ EOF
@wiki.clear_cache @wiki.clear_cache
page2 = @wiki.page('A') page2 = @wiki.page('A')
assert_not_equal page1.version.sha, page2.version.sha refute_equal page1.version.sha, page2.version.sha
assert_equal "INITIAL", page2.raw_data.strip assert_equal "INITIAL", page2.raw_data.strip
end end
@@ -583,7 +583,7 @@ EOF
{ :name => 'user1', :email => 'user1' }); { :name => 'user1', :email => 'user1' });
get page get page
assert_no_match /custom.js/, last_response.body refute_match /custom.js/, last_response.body
end end
test "add custom.js if setting" do test "add custom.js if setting" do
@@ -652,7 +652,7 @@ EOF
get "A" get "A"
assert last_response.ok? assert last_response.ok?
assert_no_match /meta name="robots" content="noindex, nofollow"/, last_response.body refute_match /meta name="robots" content="noindex, nofollow"/, last_response.body
get "A/fc66539528eb96f21b2bbdbf557788fe8a1196ac" get "A/fc66539528eb96f21b2bbdbf557788fe8a1196ac"
@@ -834,7 +834,7 @@ context "Frontend with lotr" do
get "Data.csv/#{old_sha}" get "Data.csv/#{old_sha}"
assert last_response.ok? assert last_response.ok?
assert_no_match /Samwise,Gamgee/, last_response.body refute_match /Samwise,Gamgee/, last_response.body
get "Data.csv/#{update_sha}" get "Data.csv/#{update_sha}"
assert last_response.ok? assert last_response.ok?
@@ -879,7 +879,7 @@ context "Frontend with page-file-dir" do
name = "#{dir}/baz" name = "#{dir}/baz"
get "/gollum/create/#{name}" get "/gollum/create/#{name}"
assert_match(/\/#{dir}/, last_response.body) assert_match(/\/#{dir}/, last_response.body)
assert_no_match(/[^\/]#{dir}/, last_response.body) refute_match(/[^\/]#{dir}/, last_response.body)
end end
test "use custom.css from page-file-dir path if page-file-dir is set" do test "use custom.css from page-file-dir path if page-file-dir is set" do
+1 -1
View File
@@ -35,7 +35,7 @@ context "Precious::Views::Page" do
@view.instance_variable_set :@content, page.formatted_data @view.instance_variable_set :@content, page.formatted_data
@view.instance_variable_set :@h1_title, false @view.instance_variable_set :@h1_title, false
assert_include @view.breadcrumb, "数学 📘" assert_includes @view.breadcrumb, "数学 📘"
end end
test 'page <title> is the page header from content, if present' do test 'page <title> is the page header from content, if present' do
+6 -2
View File
@@ -37,7 +37,11 @@ class TestTemplateCascade < Minitest::Unit::TestCase
get '/Home' get '/Home'
assert_equal '/Home', last_request.fullpath assert_equal '/Home', last_request.fullpath
assert last_response.ok? assert last_response.ok?
assert_no_match /PAGE_OVERRIDE/, last_response.body refute_match /PAGE_OVERRIDE/, last_response.body
assert_no_match /NAVBAR_OVERRIDE/, last_response.body refute_match /NAVBAR_OVERRIDE/, last_response.body
end
def teardown
FileUtils.rm_rf(@path)
end end
end end