Class: CLIntegracon::FileTreeSpec
- Inherits:
-
Object
- Object
- CLIntegracon::FileTreeSpec
- 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
-
#base_spec_name ⇒ String|NilClass
readonly
The name of an optional #base_spec.
-
#context ⇒ FileTreeSpecContext
readonly
The context, which configures path and file behaviors.
-
#spec_folder ⇒ String
readonly
The concrete spec folder.
Instance Method Summary collapse
-
#after_path ⇒ Pathname
The concrete after directory for this spec.
-
#base_spec ⇒ FileTreeSpec|NilClass
The spec on whose #after_path will be used as #before_path for this spec.
-
#before_path ⇒ Pathname
The concrete before directory for this spec.
-
#check_unexpected_files(&block) ⇒ Object
Compares the expected and produced directory by using the rules defined in the context for unexpected files.
-
#compare(&diff_block) ⇒ Object
Compares the expected and produced directory by using the rules defined in the context.
-
#formatter ⇒ Formatter
Return a Formatter.
-
#has_base? ⇒ Bool
Return whether this spec is based on another spec.
-
#initialize(context, spec_folder, based_on: nil) ⇒ FileTreeSpec
constructor
Init a spec with a given context.
-
#run(&block) ⇒ Object
Run this spec.
-
#spec_path ⇒ Pathname
The concrete spec path.
-
#temp_path ⇒ Pathname
The concrete temp directory for this spec.
-
#temp_raw_path ⇒ Pathname
The concrete temp raw directory for this spec.
-
#temp_transformed_path ⇒ Pathname
The concrete transformed temp directory for this spec.
Constructor Details
#initialize(context, spec_folder, based_on: nil) ⇒ FileTreeSpec
Init a spec with a given context
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_name ⇒ String|NilClass (readonly)
Returns 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 |
#context ⇒ FileTreeSpecContext (readonly)
Returns The context, which configures path and file behaviors.
18 19 20 |
# File 'lib/CLIntegracon/file_tree_spec.rb', line 18 def context @context end |
#spec_folder ⇒ String (readonly)
Returns 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_path ⇒ Pathname
Returns 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_spec ⇒ FileTreeSpec|NilClass
Returns 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_path ⇒ Pathname
Returns 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.
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
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 |
#formatter ⇒ Formatter
Return a Formatter
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.
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
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_path ⇒ Pathname
Returns 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_path ⇒ Pathname
Returns 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_path ⇒ Pathname
Returns 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_path ⇒ Pathname
Returns 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 |