Skip to content

Commit

Permalink
Passing history manager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanusz-r7 committed Mar 7, 2024
1 parent 14ef7fb commit 8e1074a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions lib/rex/ui/text/shell/history_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def initialize
# @param [Proc] block
# @return [nil]
def with_context(history_file: nil, name: nil, input_library: nil, &block)
input_library ||= :readline # Default to Readline for backwards compatibility.
push_context(history_file: history_file, name: name, input_library: input_library)
# Default to Readline for backwards compatibility.
push_context(history_file: history_file, name: name, input_library: input_library || :readline)

begin
block.call
Expand Down Expand Up @@ -69,7 +69,7 @@ def debug?

def push_context(history_file: nil, name: nil, input_library: nil)
$stderr.puts("Push context before\n#{JSON.pretty_generate(_contexts)}") if debug?
new_context = { history_file: history_file, name: name, input_library: input_library }
new_context = { history_file: history_file, name: name, input_library: input_library || :readline }

switch_context(new_context, @contexts.last)
@contexts.push(new_context)
Expand Down
30 changes: 15 additions & 15 deletions spec/lib/rex/ui/text/shell/history_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
(expect do |block|
subject.with_context(name: 'a') do
expected_contexts = [
{ history_file: nil, name: 'a' },
{ history_file: nil, input_library: :readline, name: 'a' },
]
expect(subject._contexts).to eq(expected_contexts)
block.to_proc.call
Expand All @@ -36,15 +36,15 @@

context 'when there is an existing stack' do
before(:each) do
subject.send(:push_context, history_file: nil, name: 'a')
subject.send(:push_context, history_file: nil, input_library: :readline, name: 'a')
end

it 'continues to have the previous existing stack' do
subject.with_context {
# noop
}
expected_contexts = [
{ history_file: nil, name: 'a' },
{ history_file: nil, input_library: :readline, name: 'a' },
]
expect(subject._contexts).to eq(expected_contexts)
end
Expand All @@ -53,8 +53,8 @@
(expect do |block|
subject.with_context(name: 'b') do
expected_contexts = [
{ history_file: nil, name: 'a' },
{ history_file: nil, name: 'b' },
{ history_file: nil, input_library: :readline, name: 'a' },
{ history_file: nil, input_library: :readline, name: 'b' },
]
expect(subject._contexts).to eq(expected_contexts)
block.to_proc.call
Expand All @@ -69,7 +69,7 @@
}
end.to raise_exception ArgumentError, 'Mock error'
expected_contexts = [
{ history_file: nil, name: 'a' },
{ history_file: nil, input_library: :readline, name: 'a' },
]
expect(subject._contexts).to eq(expected_contexts)
end
Expand All @@ -79,9 +79,9 @@
describe '#push_context' do
context 'when the stack is empty' do
it 'stores the history contexts' do
subject.send(:push_context, history_file: nil, name: 'a')
subject.send(:push_context, history_file: nil, input_library: :readline, name: 'a')
expected_contexts = [
{ history_file: nil, name: 'a' }
{ history_file: nil, input_library: :readline, name: 'a' }
]
expect(subject._contexts).to eq(expected_contexts)
end
Expand All @@ -90,12 +90,12 @@
context 'when multiple values are pushed' do
it 'stores the history contexts' do
subject.send(:push_context, history_file: nil, name: 'a')
subject.send(:push_context, history_file: nil, name: 'b')
subject.send(:push_context, history_file: nil, name: 'c')
subject.send(:push_context, history_file: nil, input_library: :readline, name: 'b')
subject.send(:push_context, history_file: nil, input_library: :reline, name: 'c')
expected_contexts = [
{ history_file: nil, name: 'a' },
{ history_file: nil, name: 'b' },
{ history_file: nil, name: 'c' },
{ history_file: nil, input_library: :readline, name: 'a' },
{ history_file: nil, input_library: :readline, name: 'b' },
{ history_file: nil, input_library: :reline, name: 'c' },
]
expect(subject._contexts).to eq(expected_contexts)
end
Expand All @@ -113,12 +113,12 @@
end

context 'when the stack is not empty' do
it 'continues to have an empty stack' do
it 'continues to have a non-empty stack' do
subject.send(:push_context, history_file: nil, name: 'a')
subject.send(:push_context, history_file: nil, name: 'b')
subject.send(:pop_context)
expected_contexts = [
{ history_file: nil, name: 'a' },
{ history_file: nil, input_library: :readline, name: 'a' },
]
expect(subject._contexts).to eq(expected_contexts)
end
Expand Down

0 comments on commit 8e1074a

Please sign in to comment.