Skip to content

Commit

Permalink
Merge pull request #1 from sethcall/master
Browse files Browse the repository at this point in the history
Implemented :path for Files.create, and :src for data directory copy on 'dir' method.
  • Loading branch information
alexch committed Jul 12, 2012
2 parents be68ec7 + 9107f3a commit 98d97b2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
17 changes: 15 additions & 2 deletions README.md
@@ -1,3 +1,9 @@
# Why this fork?
* To implement relative directory creation (instead of always Dir.tmpdir)
> Implemented by specifying `:path` to `Files.create`, with string path
* To implement copy of another directory to one created by this module
> Implemented by specifying `:src` to `dir` method, with string path to src dir
# Files

*a simple DSL for creating temporary files and directories*
Expand Down Expand Up @@ -42,6 +48,13 @@ In bare function mode, you call the `Files` method, which doesn't pollute the cu
end
end # "Files" returns a string with the path to the directory

src = '/path/to/some/data' # use '/path/to/some/data/.' to only copy contents of data folder, not data folder itself.

# creates a folder called 'target/$timestamp' relative to pwd
dir = Files.create :path => "target" do
dir "foo", :src => src do # creates 'target/$timestamp/foo', and copies src into foo
end
end

see `test/files_test.rb` for more usage examples

Expand All @@ -57,10 +70,10 @@ see `test/files_test.rb` for more usage examples
## TODO

* test under Windows
* :path option -- specifying the parent of the temporary dir (default: Dir.tmpdir)
* :path option -- specifying the parent of the temporary dir (default: Dir.tmpdir) -- IMPLEMENTED ON THIS FORK. pass :path to Files.create
* take a hash or a YAML file or YAML string to specify the directory layout and contents
* emit a hash or a YAML file or string to serialize the directory layout and contents for later
* copy an entire data dir
* copy an entire data dir -- IMPLEMENTED ON THIS FORK. pass :src to dir
* support symlinks (?)
* specify file write mode (?)
* play nice with FakeFS (possibly with a :fake option)
Expand Down
24 changes: 21 additions & 3 deletions lib/files.rb
@@ -1,6 +1,6 @@
require "files/version"

module Files
module Files

# class methods

Expand All @@ -17,7 +17,18 @@ def self.create options = default_options, &block
require 'fileutils'

name = options[:name]
path = File.join(Dir::tmpdir, "#{name}_#{Time.now.to_i}_#{rand(1000)}")

root = Dir::tmpdir
# if the user specified a root directory (instead of default Dir.tmpdir)
if options[:path]
# then we will create their directory for them (test context-be friendly)
root = options[:path]
FileUtils::mkdir_p(root)
# if they gave relative path, this forces absolute
root = File.expand_path(root)
end

path = File.join(root, "#{name}_#{Time.now.to_i}_#{rand(1000)}")

Files.new path, block, options
end
Expand Down Expand Up @@ -48,10 +59,17 @@ def initialize path, block, options
at_exit {remove} if options[:remove]
end

def dir name, &block
# only 1 option supported: 'src'. if specified, is copied into 'name'
def dir name, options={}, &block
path = "#{current}/#{name}"
Dir.mkdir path
@dirs << name

if options[:src]
# copy over remote dir to this one
FileUtils.cp_r(options[:src], path)
end

Dir.chdir(path) do
instance_eval &block if block
end
Expand Down
9 changes: 9 additions & 0 deletions test/files_test.rb
Expand Up @@ -75,6 +75,15 @@
end
end

# test for data directory copy
src = File.expand_path("#{here}/data")

dir = Files.create :path => "target" do
dir "foo", :src => src do
# TODO write assertions
end
end

assert { File.read("#{dir}/foo/foo.txt") == "contents of foo.txt" }
assert { File.read("#{dir}/bar/bar.txt") == "contents of bar.txt" }
assert { File.read("#{dir}/bar/baz/baz.txt") == "contents of baz.txt" }
Expand Down

0 comments on commit 98d97b2

Please sign in to comment.