Back to Repositories

Testing Custom Job Definition Templates in Whenever

This test suite validates the custom job definition functionality in the Whenever gem, focusing on task definitions, option handling, and path management. It ensures proper cron job generation with various configuration scenarios and template customizations.

Test Coverage Overview

The test suite comprehensively covers job definition functionality with different configurations and options.

Key areas tested include:
  • Basic task definitions with job templates
  • Option handling (local and global settings)
  • Path management in job definitions
  • Template variable substitution
  • Interaction between global and local option settings

Implementation Analysis

The testing approach uses minitest assertions to verify cron output patterns. It employs heredoc syntax for test case definitions and uses regex pattern matching to validate generated cron job strings.

Notable patterns include:
  • Template-based job definitions
  • Global vs local configuration precedence
  • Path resolution mechanisms
  • Option substitution handling

Technical Details

Testing tools and setup:
  • Minitest framework for assertions
  • Whenever::TestCase as base test class
  • Mocha for stubbing (path resolution)
  • Regex pattern matching for output validation
  • Heredoc syntax for test case definition

Best Practices Demonstrated

The test suite exemplifies several testing best practices for Ruby applications.

Notable practices include:
  • Isolated test cases for each scenario
  • Clear test naming conventions
  • Comprehensive edge case coverage
  • Proper stubbing of external dependencies
  • Consistent assertion patterns

javan/whenever

test/functional/output_defined_job_test.rb

            
require 'test_helper'

class OutputDefinedJobTest < Whenever::TestCase
  test "defined job with a :task" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      job_type :some_job, "before :task after"
      every 2.hours do
        some_job "during"
      end
    file

    assert_match(/^.+ .+ .+ .+ before during after$/, output)
  end

  test "defined job with a :task and some options" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      job_type :some_job, "before :task after :option1 :option2"
      every 2.hours do
        some_job "during", :option1 => 'happy', :option2 => 'birthday'
      end
    file

    assert_match(/^.+ .+ .+ .+ before during after happy birthday$/, output)
  end

  test "defined job with a :task and an option where the option is set globally" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      job_type :some_job, "before :task after :option1"
      set :option1, 'happy'
      every 2.hours do
        some_job "during"
      end
    file

    assert_match(/^.+ .+ .+ .+ before during after happy$/, output)
  end

  test "defined job with a :task and an option where the option is set globally and locally" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      job_type :some_job, "before :task after :option1"
      set :option1, 'global'
      every 2.hours do
        some_job "during", :option1 => 'local'
      end
    file

    assert_match(/^.+ .+ .+ .+ before during after local$/, output)
  end

  test "defined job with a :task and an option that is not set" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      job_type :some_job, "before :task after :option1"
      every 2.hours do
        some_job "during", :option2 => 'happy'
      end
    file

    assert_match(/^.+ .+ .+ .+ before during after :option1$/, output)
  end

  test "defined job that uses a :path where none is explicitly set" do
    Whenever.stubs(:path).returns('/my/path')

    output = Whenever.cron \
    <<-file
      set :job_template, nil
      job_type :some_job, "cd :path && :task"
      every 2.hours do
        some_job 'blahblah'
      end
    file

    assert_match two_hours + %( cd /my/path && blahblah), output
  end
end