Class: GivenFilesystem

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

Instance Method Summary collapse

Constructor Details

#initializeGivenFilesystem

Returns a new instance of GivenFilesystem.



24
25
26
27
# File 'lib/given_filesystem.rb', line 24

def initialize
  @path_elements = [ Dir.tmpdir, "given_filesystem" ]
  @base_paths = Array.new
end

Instance Method Details

#cleanupObject



29
30
31
32
33
34
35
36
# File 'lib/given_filesystem.rb', line 29

def cleanup
  @base_paths.each do |base_path|
    # Better safe than sorry, so do sanity check on path before removing it
    if base_path =~ /given_filesystem/
      FileUtils.rm_r base_path
    end
  end
end

#directory(dir_name = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/given_filesystem.rb', line 38

def directory dir_name = nil
  create_random_base_path unless path_has_base?

  @path_elements.push dir_name || random_name

  created_path = path
  FileUtils.mkdir_p created_path
  yield if block_given?
  @path_elements.pop
  created_path
end

#directory_from_data(to, from = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/given_filesystem.rb', line 50

def directory_from_data to, from = nil
  from ||= to

  create_random_base_path unless path_has_base?

  FileUtils.mkdir_p path
  @path_elements.push to

  created_path = path
  FileUtils.cp_r test_data_path(from), path
  @path_elements.pop
  created_path
end

#file(file_name = nil, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/given_filesystem.rb', line 64

def file file_name = nil, options = {}
  create_random_base_path unless path_has_base?
  
  if file_name
    @path_elements.push file_name
  else
    @path_elements.push random_name
  end
 
  FileUtils.mkdir_p File.dirname(path)
 
  created_path = path
  File.open(created_path,"w") do |file|
    if options[:from]
      test_data = test_data_path(options[:from])
      if !File.exists? test_data
        raise "Test data file '#{test_data}' doesn't exist"
      end
      file.write File.read(test_data)
    else
      file.puts "GivenFilesystem was here"
    end
  end
  @path_elements.pop
  created_path
end