mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
CLI testing. Not great but integration testing is tough.
This commit is contained in:
parent
8cd021b4ca
commit
552a4c7f27
3 changed files with 63 additions and 5 deletions
|
|
@ -2,15 +2,20 @@ require 'optparse'
|
||||||
require 'sidekiq/version'
|
require 'sidekiq/version'
|
||||||
require 'sidekiq/util'
|
require 'sidekiq/util'
|
||||||
require 'sidekiq/redis_connection'
|
require 'sidekiq/redis_connection'
|
||||||
require 'sidekiq/client'
|
|
||||||
require 'sidekiq/manager'
|
require 'sidekiq/manager'
|
||||||
|
|
||||||
module Sidekiq
|
module Sidekiq
|
||||||
class CLI
|
class CLI
|
||||||
include Util
|
include Util
|
||||||
|
|
||||||
|
attr_accessor :options, :code
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
parse_options
|
@code = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse(args=ARGV)
|
||||||
|
parse_options(args)
|
||||||
validate!
|
validate!
|
||||||
boot_system
|
boot_system
|
||||||
end
|
end
|
||||||
|
|
@ -36,6 +41,10 @@ module Sidekiq
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def die(code)
|
||||||
|
exit(code)
|
||||||
|
end
|
||||||
|
|
||||||
def detected_environment
|
def detected_environment
|
||||||
@options[:environment] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
|
@options[:environment] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
|
||||||
end
|
end
|
||||||
|
|
@ -66,11 +75,11 @@ module Sidekiq
|
||||||
log " to load your worker classes with -r [DIR|FILE]."
|
log " to load your worker classes with -r [DIR|FILE]."
|
||||||
log "=================================================================="
|
log "=================================================================="
|
||||||
log @parser
|
log @parser
|
||||||
exit(1)
|
die(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_options(argv=ARGV)
|
def parse_options(argv)
|
||||||
@options = {
|
@options = {
|
||||||
:verbose => false,
|
:verbose => false,
|
||||||
:queues => [],
|
:queues => [],
|
||||||
|
|
@ -115,7 +124,7 @@ module Sidekiq
|
||||||
@parser.banner = "sidekiq [options]"
|
@parser.banner = "sidekiq [options]"
|
||||||
@parser.on_tail "-h", "--help", "Show help" do
|
@parser.on_tail "-h", "--help", "Show help" do
|
||||||
log @parser
|
log @parser
|
||||||
exit 1
|
die 1
|
||||||
end
|
end
|
||||||
@parser.parse!(argv)
|
@parser.parse!(argv)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
0
test/fake_env.rb
Normal file
0
test/fake_env.rb
Normal file
49
test/test_cli.rb
Normal file
49
test/test_cli.rb
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
require 'helper'
|
||||||
|
require 'sidekiq/cli'
|
||||||
|
|
||||||
|
class TestCli < MiniTest::Unit::TestCase
|
||||||
|
describe 'with cli' do
|
||||||
|
before do
|
||||||
|
@cli = new_cli
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'blows up with an invalid require' do
|
||||||
|
assert_raises ArgumentError do
|
||||||
|
@cli.parse(['sidekiq', '-r', 'foobar'])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'blows up with invalid Ruby' do
|
||||||
|
@cli.parse(['sidekiq', '-r', './test/fake_env.rb'])
|
||||||
|
assert($LOADED_FEATURES.any? { |x| x =~ /fake_env/ })
|
||||||
|
assert @cli.valid?
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'changes concurrency' do
|
||||||
|
@cli.parse(['sidekiq', '-c', '60', '-r', './test/fake_env.rb'])
|
||||||
|
assert_equal 60, @cli.options[:processor_count]
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'changes queues' do
|
||||||
|
@cli.parse(['sidekiq', '-q', 'foo', '-r', './test/fake_env.rb'])
|
||||||
|
assert_equal ['foo'], @cli.options[:queues]
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'handles weights' do
|
||||||
|
@cli.parse(['sidekiq', '-q', 'foo,3', '-q', 'bar', '-r', './test/fake_env.rb'])
|
||||||
|
assert_equal %w(bar foo foo foo), @cli.options[:queues].sort
|
||||||
|
end
|
||||||
|
|
||||||
|
def new_cli
|
||||||
|
cli = Sidekiq::CLI.new
|
||||||
|
def cli.die(code)
|
||||||
|
@code = code
|
||||||
|
end
|
||||||
|
|
||||||
|
def cli.valid?
|
||||||
|
!@code
|
||||||
|
end
|
||||||
|
cli
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue