From 3a3b7e21d38de74cab090726fcf9e26fed0aea4f Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Mon, 8 Dec 2014 09:17:39 +0100 Subject: [PATCH 1/8] use Guard::Compat --- guard-livereload.gemspec | 1 + lib/guard/livereload.rb | 3 +-- lib/guard/livereload/reactor.rb | 16 ++++++++-------- spec/lib/guard/livereload/reactor_spec.rb | 16 +++++++++++----- spec/lib/guard/livereload_spec.rb | 2 ++ 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/guard-livereload.gemspec b/guard-livereload.gemspec index 68c88ae..4415c6a 100644 --- a/guard-livereload.gemspec +++ b/guard-livereload.gemspec @@ -18,6 +18,7 @@ Gem::Specification.new do |s| s.require_path = 'lib' s.add_dependency 'guard', '~> 2.8' + s.add_dependency 'guard-compat', '~> 1.0' s.add_dependency 'em-websocket', '~> 0.5' s.add_dependency 'multi_json', '~> 1.8' diff --git a/lib/guard/livereload.rb b/lib/guard/livereload.rb index 7fb6785..dac4750 100644 --- a/lib/guard/livereload.rb +++ b/lib/guard/livereload.rb @@ -1,5 +1,4 @@ -require 'guard' -require 'guard/plugin' +require 'guard/compat/plugin' module Guard class LiveReload < Plugin diff --git a/lib/guard/livereload/reactor.rb b/lib/guard/livereload/reactor.rb index 4db1933..921253f 100644 --- a/lib/guard/livereload/reactor.rb +++ b/lib/guard/livereload/reactor.rb @@ -18,14 +18,14 @@ def stop def reload_browser(paths = []) msg = "Reloading browser: #{paths.join(' ')}" - UI.info msg + Compat::UI.info msg if options[:notify] - Notifier.notify(msg, title: 'Reloading browser', image: :success) + Compat::UI.notify(msg, title: 'Reloading browser', image: :success) end paths.each do |path| data = _data(path) - UI.debug(data) + Compat::UI.debug(data) web_sockets.each { |ws| ws.send(MultiJson.encode(data)) } end end @@ -52,13 +52,13 @@ def _start_reactor ws.onclose { _disconnect(ws) } ws.onmessage { |msg| _print_message(msg) } end - UI.info "LiveReload is waiting for a browser to connect." + Compat::UI.info "LiveReload is waiting for a browser to connect." end end def _connect(ws) @connections_count += 1 - UI.info "Browser connected." if connections_count == 1 + Compat::UI.info "Browser connected." if connections_count == 1 ws.send MultiJson.encode( command: 'hello', @@ -67,8 +67,8 @@ def _connect(ws) ) @web_sockets << ws rescue - UI.error $! - UI.error $!.backtrace + Compat::UI.error $! + Compat::UI.error $!.backtrace end def _disconnect(ws) @@ -77,7 +77,7 @@ def _disconnect(ws) def _print_message(message) message = MultiJson.decode(message) - UI.info "Browser URL: #{message['url']}" if message['command'] == 'url' + Compat::UI.info "Browser URL: #{message['url']}" if message['command'] == 'url' end end diff --git a/spec/lib/guard/livereload/reactor_spec.rb b/spec/lib/guard/livereload/reactor_spec.rb index 4c0abc0..f88898d 100644 --- a/spec/lib/guard/livereload/reactor_spec.rb +++ b/spec/lib/guard/livereload/reactor_spec.rb @@ -2,22 +2,28 @@ describe Guard::LiveReload::Reactor do let(:paths) { %w[stylesheets/layout.css stylesheets/style.css] } - before { allow(Guard::UI).to receive(:info) } + + before do + allow(Guard::Compat::UI).to receive(:info) + allow(Guard::Compat::UI).to receive(:debug) + allow(Guard::Compat::UI).to receive(:error) + allow(Guard::Compat::UI).to receive(:warning) + end describe "#reload_browser(paths = [])" do it "displays a message" do - expect(Guard::UI).to receive(:info). + expect(Guard::Compat::UI).to receive(:info). with('Reloading browser: stylesheets/layout.css stylesheets/style.css') new_live_reactor.reload_browser(paths) end it 'by default does not send notification' do - expect(::Guard::Notifier).to_not receive(:notify) + expect(Guard::Compat::UI).to_not receive(:notify) new_live_reactor.reload_browser(paths) end it 'optionally pushes notification' do - expect(::Guard::Notifier).to receive(:notify). + expect(Guard::Compat::UI).to receive(:notify). with(kind_of(String), have_key(:title)) new_live_reactor(notify: true).reload_browser(paths) end @@ -48,7 +54,7 @@ let(:reactor) { new_live_reactor } it "displays a message once" do - expect(Guard::UI).to receive(:info).with("Browser connected.").once + expect(Guard::Compat::UI).to receive(:info).with("Browser connected.").once reactor.send(:_connect, ws) reactor.send(:_connect, ws) end diff --git a/spec/lib/guard/livereload_spec.rb b/spec/lib/guard/livereload_spec.rb index 3a6ed96..72b5249 100644 --- a/spec/lib/guard/livereload_spec.rb +++ b/spec/lib/guard/livereload_spec.rb @@ -1,5 +1,7 @@ require 'spec_helper' +require "guard/compat/test/helper" + describe Guard::LiveReload do let(:plugin) { Guard::LiveReload.new } let(:reactor) { double(Guard::LiveReload::Reactor) } From bd80ddde5eac6641574739955a4b8154c57d9cfc Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Mon, 8 Dec 2014 09:17:57 +0100 Subject: [PATCH 2/8] update to RSpec 3.1 --- .rspec | 2 ++ guard-livereload.gemspec | 2 +- spec/lib/guard/livereload/reactor_spec.rb | 4 +--- spec/lib/guard/livereload_spec.rb | 4 +--- spec/spec_helper.rb | 29 ++++++++++++++++++++--- 5 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 .rspec diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..83e16f8 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--require spec_helper diff --git a/guard-livereload.gemspec b/guard-livereload.gemspec index 4415c6a..13b405c 100644 --- a/guard-livereload.gemspec +++ b/guard-livereload.gemspec @@ -24,5 +24,5 @@ Gem::Specification.new do |s| s.add_development_dependency 'bundler', '>= 1.3.5' s.add_development_dependency 'rake' - s.add_development_dependency 'rspec' + s.add_development_dependency 'rspec', '~> 3.1' end diff --git a/spec/lib/guard/livereload/reactor_spec.rb b/spec/lib/guard/livereload/reactor_spec.rb index f88898d..4cf43a3 100644 --- a/spec/lib/guard/livereload/reactor_spec.rb +++ b/spec/lib/guard/livereload/reactor_spec.rb @@ -1,6 +1,4 @@ -require 'spec_helper' - -describe Guard::LiveReload::Reactor do +RSpec.describe Guard::LiveReload::Reactor do let(:paths) { %w[stylesheets/layout.css stylesheets/style.css] } before do diff --git a/spec/lib/guard/livereload_spec.rb b/spec/lib/guard/livereload_spec.rb index 72b5249..0a23254 100644 --- a/spec/lib/guard/livereload_spec.rb +++ b/spec/lib/guard/livereload_spec.rb @@ -1,8 +1,6 @@ -require 'spec_helper' - require "guard/compat/test/helper" -describe Guard::LiveReload do +RSpec.describe Guard::LiveReload do let(:plugin) { Guard::LiveReload.new } let(:reactor) { double(Guard::LiveReload::Reactor) } before { allow(plugin).to receive(:reactor) { reactor } } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7771fe5..075466f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,10 +3,33 @@ require 'rspec' require 'guard/livereload' -ENV["GUARD_ENV"] = 'test' RSpec.configure do |config| - config.color = true - config.filter_run focus: true + + def ci? + ENV['CI'] == 'true' + end + + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true + end + + config.filter_run focus: !ci? config.run_all_when_everything_filtered = true + + config.disable_monkey_patching! + + config.warnings = true + + config.default_formatter = 'doc' if config.files_to_run.one? + + #config.profile_examples = 10 + + config.order = :random + + Kernel.srand config.seed end From c57f3074af0abfa8a030557574364122a36cc8fa Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Tue, 9 Dec 2014 05:22:33 +0100 Subject: [PATCH 3/8] do not require gems in Gemfile --- Gemfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 43b3712..2651f7f 100644 --- a/Gemfile +++ b/Gemfile @@ -3,10 +3,10 @@ source "https://rubygems.org" gemspec group :development do - gem 'ruby_gntp' - gem 'guard-rspec' + gem "ruby_gntp", require: false + gem "guard-rspec", require: false end group :test do - gem 'coveralls', require: false + gem "coveralls", require: false end From f66c465bea011bb28b29114a72b77c105265a518 Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Tue, 9 Dec 2014 05:23:07 +0100 Subject: [PATCH 4/8] use halt_on_fail in Guardfile --- Guardfile | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Guardfile b/Guardfile index 04ac1da..aecb1fc 100644 --- a/Guardfile +++ b/Guardfile @@ -1,5 +1,13 @@ -guard :rspec do - watch(%r{spec/.+_spec.rb}) - watch(%r{lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" } - watch('spec/spec_helper.rb') { "spec" } +group :specs, halt_on_fail: true do + guard :rspec, cmd: "bundle exec rspec", failed_mode: :keep do + watch(%r{spec/.+_spec.rb}) + watch(%r{lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" } + watch("spec/spec_helper.rb") { "spec" } + end + + guard :rubocop, all_on_start: false, cli: "--rails" do + watch(%r{.+\.rb$}) { |m| m[0] } + watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) } + watch(%r{(?:.+/)?\.rubocop_todo\.yml$}) { |m| File.dirname(m[0]) } + end end From a78da8c029d0d4c00e2ca1811c2b504d1f5e0adf Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Tue, 9 Dec 2014 05:22:42 +0100 Subject: [PATCH 5/8] add hound-tools --- Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile b/Gemfile index 2651f7f..e62a846 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ gemspec group :development do gem "ruby_gntp", require: false gem "guard-rspec", require: false + gem "hound-tools", '~> 0.0.4', require: false end group :test do From b74f1286100762391aec0e9db9f3354a99fe224c Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Tue, 9 Dec 2014 05:25:41 +0100 Subject: [PATCH 6/8] add RuboCop to Rakefile --- Rakefile | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Rakefile b/Rakefile index cea94d9..423596a 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,21 @@ require "bundler/gem_tasks" -require 'rspec/core/rake_task' -RSpec::Core::RakeTask.new(:spec) -task :default => :spec +require "rspec/core/rake_task" + +def ci? + ENV["CI"] == "true" +end + +RSpec::Core::RakeTask.new(:spec) do |t| + t.verbose = ci? +end + +default_tasks = [:spec] + +unless ci? + require "rubocop/rake_task" + RuboCop::RakeTask.new(:rubocop) + default_tasks << :rubocop +end + +task default: default_tasks From c5b67859d08065152d53936b6b03db3d36470806 Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Tue, 9 Dec 2014 05:29:54 +0100 Subject: [PATCH 7/8] RuboCop setup + fix offenses --- .hound.yml | 6 + .hound/defaults.yml | 265 ++++++++++++++++++++++ .hound/overrides.yml | 19 ++ .rubocop.yml | 8 + .rubocop_merged_for_hound.yml | 41 ++++ .rubocop_todo.yml | 11 + Guardfile | 6 +- guard-livereload.gemspec | 4 +- lib/guard/livereload.rb | 1 - lib/guard/livereload/reactor.rb | 7 +- lib/guard/livereload/websocket.rb | 32 ++- spec/lib/guard/livereload/reactor_spec.rb | 20 +- spec/lib/guard/livereload_spec.rb | 42 ++-- spec/spec_helper.rb | 2 +- 14 files changed, 412 insertions(+), 52 deletions(-) create mode 100644 .hound.yml create mode 100644 .hound/defaults.yml create mode 100644 .hound/overrides.yml create mode 100644 .rubocop.yml create mode 100644 .rubocop_merged_for_hound.yml create mode 100644 .rubocop_todo.yml diff --git a/.hound.yml b/.hound.yml new file mode 100644 index 0000000..d48feea --- /dev/null +++ b/.hound.yml @@ -0,0 +1,6 @@ +ruby: + enabled: true + config_file: .rubocop_merged_for_hound.yml + +coffee_script: + enabled: true diff --git a/.hound/defaults.yml b/.hound/defaults.yml new file mode 100644 index 0000000..23410af --- /dev/null +++ b/.hound/defaults.yml @@ -0,0 +1,265 @@ +# +# NOTE: * DO NOT EDIT THIS FILE! * +# +# This file should match the config/style_guides/ruby.yml from the +# thoughtbot/hound project + +AllCops: + Exclude: + - db/schema.rb + +AccessorMethodName: + Enabled: false + +ActionFilter: + Enabled: false + +Alias: + Enabled: false + +ArrayJoin: + Enabled: false + +AsciiComments: + Enabled: false + +AsciiIdentifiers: + Enabled: false + +Attr: + Enabled: false + +BlockNesting: + Enabled: false + +CaseEquality: + Enabled: false + +CharacterLiteral: + Enabled: false + +ClassAndModuleChildren: + Enabled: false + +ClassLength: + Enabled: false + +ClassVars: + Enabled: false + +CollectionMethods: + PreferredMethods: + find: detect + reduce: inject + collect: map + find_all: select + +ColonMethodCall: + Enabled: false + +CommentAnnotation: + Enabled: false + +CyclomaticComplexity: + Enabled: false + +Delegate: + Enabled: false + +DeprecatedHashMethods: + Enabled: false + +Documentation: + Enabled: false + +DotPosition: + EnforcedStyle: trailing + +DoubleNegation: + Enabled: false + +EachWithObject: + Enabled: false + +EmptyLiteral: + Enabled: false + +Encoding: + Enabled: false + +EvenOdd: + Enabled: false + +FileName: + Enabled: false + +FlipFlop: + Enabled: false + +FormatString: + Enabled: false + +GlobalVars: + Enabled: false + +GuardClause: + Enabled: false + +IfUnlessModifier: + Enabled: false + +IfWithSemicolon: + Enabled: false + +InlineComment: + Enabled: false + +Lambda: + Enabled: false + +LambdaCall: + Enabled: false + +LineEndConcatenation: + Enabled: false + +LineLength: + Max: 80 + +MethodLength: + Enabled: false + +ModuleFunction: + Enabled: false + +NegatedIf: + Enabled: false + +NegatedWhile: + Enabled: false + +Next: + Enabled: false + +NilComparison: + Enabled: false + +Not: + Enabled: false + +NumericLiterals: + Enabled: false + +OneLineConditional: + Enabled: false + +OpMethod: + Enabled: false + +ParameterLists: + Enabled: false + +PercentLiteralDelimiters: + Enabled: false + +PerlBackrefs: + Enabled: false + +PredicateName: + NamePrefixBlacklist: + - is_ + +Proc: + Enabled: false + +RaiseArgs: + Enabled: false + +RegexpLiteral: + Enabled: false + +SelfAssignment: + Enabled: false + +SingleLineBlockParams: + Enabled: false + +SingleLineMethods: + Enabled: false + +SignalException: + Enabled: false + +SpecialGlobalVars: + Enabled: false + +StringLiterals: + EnforcedStyle: double_quotes + +VariableInterpolation: + Enabled: false + +TrailingComma: + Enabled: false + +TrivialAccessors: + Enabled: false + +VariableInterpolation: + Enabled: false + +WhenThen: + Enabled: false + +WhileUntilModifier: + Enabled: false + +WordArray: + Enabled: false + +# Lint + +AmbiguousOperator: + Enabled: false + +AmbiguousRegexpLiteral: + Enabled: false + +AssignmentInCondition: + Enabled: false + +ConditionPosition: + Enabled: false + +DeprecatedClassMethods: + Enabled: false + +ElseLayout: + Enabled: false + +HandleExceptions: + Enabled: false + +InvalidCharacterLiteral: + Enabled: false + +LiteralInCondition: + Enabled: false + +LiteralInInterpolation: + Enabled: false + +Loop: + Enabled: false + +ParenthesesAsGroupedExpression: + Enabled: false + +RequireParentheses: + Enabled: false + +UnderscorePrefixedVariableName: + Enabled: false + +Void: + Enabled: false diff --git a/.hound/overrides.yml b/.hound/overrides.yml new file mode 100644 index 0000000..d570713 --- /dev/null +++ b/.hound/overrides.yml @@ -0,0 +1,19 @@ +# NOTE: DO NOT put a 'inheried_from' in this file, because Hound with silently +# ignore it +# +# Files you want to exclude +AllCops: + # Rails cops (so we don't need to run with -R option) + RunRailsCops: true + Exclude: + - db/schema.rb + - Gemfile + - Rakefile + +# TODO: put your overrides here: + +StringLiterals: + EnforcedStyle: single_quotes +# +# LineLength: +# Max: 200 diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..cae2041 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,8 @@ +inherit_from: + - .hound/defaults.yml + - .hound/overrides.yml + - .rubocop_todo.yml + +# Rails cops (so we don't need to run with -R option) +AllCops: + RunRailsCops: true diff --git a/.rubocop_merged_for_hound.yml b/.rubocop_merged_for_hound.yml new file mode 100644 index 0000000..02ea994 --- /dev/null +++ b/.rubocop_merged_for_hound.yml @@ -0,0 +1,41 @@ +# This is a file generated by `hound-tools` +# +# We don't include .hound/defaults.yml, because Hound has internally +# loaded them at this point +# +# --------------------------------- +# .hound/overrides.yml +# --------------------------------- +# NOTE: DO NOT put a 'inheried_from' in this file, because Hound with silently +# ignore it +# +# Files you want to exclude +AllCops: + # Rails cops (so we don't need to run with -R option) + RunRailsCops: true + Exclude: + - db/schema.rb + - Gemfile + - Rakefile + +# TODO: put your overrides here: + +StringLiterals: + EnforcedStyle: single_quotes +# +# LineLength: +# Max: 200 +# --------------------------------- +# .rubocop_todo.yml +# --------------------------------- +# This configuration was generated by `rubocop --auto-gen-config` +# on 2014-12-09 05:16:56 +0100 using RuboCop version 0.25.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 12 +# Configuration parameters: AllowURI. +Metrics/LineLength: + Max: 163 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..21dc8d3 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,11 @@ +# This configuration was generated by `rubocop --auto-gen-config` +# on 2014-12-09 05:16:56 +0100 using RuboCop version 0.25.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 12 +# Configuration parameters: AllowURI. +Metrics/LineLength: + Max: 163 diff --git a/Guardfile b/Guardfile index aecb1fc..bb508bd 100644 --- a/Guardfile +++ b/Guardfile @@ -1,11 +1,11 @@ group :specs, halt_on_fail: true do - guard :rspec, cmd: "bundle exec rspec", failed_mode: :keep do + guard :rspec, cmd: 'bundle exec rspec', failed_mode: :keep do watch(%r{spec/.+_spec.rb}) watch(%r{lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" } - watch("spec/spec_helper.rb") { "spec" } + watch('spec/spec_helper.rb') { 'spec' } end - guard :rubocop, all_on_start: false, cli: "--rails" do + guard :rubocop, all_on_start: false, cli: '--rails' do watch(%r{.+\.rb$}) { |m| m[0] } watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) } watch(%r{(?:.+/)?\.rubocop_todo\.yml$}) { |m| File.dirname(m[0]) } diff --git a/guard-livereload.gemspec b/guard-livereload.gemspec index 13b405c..67f3164 100644 --- a/guard-livereload.gemspec +++ b/guard-livereload.gemspec @@ -4,14 +4,14 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'guard/livereload/version' Gem::Specification.new do |s| - s.name = "guard-livereload" + s.name = 'guard-livereload' s.version = Guard::LiveReloadVersion::VERSION s.author = 'Thibaud Guillaume-Gentil' s.email = 'thibaud@thibaud.me' s.summary = 'Guard plugin for livereload' s.description = "Guard::LiveReload automatically reloads your browser when 'view' files are modified." s.homepage = 'https://rubygems.org/gems/guard-livereload' - s.license = "MIT" + s.license = 'MIT' s.files = `git ls-files`.split($/) s.test_files = s.files.grep(%r{^spec/}) diff --git a/lib/guard/livereload.rb b/lib/guard/livereload.rb index dac4750..449ae06 100644 --- a/lib/guard/livereload.rb +++ b/lib/guard/livereload.rb @@ -30,6 +30,5 @@ def run_on_modifications(paths) sleep options[:grace_period] reactor.reload_browser(paths) end - end end diff --git a/lib/guard/livereload/reactor.rb b/lib/guard/livereload/reactor.rb index 921253f..1b83181 100644 --- a/lib/guard/livereload/reactor.rb +++ b/lib/guard/livereload/reactor.rb @@ -30,7 +30,7 @@ def reload_browser(paths = []) end end - private + private def _data(path) data = { @@ -52,13 +52,13 @@ def _start_reactor ws.onclose { _disconnect(ws) } ws.onmessage { |msg| _print_message(msg) } end - Compat::UI.info "LiveReload is waiting for a browser to connect." + Compat::UI.info 'LiveReload is waiting for a browser to connect.' end end def _connect(ws) @connections_count += 1 - Compat::UI.info "Browser connected." if connections_count == 1 + Compat::UI.info 'Browser connected.' if connections_count == 1 ws.send MultiJson.encode( command: 'hello', @@ -79,7 +79,6 @@ def _print_message(message) message = MultiJson.decode(message) Compat::UI.info "Browser URL: #{message['url']}" if message['command'] == 'url' end - end end end diff --git a/lib/guard/livereload/websocket.rb b/lib/guard/livereload/websocket.rb index 5a5a711..620e2e6 100644 --- a/lib/guard/livereload/websocket.rb +++ b/lib/guard/livereload/websocket.rb @@ -6,7 +6,6 @@ module Guard class LiveReload class WebSocket < EventMachine::WebSocket::Connection - def dispatch(data) parser = Http::Parser.new parser << data @@ -14,14 +13,9 @@ def dispatch(data) request_path = '.' + URI.parse(parser.request_url).path request_path += '/index.html' if File.directory? request_path if parser.http_method != 'GET' || parser.upgrade? - super #pass the request to websocket - elsif request_path == './livereload.js' - _serve_file(_livereload_js_file) - elsif File.readable?(request_path) && !File.directory?(request_path) - _serve_file(request_path) + super # pass the request to websocket else - send_data("HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\n404 Not Found") - close_connection_after_writing + _serve(request_path) end end @@ -29,7 +23,15 @@ def dispatch(data) def _serve_file(path) UI.debug "Serving file #{path}" - send_data "HTTP/1.1 200 OK\r\nContent-Type: #{_content_type(path)}\r\nContent-Length: #{File.size path}\r\n\r\n" + + data = [ + 'HTTP/1.1 200 OK', + 'Content-Type: %s', + 'Content-Length: %s', + '', + ''] + data = format(data * "\r\n", _content_type(path), File.size(path)) + send_data(data) stream_file_data(path).callback { close_connection_after_writing } end @@ -46,9 +48,19 @@ def _content_type(path) end def _livereload_js_file - File.expand_path("../../../../js/livereload.js", __FILE__) + File.expand_path('../../../../js/livereload.js', __FILE__) end + def _serve(path) + return _serve_file(_livereload_js_file) if path == './livereload.js' + return _serve_file(path) if _readable_file(path) + send_data("HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\n404 Not Found") + close_connection_after_writing + end + + def _readable_file(path) + File.readable?(path) && !File.directory?(path) + end end end end diff --git a/spec/lib/guard/livereload/reactor_spec.rb b/spec/lib/guard/livereload/reactor_spec.rb index 4cf43a3..a48aed9 100644 --- a/spec/lib/guard/livereload/reactor_spec.rb +++ b/spec/lib/guard/livereload/reactor_spec.rb @@ -8,8 +8,8 @@ allow(Guard::Compat::UI).to receive(:warning) end - describe "#reload_browser(paths = [])" do - it "displays a message" do + describe '#reload_browser(paths = [])' do + it 'displays a message' do expect(Guard::Compat::UI).to receive(:info). with('Reloading browser: stylesheets/layout.css stylesheets/style.css') new_live_reactor.reload_browser(paths) @@ -26,7 +26,7 @@ new_live_reactor(notify: true).reload_browser(paths) end - it "each web socket receives send with data containing default options for each path modified" do + it 'each web socket receives send with data containing default options for each path modified' do reactor = new_live_reactor paths.each do |path| reactor.web_sockets.each do |ws| @@ -36,7 +36,7 @@ reactor.reload_browser(paths) end - it "each web socket receives send with data containing custom options for each path modified" do + it 'each web socket receives send with data containing custom options for each path modified' do reactor = new_live_reactor(apply_css_live: false, apply_js_live: false) paths.each do |path| reactor.web_sockets.each do |ws| @@ -47,20 +47,20 @@ end end - describe "#_connect(ws)" do + describe '#_connect(ws)' do let(:ws) { double.as_null_object } let(:reactor) { new_live_reactor } - it "displays a message once" do - expect(Guard::Compat::UI).to receive(:info).with("Browser connected.").once + it 'displays a message once' do + expect(Guard::Compat::UI).to receive(:info).with('Browser connected.').once reactor.send(:_connect, ws) reactor.send(:_connect, ws) end - it "increments the connection count" do - expect { + it 'increments the connection count' do + expect do reactor.send(:_connect, ws) - }.to change { reactor.connections_count }.from(0).to 1 + end.to change { reactor.connections_count }.from(0).to 1 end end diff --git a/spec/lib/guard/livereload_spec.rb b/spec/lib/guard/livereload_spec.rb index 0a23254..efbdbba 100644 --- a/spec/lib/guard/livereload_spec.rb +++ b/spec/lib/guard/livereload_spec.rb @@ -1,12 +1,12 @@ -require "guard/compat/test/helper" +require 'guard/compat/test/helper' RSpec.describe Guard::LiveReload do let(:plugin) { Guard::LiveReload.new } let(:reactor) { double(Guard::LiveReload::Reactor) } before { allow(plugin).to receive(:reactor) { reactor } } - describe "#initialize" do - describe ":host option" do + describe '#initialize' do + describe ':host option' do it "is '0.0.0.0' by default" do plugin = Guard::LiveReload.new expect(plugin.options[:host]).to eq '0.0.0.0' @@ -18,7 +18,7 @@ end end - describe ":port option" do + describe ':port option' do it "is '35729' by default" do plugin = Guard::LiveReload.new expect(plugin.options[:port]).to eq '35729' @@ -30,45 +30,45 @@ end end - describe ":apply_css_live option" do - it "is true by default" do + describe ':apply_css_live option' do + it 'is true by default' do plugin = Guard::LiveReload.new expect(plugin.options[:apply_css_live]).to be_truthy end - it "can be set to false" do + it 'can be set to false' do plugin = Guard::LiveReload.new(apply_css_live: false) expect(plugin.options[:apply_css_live]).to be_falsey end end - describe ":override_url option" do - it "is false by default" do + describe ':override_url option' do + it 'is false by default' do plugin = Guard::LiveReload.new expect(plugin.options[:override_url]).to be_falsey end - it "can be set to false" do + it 'can be set to false' do plugin = Guard::LiveReload.new(override_url: true) expect(plugin.options[:override_url]).to be_truthy end end - describe ":grace_period option" do - it "is 0 by default" do + describe ':grace_period option' do + it 'is 0 by default' do plugin = Guard::LiveReload.new expect(plugin.options[:grace_period]).to eq 0 end - it "can be set to 0.5" do + it 'can be set to 0.5' do plugin = Guard::LiveReload.new(grace_period: 0.5) expect(plugin.options[:grace_period]).to eq 0.5 end end end - describe "#start" do - it "creates reactor with default options" do + describe '#start' do + it 'creates reactor with default options' do plugin = Guard::LiveReload.new expect(Guard::LiveReload::Reactor).to receive(:new).with( host: '0.0.0.0', @@ -80,7 +80,7 @@ plugin.start end - it "creates reactor with given options" do + it 'creates reactor with given options' do plugin = Guard::LiveReload.new(host: '127.3.3.1', port: '12345', apply_css_live: false, override_url: true, grace_period: 1) expect(Guard::LiveReload::Reactor).to receive(:new).with( host: '127.3.3.1', @@ -93,20 +93,20 @@ end end - describe "#stop" do - it "stops the reactor" do + describe '#stop' do + it 'stops the reactor' do expect(reactor).to receive(:stop) plugin.stop end end - describe "#run_on_modifications" do - it "reloads browser" do + describe '#run_on_modifications' do + it 'reloads browser' do expect(reactor).to receive(:reload_browser).with(['foo']) plugin.run_on_modifications(['foo']) end - it "can wait 0.5 seconds before reloading the browser" do + it 'can wait 0.5 seconds before reloading the browser' do expect(reactor).to receive(:reload_browser).with(['foo']) expect(plugin).to receive(:sleep).with(0.5) plugin.options[:grace_period] = 0.5 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 075466f..bde0105 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,7 +27,7 @@ def ci? config.default_formatter = 'doc' if config.files_to_run.one? - #config.profile_examples = 10 + # config.profile_examples = 10 config.order = :random From fee7448d6139537be696481bbe104d8c40adb71f Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Tue, 9 Dec 2014 05:42:46 +0100 Subject: [PATCH 8/8] fix gem deps for Travis --- Gemfile | 2 ++ guard-livereload.gemspec | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index e62a846..ab7899c 100644 --- a/Gemfile +++ b/Gemfile @@ -10,4 +10,6 @@ end group :test do gem "coveralls", require: false + gem "rake", require: false + gem 'rspec', '~> 3.1' end diff --git a/guard-livereload.gemspec b/guard-livereload.gemspec index 67f3164..363f549 100644 --- a/guard-livereload.gemspec +++ b/guard-livereload.gemspec @@ -23,6 +23,4 @@ Gem::Specification.new do |s| s.add_dependency 'multi_json', '~> 1.8' s.add_development_dependency 'bundler', '>= 1.3.5' - s.add_development_dependency 'rake' - s.add_development_dependency 'rspec', '~> 3.1' end