Skip to content

Commit e756694

Browse files
committed
Simplify get/getg rspec
1 parent c6121f0 commit e756694

File tree

1 file changed

+32
-79
lines changed

1 file changed

+32
-79
lines changed

spec/lib/msf/ui/console/command_dispatcher/core_spec.rb

Lines changed: 32 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -99,65 +99,37 @@ def cell(table, row, column)
9999
it { is_expected.to respond_to :cmd_get }
100100
it { is_expected.to respond_to :cmd_getg }
101101

102-
describe "#cmd_get" do
103-
describe "without arguments" do
104-
it "should show a help message" do
105-
core.cmd_get
106-
@output.join.should =~ /Usage: get /
107-
end
102+
def set_and_test_variable(name, framework_value, module_value, framework_re, module_re)
103+
# set the current module
104+
allow(core).to receive(:active_module).and_return(mod)
105+
# always assume set variables validate (largely irrelevant because ours are random)
106+
allow(driver).to receive(:on_variable_set).and_return(true)
107+
# the specified global value
108+
core.cmd_setg(name, framework_value) if framework_value
109+
# set the specified local value
110+
core.cmd_set(name, module_value) if module_value
111+
112+
# test the global value if specified
113+
if framework_re
114+
@output = []
115+
core.cmd_getg(name)
116+
@output.join.should =~ framework_re
108117
end
109118

110-
describe "with arguments" do
111-
let(:name) { ::Rex::Text.rand_text_alpha(10).upcase }
112-
113-
context "with an active module" do
114-
let(:mod) do
115-
mod = ::Msf::Module.new
116-
mod.send(:initialize, {})
117-
mod
118-
end
119-
120-
it "should show no value if not set in the framework or module" do
121-
allow(core).to receive(:active_module).and_return(mod)
122-
allow(driver).to receive(:on_variable_set).and_return(true)
123-
core.cmd_get(name)
124-
@output.join.should =~ /^#{name} => $/
125-
end
126-
127-
it "should show no value when only the framework has this variable" do
128-
allow(core).to receive(:active_module).and_return(mod)
129-
allow(driver).to receive(:on_variable_set).and_return(true)
130-
core.cmd_setg(name, 'FRAMEWORK')
131-
@output = []
132-
core.cmd_get(name)
133-
@output.join.should =~ /^#{name} => $/
134-
end
135-
136-
it "should show the module's value when only the module has this variable" do
137-
allow(core).to receive(:active_module).and_return(mod)
138-
allow(driver).to receive(:on_variable_set).and_return(true)
139-
core.cmd_set(name, 'MODULE')
140-
@output = []
141-
core.cmd_get(name)
142-
@output.join.should =~ /^#{name} => MODULE$/
143-
end
144-
145-
it "should show the module's value when both the module and the framework have this variable" do
146-
allow(core).to receive(:active_module).and_return(mod)
147-
allow(driver).to receive(:on_variable_set).and_return(true)
148-
core.cmd_setg(name, 'FRAMEWORK')
149-
core.cmd_set(name, 'MODULE')
150-
@output = []
151-
core.cmd_get(name)
152-
@output.join.should =~ /^#{name} => MODULE$/
153-
end
154-
end
119+
# test the local value if specified
120+
if module_re
121+
@output = []
122+
core.cmd_get(name)
123+
@output.join.should =~ module_re
155124
end
156125
end
157126

158-
describe "#cmd_getg" do
127+
describe "#cmd_get and #cmd_getg" do
159128
describe "without arguments" do
160-
it "should show a help message" do
129+
it "should show the correct help message" do
130+
core.cmd_get
131+
@output.join.should =~ /Usage: get /
132+
@output = []
161133
core.cmd_getg
162134
@output.join.should =~ /Usage: getg /
163135
end
@@ -174,38 +146,19 @@ def cell(table, row, column)
174146
end
175147

176148
it "should show no value if not set in the framework or module" do
177-
allow(core).to receive(:active_module).and_return(mod)
178-
allow(driver).to receive(:on_variable_set).and_return(true)
179-
core.cmd_getg(name)
180-
@output.join.should =~ /^#{name} => $/
149+
set_and_test_variable(name, nil, nil, /^#{name} => $/, /^#{name} => $/)
181150
end
182151

183-
it "should show no value when only the module has this variable" do
184-
allow(core).to receive(:active_module).and_return(mod)
185-
allow(driver).to receive(:on_variable_set).and_return(true)
186-
core.cmd_set(name, 'MODULE')
187-
@output = []
188-
core.cmd_getg(name)
189-
@output.join.should =~ /^#{name} => $/
152+
it "should show the correct value when only the module has this variable" do
153+
set_and_test_variable(name, nil, 'MODULE', /^#{name} => $/, /^#{name} => MODULE$/)
190154
end
191155

192-
it "should show the framework's value when only the framework has this variable" do
193-
allow(core).to receive(:active_module).and_return(mod)
194-
allow(driver).to receive(:on_variable_set).and_return(true)
195-
core.cmd_setg(name, 'FRAMEWORK')
196-
@output = []
197-
core.cmd_getg(name)
198-
@output.join.should =~ /^#{name} => FRAMEWORK$/
156+
it "should show the correct value when only the framework has this variable" do
157+
set_and_test_variable(name, 'FRAMEWORK', nil, /^#{name} => FRAMEWORK$/, /^#{name} => $/)
199158
end
200159

201-
it "should show the framework's value when both the module and the framework have this variable" do
202-
allow(core).to receive(:active_module).and_return(mod)
203-
allow(driver).to receive(:on_variable_set).and_return(true)
204-
core.cmd_setg(name, 'FRAMEWORK')
205-
core.cmd_set(name, 'MODULE')
206-
@output = []
207-
core.cmd_getg(name)
208-
@output.join.should =~ /^#{name} => FRAMEWORK$/
160+
it "should show the correct value when both the module and the framework have this variable" do
161+
set_and_test_variable(name, 'FRAMEWORK', 'MODULE', /^#{name} => FRAMEWORK$/, /^#{name} => MODULE$/)
209162
end
210163
end
211164
end

0 commit comments

Comments
 (0)