Class: CLIntegracon::FileTreeSpec

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, spec_folder) ⇒ 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



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

def initialize(context, spec_folder)
  @context = context
  @spec_folder = spec_folder
end

Instance Attribute Details

#contextFileTreeSpecContext (readonly)

Returns The context, which configures path and file behaviors.

Returns:



10
11
12
# File 'lib/CLIntegracon/file_tree_spec.rb', line 10

def context
  @context
end

#spec_folderString (readonly)

Returns The concrete spec folder.

Returns:

  • (String)

    The concrete spec folder



14
15
16
# File 'lib/CLIntegracon/file_tree_spec.rb', line 14

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



30
31
32
# File 'lib/CLIntegracon/file_tree_spec.rb', line 30

def after_path
  spec_path + context.after_dir
end

#before_pathPathname

Returns The concrete before directory for this spec.

Returns:

  • (Pathname)

    The concrete before directory for this spec



24
25
26
# File 'lib/CLIntegracon/file_tree_spec.rb', line 24

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.



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/CLIntegracon/file_tree_spec.rb', line 107

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.reject! { |path| !path.file? }

  # Filter ignored paths
  unexpected_files.reject! { |path| special_behavior_for_path(path) == context.class.nop }

  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.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/CLIntegracon/file_tree_spec.rb', line 78

def compare(&diff_block)
  transform_paths!

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

    next unless expected.file?

    block = special_behavior_for_path relative_path
    next if block == context.class.nop

    diff = diff_files(expected, relative_path)
    diff.preparator = block unless block.nil?

    diff_block.call diff
  end
end

#formatterFormatter

Return a Formatter

Returns:



125
126
127
# File 'lib/CLIntegracon/file_tree_spec.rb', line 125

def formatter
  @formatter ||= Formatter.new(self)
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.



61
62
63
64
65
66
67
68
69
# File 'lib/CLIntegracon/file_tree_spec.rb', line 61

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



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

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



36
37
38
# File 'lib/CLIntegracon/file_tree_spec.rb', line 36

def temp_path
  context.temp_path + spec_folder
end