I'm trying to write a small ruby gem that does some template generating, using Bundler and Thor. I'm writing the tests in Cucumber and Aruba, and I'm having trouble getting them to pass.
I have the following Thor CLI class defined in the app:
require 'thor'
require 'sleipnir'
require 'sleipnir/generators/layout'
require 'sleipnir/generators/app'
module Sleipnir
class CLI < Thor
desc "app", "Generates an app directory and copies the appropriate files"
def app(app_name)
Sleipnir::Generators::App.start([app_name])
end
desc "layout", "Generates specific layout based on template type"
def layout(template_type)
Sleipnir::Generators::Layout.start([template_type])
end
end
end
This is the app.rb
file:
require 'thor/group'
module Sleipnir
module Generators
class App < Thor::Group
include Thor::Actions
argument :app_name, :type => :string
class_option :template_type, :default => :erb, :required => true
def self.source_root
File.dirname(__FILE__)
end
def create_app_dir
empty_directory(app_name)
end
def copy_app_scaffold
directory("app", app_name)
end
end
end
end
And the layout.rb
file:
require 'thor/group'
module Sleipnir
module Generators
class Layout < Thor::Group
include Thor::Actions
class_option :template_type, :default => :erb, :required => true
def self.source_root
File.dirname(__FILE__) + "/template"
end
def copy_layout
template_type = options[:template_type]
template("layout_template.#{template_type}", "views/layout.#{template_type}")
end
end
end
end
I have a cucumber test written for the app
method, and it passes. However, the layout
method is failing. Here is the test:
Feature: Generate
In order to generate templates
As a CLI
I want to run the generator
Scenario: Layout
When I run `sleipnir app test_app`
Then the following directories should exist:
| test_app/views |
When I run `sleipnir layout --template_type "erb"`
Then the following files should exist:
| test_app/views/layout.erb |
The first part of the test passes just fine (i.e. the directory is created), but the part about verifying the file exists fails. I've checked the file structure, and the layout_template.erb
file exists, so I can't figure out why it isn't be templated properly.