Back to Repositories

Implementing Ruby Test Environment Configuration in jordansissel/fpm

This spec_setup.rb file establishes the core testing environment and configuration for the FPM (Effing Package Management) project. It configures logging, path settings, and utility methods essential for running the test suite efficiently and consistently.

Test Coverage Overview

The test setup provides comprehensive configuration for the FPM testing environment.

Key areas covered include:
  • Ruby environment configuration and gem dependencies
  • Library path management for FPM core functionality
  • Logging system setup with configurable debug levels
  • System call output management

Implementation Analysis

The testing approach focuses on establishing a controlled test environment with proper isolation and logging capabilities.

The implementation utilizes Ruby’s module system to modify kernel behavior and implements custom logging through the Cabin gem. The setup demonstrates sophisticated Ruby metaprogramming by aliasing system methods and managing I/O streams.

Technical Details

Testing components include:
  • Cabin gem for logging management
  • Insist gem for assertions
  • Ruby standard libraries (tmpdir, tempfile, fileutils)
  • Custom kernel method overrides for system call management
  • Environment-aware debug logging configuration

Best Practices Demonstrated

The test setup exhibits several testing best practices including proper isolation of test environments, configurable logging levels, and clean I/O management.

Notable practices include:
  • Environment-aware debug configuration
  • Centralized logging setup
  • Clean separation of concerns in test utilities
  • Proper management of system resources and file handles

jordansissel/fpm

spec/spec_setup.rb

            
require "rubygems" # for ruby 1.8
require "insist" # gem "insist"
require "cabin" # gem "cabin"
require "tmpdir" # stdlib
require "tempfile" # stdlib
require "fileutils" # stdlib
require "date" # stdlib

# put "lib" in RUBYLIB
$: << File.join(File.dirname(File.dirname(__FILE__)), "lib")

# for method "program_exists?" etc
require "fpm/util"
include FPM::Util

# Enable debug logs if requested.
if $DEBUG or ENV["DEBUG"]
  Cabin::Channel.get.level = :debug
  Cabin::Channel.get.subscribe(STDOUT)
else
  class << Cabin::Channel.get
    alias_method :subscribe_, :subscribe
    def subscribe(io)
      return if io == STDOUT
      subscribe_(io)
      #puts caller.join("\n")
    end
  end
end

Cabin::Channel.get.level = :error
spec_logger = Cabin::Channel.get("rspec")
spec_logger.subscribe(STDOUT)
spec_logger.level = :error

# Quiet the output of all system() calls
module Kernel
  alias_method :orig_system, :system
  def system(*args)
    old_stdout = $stdout.clone
    old_stderr = $stderr.clone
    null = File.new("/dev/null", "w")
    $stdout.reopen(null)
    $stderr.reopen(null)
    value = orig_system(*args)
    $stdout.reopen(old_stdout)
    $stderr.reopen(old_stderr)
    null.close
    return value
  end
end