Module: TestConstruct::PathnameExtensions

Defined in:
lib/test_construct/pathname_extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#construct__chdir_defaultObject

Returns the value of attribute construct__chdir_default.



4
5
6
# File 'lib/test_construct/pathname_extensions.rb', line 4

def construct__chdir_default
  @construct__chdir_default
end

#construct__orig_dirObject

Returns the value of attribute construct__orig_dir.



4
5
6
# File 'lib/test_construct/pathname_extensions.rb', line 4

def construct__orig_dir
  @construct__orig_dir
end

#construct__rootObject

Returns the value of attribute construct__root.



4
5
6
# File 'lib/test_construct/pathname_extensions.rb', line 4

def construct__root
  @construct__root
end

Instance Method Details

#annotate_exception!(error) ⇒ Object



77
78
79
80
# File 'lib/test_construct/pathname_extensions.rb', line 77

def annotate_exception!(error)
  error.message << exception_message_annotation
  error
end

#chdir(&block) ⇒ Object

Note: Pathname implements #chdir directly, but it is deprecated in favor of Dir.chdir



52
53
54
# File 'lib/test_construct/pathname_extensions.rb', line 52

def chdir(&block)
  Dir.chdir(self, &block)
end

#destroy!Object



56
57
58
# File 'lib/test_construct/pathname_extensions.rb', line 56

def destroy!
  rmtree
end

#directory(path, opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/test_construct/pathname_extensions.rb', line 5

def directory(path, opts = {})
  chdir = opts.fetch(:chdir, construct__chdir_default)
  subdir = (self + path)
  subdir.mkpath
  subdir.extend(PathnameExtensions)
  subdir.construct__root = construct__root || self
  subdir.maybe_change_dir(chdir) do
    yield(subdir) if block_given?
  end
  subdir
end

#exception_message_annotationObject



82
83
84
# File 'lib/test_construct/pathname_extensions.rb', line 82

def exception_message_annotation
  "\nTestConstruct files kept at: #{self}"
end

#file(filepath, contents = nil, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/test_construct/pathname_extensions.rb', line 17

def file(filepath, contents = nil, &block)
  path = (self+filepath)
  path.dirname.mkpath
  mode = RUBY_PLATFORM =~ /mingw|mswin/ ? 'wb:UTF-8' : 'w'
  File.open(path, mode) do |f|
    if(block)
      if(block.arity==1)
        block.call(f)
      else
        f << block.call
      end
    else
      f << contents
    end
  end
  path
end

#finalizeObject



60
61
62
63
# File 'lib/test_construct/pathname_extensions.rb', line 60

def finalize
  revert_cwd
  destroy! unless keep?
end

#keepObject



65
66
67
68
69
70
71
# File 'lib/test_construct/pathname_extensions.rb', line 65

def keep
  if construct__root
    construct__root.keep
  else
    @keep = true
  end
end

#keep?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/test_construct/pathname_extensions.rb', line 73

def keep?
  defined?(@keep) && @keep
end

#maybe_change_dir(chdir, &block) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/test_construct/pathname_extensions.rb', line 35

def maybe_change_dir(chdir, &block)
  if(chdir)
    self.construct__orig_dir ||= Pathname.pwd
    self.chdir(&block)
  else
    block.call if block
  end
end

#revert_cwdObject



44
45
46
47
48
# File 'lib/test_construct/pathname_extensions.rb', line 44

def revert_cwd
  if construct__orig_dir
    Dir.chdir(construct__orig_dir)
  end
end