Module: Tap::Generator::Preview

Defined in:
lib/tap/generator/preview.rb

Overview

Preview is a testing module designed so that process will return an array of relative paths for the created files/directories (which are easy to specify in a test). Preview also collects the content of created files to be tested as needed.

class Sample < Tap::Generator::Base
  def manifest(m)
    dir = path('dir')

    m.directory dir
    m.file(File.join(dir, 'file.txt')) {|io| io << "content"}
  end
end

These assertions will pass:

s = Sample.new.extend Preview
assert_equal %w{
  dir
  dir/file.txt
}, s.process

assert_equal "content", s.preview['dir/file.txt']

Note that relative paths are relative to destination_root.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionObject

The action for self (default :preview)



38
39
40
# File 'lib/tap/generator/preview.rb', line 38

def action
  @action
end

#previewObject

A hash of (relative_path, content) pairs representing content built to files.



35
36
37
# File 'lib/tap/generator/preview.rb', line 35

def preview
  @preview
end

Class Method Details

.extended(base) ⇒ Object

:nodoc:



40
41
42
43
# File 'lib/tap/generator/preview.rb', line 40

def self.extended(base) # :nodoc:
  base.instance_variable_set(:@preview, {})
  base.instance_variable_set(:@action, :preview)
end

Instance Method Details

#directory(target, options = {}) ⇒ Object

Returns the relative path of the target.



53
54
55
# File 'lib/tap/generator/preview.rb', line 53

def directory(target, options={})
  relative_path(target)
end

#file(target, options = {}) ⇒ Object

Returns the relative path of the target. If a block is given, the block will be called with a StringIO and the results stored in builds.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tap/generator/preview.rb', line 60

def file(target, options={})
  target = relative_path(target)
  
  if block_given?
    io = StringIO.new
    yield(io)
    preview[target] = io.string
  end
  
  target
end

#relative_path(path) ⇒ Object

Returns the path of path, relative to destination_root. If path is destination_root, ‘.’ will be returned.



47
48
49
50
# File 'lib/tap/generator/preview.rb', line 47

def relative_path(path)
  path = Root::Utils.relative_path(destination_root, path, destination_root) || path
  path.empty? ? "." : path
end