Module: Noumenon::Spec::ThemeHelpers

Defined in:
lib/noumenon/spec/theme_helpers.rb

Overview

Methods which can be used in RSpec based tests to create and use themes that will be recreated on each test run.

Instance Method Summary collapse

Instance Method Details

#create_theme(description = {}) ⇒ Noumenon::Theme?

Create a new theme directory at #theme_path.

Parameters:

  • description (Hash, nil) (defaults to: {})

    Either the description to write to theme.yml, or nil to prevent creation of the theme.yml file.

Returns:

  • (Noumenon::Theme, nil)

    If a description was provided the theme will be loaded, otherwise nil will be returned.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/noumenon/spec/theme_helpers.rb', line 23

def create_theme(description = {})
  FileUtils.mkdir_p theme_path
  FileUtils.mkdir_p File.join(theme_path, "templates")
  FileUtils.mkdir_p File.join(theme_path, "layouts")
  FileUtils.mkdir_p File.join(theme_path, "assets")
  
  if description
    File.open(File.join(theme_path, "theme.yml"), "w") do |f|
      f.print description.to_yaml
    end
    
    return Noumenon::Theme.load(theme_path)
  end
end

#theme_pathString

The path to the current theme.

Returns:

  • (String)


13
14
15
# File 'lib/noumenon/spec/theme_helpers.rb', line 13

def theme_path
  File.expand_path("../../../../tmp/example_theme", __FILE__)
end

#with_temporary_theme(description = {}) ⇒ Object

Creates a theme with the provided description, runs the block, and then deletes the theme again.

Useful in RSpec around blocks:

Examples:

Using with RSpec

describe "theme stuff" do
  include Noumenon::Spec::ThemeHelpers

  around do |ex|
    with_temporary_theme(name: "My Theme") do
      ex.run
    end
  end

  it "should have the right name" do
    theme = Noumenon::Theme.load(theme_path)
    theme.name.should == "My Theme"
  end
end


57
58
59
60
61
62
63
# File 'lib/noumenon/spec/theme_helpers.rb', line 57

def with_temporary_theme(description = {})
  create_theme(description)
  
  yield
  
  FileUtils.rm_r theme_path
end