Class: CLIntegracon::FileTreeSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/CLIntegracon/file_tree_spec.rb

Overview

FileTreeSpec represents a single specification, which is mirrored on the file system in the spec directory by a direct children. It contains a before directory (#before_path) and an after directory (#after_path) or if it is initialized with a #base_spec, the before directory of this spec is used. The before directory contents in the #spec_path of the child spec, can contain further files, which overwrite, if given, the inherited contents.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, spec_folder, based_on: nil) ⇒ FileTreeSpec

Init a spec with a given context

Parameters:

  • context (FileTreeSpecContext)

    The context, which configures path and file behaviors

  • spec_folder (String)

    The concrete spec folder

  • based_on (String) (defaults to: nil)

    @see #base_spec_name



90
91
92
93
94
# File 'lib/CLIntegracon/file_tree_spec.rb', line 90

def initialize(context, spec_folder, based_on: nil)
  @context = context
  @spec_folder = spec_folder
  @base_spec_name = based_on
end

Instance Attribute Details

#base_spec_nameString|NilClass (readonly)

Returns The name of an optional #base_spec.

Returns:

  • (String|NilClass)

    The name of an optional #base_spec.



62
63
64
# File 'lib/CLIntegracon/file_tree_spec.rb', line 62

def base_spec_name
  @base_spec_name
end

#contextFileTreeSpecContext (readonly)

Returns The context, which configures path and file behaviors.

Returns:



18
19
20
# File 'lib/CLIntegracon/file_tree_spec.rb', line 18

def context
  @context
end

#spec_folderString (readonly)

Returns The concrete spec folder.

Returns:

  • (String)

    The concrete spec folder



22
23
24
# File 'lib/CLIntegracon/file_tree_spec.rb', line 22

def spec_folder
  @spec_folder
end

Instance Method Details

#after_pathPathname

Returns The concrete after directory for this spec.

Returns:

  • (Pathname)

    The concrete after directory for this spec



38
39
40
# File 'lib/CLIntegracon/file_tree_spec.rb', line 38

def after_path
  spec_path + context.after_dir
end

#base_specFileTreeSpec|NilClass

Returns The spec on whose #after_path will be used as #before_path for this spec.

Returns:

  • (FileTreeSpec|NilClass)

    The spec on whose #after_path will be used as #before_path for this spec.



75
76
77
# File 'lib/CLIntegracon/file_tree_spec.rb', line 75

def base_spec
  has_base? ? context.spec(base_spec_name) : nil
end

#before_pathPathname

Returns The concrete before directory for this spec.

Returns:

  • (Pathname)

    The concrete before directory for this spec



32
33
34
# File 'lib/CLIntegracon/file_tree_spec.rb', line 32

def before_path
  spec_path + context.before_dir
end

#check_unexpected_files(&block) ⇒ Object

Compares the expected and produced directory by using the rules defined in the context for unexpected files.

This is separate because you probably don’t want to define an extra test case for each file, which wasn’t expected at all. So you can keep your test cases consistent.

Parameters:

  • ] (Block<(Array)->())

    diff_block The block, where you will likely define a test that no unexpected files exists. It will receive an Array.



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/CLIntegracon/file_tree_spec.rb', line 151

def check_unexpected_files(&block)
  expected_files = glob_all after_path
  produced_files = glob_all
  unexpected_files = produced_files - expected_files

  # Select only files
  unexpected_files.select! { |path| path.file? }

  # Filter ignored paths
  unexpected_files.reject! { |path| context.ignores?(path) }

  block.call unexpected_files
end

#compare(&diff_block) ⇒ Object

Compares the expected and produced directory by using the rules defined in the context

Parameters:

  • ] (Block<(Diff)->())

    diff_block The block, where you will likely define a test for each file to compare. It will receive a Diff of each of the expected and produced files.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/CLIntegracon/file_tree_spec.rb', line 121

def compare(&diff_block)
  # Get a copy of the outputs before any transformations are applied
  FileUtils.cp_r("#{temp_transformed_path}/.", temp_raw_path)

  transform_paths!

  glob_all(after_path).each do |relative_path|
    expected = after_path + relative_path

    next unless expected.file?
    next if context.ignores?(relative_path)

    block = context.preprocessors_for(relative_path).first
    diff = diff_files(expected, relative_path, &block)

    diff_block.call diff
  end
end

#formatterFormatter

Return a Formatter

Returns:



169
170
171
# File 'lib/CLIntegracon/file_tree_spec.rb', line 169

def formatter
  @formatter ||= Formatter.new(self)
end

#has_base?Bool

Return whether this spec is based on another spec.

Returns:

  • (Bool)


68
69
70
# File 'lib/CLIntegracon/file_tree_spec.rb', line 68

def has_base?
  !base_spec_name.nil?
end

#run(&block) ⇒ Object

Run this spec

Parameters:

  • ] (Block<(FileTreeSpec)->())

    block The block, which will be executed after chdir into the created temporary directory. In this block you will likely run your modifications to the file system and use the received FileTreeSpec instance to make asserts with the test framework of your choice.



104
105
106
107
108
109
110
111
112
# File 'lib/CLIntegracon/file_tree_spec.rb', line 104

def run(&block)
  prepare!

  copy_files!

  Dir.chdir(temp_transformed_path) do
    block.call self
  end
end

#spec_pathPathname

Returns The concrete spec path.

Returns:

  • (Pathname)

    The concrete spec path



26
27
28
# File 'lib/CLIntegracon/file_tree_spec.rb', line 26

def spec_path
  context.spec_path + spec_folder
end

#temp_pathPathname

Returns The concrete temp directory for this spec.

Returns:

  • (Pathname)

    The concrete temp directory for this spec



44
45
46
# File 'lib/CLIntegracon/file_tree_spec.rb', line 44

def temp_path
  context.temp_path + spec_folder
end

#temp_raw_pathPathname

Returns The concrete temp raw directory for this spec.

Returns:

  • (Pathname)

    The concrete temp raw directory for this spec



50
51
52
# File 'lib/CLIntegracon/file_tree_spec.rb', line 50

def temp_raw_path
  temp_path + 'raw'
end

#temp_transformed_pathPathname

Returns The concrete transformed temp directory for this spec.

Returns:

  • (Pathname)

    The concrete transformed temp directory for this spec



56
57
58
# File 'lib/CLIntegracon/file_tree_spec.rb', line 56

def temp_transformed_path
  temp_path + 'transformed'
end