Skip to content

Commit

Permalink
Remove FakeWeb, use Webmock (#2282)
Browse files Browse the repository at this point in the history
* remove fakeweb

* remove fakeweb stub with webmock

* remove fakeweb ext

* format/rubocop

* lock webmock version, rubocops

---------

Co-authored-by: Arthur Chiu <[email protected]>
  • Loading branch information
achiurizo and achiurizo authored Jan 25, 2024
1 parent 65044af commit 27d4fb4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Style/BlockDelimiters:
Enabled: false
Style/CharacterLiteral:
Enabled: false
Style/ClassAndModuleChildren:
Enabled: false
Style/ClassCheck:
Enabled: false
Style/ClassVars:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ group :development do
end

gem "builder", ">= 2.1.2"
gem "fakeweb", ">= 1.2.8"
gem "minitest", ">= 4.0"
gem "mocha", ">= 2.0"
gem "oga", ">= 2.5", "< 3"
Expand All @@ -52,6 +51,7 @@ group :development do
gem 'rb-readline', '~> 0.4.2'
gem 'rubocop', '~> 1.6', :platforms => [:mri]
gem 'rubocop-minitest', '~>0.34.4', :platforms => [:mri]
gem "webmock", "~> 3.19"
gem "yard", ">= 0.7.2"

platforms :jruby do
Expand Down
44 changes: 26 additions & 18 deletions padrino-gen/test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
require 'minitest/autorun'
require 'minitest/pride'
require 'rack/test'
require 'fakeweb'
require 'thor/group'
require 'padrino-gen'
require 'padrino-core'
require 'padrino-mailer'
require 'padrino-helpers'

require 'ext/minitest-spec'
require 'ext/fakeweb-ruby24'
require 'mocha/minitest'
require 'webmock/minitest'

Padrino::Generators.load_components!

# register fake URL to avoid downloading static files every time tests run
fake_uri_base = "https://raw.github.com/padrino/padrino-static/master/"
%W[
js/dojo.js ujs/dojo.js
js/ext.js ujs/ext.js
js/jquery.js ujs/jquery.js
js/mootools.js ujs/mootools.js
js/right.js ujs/right.js
js/protopak.js js/lowpro.js ujs/prototype.js
].each do |suffix|
FakeWeb.register_uri(:get, fake_uri_base + suffix, :body => '')
end

class Minitest::Spec
def stop_time_for_test
time = Time.now
Time.stubs(:now).returns(time)
return time
time
end

def stub_static_files
# register fake URL to avoid downloading static files every time tests run
fake_uri_base = "https://raw.github.com/padrino/padrino-static/master/"
%w[
js/dojo.js
js/ext.js
js/jquery.js
js/lowpro.js
js/mootools.js
js/protopak.js
js/right.js
ujs/dojo.js
ujs/ext.js
ujs/jquery.js
ujs/mootools.js
ujs/prototype.js
ujs/right.js
].each do |suffix|
WebMock::API.stub_request(:get, fake_uri_base + suffix)
end
end

# generate(:controller, 'DemoItems', '-r=/tmp/sample_project')
Expand All @@ -44,7 +52,7 @@ def generate(name, *params)
def generate_with_parts(name, *params)
features, constants = [$", Object.constants].map{|x| Marshal.load(Marshal.dump(x)) }

if root = params.find{|x| x.index(/\-r=|\-\-root=/) }
if root = params.find{|x| x.index(/-r=|--root=/) }
root = root.split(/=/)[1]
options, model_path = {}, File.expand_path(File.join(root, "/models/**/*.rb"))
options = params.pop if params.last.is_a?(Hash)
Expand All @@ -69,7 +77,7 @@ def expects_generated_project(options={})
options = options.dup
project_root = options.delete(:root)
project_name = options.delete(:name)
components = options.sort_by{ |k, v| k.to_s }.map{ |component, value| "--#{component}=#{value}" }
components = options.sort_by{ |k, _v| k.to_s }.map{ |component, value| "--#{component}=#{value}" }
params = [project_name, *components].push("-r=#{project_root}")
Padrino.expects(:bin_gen).with(*params.unshift('project')).returns(true)
end
Expand Down
1 change: 1 addition & 0 deletions padrino-gen/test/test_component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
def setup
@apptmp = "#{Dir.tmpdir}/padrino-tests/#{SecureRandom.hex}"
`mkdir -p #{@apptmp}`
stub_static_files
end

def teardown
Expand Down
28 changes: 14 additions & 14 deletions padrino-gen/test/test_plugin_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ def teardown

describe "the plugin generator" do
it 'should respect --root option' do
path = File.expand_path('../fixtures/plugin_template.rb', __FILE__)
path = File.expand_path('fixtures/plugin_template.rb', __dir__)
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}") }
out, err = capture_io { generate(:plugin, path, "--root=#{@apptmp}/sample_project") }
refute_match /You are not at the root/, out
out, _err = capture_io { generate(:plugin, path, "--root=#{@apptmp}/sample_project") }
refute_match(/You are not at the root/, out)
end
end

describe "the plugin destroy option" do
it 'should remove the plugin instance' do
path = File.expand_path('../fixtures/plugin_template.rb', __FILE__)
path = File.expand_path('fixtures/plugin_template.rb', __dir__)
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}") }
capture_io { generate(:plugin, path, "--root=#{@apptmp}/sample_project") }
capture_io { generate(:plugin, path, "--root=#{@apptmp}/sample_project", '-d') }
assert_no_file_exists("#{@apptmp}/sample_project/lib/hoptoad_initializer.rb")
assert_no_match_in_file(/enable \:raise_errors/,"#{@apptmp}/sample_project/app/app.rb")
assert_no_match_in_file(/rack\_hoptoad/, "#{@apptmp}/sample_project/Gemfile")
assert_no_match_in_file(/enable :raise_errors/,"#{@apptmp}/sample_project/app/app.rb")
assert_no_match_in_file(/rack_hoptoad/, "#{@apptmp}/sample_project/Gemfile")
end
end

Expand All @@ -54,14 +54,14 @@ def teardown

it 'should resolve generic url properly' do
template_file = 'http://www.example.com/test.rb'
FakeWeb.register_uri :get, template_file, :body => ''
stub_request :get, template_file
project_gen = Padrino::Generators::Project.new(['sample_project'], ["-p=#{template_file}", "-r=#{@apptmp}"], {})
project_gen.expects(:apply).with(template_file).returns(true).once
capture_io { project_gen.invoke_all }
end

it 'should resolve gist url properly' do
FakeWeb.register_uri(:get, "https://gist.github.com/357045", :body => '<a href="/raw/357045/4356/blog_template.rb">raw</a>')
stub_request(:get, "https://gist.github.com/357045").to_return(body: '<a href="/raw/357045/4356/blog_template.rb">raw</a>')
template_file = 'https://gist.github.com/357045'
resolved_path = 'https://gist.github.com/raw/357045/4356/blog_template.rb'
project_gen = Padrino::Generators::Project.new(['sample_project'], ["-p=#{template_file}", "-r=#{@apptmp}"], {})
Expand All @@ -72,7 +72,7 @@ def teardown
it 'should resolve official template' do
template_file = 'sampleblog'
resolved_path = "https://raw.github.com/padrino/padrino-recipes/master/templates/sampleblog_template.rb"
FakeWeb.register_uri :get, resolved_path, :body => template_file
stub_request(:get, resolved_path).to_return(body: template_file)
project_gen = Padrino::Generators::Project.new(['sample_project'], ["-p=#{template_file}", "-r=#{@apptmp}"], {})
project_gen.expects(:apply).with(resolved_path).returns(true).once
capture_io { project_gen.invoke_all }
Expand All @@ -88,7 +88,7 @@ def teardown
it 'should resolve official plugin' do
template_file = 'hoptoad'
resolved_path = "https://raw.github.com/padrino/padrino-recipes/master/plugins/hoptoad_plugin.rb"
FakeWeb.register_uri :get, resolved_path, :body => template_file
stub_request(:get, resolved_path).to_return(body: template_file)
plugin_gen = Padrino::Generators::Plugin.new([ template_file], ["-r=#{@apptmp}/sample_project"],{})
plugin_gen.expects(:in_app_root?).returns(true).once
plugin_gen.expects(:apply).with(resolved_path).returns(true).once
Expand All @@ -98,7 +98,7 @@ def teardown
it 'should print a warning if template cannot be found' do
template_file = 'hwat'
resolved_path = "https://raw.github.com/padrino/padrino-recipes/master/plugins/hwat_plugin.rb"
FakeWeb.register_uri :get, resolved_path, :status => 404
stub_request(:get, resolved_path).to_return(status: 404)
plugin_gen = Padrino::Generators::Plugin.new([ template_file], ["-r=#{@apptmp}/sample_project"],{})
plugin_gen.expects(:in_app_root?).returns(true).once
# Use regex to ignore trailing whitespace in message
Expand All @@ -124,7 +124,7 @@ def teardown
describe "with git commands" do
it 'should generate a repository correctly' do
skip 'Change stubs here'
expects_generated_project :test => :rspec, :orm => :activerecord, :name => 'sample_git', :root => "#{@apptmp}"
expects_generated_project :test => :rspec, :orm => :activerecord, :name => 'sample_git', :root => @apptmp.to_s
expects_git :init, :root => "#{@apptmp}/sample_git"
expects_git :add, :arguments => '.', :root => "#{@apptmp}/sample_git"
expects_git :commit, :arguments => 'hello', :root => "#{@apptmp}/sample_git"
Expand All @@ -135,7 +135,7 @@ def teardown

describe "with rake invocations" do
it 'should Run rake task and list tasks' do
expects_generated_project :test => :shoulda, :orm => :activerecord, :name => 'sample_rake', :root => "#{@apptmp}"
expects_generated_project :test => :shoulda, :orm => :activerecord, :name => 'sample_rake', :root => @apptmp.to_s
expects_rake "custom", :root => "#{@apptmp}/sample_rake"
rake_template_path = File.join(File.dirname(__FILE__), 'fixtures', 'rake_template.rb')
capture_io { generate(:project, 'sample_rake', "-p=#{rake_template_path}", "-r=#{@apptmp}", '> /dev/null') }
Expand All @@ -144,7 +144,7 @@ def teardown

describe "with admin commands" do
it 'should generate correctly an admin' do
expects_generated_project :test => :shoulda, :orm => :activerecord, :name => 'sample_admin', :root => "#{@apptmp}"
expects_generated_project :test => :shoulda, :orm => :activerecord, :name => 'sample_admin', :root => @apptmp.to_s
expects_generated :model, "post title:string body:text -r=#{@apptmp}/sample_admin"
expects_rake "ar:create", :root => "#{@apptmp}/sample_admin"
expects_generated :admin, "-r=#{@apptmp}/sample_admin"
Expand Down
1 change: 1 addition & 0 deletions padrino-gen/test/test_project_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
def setup
@apptmp = "#{Dir.tmpdir}/padrino-tests/#{SecureRandom.hex}"
`mkdir -p #{@apptmp}`
stub_static_files
end

def teardown
Expand Down
5 changes: 0 additions & 5 deletions padrino/test/ext/fakeweb-ruby24.rb

This file was deleted.

0 comments on commit 27d4fb4

Please sign in to comment.