Back to Repositories

Testing Logger Implementation and Configuration in geocoder

This test suite validates the logging functionality in the Geocoder gem, focusing on log level management, message handling, and configuration validation. The tests ensure proper logging behavior across different severity levels and logger implementations.

Test Coverage Overview

The test suite provides comprehensive coverage of Geocoder’s logging system, including log level filtering, message output verification, and error handling. Key test cases validate:
  • Basic logging functionality and message output
  • Log level threshold filtering
  • Kernel logger implementation
  • Invalid logger configuration handling
  • Return value consistency across logging operations

Implementation Analysis

The testing approach utilizes Ruby’s built-in Logger class and temporary file handling for output verification. The suite employs a systematic pattern of setup and teardown to ensure clean test isolation, with specific focus on:
  • Temporary file management for log capture
  • Logger configuration validation
  • Multiple severity level testing
  • Exception handling verification

Technical Details

Testing tools and configuration include:
  • Ruby Logger class for logging implementation
  • Tempfile for temporary log file management
  • GeocoderTestCase as the base test class
  • Setup/teardown hooks for resource management
  • Assert statements for validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Proper resource cleanup in teardown
  • Isolation of test cases
  • Comprehensive error case coverage
  • Clear test naming conventions
  • Effective use of setup/teardown hooks
  • Thorough validation of both positive and negative scenarios

alexreisner/geocoder

test/unit/logger_test.rb

            
# encoding: utf-8
require 'test_helper'
require 'logger'
require 'tempfile'

class LoggerTest < GeocoderTestCase

  def setup
    @tempfile = Tempfile.new("log")
    @logger = Logger.new(@tempfile.path)
    Geocoder.configure(logger: @logger)
  end

  def teardown
    @logger.close
    @tempfile.close
  end

  def test_set_logger_logs
    assert_equal nil, Geocoder.log(:warn, "should log")
    assert_match(/should log\n$/, @tempfile.read)
  end

  def test_logger_does_not_log_severity_too_low
    @logger.level = Logger::ERROR
    Geocoder.log(:info, "should not log")
    assert_equal "", @tempfile.read
  end

  def test_logger_logs_when_severity_high_enough
    @logger.level = Logger::DEBUG
    Geocoder.log(:warn, "important: should log!")
    assert_match(/important: should log/, @tempfile.read)
  end

  def test_kernel_logger_does_not_log_severity_too_low
    assert_nothing_raised do
      Geocoder.configure(logger: :kernel, kernel_logger_level: ::Logger::FATAL)
      Geocoder.log(:info, "should not log")
    end
  end

  def test_kernel_logger_logs_when_severity_high_enough
    assert_raises RuntimeError do
      Geocoder.configure(logger: :kernel, kernel_logger_level: ::Logger::DEBUG)
      Geocoder.log(:error, "important: should log!")
    end
  end

  def test_raise_configruation_error_for_invalid_logger
    Geocoder.configure(logger: {})
    assert_raises Geocoder::ConfigurationError do
      Geocoder.log(:info, "should raise error")
    end
  end

  def test_set_logger_always_returns_nil
    assert_equal nil, Geocoder.log(:info, "should log")
  end

  def test_kernel_logger_always_returns_nil
    Geocoder.configure(logger: :kernel)
    silence_warnings do
      assert_equal nil, Geocoder.log(:warn, "should log")
    end
  end
end