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)

    @see #base_spec_name


78
79
80
81
82
# File 'lib/CLIntegracon/file_tree_spec.rb', line 78

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.


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

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.


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

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.


136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/CLIntegracon/file_tree_spec.rb', line 136

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.


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/CLIntegracon/file_tree_spec.rb', line 109

def compare(&diff_block)
  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:


154
155
156
# File 'lib/CLIntegracon/file_tree_spec.rb', line 154

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

#has_base?Bool

Return whether this spec is based on another spec.

Returns:

  • (Bool)

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

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.


92
93
94
95
96
97
98
99
100
# File 'lib/CLIntegracon/file_tree_spec.rb', line 92

def run(&block)
  prepare!

  copy_files!

  Dir.chdir(temp_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