module Cog::SpecHelpers

Modules and classes to help write specs for testing cog

Requiring the helpers will make extra {SpecHelpers::Matchers} available to your RSpec tests. These are useful for testing a {SpecHelpers::Invocation}, which is returned from a call to {SpecHelpers::Runner#run}

@example

require 'cog/spec_helpers'

describe 'The command line interface' do

  include Cog::SpecHelpers

  before :all do
    @cog = Cog::SpecHelpers::Runner.new 'bin/cog'
  end

  it 'should print help when no args are passed' do
    expect(@cog.run).to show_help
  end

  context 'in an uninitialized project' do

    before :each do
      use_fixture :uninitialized
    end

    it 'running `cog init` should make a Cogfile' do
      expect(@cog.run(:init)).to make(cogfile_path)
    end

  end
end

Public Instance Methods

active_fixture_dir() click to toggle source

@return [String] directory of the active fixture.

# File lib/cog/spec_helpers.rb, line 47
def active_fixture_dir
  File.join spec_root, 'active_fixture'
end
active_home_fixture_dir() click to toggle source

@return [String] directory of the active home fixture.

# File lib/cog/spec_helpers.rb, line 52
def active_home_fixture_dir
  File.join spec_root, 'active_home_fixture'
end
cog_directory() click to toggle source

@return [String] path to the cog directory in the active spec fixture

# File lib/cog/spec_helpers.rb, line 62
def cog_directory
  File.join active_fixture_dir, 'cog'
end
cogfile_path() click to toggle source

@return [String] path to the Cogfile in the active spec fixture

# File lib/cog/spec_helpers.rb, line 57
def cogfile_path
  File.join active_fixture_dir, 'Cogfile'
end
generated_file(filename) click to toggle source

@param filename [String] name of a generated source file @return [String] absolute path to the generated file

# File lib/cog/spec_helpers.rb, line 86
def generated_file(filename)
  File.expand_path File.join(active_fixture_dir, 'src', filename)
end
generator(name) click to toggle source

@param name [String] active fixture generator identifier @return [String] path to the generator with the given name

# File lib/cog/spec_helpers.rb, line 68
def generator(name)
  File.expand_path File.join(active_fixture_dir, 'cog', 'generators', "#{name}.rb")
end
plugin(name) click to toggle source

@param name [String] plugin name @return [String] absolute file system path to the plugin directory

# File lib/cog/spec_helpers.rb, line 80
def plugin(name)
  File.expand_path File.join(active_fixture_dir, 'cog', 'plugins', name.to_s)
end
spec_root() click to toggle source

@return [String] absolute path to the root spec directory

# File lib/cog/spec_helpers.rb, line 42
def spec_root
  File.expand_path File.join(File.dirname(__FILE__), '..', '..', 'spec')
end
template(name) click to toggle source

@param name [String] template identifier (without the .erb extension) @return [String] absolute file system path to the template

# File lib/cog/spec_helpers.rb, line 74
def template(name)
  File.expand_path File.join(active_fixture_dir, 'cog', 'templates', name.to_s)
end
use_fixture(name) click to toggle source

The next cog spec will execute in a fresh copy of the given fixture Fixture directories are stored in spec/fixtures. @param name [String] name of the fixture @return [nil]

# File lib/cog/spec_helpers.rb, line 94
def use_fixture(name)
  path = File.join spec_root, 'fixtures', name.to_s
  copy_fixture path, active_fixture_dir
  Dir.chdir active_fixture_dir
  nil
end
use_home_fixture(name) click to toggle source

The next cog spec will execute in a fresh copy of the given home fixture Home fixture directories are stored in spec/home_fixtures @param name [String] name of the home fixture @return [nil]

# File lib/cog/spec_helpers.rb, line 105
def use_home_fixture(name)
  path = File.join spec_root, 'home_fixtures', name.to_s
  copy_fixture path, active_home_fixture_dir
  nil
end

Private Instance Methods

copy_fixture(source, dest) click to toggle source
# File lib/cog/spec_helpers.rb, line 113
def copy_fixture(source, dest)
  if File.exists?(source) && File.directory?(source)
    FileUtils.rm_rf dest if File.exists? dest
    FileUtils.cp_r source, dest
  else
    throw :invalid_fixture_name
  end
end