Class: FileSystemCleaner

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

Overview

Usage:

RSpec.configure do |config|

fs_cleaner = FileSystemCleaner.new(
  Rails.root.join("public", "system", "test", "files"),
  Rails.root.join("tmp", "files.bak")
)

config.before(:each) { fs_cleaner.save_files!(example) }
config.after(:each)  { fs_cleaner.restore_files!(example) }

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filesystem_under_test, backup_location) ⇒ FileSystemCleaner

Returns a new instance of FileSystemCleaner.



19
20
21
22
# File 'lib/filesystem_cleaner.rb', line 19

def initialize filesystem_under_test, backup_location
  @filesystem_under_test = filesystem_under_test
  @backup_location       = backup_location
end

Instance Attribute Details

#backup_locationObject (readonly)

Returns the value of attribute backup_location.



17
18
19
# File 'lib/filesystem_cleaner.rb', line 17

def backup_location
  @backup_location
end

#filesystem_under_testObject (readonly)

Returns the value of attribute filesystem_under_test.



17
18
19
# File 'lib/filesystem_cleaner.rb', line 17

def filesystem_under_test
  @filesystem_under_test
end

Instance Method Details

#restore_files!(example) ⇒ Object



29
30
31
32
# File 'lib/filesystem_cleaner.rb', line 29

def restore_files!(example)
  return unless should_perform?(example)
  `rsync -a --delete #{backup_location}/ #{filesystem_under_test}/`
end

#save_files!(example) ⇒ Object



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

def save_files!(example)
  return unless should_perform?(example)
  `rsync -a #{filesystem_under_test}/ #{backup_location}`
end