Lint the whole project

This commit is contained in:
Dzmitry Plashchynski
2015-03-13 20:32:27 +02:00
parent 1aa27baca8
commit a9798acb35
23 changed files with 203 additions and 172 deletions

View File

@@ -1,15 +1,16 @@
require "spec_helper"
require 'spec_helper'
require 'crono/cli'
class TestJob
def perform;end
def perform
end
end
describe Crono::CLI do
let(:cli) { Crono::CLI.instance }
describe "#run" do
it "should try to initialize rails with #load_rails and start working loop" do
describe '#run' do
it 'should initialize rails with #load_rails and start working loop' do
expect(cli).to receive(:load_rails)
expect(cli).to receive(:start_working_loop)
expect(cli).to receive(:parse_options)
@@ -18,34 +19,34 @@ describe Crono::CLI do
end
end
describe "#start_working_loop" do
it "should start working loop"
describe '#start_working_loop' do
it 'should start working loop'
end
describe "#parse_options" do
it "should set cronotab" do
cli.send(:parse_options, ["--cronotab", "/tmp/cronotab.rb"])
expect(cli.config.cronotab).to be_eql "/tmp/cronotab.rb"
describe '#parse_options' do
it 'should set cronotab' do
cli.send(:parse_options, ['--cronotab', '/tmp/cronotab.rb'])
expect(cli.config.cronotab).to be_eql '/tmp/cronotab.rb'
end
it "should set logfile" do
cli.send(:parse_options, ["--logfile", "log/crono.log"])
expect(cli.config.logfile).to be_eql "log/crono.log"
it 'should set logfile' do
cli.send(:parse_options, ['--logfile', 'log/crono.log'])
expect(cli.config.logfile).to be_eql 'log/crono.log'
end
it "should set pidfile" do
cli.send(:parse_options, ["--pidfile", "tmp/pids/crono.0.log"])
expect(cli.config.pidfile).to be_eql "tmp/pids/crono.0.log"
it 'should set pidfile' do
cli.send(:parse_options, ['--pidfile', 'tmp/pids/crono.0.log'])
expect(cli.config.pidfile).to be_eql 'tmp/pids/crono.0.log'
end
it "should set daemonize" do
cli.send(:parse_options, ["--daemonize"])
it 'should set daemonize' do
cli.send(:parse_options, ['--daemonize'])
expect(cli.config.daemonize).to be true
end
it "should set environment" do
cli.send(:parse_options, ["--environment", "production"])
expect(cli.config.environment).to be_eql("production")
it 'should set environment' do
cli.send(:parse_options, ['--environment', 'production'])
expect(cli.config.environment).to be_eql('production')
end
end
end

View File

@@ -1,15 +1,15 @@
require "spec_helper"
require 'spec_helper'
describe Crono::Config do
describe "#initialize" do
it "should initialize with default configuration options" do
ENV["RAILS_ENV"] = "test"
describe '#initialize' do
it 'should initialize with default configuration options' do
ENV['RAILS_ENV'] = 'test'
@config = Crono::Config.new
expect(@config.cronotab).to be Crono::Config::CRONOTAB
expect(@config.logfile).to be Crono::Config::LOGFILE
expect(@config.pidfile).to be Crono::Config::PIDFILE
expect(@config.daemonize).to be false
expect(@config.environment).to be_eql ENV["RAILS_ENV"]
expect(@config.environment).to be_eql ENV['RAILS_ENV']
end
end
end

View File

@@ -1,66 +1,67 @@
require "spec_helper"
require 'spec_helper'
class TestJob
def perform;end
def perform
end
end
class TestFailingJob
def perform
raise "Some error"
fail 'Some error'
end
end
describe Crono::Job do
let(:period) { Crono::Period.new(2.day) }
let(:job) { Crono::Job.new(TestJob, period) }
let(:job) { Crono::Job.new(TestJob, period) }
let(:failing_job) { Crono::Job.new(TestFailingJob, period) }
it "should contain performer and period" do
it 'should contain performer and period' do
expect(job.performer).to be TestJob
expect(job.period).to be period
end
describe "#perform" do
describe '#perform' do
after { job.send(:model).destroy }
it "should run performer in separate thread" do
it 'should run performer in separate thread' do
expect(job).to receive(:save)
thread = job.perform.join
expect(thread).to be_stop
end
it "should save performin errors to log" do
it 'should save performin errors to log' do
thread = failing_job.perform.join
expect(thread).to be_stop
saved_log = Crono::CronoJob.find_by(job_id: failing_job.job_id).log
expect(saved_log).to include "Some error"
expect(saved_log).to include 'Some error'
end
it "should set Job#healthy to true if perform ok" do
thread = job.perform.join
it 'should set Job#healthy to true if perform ok' do
job.perform.join
expect(job.healthy).to be true
end
it "should set Job#healthy to false if perform with error" do
thread = failing_job.perform.join
it 'should set Job#healthy to false if perform with error' do
failing_job.perform.join
expect(failing_job.healthy).to be false
end
end
describe "#description" do
it "should return job identificator" do
expect(job.description).to be_eql("Perform TestJob every 2 days")
describe '#description' do
it 'should return job identificator' do
expect(job.description).to be_eql('Perform TestJob every 2 days')
end
end
describe "#save" do
it "should save new job to DB" do
describe '#save' do
it 'should save new job to DB' do
expect(Crono::CronoJob.where(job_id: job.job_id)).to_not exist
job.save
expect(Crono::CronoJob.where(job_id: job.job_id)).to exist
end
it "should update saved job" do
it 'should update saved job' do
job.last_performed_at = Time.now
job.healthy = true
job.save
@@ -69,8 +70,8 @@ describe Crono::Job do
expect(@crono_job.healthy).to be true
end
it "should save and truncate job log" do
message = "test message"
it 'should save and truncate job log' do
message = 'test message'
job.send(:log, message)
job.save
expect(job.send(:model).reload.log).to include message
@@ -78,37 +79,37 @@ describe Crono::Job do
end
end
describe "#load" do
describe '#load' do
before do
@saved_last_performed_at = job.last_performed_at = Time.now
job.save
end
it "should load last_performed_at from DB" do
it 'should load last_performed_at from DB' do
@job = Crono::Job.new(TestJob, period)
@job.load
expect(@job.last_performed_at.utc.to_s).to be_eql @saved_last_performed_at.utc.to_s
end
end
describe "#log" do
it "should write log messages to both common and job log" do
message = "Test message"
describe '#log' do
it 'should write log messages to both common and job log' do
message = 'Test message'
expect(job.logger).to receive(:log).with(Logger::INFO, message)
expect(job.job_logger).to receive(:log).with(Logger::INFO, message)
job.send(:log, message)
end
it "should write job log to Job#job_log" do
message = "Test message"
it 'should write job log to Job#job_log' do
message = 'Test message'
job.send(:log, message)
expect(job.job_log.string).to include(message)
end
end
describe "#log_error" do
it "should call log with ERROR severity" do
message = "Test message"
describe '#log_error' do
it 'should call log with ERROR severity' do
message = 'Test message'
expect(job).to receive(:log).with(message, Logger::ERROR)
job.send(:log_error, message)
end

View File

@@ -1,26 +1,26 @@
require "spec_helper"
require 'spec_helper'
describe Crono::CronoJob do
let(:valid_attrs) do
{
job_id: "Perform TestJob every 3 days"
job_id: 'Perform TestJob every 3 days'
}
end
it "should validate presence of job_id" do
@crono_job = Crono::CronoJob.new()
it 'should validate presence of job_id' do
@crono_job = Crono::CronoJob.new
expect(@crono_job).not_to be_valid
expect(@crono_job.errors.added?(:job_id, :blank)).to be true
end
it "should validate uniqueness of job_id" do
Crono::CronoJob.create!(job_id: "TestJob every 2 days")
@crono_job = Crono::CronoJob.create(job_id: "TestJob every 2 days")
it 'should validate uniqueness of job_id' do
Crono::CronoJob.create!(job_id: 'TestJob every 2 days')
@crono_job = Crono::CronoJob.create(job_id: 'TestJob every 2 days')
expect(@crono_job).not_to be_valid
expect(@crono_job.errors.added?(:job_id, :taken)).to be true
end
it "should save job_id to DB" do
it 'should save job_id to DB' do
Crono::CronoJob.create!(valid_attrs)
@crono_job = Crono::CronoJob.find_by(job_id: valid_attrs[:job_id])
expect(@crono_job).to be_present

View File

@@ -1,12 +1,13 @@
require "spec_helper"
require 'spec_helper'
class TestJob
def perform;end
def perform
end
end
describe Crono::PerformerProxy do
it "should add job to schedule" do
it 'should add job to schedule' do
expect(Crono.scheduler).to receive(:add_job).with(kind_of(Crono::Job))
Crono.perform(TestJob).every(2.days, at: "15:30")
Crono.perform(TestJob).every(2.days, at: '15:30')
end
end

View File

@@ -1,4 +1,4 @@
require "spec_helper"
require 'spec_helper'
describe Crono::Period do
around(:each) do |example|
@@ -7,47 +7,48 @@ describe Crono::Period do
end
end
describe "#description" do
it "should return period description" do
@period = Crono::Period.new(2.day, at: "15:20")
expect(@period.description).to be_eql("every 2 days at 15:20")
describe '#description' do
it 'should return period description' do
@period = Crono::Period.new(2.day, at: '15:20')
expect(@period.description).to be_eql('every 2 days at 15:20')
end
end
describe "#next" do
context "in daily basis" do
it "should return the time 2 days from now" do
describe '#next' do
context 'in daily basis' do
it 'should return the time 2 days from now' do
@period = Crono::Period.new(2.day)
expect(@period.next).to be_eql(2.day.from_now)
end
it "should set time to 'at' time as a string" do
time = 10.minutes.ago
@period = Crono::Period.new(2.day, at: [time.hour, time.min].join(':'))
at = [time.hour, time.min].join(':')
@period = Crono::Period.new(2.day, at: at)
expect(@period.next).to be_eql(2.day.from_now.change(hour: time.hour, min: time.min))
end
it "should set time to 'at' time as a hash" do
time = 10.minutes.ago
at = {hour: time.hour, min: time.min}
at = { hour: time.hour, min: time.min }
@period = Crono::Period.new(2.day, at: at)
expect(@period.next).to be_eql(2.day.from_now.change(at))
end
it "should raise error when 'at' is wrong" do
expect {
@period = Crono::Period.new(2.day, at: 1)
Crono::Period.new(2.day, at: 1)
}.to raise_error("Unknown 'at' format")
end
it "should return time in relation to last time" do
it 'should return time in relation to last time' do
@period = Crono::Period.new(2.day)
expect(@period.next(since: 1.day.ago)).to be_eql(1.day.from_now)
end
it "should return today time if it is first run and not too late" do
it 'should return today time if it is first run and not too late' do
time = 10.minutes.from_now
at = {hour: time.hour, min: time.min}
at = { hour: time.hour, min: time.min }
@period = Crono::Period.new(2.day, at: at)
expect(@period.next.utc.to_s).to be_eql(Time.now.change(at).utc.to_s)
end

View File

@@ -1,30 +1,31 @@
require "spec_helper"
require 'spec_helper'
class TestJob
def perform;end
def perform
end
end
describe Crono::Scheduler do
before(:each) do
@scheduler = Crono::Scheduler.new
@jobs = [
Crono::Period.new(3.day, at: 10.minutes.from_now.strftime("%H:%M")),
Crono::Period.new(1.day, at: 20.minutes.from_now.strftime("%H:%M")),
Crono::Period.new(7.day, at: 40.minutes.from_now.strftime("%H:%M"))
Crono::Period.new(3.day, at: 10.minutes.from_now.strftime('%H:%M')),
Crono::Period.new(1.day, at: 20.minutes.from_now.strftime('%H:%M')),
Crono::Period.new(7.day, at: 40.minutes.from_now.strftime('%H:%M'))
].map { |period| Crono::Job.new(TestJob, period) }
@scheduler.jobs = @jobs
end
describe "#add_job" do
it "should call Job#load on Job" do
@job = Crono::Job.new(TestJob, Crono::Period.new(10.day, at: "04:05"))
describe '#add_job' do
it 'should call Job#load on Job' do
@job = Crono::Job.new(TestJob, Crono::Period.new(10.day, at: '04:05'))
expect(@job).to receive(:load)
@scheduler.add_job(@job)
end
end
describe "#next" do
it "should return next job in schedule" do
describe '#next' do
it 'should return next job in schedule' do
expect(@scheduler.next).to be @jobs[0]
end
end

View File

@@ -6,9 +6,10 @@ require 'byebug'
require 'crono'
require 'generators/crono/install/templates/migrations/create_crono_jobs.rb'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: "file::memory:?cache=shared")
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: 'file::memory:?cache=shared'
)
ActiveRecord::Base.logger = Logger.new(STDOUT)
CreateCronoJobs.up
RSpec.configure do |config|
end

View File

@@ -1,44 +1,47 @@
require "spec_helper"
require "rack/test"
require 'spec_helper'
require 'rack/test'
include Rack::Test::Methods
describe Crono::Web do
let(:app) { Crono::Web }
before do
@test_job_id = "Perform TestJob every 5 seconds"
@test_job_log = "All runs ok"
@test_job = Crono::CronoJob.create!(job_id: @test_job_id, log: @test_job_log)
@test_job_id = 'Perform TestJob every 5 seconds'
@test_job_log = 'All runs ok'
@test_job = Crono::CronoJob.create!(
job_id: @test_job_id,
log: @test_job_log
)
end
after { @test_job.destroy }
describe "/" do
it "should show all jobs" do
describe '/' do
it 'should show all jobs' do
get '/'
expect(last_response).to be_ok
expect(last_response.body).to include @test_job_id
end
it "should show a error mark when a job is unhealthy" do
it 'should show a error mark when a job is unhealthy' do
@test_job.update(healthy: false)
get '/'
expect(last_response.body).to include "Error"
expect(last_response.body).to include 'Error'
end
end
describe "/job/:id" do
it "should show job log" do
describe '/job/:id' do
it 'should show job log' do
get "/job/#{@test_job.id}"
expect(last_response).to be_ok
expect(last_response.body).to include @test_job_id
expect(last_response.body).to include @test_job_log
end
it "should show a message about the unhealthy job" do
it 'should show a message about the unhealthy job' do
@test_job.update(healthy: false)
get "/job/#{@test_job.id}"
expect(last_response.body).to include "An error occurs during the last execution of this job"
expect(last_response.body).to include 'An error occurs during the last execution of this job'
end
end
end