Back to Repositories

Testing Output Redirection Configuration in Whenever Cron Jobs

This test suite validates output redirection functionality in the Whenever gem, focusing on how command outputs are handled in cron jobs. It thoroughly examines various output configuration scenarios and their impact on command execution.

Test Coverage Overview

The test suite provides comprehensive coverage of output redirection scenarios in cron job configurations.

Key areas tested include:
  • Null output handling
  • Standard and error output configuration
  • Output overriding mechanisms
  • Legacy cron log support
  • Lambda-based output formatting

Implementation Analysis

The testing approach utilizes Minitest’s assertion framework to verify correct output string formatting. It employs heredoc syntax for test case definitions and implements systematic verification of output patterns using regex matching.

Notable patterns include:
  • Command output redirection syntax validation
  • Global vs command-specific output configuration
  • Standard output and error stream handling

Technical Details

Testing tools and configuration:
  • Minitest framework for test organization
  • Regular expressions for output validation
  • Whenever::TestCase as the base test class
  • Heredoc syntax for cron configuration
  • Custom assertion methods for pattern matching

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable examples include:
  • Isolated test cases for each output scenario
  • Comprehensive edge case coverage
  • Clear test naming conventions
  • Consistent assertion patterns
  • Thorough validation of both positive and negative cases

javan/whenever

test/functional/output_redirection_test.rb

            
require 'test_helper'

class OutputRedirectionTest < Whenever::TestCase
  test "command when the output is set to nil" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :output, nil
      every 2.hours do
        command "blahblah"
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah >> \/dev\/null 2>&1$/, output)
  end


  test "command when the output is set" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :output, 'logfile.log'
      every 2.hours do
        command "blahblah"
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah >> logfile.log 2>&1$/, output)
  end

  test "command when the error and standard output is set by the command" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      every 2.hours do
        command "blahblah", :output => {:standard => 'dev_null', :error => 'dev_err'}
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah >> dev_null 2>> dev_err$/, output)
  end

  test "command when the output is set and the comand overrides it" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :output, 'logfile.log'
      every 2.hours do
        command "blahblah", :output => 'otherlog.log'
      end
    file

    assert_no_match(/.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, output)
    assert_match(/^.+ .+ .+ .+ blahblah >> otherlog.log 2>&1$/, output)
  end

  test "command when the output is set and the comand overrides with standard and error" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :output, 'logfile.log'
      every 2.hours do
        command "blahblah", :output => {:error => 'dev_err', :standard => 'dev_null' }
      end
    file

    assert_no_match(/.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, output)
    assert_match(/^.+ .+ .+ .+ blahblah >> dev_null 2>> dev_err$/, output)
  end

  test "command when the output is set and the comand rejects it" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :output, 'logfile.log'
      every 2.hours do
        command "blahblah", :output => false
      end
    file

    assert_no_match(/.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, output)
    assert_match(/^.+ .+ .+ .+ blahblah$/, output)
  end

  test "command when the output is set and is overridden by the :set option" do
    output = Whenever.cron :set => 'output=otherlog.log', :string => \
    <<-file
      set :job_template, nil
      set :output, 'logfile.log'
      every 2.hours do
        command "blahblah"
      end
    file

    assert_no_match(/.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, output)
    assert_match(/^.+ .+ .+ .+ blahblah >> otherlog.log 2>&1/, output)
  end

  test "command when the error and standard output is set" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :output, {:error => 'dev_err', :standard => 'dev_null' }
      every 2.hours do
        command "blahblah"
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah >> dev_null 2>> dev_err$/, output)
  end

  test "command when error output is set" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :output, {:error => 'dev_null'}
      every 2.hours do
        command "blahblah"
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah 2>> dev_null$/, output)
  end

  test "command when the standard output is set" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :output, {:standard => 'dev_out'}
      every 2.hours do
        command "blahblah"
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah >> dev_out$/, output)
  end

  test "command when error output is set by the command" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      every 2.hours do
        command "blahblah", :output => {:error => 'dev_err'}
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah 2>> dev_err$/, output)
  end

  test "command when standard output is set by the command" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      every 2.hours do
        command "blahblah", :output => {:standard => 'dev_out'}
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah >> dev_out$/, output)
  end

  test "command when standard output is set to nil" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      every 2.hours do
        command "blahblah", :output => {:standard => nil}
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah > \/dev\/null$/, output)
  end

  test "command when standard error is set to nil" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      every 2.hours do
        command "blahblah", :output => {:error => nil}
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah 2> \/dev\/null$/, output)
  end

  test "command when standard output and standard error is set to nil" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      every 2.hours do
        command "blahblah", :output => {:error => nil, :standard => nil}
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah > \/dev\/null 2>&1$/, output)
  end

  test "command when standard output is set and standard error is set to nil" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      every 2.hours do
        command "blahblah", :output => {:error => nil, :standard => 'my.log'}
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah >> my.log 2> \/dev\/null$/, output)
  end

  test "command when standard output is nil and standard error is set" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      every 2.hours do
        command "blahblah", :output => {:error => 'my_error.log', :standard => nil}
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah >> \/dev\/null 2>> my_error.log$/, output)
  end

  test "command when the deprecated :cron_log is set" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :cron_log, "cron.log"
      every 2.hours do
        command "blahblah"
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah >> cron.log 2>&1$/, output)
  end


  test "a command when the standard output is set to a lambda" do
    output = Whenever.cron \
    <<-file
      set :job_template, nil
      set :output, lambda { "2>&1 | logger -t whenever_cron" }
      every 2.hours do
        command "blahblah"
      end
    file

    assert_match(/^.+ .+ .+ .+ blahblah 2>&1 | logger -t whenever_cron$/, output)
  end
end