Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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"
45 changes: 29 additions & 16 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
```
28 changes: 25 additions & 3 deletions lib/shoulda/whenever/schedule_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -83,6 +96,7 @@ def with_roles(roles)
def description
[
base_description,
mailto_description,
duration_description,
time_description,
roles_description
Expand All @@ -92,6 +106,7 @@ def description
def failure_message
[
base_failure_message,
mailto_description,
duration_description,
time_description,
roles_description
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/shoulda/whenever/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Shoulda
module Whenever
VERSION = "0.0.2".freeze
VERSION = "0.0.3".freeze
end
end
7 changes: 4 additions & 3 deletions shoulda-whenever.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 31 additions & 0 deletions spec/shoulda/whenever/schedule_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down