Back to Repositories

Testing OS X Package Management Implementation in jordansissel/fpm

This test suite validates the functionality of OS X package (osxpkg) creation and management in the FPM (Effing Package Management) tool. It focuses on package identifier formatting, filename generation, and package input/output operations.

Test Coverage Overview

The test suite provides comprehensive coverage of OS X package handling functionality in FPM. It verifies package identifier formatting, filename generation, and package input/output operations.

  • Package identifier formatting with domain prefix
  • Default package naming conventions
  • Package creation and reading verification
  • Platform-specific test handling for Darwin/OS X

Implementation Analysis

The testing approach utilizes RSpec’s behavior-driven development framework to validate OS X package functionality. The tests employ context-specific skipping for non-Darwin platforms and temporary file handling for package generation tests.

  • Conditional test execution based on platform
  • Before/After hooks for test setup and cleanup
  • Nested describe blocks for logical test organization
  • Temporary file management for package testing

Technical Details

  • RSpec testing framework
  • FPM package management library
  • OS X pkgbuild tool integration
  • Platform detection using uname command
  • Temporary file handling for test artifacts
  • Custom matchers with ‘insist’ assertions

Best Practices Demonstrated

The test suite exemplifies strong testing practices through proper test isolation, cleanup, and organization. It handles platform-specific requirements gracefully and ensures proper resource management.

  • Proper test setup and teardown
  • Platform-specific test handling
  • Descriptive context and example naming
  • Resource cleanup in after blocks
  • Modular test organization

jordansissel/fpm

spec/fpm/package/osxpkg_spec.rb

            
require "spec_setup"
require "fpm" # local
require "fpm/package/osxpkg" # local

platform_is_darwin = (%x{uname -s}.chomp == "Darwin")
if !platform_is_darwin
  Cabin::Channel.get("rspec").warn("Skipping OS X pkg tests requiring 'pkgbuild', " \
      "which requires a Darwin platform.")
end

describe FPM::Package::OSXpkg, :if => platform_is_darwin do
  describe "#identifier" do
    it "should be of the form reverse.domain.pkgname" do
      subject.name = "name"
      subject.attributes[:osxpkg_identifier_prefix] = "org.great"
      insist { subject.identifier } == \
      "#{subject.attributes[:osxpkg_identifier_prefix]}.#{subject.name}"
    end

    it "should be the name only if a prefix was not given" do
      subject.name = "name"
      subject.attributes[:osxpkg_identifier_prefix] = nil
      insist { subject.identifier } == subject.name
    end
  end

  describe "#to_s" do
    it "should have a default output usable as a filename" do
      subject.name = "name"
      subject.version = "123"

      # We like the format 'name-version.pkg'
      insist { subject.to_s } == "name-123.pkg"
    end
  end

  describe "#output" do
    before do
      skip("Current platform is not darwin/osx") unless platform_is_darwin
    end

    before :all do
      skip("Current platform is not darwin/osx") unless platform_is_darwin
      # output a package, use it as the input, set the subject to that input
      # package. This helps ensure that we can write and read packages
      # properly.
      tmpfile = Tempfile.new("fpm-test-osxpkg")
      @target = tmpfile.path
      # The target file must not exist.
      tmpfile.unlink

      @original = FPM::Package::OSXpkg.new
      @original.name = "name"
      @original.version = "123"
      @original.attributes[:osxpkg_identifier_prefix] = "org.my"
      @original.output(@target)

      @input = FPM::Package::OSXpkg.new
      @input.input(@target)
    end

    after :all do
      @original.cleanup if @original
      @input.cleanup if @input
    end # after

    context "package attributes" do
      it "should have the correct name" do
        insist { @input.name } == @original.name
      end

      it "should have the correct version" do
        insist { @input.version } == @original.version
      end
    end # package attributes
  end # #output
end # describe FPM::Package:OSXpkg