whenever-test 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8e58e9d99279233e6a8275a597c0027ad970833c
4
+ data.tar.gz: 3e1c0a686c3c37d8ce9624fbed598330c6848840
5
+ SHA512:
6
+ metadata.gz: ab551e5513e11c13624fe18334f5f26ef31b06546fcba4e39e4cfdffbf1ee41a27322af8c80bdd8f72fe5672134753d32d8c3eddc15d38dcb8921af707aedf53
7
+ data.tar.gz: 8e6b25ec2930806c7073e8c4df9ddec90162d963ef2c98c853ccab87a5f95798f2b6927d7a8331b5370507ad68ce3d948b27d6a6f00766c94b3df64a6775ea18
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem "bundler"
7
+ gem "rake"
8
+ gem "pry-byebug"
9
+ end
10
+
11
+ group :test do
12
+ gem "minitest"
13
+ gem "mocha", require: false
14
+ end
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ whenever-test (1.0.0)
5
+ whenever
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ byebug (4.0.5)
11
+ columnize (= 0.9.0)
12
+ chronic (0.10.2)
13
+ coderay (1.1.0)
14
+ columnize (0.9.0)
15
+ metaclass (0.0.4)
16
+ method_source (0.8.2)
17
+ minitest (5.7.0)
18
+ mocha (0.14.0)
19
+ metaclass (~> 0.0.1)
20
+ pry (0.10.1)
21
+ coderay (~> 1.1.0)
22
+ method_source (~> 0.8.1)
23
+ slop (~> 3.4)
24
+ pry-byebug (3.1.0)
25
+ byebug (~> 4.0)
26
+ pry (~> 0.10)
27
+ rake (10.4.2)
28
+ slop (3.6.0)
29
+ whenever (0.9.4)
30
+ chronic (>= 0.6.3)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ bundler
37
+ minitest
38
+ mocha
39
+ pry-byebug
40
+ rake
41
+ whenever-test!
42
+
43
+ BUNDLED WITH
44
+ 1.10.4
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Heart Bits
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,107 @@
1
+ ### Whenever::Test
2
+
3
+ [![Gem Downloads](http://img.shields.io/gem/dt/whenever-test.svg)](https://rubygems.org/gems/whenever-test)
4
+ [![Build Status](https://snap-ci.com/heartbits/whenever-test/branch/master/build_image)](https://snap-ci.com/heartbits/whenever-test/branch/master)
5
+ [![Code Climate](https://codeclimate.com/github/heartbits/whenever-test/badges/gpa.svg)](https://codeclimate.com/github/heartbits/whenever-test)
6
+ [![GitHub Issues](https://img.shields.io/github/issues/heartbits/whenever-test.svg)](https://github.com/heartbits/whenever-test/issues)
7
+ [![GitHub License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/heartbits/whenever-test)
8
+
9
+ A gem that adds test support to [Whenever](https://github.com/javan/whenever) gem.
10
+
11
+ ### Background
12
+
13
+ I've been part of many projects that used whenever gem, but testing the `schedule.rb` file was always neglected.
14
+
15
+ Turns out your jobs defined in Whenever schedule might reference rake tasks that don't exist in runtime and not even have correct ruby syntax (in case or runner-type jobs).
16
+
17
+ This gem adds a support class so you can write specs that asserts against definitions in the `schedule.rb` file. To make sure ruby statements referenced in runner-type jobs actually work, you can `instance_eval` them and write expectations on what should happen, and then you'll be sure cron jobs won't have runtime issues that are detected only in staging or production environments.
18
+
19
+ NOTE: *This gem is test-framework agnostic, so you can use with RSpec, MiniTest, ...*
20
+
21
+ ### Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ group :test do
27
+ gem 'whenever-test'
28
+ end
29
+ ```
30
+
31
+ Available in RubyGems: https://rubygems.org/gems/whenever-test
32
+
33
+ ### Usage
34
+
35
+ Suppose you have a schedule such as:
36
+
37
+ ```ruby
38
+ # config/schedule.rb
39
+ job_type :curl, 'curl :task'
40
+
41
+ every :hour { runner 'TimeoutOffers.perform_async' }
42
+ every :minute { curl 'http://myapp.com/cron-alive' }
43
+
44
+ if @environment == 'staging'
45
+ every :day { rake 'db_sync:import' }
46
+ end
47
+ ```
48
+
49
+ You can write a spec such as (Minitest was used in this example):
50
+
51
+ ```ruby
52
+ # spec/whenever_spec.rb
53
+ require 'spec_helper'
54
+
55
+ describe 'Whenever Schedule' do
56
+ before do
57
+ load 'Rakefile' # Makes sure rake tasks are loaded so you can assert in rake jobs
58
+ end
59
+
60
+ it 'makes sure `runner` statements exist' do
61
+ schedule = Whenever::Test::Schedule.new(file: 'config/schedule.rb')
62
+
63
+ assert_equal 2, schedule.jobs[:runner].count
64
+
65
+ # Executes the actual ruby statement to make sure all constants and methods exist:
66
+ schedule.jobs[:runner].each { |job| instance_eval job[:task] }
67
+ end
68
+
69
+ it 'makes sure `rake` statements exist' do
70
+ # config/schedule.rb file is used by default in constructor:
71
+ schedule = Whenever::Test::Schedule.new(vars: { environment: 'staging' })
72
+
73
+ # Makes sure the rake task is defined:
74
+ assert Rake::Task.task_defined?(schedule.jobs[:rake].first[:task])
75
+ end
76
+
77
+ it 'makes sure cron alive monitor is registered in minute basis' do
78
+ schedule = Whenever::Test::Schedule.new(file: fixture)
79
+
80
+ assert_equal 'http://myapp.com/cron-alive', schedule.jobs[:curl].first[:task]
81
+ assert_equal 'curl :task', schedule.jobs[:ping].first[:command]
82
+ assert_equal [:minute], schedule.jobs[:ping].first[:every]
83
+ end
84
+ end
85
+ ```
86
+
87
+ **Now the important part:** these specs guarantee that:
88
+
89
+ 1. If `TimeoutOffers` constant is not defined or `TimeoutOffers.perform_async` methods doesn't exist, an error will raise
90
+ 2. If Rake task `db_sync:import` doesn't exist, an error will raise
91
+ 3. You should have a custom task named `curl` to make sure that job will work
92
+
93
+ ### How it works
94
+
95
+ This gem implements a class that has the same DSL interface as Whenever gem. It basically runs the `schedule.rb` file against the `Whenever::Test::DSLInterpreter` and stores all statements and parameters found so you can easily query them in your tests.
96
+
97
+ ### Contributors
98
+
99
+ * Rafael Sales [@rafaelsales](https://github.com/rafaelsales)
100
+
101
+ ### Contributing
102
+
103
+ 1. Fork it ( https://github.com/heartbits/whenever-test/fork )
104
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
105
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
106
+ 4. Push to the branch (`git push origin my-new-feature`)
107
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs += ["spec", "lib"]
6
+ t.test_files = FileList['spec/**/*_spec.rb']
7
+ end
@@ -0,0 +1,3 @@
1
+ require 'whenever'
2
+
3
+ Dir.glob(File.join(File.dirname(__FILE__), "/test/**/*.rb")).sort.each { |f| require f }
@@ -0,0 +1,30 @@
1
+ module Whenever::Test
2
+ class DSLInterpreter
3
+ def initialize(schedule_world, vars)
4
+ @_world = schedule_world
5
+ vars.each do |name, value|
6
+ instance_variable_set("@#{name}", value)
7
+ end
8
+ end
9
+
10
+ def job_type(job, command)
11
+ @_world.jobs[job] = []
12
+ define_singleton_method(job) do |task, *args|
13
+ @_world.jobs[job] << StrictHash[task: task, every: @_current_every, command: command]
14
+ end
15
+ end
16
+
17
+ def every(*args, &block)
18
+ @_current_every = args
19
+ yield
20
+ end
21
+
22
+ def set(name, value)
23
+ @_world.sets[name] = value
24
+ end
25
+
26
+ def env(name, value)
27
+ @_world.envs[name] = value
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ module Whenever::Test
2
+ class Schedule
3
+ attr_accessor :jobs, :envs, :sets
4
+
5
+ def initialize(file: 'config/schedule.rb', vars: {})
6
+ self.jobs = {}
7
+ self.envs = {}
8
+ self.sets = {}
9
+
10
+ dsl = DSLInterpreter.new(self, vars)
11
+ setup_whenever(dsl)
12
+ parse(dsl, file)
13
+ end
14
+
15
+ private
16
+
17
+ def setup_whenever(dsl)
18
+ whenever_setup_file = File.join(Gem.loaded_specs['whenever'].full_gem_path, 'lib', 'whenever', 'setup.rb').to_s
19
+ dsl.instance_eval File.read(whenever_setup_file)
20
+ end
21
+
22
+ def parse(dsl, file)
23
+ dsl.instance_eval File.read(file)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module Whenever::Test
2
+ class StrictHash < Hash
3
+ alias [] fetch
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ namespace :db_sync do
2
+ task :import do
3
+ end
4
+ end
@@ -0,0 +1,22 @@
1
+ set :output, '/var/log/whenever.log'
2
+ set :runner_command, 'bundle exec rails runner'
3
+ job_type :ping, 'ping :task'
4
+ env :PATH, ENV['PATH']
5
+
6
+ every :day do
7
+ runner 'UpdateAnalyticsMetricsWorker.perform_async'
8
+ end
9
+
10
+ every :hour do
11
+ runner 'TimeoutCampaings.perform_async'
12
+ end
13
+
14
+ every :minute do
15
+ ping 'http://myapp.com/cron-alive'
16
+ end
17
+
18
+ if @environment == 'staging'
19
+ every :day do
20
+ rake 'db_sync:import'
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ require 'pry'
2
+ require 'whenever/test'
3
+ require 'minitest/autorun'
4
+ require 'minitest/pride'
5
+ require 'mocha/setup'
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Whenever::Test::Schedule do
4
+ let(:fixture) { 'spec/fixtures/schedule.rb' }
5
+ before { load 'spec/fixtures/db_sync.rake' }
6
+
7
+ describe '#jobs' do
8
+ it 'makes sure `runner` statements are captured' do
9
+ schedule = Whenever::Test::Schedule.new(file: fixture)
10
+
11
+ assert_equal 2, schedule.jobs[:runner].count
12
+
13
+ # Executes the actual ruby statement to make sure the constant and method exists:
14
+ schedule.jobs[:runner].each { |job| instance_eval job[:task] }
15
+ end
16
+
17
+ it 'makes sure `rake` statements are captured' do
18
+ schedule = Whenever::Test::Schedule.new(file: fixture, vars: { environment: 'staging' })
19
+
20
+ # Makes sure the rake task is defined:
21
+ assert Rake::Task.task_defined?(schedule.jobs[:rake].first[:task])
22
+ end
23
+
24
+ it 'makes sure custom job_type tasks are registered' do
25
+ schedule = Whenever::Test::Schedule.new(file: fixture)
26
+
27
+ assert_equal 'http://myapp.com/cron-alive', schedule.jobs[:ping].first[:task]
28
+ assert_equal 'ping :task', schedule.jobs[:ping].first[:command]
29
+ assert_equal [:minute], schedule.jobs[:ping].first[:every]
30
+ end
31
+ end
32
+
33
+ it 'makes sure `set` statements are captured ' do
34
+ schedule = Whenever::Test::Schedule.new(file: fixture)
35
+
36
+ assert_equal 'bundle exec rails runner', schedule.sets[:runner_command]
37
+ end
38
+
39
+ it 'makes sure `env` statements are captured ' do
40
+ schedule = Whenever::Test::Schedule.new(file: fixture)
41
+
42
+ assert_equal ENV['PATH'], schedule.envs[:PATH]
43
+ end
44
+
45
+ class TimeoutCampaings
46
+ def self.perform_async; end
47
+ end
48
+
49
+ class UpdateAnalyticsMetricsWorker
50
+ def self.perform_async; end
51
+ end
52
+ end
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'whenever-test'
7
+ spec.version = '1.0.0'
8
+ spec.authors = ['Rafael Sales']
9
+ spec.email = ['rafaelcds@gmail.com']
10
+ spec.summary = %q{Test Whenever scheduled jobs}
11
+ spec.description = %q{Whenever gem doesn't provide test support, so whenever-test makes that possible}
12
+ spec.homepage = 'https://github.com/heartbits/whenever-test'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_runtime_dependency 'whenever'
21
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: whenever-test
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Rafael Sales
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: whenever
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Whenever gem doesn't provide test support, so whenever-test makes that
28
+ possible
29
+ email:
30
+ - rafaelcds@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE
39
+ - README.md
40
+ - Rakefile
41
+ - lib/whenever/test.rb
42
+ - lib/whenever/test/dsl.rb
43
+ - lib/whenever/test/schedule.rb
44
+ - lib/whenever/test/strict_hash.rb
45
+ - spec/fixtures/db_sync.rake
46
+ - spec/fixtures/schedule.rb
47
+ - spec/spec_helper.rb
48
+ - spec/whenever/test/schedule_spec.rb
49
+ - whenever-test.gemspec
50
+ homepage: https://github.com/heartbits/whenever-test
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.4.5
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Test Whenever scheduled jobs
74
+ test_files:
75
+ - spec/fixtures/db_sync.rake
76
+ - spec/fixtures/schedule.rb
77
+ - spec/spec_helper.rb
78
+ - spec/whenever/test/schedule_spec.rb