Skip to content

Commit

Permalink
Fix more specs
Browse files Browse the repository at this point in the history
Signed-off-by: Rémy Coutable <remy@rymai.me>
  • Loading branch information
rymai committed Mar 4, 2018
1 parent 1814234 commit 7dcedd2
Show file tree
Hide file tree
Showing 32 changed files with 344 additions and 456 deletions.
2 changes: 2 additions & 0 deletions lib/guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module Guard
Deprecated::Guard.add_deprecated(self) unless Config.new.strict?

class << self
attr_reader :engine

# @private api

# Backward-compatibility with the Guard singleton approach
Expand Down
4 changes: 1 addition & 3 deletions lib/guard/cli/environments/evaluate_only.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ def evaluate
# not global Guard object (setting global state only needed before
# start() is called)
#
engine = Guard.init(@options)
session = engine.state.session
Guardfile::Evaluator.new(session.evaluator_options).evaluate
Guardfile::Evaluator.new(engine: Guard.init(@options)).evaluate
rescue \
Dsl::Error,
Guardfile::Evaluator::NoPluginsError,
Expand Down
2 changes: 1 addition & 1 deletion lib/guard/cli/environments/valid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize_guardfile(plugin_names = [])
bare = @options[:bare]

engine = Guard.init(@options)
session = engine.state.session
session = engine.session

generator = Guardfile::Generator.new
begin
Expand Down
75 changes: 37 additions & 38 deletions lib/guard/deprecated/guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ module ClassMethods
over to: https://github.com/guard/guard/wiki/Upgrading-to-Guard-2.0%s
EOS

# @deprecated Use `Guard.plugins(filter)` instead.
# @deprecated Use `Guard.engine.plugins(filter)` instead.
#
# @see https://github.com/guard/guard/wiki/Upgrading-to-Guard-2.0 How to
# upgrade for Guard 2.0
#
GUARDS = <<-EOS.gsub(/^\s*/, "")
Starting with Guard 2.0 'Guard.guards(filter)' is deprecated.
Please use 'Guard.plugins(filter)' instead.
Please use 'Guard.engine.plugins(filter)' instead.
#{MORE_INFO_ON_UPGRADING_TO_GUARD_2 % '#deprecated-methods'}
EOS

def guards(filter = nil)
::Guard::UI.deprecation(GUARDS)
@engine.state.session.plugins.all(filter)
engine.plugins.all(filter)
end

# @deprecated Use `Guard.add_plugin(name, options = {})` instead.
# @deprecated Use `Guard.engine.plugins.add(name, options = {})` instead.
#
# @see https://github.com/guard/guard/wiki/Upgrading-to-Guard-2.0 How to
# upgrade for Guard 2.0
Expand All @@ -49,7 +49,7 @@ def guards(filter = nil)
Starting with Guard 2.0 'Guard.add_guard(name, options = {})' is
deprecated.
Please use 'Guard.add_plugin(name, options = {})' instead.
Please use 'Guard.engine.plugins.add(name, options = {})' instead.
#{MORE_INFO_ON_UPGRADING_TO_GUARD_2 % '#deprecated-methods'}
EOS
Expand All @@ -60,7 +60,7 @@ def add_guard(*args)
end

# @deprecated Use
# `Guard::PluginUtil.new(name).plugin_class(fail_gracefully:
# `Guard::PluginUtil.new(engine: engine, name: name).plugin_class(fail_gracefully:
# fail_gracefully)` instead.
#
# @see https://github.com/guard/guard/wiki/Upgrading-to-Guard-2.0 How to
Expand All @@ -70,33 +70,33 @@ def add_guard(*args)
Starting with Guard 2.0 'Guard.get_guard_class(name, fail_gracefully
= false)' is deprecated and is now always on.
Please use 'Guard::PluginUtil.new(name).plugin_class(fail_gracefully:
Please use 'Guard::PluginUtil.new(engine: engine, name: name).plugin_class(fail_gracefully:
fail_gracefully)' instead.
#{MORE_INFO_ON_UPGRADING_TO_GUARD_2 % '#deprecated-methods'}
EOS

def get_guard_class(name, fail_gracefully = false)
UI.deprecation(GET_GUARD_CLASS)
PluginUtil.new(name).plugin_class(fail_gracefully: fail_gracefully)
PluginUtil.new(engine: engine, name: name).plugin_class(fail_gracefully: fail_gracefully)
end

# @deprecated Use `Guard::PluginUtil.new(name).plugin_location` instead.
# @deprecated Use `Guard::PluginUtil.new(engine: engine, name: name).plugin_location` instead.
#
# @see https://github.com/guard/guard/wiki/Upgrading-to-Guard-2.0 How to
# upgrade for Guard 2.0
#
LOCATE_GUARD = <<-EOS.gsub(/^\s*/, "")
Starting with Guard 2.0 'Guard.locate_guard(name)' is deprecated.
Please use 'Guard::PluginUtil.new(name).plugin_location' instead.
Please use 'Guard::PluginUtil.new(engine: engine, name: name).plugin_location' instead.
#{MORE_INFO_ON_UPGRADING_TO_GUARD_2 % '#deprecated-methods'}
EOS

def locate_guard(name)
UI.deprecation(LOCATE_GUARD)
PluginUtil.new(name).plugin_location
PluginUtil.new(engine: engine, name: name).plugin_location
end

# @deprecated Use `Guard::PluginUtil.plugin_names` instead.
Expand Down Expand Up @@ -151,8 +151,7 @@ def listener=(_)

def evaluator
UI.deprecation(EVALUATOR)
options = @engine.state.session.evaluator_options
::Guard::Guardfile::Evaluator.new(options)
::Guard::Guardfile::Evaluator.new(engine: engine)
end

RESET_EVALUATOR = <<-EOS.gsub(/^\s*/, "")
Expand All @@ -169,7 +168,7 @@ def reset_evaluator(_options)

def runner
UI.deprecation(RUNNER)
::Guard::Runner.new
::Guard::Runner.new(engine: engine)
end

EVALUATE_GUARDFILE = <<-EOS.gsub(/^\s*/, "")
Expand All @@ -178,11 +177,10 @@ def runner

def evaluate_guardfile
UI.deprecation(EVALUATE_GUARDFILE)
options = @engine.state.session.evaluator_options
evaluator = ::Guard::Guardfile::Evaluator.new(options)
evaluator = ::Guard::Guardfile::Evaluator.new(engine: engine)
evaluator.evaluate
msg = "No plugins found in Guardfile, please add at least one."
::Guard::UI.error msg if _pluginless_guardfile?
::Guard::UI.error(msg) if _pluginless_guardfile?
end

OPTIONS = <<-EOS.gsub(/^\s*/, "")
Expand All @@ -196,12 +194,13 @@ def options
UI.deprecation(OPTIONS)

Class.new(Hash) do
def initialize
super(to_hash)
def initialize(engine:)
@engine = engine
super(hash)
end

def to_hash
session = @engine.state.session
session = @engine.session
{
clear: session.clearing?,
debug: session.debug?,
Expand All @@ -224,7 +223,7 @@ def fetch(key, *args)
def []=(key, value)
case key
when :clear
@engine.state.session.clearing(value)
@engine.session.clearing(value)
else
msg = "Oops! Guard.option[%s]= is unhandled or unsupported." \
"Please file an issue if you rely on this option working."
Expand All @@ -240,87 +239,87 @@ def verify_key!(hash, key)
"Please file an issue if you rely on this option working."
fail NotImplementedError, format(msg, key)
end
end.new
end.new(engine: engine)
end

ADD_GROUP = <<-EOS.gsub(/^\s*/, "")
add_group is deprecated since 2.10.0 in favor of
Guard.state.session.groups.add
Guard.engine.groups.add
EOS

def add_group(name, options = {})
UI.deprecation(ADD_GROUP)
@engine.state.session.groups.add(name, options)
engine.groups.add(name, options)
end

ADD_PLUGIN = <<-EOS.gsub(/^\s*/, "")
add_plugin is deprecated since 2.10.0 in favor of
Guard.state.session.plugins.add
Guard.engine.plugins.add
EOS

def add_plugin(name, options = {})
UI.deprecation(ADD_PLUGIN)
@engine.state.session.plugins.add(name, options)
engine.plugins.add(name, options)
end

GROUP = <<-EOS.gsub(/^\s*/, "")
group is deprecated since 2.10.0 in favor of
Guard.state.session.group.add(filter).first
Guard.engine.groups.all(filter).first
EOS

def group(filter)
UI.deprecation(GROUP)
@engine.state.session.groups.all(filter).first
engine.groups.all(filter).first
end

PLUGIN = <<-EOS.gsub(/^\s*/, "")
plugin is deprecated since 2.10.0 in favor of
Guard.state.session.group.add(filter).first
Guard.engine.plugins.all(filter).first
EOS

def plugin(filter)
UI.deprecation(PLUGIN)
@engine.state.session.plugins.all(filter).first
engine.plugins.all(filter).first
end

GROUPS = <<-EOS.gsub(/^\s*/, "")
group is deprecated since 2.10.0 in favor of
Guard.state.session.groups.all(filter)
Guard.engine.groups.all(filter)
EOS

def groups(filter)
UI.deprecation(GROUPS)
@engine.state.session.groups.all(filter)
engine.groups.all(filter)
end

PLUGINS = <<-EOS.gsub(/^\s*/, "")
plugins is deprecated since 2.10.0 in favor of
Guard.state.session.plugins.all(filter)
Guard.engine.plugins.all(filter)
EOS

def plugins(filter)
UI.deprecation(PLUGINS)
@engine.state.session.plugins.all(filter)
engine.plugins.all(filter)
end

SCOPE = <<-EOS.gsub(/^\s*/, "")
scope is deprecated since 2.10.0 in favor of
Guard.state.scope.to_hash
Guard.engine.scope.to_hash
EOS

def scope
UI.deprecation(SCOPE)
@engine.state.scope.to_hash
engine.scope.to_hash
end

SCOPE_ASSIGN = <<-EOS.gsub(/^\s*/, "")
scope= is deprecated since 2.10.0 in favor of
Guard.state.scope.to_hash
Guard.engine.scope.to_hash
EOS

def scope=(scope)
UI.deprecation(SCOPE_ASSIGN)
@engine.state.scope.from_interactor(scope)
engine.scope.from_interactor(scope)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/guard/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def callback(*args, &block)
#
def ignore(*regexps)
# TODO: use guardfile results class
@engine.state.session.guardfile_ignore = regexps
@engine.session.guardfile_ignore = regexps
end

# TODO: deprecate
Expand Down
2 changes: 1 addition & 1 deletion lib/guard/dsl_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Guard
class DslReader < Dsl
attr_reader :plugin_names

def initialize
def initialize(engine:)
super
@plugin_names = []
end
Expand Down
2 changes: 1 addition & 1 deletion lib/guard/guardfile/evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def initialize(engine:)
@path = nil
@user_config = nil

opts = _from_deprecated(engine.state.session.evaluator_options)
opts = _from_deprecated(engine.session.evaluator_options)

if opts[:contents]
@type = :inline
Expand Down
8 changes: 7 additions & 1 deletion lib/guard/guardfile/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Generator
require "guard"
require "guard/ui"

attr_reader :engine

INFO_TEMPLATE_ADDED =
"%s template added to Guardfile, feel free to edit it"

Expand Down Expand Up @@ -57,6 +59,10 @@ def message
end
end

def initialize(engine:)
@engine = engine
end

# Creates the initial Guardfile template when it does not
# already exist.
#
Expand All @@ -83,7 +89,7 @@ def create_guardfile
def initialize_template(plugin_name)
guardfile = Pathname("Guardfile")

plugin_util = PluginUtil.new(plugin_name)
plugin_util = PluginUtil.new(engine: engine, name: plugin_name)
# TODO: change to "valid?" method
plugin_class = plugin_util.plugin_class(fail_gracefully: true)
if plugin_class
Expand Down
5 changes: 3 additions & 2 deletions lib/guard/internals/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ def remove(plugin)
# configs or groups)
def add(name, options)
plugin_util = PluginUtil.new(engine: @engine, name: name)
@plugins << plugin_util.initialize_plugin(options)
@plugins
plugin = plugin_util.initialize_plugin(options)
@plugins << plugin
plugin
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/guard/jobs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Jobs
class Base
attr_reader :engine

def initialize(_engine:, _options: {}); end
def initialize(engine: nil, options: {}); end

# @return [Symbol] :stopped once job is finished
# @return [Symbol] :exit to tell Guard to terminate
Expand Down
3 changes: 1 addition & 2 deletions lib/guard/plugin_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ def add_to_guardfile

require_relative "guardfile/evaluator"
# TODO: move this to Generator?
options = @engine.state.session.evaluator_options
evaluator = Guardfile::Evaluator.new(options)
evaluator = Guardfile::Evaluator.new(engine: @engine)
begin
evaluator.evaluate
rescue Guard::Guardfile::Evaluator::NoPluginsError
Expand Down

0 comments on commit 7dcedd2

Please sign in to comment.