diff --git a/Gemfile b/Gemfile index a1b8a69..5c91572 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,5 @@ source "https://rubygems.org" -gem "rspec", "~> 3.4.0" -gem "whenever", "~> 0.9.4" +gem "rspec", "~> 3.7" +gem "whenever", "~> 0.10" +gem "activesupport", "~> 5.2" diff --git a/Gemfile.lock b/Gemfile.lock index d03e4fd..e8cf640 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,30 +1,43 @@ GEM remote: https://rubygems.org/ specs: + activesupport (5.2.3) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 0.7, < 2) + minitest (~> 5.1) + tzinfo (~> 1.1) chronic (0.10.2) - diff-lcs (1.2.5) - rspec (3.4.0) - rspec-core (~> 3.4.0) - rspec-expectations (~> 3.4.0) - rspec-mocks (~> 3.4.0) - rspec-core (3.4.4) - rspec-support (~> 3.4.0) - rspec-expectations (3.4.0) + concurrent-ruby (1.1.5) + diff-lcs (1.3) + i18n (1.6.0) + concurrent-ruby (~> 1.0) + minitest (5.11.3) + rspec (3.7.0) + rspec-core (~> 3.7.0) + rspec-expectations (~> 3.7.0) + rspec-mocks (~> 3.7.0) + rspec-core (3.7.1) + rspec-support (~> 3.7.0) + rspec-expectations (3.7.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.4.0) - rspec-mocks (3.4.1) + rspec-support (~> 3.7.0) + rspec-mocks (3.7.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.4.0) - rspec-support (3.4.1) - whenever (0.9.4) + rspec-support (~> 3.7.0) + rspec-support (3.7.1) + thread_safe (0.3.6) + tzinfo (1.2.5) + thread_safe (~> 0.1) + whenever (0.10.0) chronic (>= 0.6.3) PLATFORMS ruby DEPENDENCIES - rspec (~> 3.4.0) - whenever (~> 0.9.4) + activesupport (~> 5.2) + rspec (~> 3.7) + whenever (~> 0.10) BUNDLED WITH - 1.11.2 + 1.17.3 diff --git a/README.md b/README.md index e1478b2..8774097 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ This gem was born out of a desire to test the schedule for tasks being scheduled Add this to you gemfile: ```ruby -gem "shoulda-whenever", "~> 0.0.2" +gem "shoulda-whenever" ``` Create a new schedule to be tested at `config/schedule.rb` (or anywhere really, but for the sake of the README, that's where it is): @@ -31,3 +31,37 @@ describe "Schedule" do end end ``` + +## Additional Filters + +### Mail To + +If you need to check the task output is sent to a specific email address. + +```ruby +every :monday, at: "07:00 AM", mailto: "info@test.com" do + runner "Notifier.send_good_morning_email" +end +``` + +```ruby +it "sends out good morning emails every monday at 7:00" do + expect(whenever).to schedule("Notifier.send_good_morning_email").every(:monday).at("07:00 AM").with_mailto("info@test.com") +end +``` + +### Roles + +If you need to check the task is set for specific roles. + +```ruby +every :monday, at: "07:00 AM", roles: [:web] do + runner "Notifier.send_good_morning_email" +end +``` + +```ruby +it "sends out good morning emails every monday at 7:00 on web servers" do + expect(whenever).to schedule("Notifier.send_good_morning_email").every(:monday).at("07:00 AM").with_roles([:web]) +end +``` diff --git a/lib/shoulda/whenever/schedule_matcher.rb b/lib/shoulda/whenever/schedule_matcher.rb index da44b08..40fcf37 100644 --- a/lib/shoulda/whenever/schedule_matcher.rb +++ b/lib/shoulda/whenever/schedule_matcher.rb @@ -9,17 +9,18 @@ def schedule(task) alias_method :schedule_command, :schedule class ScheduleMatcher - attr_reader :duration, :time, :task, :roles + attr_reader :duration, :time, :task, :roles, :mailto def initialize(task) @task = task @duration = nil @time = nil @roles = nil + @mailto = :default_mailto end def matches?(subject) - jobs = subject.instance_variable_get("@jobs") + jobs = get_jobs_by_mailto(subject) jobs = filter_jobs_by_duration(jobs) jobs = filter_jobs_by_time(jobs) @@ -29,11 +30,17 @@ def matches?(subject) jobs.any? end + def get_jobs_by_mailto(subject) + subject.instance_variable_get("@jobs")[@mailto] || {} + end + def filter_jobs_by_duration(jobs) if duration.nil? jobs.values.flatten else - duration_to_fetch = if duration.is_a?(String) || duration.is_a?(Symbol) + duration_to_fetch = if duration.is_a?(String) || + duration.is_a?(Symbol) || + duration.is_a?(ActiveSupport::Duration) duration else duration.to_i @@ -61,6 +68,12 @@ def filter_jobs_by_task(jobs) end end + def with_mailto(mailto) + @mailto = mailto + + self + end + def every(duration) @duration = duration @@ -83,6 +96,7 @@ def with_roles(roles) def description [ base_description, + mailto_description, duration_description, time_description, roles_description @@ -92,6 +106,7 @@ def description def failure_message [ base_failure_message, + mailto_description, duration_description, time_description, roles_description @@ -101,6 +116,7 @@ def failure_message def failure_message_when_negated [ base_failure_message_when_negated, + mailto_description, duration_description, time_description, roles_description @@ -113,6 +129,12 @@ def base_description "schedule \"#{ task }\"" end + def mailto_description + unless mailto == :default_mailto + "with_mailto \"#{ mailto }\"" + end + end + def duration_description unless duration.nil? if duration.is_a?(String) || duration.is_a?(Symbol) diff --git a/lib/shoulda/whenever/version.rb b/lib/shoulda/whenever/version.rb index e11b6ad..4c69b73 100644 --- a/lib/shoulda/whenever/version.rb +++ b/lib/shoulda/whenever/version.rb @@ -1,5 +1,5 @@ module Shoulda module Whenever - VERSION = "0.0.2".freeze + VERSION = "0.0.3".freeze end end diff --git a/shoulda-whenever.gemspec b/shoulda-whenever.gemspec index cc47705..07eba25 100644 --- a/shoulda-whenever.gemspec +++ b/shoulda-whenever.gemspec @@ -16,8 +16,9 @@ Gem::Specification.new do |s| s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.require_paths = ["lib"] - s.required_ruby_version = '>= 1.9.3' + s.required_ruby_version = '>= 2.3.8' - s.add_development_dependency "rspec", "~> 3.4", ">= 3.4.0" - s.add_development_dependency "whenever", "~> 0.9.4" + s.add_development_dependency "rspec", "~> 3.7" + s.add_development_dependency "whenever", "~> 0.10" + s.add_development_dependency "activesupport", "~> 5.2" end diff --git a/spec/shoulda/whenever/schedule_matcher_spec.rb b/spec/shoulda/whenever/schedule_matcher_spec.rb index 87698c5..a1c19cc 100644 --- a/spec/shoulda/whenever/schedule_matcher_spec.rb +++ b/spec/shoulda/whenever/schedule_matcher_spec.rb @@ -2,6 +2,7 @@ require "whenever" require "shoulda/whenever/schedule_matcher" require "rspec/matchers/fail_matchers" +require "active_support/duration" describe Shoulda::Whenever::ScheduleMatcher do include Shoulda::Whenever @@ -16,6 +17,17 @@ end end + context "with a mailto" do + it "includes the mailto under which the task is being scheduled" do + expect( + described_class.new("rake:every:10:minutes") + .every(Whenever::NumericSeconds.seconds(10, "minutes")) + .with_mailto('test@info.com') + .description + ).to eq("schedule \"rake:every:10:minutes\" with_mailto \"test@info.com\" every 600 seconds") + end + end + context "with a duration" do it "includes the duration at which the task is being scheduled" do expect( @@ -107,6 +119,25 @@ end end + context "a task that is scheduled with a certain mailto" do + let(:schedule_string) do + <<-SCHEDULE + every 3.hours, mailto: 'test@info.com' do + rake "rake:every:3:hours" + end + SCHEDULE + end + + it "passes" do + expect(whenever).to schedule("rake:every:3:hours").every(Whenever::NumericSeconds.seconds(3, "hours")).with_mailto('test@info.com') + end + it "fails" do + expect { + expect(whenever).not_to schedule("rake:every:3:hours").every(Whenever::NumericSeconds.seconds(3, "hours")).with_mailto('test@info.com') + }.to fail_with("expected not to schedule \"rake:every:3:hours\" with_mailto \"test@info.com\" every 10800 seconds") + end + end + context "a task that is scheduled after a certain duration" do let(:schedule_string) do <<-SCHEDULE