Skip to content

Commit

Permalink
Document Filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Nov 10, 2011
1 parent bd9b01e commit b8f7cbf
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions features/support/filesystem.rb
@@ -1,27 +1,49 @@
require 'fileutils' require 'fileutils'
require 'open4' require 'open4'


# The Filesystem module runs commands, captures their output and exit status
# and lets the host know about it.
#
module Filesystem module Filesystem
# Writes a file with some contents.
#
# @param [String] filename
# The file name to write.
#
# @param [String] contents
# The contents to include in the file.
#
# @api public
def write_file(filename, contents) def write_file(filename, contents)
in_current_dir do in_current_dir do
mkdir(File.dirname(filename)) mkdir(File.dirname(filename))
File.open(filename, 'w') { |f| f << contents } File.open(filename, 'w') { |f| f << contents }
end end
end end


# Executes a code block within a particular directory.
#
# @param [Proc] block
# The block to execute
#
# @api public
def in_current_dir(&block) def in_current_dir(&block)
mkdir(current_dir) mkdir(current_dir)
Dir.chdir(current_dir, &block) Dir.chdir(current_dir, &block)
end end


def current_dir # Runs a command in the current directory.
File.join(*dirs) #
end # It populates the following instance variables:

#
def dirs # * @stdout - The standard output captured from the process.
['tmp/fs'] # * @stderr - The standard error captured from the process.
end # * @last_exit_status - The process exit status.

#
# @param [String] command
# The command to run.
#
# @api public
def run(command) def run(command)
in_current_dir do in_current_dir do
pid = Open4.popen4(command) do |pid, stdin, stdout, stderr| pid = Open4.popen4(command) do |pid, stdin, stdout, stderr|
Expand All @@ -32,7 +54,18 @@ def run(command)
end end
end end


private

def mkdir(dirname) def mkdir(dirname)
FileUtils.mkdir_p(dirname) unless File.directory?(dirname) FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
end end

def current_dir
File.join(*dirs)
end

def dirs
['tmp/fs']
end

end end

0 comments on commit b8f7cbf

Please sign in to comment.