From ab74cfd08548873acf1ef55809b8c99f7acfbdab Mon Sep 17 00:00:00 2001 From: Brian P O'Rourke Date: Thu, 5 Mar 2020 12:48:42 -0800 Subject: [PATCH 1/2] Fix readline specs An error was introduced in commit 7061b06fd87dc8b952f9072507ee991ee0a0858e where readline specs fail because `require "readline"` is be called after the spec has established a mock `::Readline`. Prior to that change `require "readline"` was always only called once. This patch: * memoizes the `require` to ensure that it only happens once * leaves the `Object.const_defined?` check in place to keep tests simple * eagerly calls `available?` in the readline spec to match the original author's intent by requiring `readline` before the specs start. --- lib/thor/line_editor/readline.rb | 4 ++-- spec/line_editor/readline_spec.rb | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/thor/line_editor/readline.rb b/lib/thor/line_editor/readline.rb index 2fd925d96..8c1dc16f4 100644 --- a/lib/thor/line_editor/readline.rb +++ b/lib/thor/line_editor/readline.rb @@ -2,8 +2,8 @@ class Thor module LineEditor class Readline < Basic def self.available? - begin - require "readline" + @readline_reqd ||= begin + require "readline" rescue LoadError end diff --git a/spec/line_editor/readline_spec.rb b/spec/line_editor/readline_spec.rb index 86cf7466e..158ba505a 100644 --- a/spec/line_editor/readline_spec.rb +++ b/spec/line_editor/readline_spec.rb @@ -2,6 +2,8 @@ describe Thor::LineEditor::Readline do before do + # Eagerly check Readline availability and cache for this spec + Thor::LineEditor::Readline.available? unless defined? ::Readline ::Readline = double("Readline") allow(::Readline).to receive(:completion_append_character=).with(nil) From 4a8cdc8209048be3933a737e1176c3143ea90d79 Mon Sep 17 00:00:00 2001 From: Brian P O'Rourke Date: Thu, 5 Mar 2020 13:31:13 -0800 Subject: [PATCH 2/2] Drop unneeded require memoization --- lib/thor/line_editor/readline.rb | 4 ++-- spec/line_editor/readline_spec.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/thor/line_editor/readline.rb b/lib/thor/line_editor/readline.rb index 8c1dc16f4..2fd925d96 100644 --- a/lib/thor/line_editor/readline.rb +++ b/lib/thor/line_editor/readline.rb @@ -2,8 +2,8 @@ class Thor module LineEditor class Readline < Basic def self.available? - @readline_reqd ||= begin - require "readline" + begin + require "readline" rescue LoadError end diff --git a/spec/line_editor/readline_spec.rb b/spec/line_editor/readline_spec.rb index 158ba505a..7ab67aaa9 100644 --- a/spec/line_editor/readline_spec.rb +++ b/spec/line_editor/readline_spec.rb @@ -2,7 +2,7 @@ describe Thor::LineEditor::Readline do before do - # Eagerly check Readline availability and cache for this spec + # Eagerly check Readline availability before mocking Thor::LineEditor::Readline.available? unless defined? ::Readline ::Readline = double("Readline")