Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Committed improvements #35

Merged
merged 12 commits into from
Apr 10, 2018
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changes

## master

* Don't notify about start when no start
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

* Improve `guard-compat` using (https://github.com/guard/guard-compat#migrating-your-api-calls)
* Remove unused `pry` dependency
* Update versions of dependencies

## 0.4.1

* Improve notifications. Via #30
Expand Down
7 changes: 3 additions & 4 deletions guard-puma.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ Gem::Specification.new do |gem|
gem.add_dependency "guard", "~> 2.14"
gem.add_dependency "guard-compat", "~> 1.2"
gem.add_dependency "puma", "~> 3.6"
gem.add_development_dependency "rake", "~> 10.4"
gem.add_development_dependency "rspec", "~> 3.5.0"
gem.add_development_dependency "guard-rspec", "~> 4.7.0"
gem.add_development_dependency "pry"
gem.add_development_dependency "rake", "~> 12"
gem.add_development_dependency "rspec", "~> 3.7"
gem.add_development_dependency "guard-rspec", "~> 4.7"
end
42 changes: 27 additions & 15 deletions lib/guard/puma.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
require "guard"
require "guard/plugin"
require "guard/puma/runner"
require "rbconfig"
require "guard/puma/version"
require "guard/compat/plugin"
require 'guard/compat/plugin'
require_relative 'puma/runner'
require_relative 'puma/version'

module Guard
class Puma < Plugin
Expand Down Expand Up @@ -31,32 +28,47 @@ def initialize(options = {})
end

def start
return unless options[:start_on_start]
server = options[:server] ? "#{options[:server]} and " : ""
UI.info "Puma starting#{port_text} in #{server}#{options[:environment]} environment."
runner.start if options[:start_on_start]
Compat::UI.info(
"Puma starting#{port_text} in #{server}#{options[:environment]} environment."
)
runner.start
end

def reload
UI.info "Restarting Puma..."
Compat::UI.info "Restarting Puma..."
if options[:notifications].include?(:restarting)
Notifier.notify("Puma restarting#{port_text} in #{options[:environment]} environment...", :title => "Restarting Puma...", :image => :pending)
Compat::UI.notify(
"Puma restarting#{port_text} in #{options[:environment]} environment...",
title: "Restarting Puma...", image: :pending
)
end
if runner.restart
UI.info "Puma restarted"
Compat::UI.info "Puma restarted"
if options[:notifications].include?(:restarted)
Notifier.notify("Puma restarted#{port_text}.", :title => "Puma restarted!", :image => :success)
Compat::UI.notify(
"Puma restarted#{port_text}.",
title: "Puma restarted!", image: :success
)
end
else
UI.info "Puma NOT restarted, check your log files."
Compat::UI.info "Puma NOT restarted, check your log files."
if options[:notifications].include?(:not_restarted)
Notifier.notify("Puma NOT restarted, check your log files.", :title => "Puma NOT restarted!", :image => :failed)
Compat::UI.notify(
"Puma NOT restarted, check your log files.",
title: "Puma NOT restarted!", image: :failed
)
end
end
end

def stop
if options[:notifications].include?(:stopped)
Notifier.notify("Until next time...", :title => "Puma shutting down.", :image => :pending)
Compat::UI.notify(
"Until next time...",
title: "Puma shutting down.", image: :pending
)
end
runner.halt
end
Expand Down
3 changes: 1 addition & 2 deletions lib/guard/puma/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ class PumaRunner

def initialize(options)
@control_token = options.delete(:control_token) { |_| ::Puma::Configuration.random_token }
@control = "localhost"
@control_port = (options.delete(:control_port) || '9293')
@control_url = "#{@control}:#{@control_port}"
@control_url = "localhost:#{@control_port}"
@quiet = options.delete(:quiet) { true }
@options = options

Expand Down
7 changes: 3 additions & 4 deletions spec/lib/guard/puma/runner_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
require 'spec_helper'
require 'guard/puma/runner'
require 'pry'

describe Guard::PumaRunner do
let(:runner) { Guard::PumaRunner.new(options) }
let(:environment) { 'development' }
let(:port) { 4000 }

let(:default_options) { { :environment => environment, :port => port } }
let(:default_options) { { environment: environment, port: port } }
let(:options) { default_options }

describe "#initialize" do
Expand All @@ -16,7 +15,7 @@
end
end

%w(halt restart).each do |cmd|
%w[halt restart].each do |cmd|
describe cmd do
before do
allow(runner).to receive(:build_uri).with(cmd).and_return(uri)
Expand All @@ -29,7 +28,7 @@
end
end

describe '#start' do
describe "#start" do
context "when on Windows" do
before do
allow(runner).to receive(:in_windows_cmd?).and_return(true)
Expand Down
72 changes: 37 additions & 35 deletions spec/lib/guard/puma_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,20 @@
end

describe '#start' do

context 'start on start' do
context "start on start" do
it "runs startup" do
expect(guard).to receive(:start).once
expect(guard.runner).to receive(:start).once
expect(Guard::Compat::UI).to receive(:info).with(/Puma starting/)
guard.start
end
end

context 'no start on start' do
let(:options) { { :start_on_start => false } }
context "no start on start" do
let(:options) { { start_on_start: false } }

it "shows the right message and not run startup" do
expect(guard.runner).to receive(:start).never
it "doesn't show the message and not run startup" do
expect(guard.runner).not_to receive(:start)
expect(Guard::Compat::UI).not_to receive(:info).with(/Puma starting/)
guard.start
end
end
Expand All @@ -81,110 +82,111 @@

context "when no config option set" do
it "contains port" do
expect(Guard::UI).to receive(:info).with(/starting on port 4000/)
expect(Guard::Compat::UI).to receive(:info)
.with(/starting on port 4000/)
guard.start
end
end

context "when config option set" do
let(:options) { { :config => 'config.rb' } }
let(:options) { { config: 'config.rb' } }

it "doesn't contain port" do
expect(Guard::UI).to receive(:info).with(/starting/)
expect(Guard::Compat::UI).to receive(:info).with(/starting/)
guard.start
end
end
end
end

describe '#reload' do

describe "#reload" do
before do
expect(Guard::UI).to receive(:info).with('Restarting Puma...')
expect(Guard::UI).to receive(:info).with('Puma restarted')
expect(Guard::Compat::UI).to receive(:info).with('Restarting Puma...')
expect(Guard::Compat::UI).to receive(:info).with('Puma restarted')
allow(guard.runner).to receive(:restart).and_return(true)
allow_any_instance_of(Guard::PumaRunner).to receive(:halt)
end

let(:runner_stub) { allow_any_instance_of(Guard::PumaRunner).to receive(:halt) }

context "with default options" do
it "restarts and show the message" do
expect(Guard::Notifier).to receive(:notify).with(
expect(Guard::Compat::UI).to receive(:notify).with(
/restarting on port 4000/,
hash_including(:title => "Restarting Puma...", :image => :pending)
hash_including(title: "Restarting Puma...", image: :pending)
)

expect(Guard::Notifier).to receive(:notify).with(
expect(Guard::Compat::UI).to receive(:notify).with(
"Puma restarted on port 4000.",
hash_including(:title => "Puma restarted!", :image => :success)
hash_including(title: "Puma restarted!", image: :success)
)

guard.reload
end
end

context "with config option set" do
let(:options) { { :config => "config.rb" } }
let(:options) { { config: "config.rb" } }

it "restarts and show the message" do
expect(Guard::Notifier).to receive(:notify).with(
expect(Guard::Compat::UI).to receive(:notify).with(
/restarting/,
hash_including(:title => "Restarting Puma...", :image => :pending)
hash_including(title: "Restarting Puma...", image: :pending)
)

expect(Guard::Notifier).to receive(:notify).with(
expect(Guard::Compat::UI).to receive(:notify).with(
"Puma restarted.",
hash_including(:title => "Puma restarted!", :image => :success)
hash_including(title: "Puma restarted!", image: :success)
)

guard.reload
end
end

context "with custom :notifications option" do
let(:options) { { :notifications => [:restarted] } }
let(:options) { { notifications: [:restarted] } }

it "restarts and show the message only about restarted" do
expect(Guard::Notifier).not_to receive(:notify).with(/restarting/)
expect(Guard::Notifier).to receive(:notify).with(/restarted/, kind_of(Hash))
expect(Guard::Compat::UI).not_to receive(:notify).with(/restarting/)
expect(Guard::Compat::UI).to receive(:notify)
.with(/restarted/, kind_of(Hash))

guard.reload
end
end

context "with empty :notifications option" do
let(:options) { { :notifications => [] } }
let(:options) { { notifications: [] } }

it "restarts and doesn't show the message" do
expect(Guard::Notifier).not_to receive(:notify)
expect(Guard::Compat::UI).not_to receive(:notify)

guard.reload
end
end

end

describe '#stop' do
describe "#stop" do
context "with default options" do
it "stops correctly with notification" do
expect(Guard::Notifier).to receive(:notify).with('Until next time...', anything)
expect(Guard::Compat::UI).to receive(:notify)
.with('Until next time...', anything)
expect(guard.runner).to receive(:halt).once
guard.stop
end
end

context "with custom :notifications option" do
let(:options) { { :notifications => [] } }
let(:options) { { notifications: [] } }

it "stops correctly without notification" do
expect(Guard::Notifier).not_to receive(:notify)
expect(Guard::Compat::UI).not_to receive(:notify)
expect(guard.runner).to receive(:halt).once
guard.stop
end
end
end

describe '#run_on_changes' do
describe "#run_on_changes" do
it "reloads on change" do
expect(guard).to receive(:reload).once
guard.run_on_changes([])
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
config.run_all_when_everything_filtered = true
config.filter_run :focus
config.mock_with :rspec
config.color = true
end