Skip to content

Add warning against unknown commands #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/redis/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,12 @@ class Namespace

attr_writer :namespace
attr_reader :redis
attr_accessor :warning

def initialize(namespace, options = {})
@namespace = namespace
@redis = options[:redis] || Redis.current
@warning = options[:warning] || false
end

# Ruby defines a now deprecated type method so we need to override it here
Expand Down Expand Up @@ -236,8 +238,10 @@ def method_missing(command, *args, &block)
COMMANDS[ALIASES[command.to_s]]

# redis-namespace does not know how to handle this command.
# Passing it to @redis as is.
# Passing it to @redis as is, where redis-namespace shows
# a warning message if @warning is set.
if handling.nil?
warn("Passing '#{command}' command to redis as is.") if @warning
return @redis.send(command, *args, &block)
end

Expand Down
11 changes: 11 additions & 0 deletions spec/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,17 @@
@namespaced.respond_to?(:namespace=).should == true
end

it "should respond to :warning=" do
@namespaced.respond_to?(:warning=).should == true
end

it "should warn against unknown commands if :warning is true" do
@namespaced.warning = true
capture_stderr {
@namespaced.unknown('foo')
}.should == "Passing 'unknown' command to redis as is."
end

# Redis 2.6 RC reports its version as 2.5.
if @redis_version >= Gem::Version.new("2.5.0")
describe "redis 2.6 commands" do
Expand Down
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'redis/namespace'

def capture_stderr
require 'stringio'
begin
original, $stderr = $stderr, StringIO.new
yield
rescue Redis::CommandError
# ignore Redis::CommandError for test and
# return captured messages
$stderr.string.chomp
ensure
$stderr = original
end
end

RSpec::Matchers.define :have_key do |expected|
match do |redis|
redis.exists(expected)
Expand Down