Skip to content

Commit eda1e1c

Browse files
committed
Make call("info") return raw string like redis-rb
In redis-rb: - redis.info() returns a parsed Hash - redis.call("info") returns a raw string I've added a info_raw method, and updated `call` to use it for info commands.
1 parent fd63d52 commit eda1e1c

File tree

3 files changed

+72
-34
lines changed

3 files changed

+72
-34
lines changed

lib/mock_redis/database.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,15 @@ def call(*command, &_block)
5252
# flatten any nested arrays (eg from [:call, ["GET", "X"]] in pipelined commands)
5353
command = command.flatten
5454

55-
if command[0].downcase.to_s.include?('expire')
55+
cmd_name = command[0].downcase.to_s
56+
57+
if cmd_name.include?('expire')
5658
send_expires(command)
59+
elsif cmd_name == 'info'
60+
# call(:info) returns a string, not a parsed hash
61+
info_raw(*command[1..])
5762
else
58-
public_send(command[0].downcase, *command[1..])
63+
public_send(cmd_name, *command[1..])
5964
end
6065
end
6166

lib/mock_redis/info_method.rb

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -125,36 +125,63 @@ module InfoMethod
125125
}.freeze
126126
# rubocop:enable Layout/LineLength
127127

128-
DEFAULT_INFO = [
129-
SERVER_INFO,
130-
CLIENTS_INFO,
131-
MEMORY_INFO,
132-
PERSISTENCE_INFO,
133-
STATS_INFO,
134-
REPLICATION_INFO,
135-
CPU_INFO,
136-
KEYSPACE_INFO,
137-
].inject({}) { |memo, info| memo.merge(info) }
138-
139-
ALL_INFO = [
140-
DEFAULT_INFO,
141-
COMMAND_STATS_COMBINED_INFO,
142-
].inject({}) { |memo, info| memo.merge(info) }
128+
SECTIONS = {
129+
server: SERVER_INFO,
130+
clients: CLIENTS_INFO,
131+
memory: MEMORY_INFO,
132+
persistence: PERSISTENCE_INFO,
133+
stats: STATS_INFO,
134+
replication: REPLICATION_INFO,
135+
cpu: CPU_INFO,
136+
keyspace: KEYSPACE_INFO,
137+
commandstats: COMMAND_STATS_COMBINED_INFO,
138+
}
139+
SECTION_NAMES = {
140+
server: 'Server',
141+
clients: 'Clients',
142+
memory: 'Memory',
143+
persistence: 'Persistence',
144+
stats: 'Stats',
145+
replication: 'Replication',
146+
cpu: 'Cpu',
147+
keyspace: 'Keyspace',
148+
commandstats: 'Commandstats',
149+
}
150+
DEFAULT_SECTIONS = [:server, :clients, :memory, :persistence, :stats, :replication, :cpu, :keyspace]
151+
ALL_SECTIONS = DEFAULT_SECTIONS + [:commandstats]
143152

144153
def info(section = :default)
145-
section = section.to_sym if section.is_a?(String)
154+
if section.to_s.downcase == 'commandstats'
155+
# `redis.info(:commandstats)` gives a nested hash structure,
156+
# unlike when commandstats is printed as part of `redis.info(:all)`
157+
COMMAND_STATS_SOLO_INFO
158+
else
159+
sections = relevant_info_sections(section)
160+
sections.inject({}) { |memo, section| memo.merge(SECTIONS[section]) }
161+
end
162+
end
163+
164+
private
165+
166+
# Format info hash as raw string (used by call("info"))
167+
def info_raw(section = :default)
168+
sections = relevant_info_sections(section)
169+
sections.map do |name|
170+
header = "# " + SECTION_NAMES[name]
171+
lines = SECTIONS[name].map { |k, v| "#{k}:#{v}" }
172+
[header, *lines].join("\n") + "\n"
173+
end.join("\n")
174+
end
175+
176+
def relevant_info_sections(section)
177+
section = section.to_s.downcase.to_sym
146178
case section
147-
when :default; DEFAULT_INFO
148-
when :all; ALL_INFO
149-
when :server; SERVER_INFO
150-
when :clients; CLIENTS_INFO
151-
when :memory; MEMORY_INFO
152-
when :persistence; PERSISTENCE_INFO
153-
when :stats; STATS_INFO
154-
when :replication; REPLICATION_INFO
155-
when :cpu; CPU_INFO
156-
when :keyspace; KEYSPACE_INFO
157-
when :commandstats; COMMAND_STATS_SOLO_INFO
179+
when :default
180+
DEFAULT_SECTIONS
181+
when :all
182+
ALL_SECTIONS
183+
else
184+
[section]
158185
end
159186
end
160187
end

spec/commands/info_spec.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,18 @@
6060
expect(redis.info(:commandstats)['sunionstore']['usec']).to be_a(String)
6161
end
6262

63-
it 'works when called through call method' do
64-
expect(redis.call('info')).to be_a(Hash)
63+
it 'returns raw string when called through call method' do
64+
result = redis.call('info')
65+
expect(result).to be_a(String)
66+
expect(result).to include('# Server')
67+
expect(result).to include('arch_bits:64')
6568
end
6669

67-
it 'works when called through call method with section' do
68-
info = redis.call('info', 'server')
69-
expect(info['arch_bits']).to be_a(String)
70+
it 'returns raw string with section when called through call method' do
71+
result = redis.call('info', 'server')
72+
expect(result).to be_a(String)
73+
expect(result).to include('# Server')
74+
expect(result).to include('arch_bits:64')
75+
expect(result).not_to include('# Clients')
7076
end
7177
end

0 commit comments

Comments
 (0)