Ever want to create a whole bunch of files at once? Like when you’re
writing tests for a tool that processes files? The Files gem lets you
cleanly specify those files and their contents inside your test code,
instead of forcing you to create a fixture directory and check it in
to your repo. It puts them in a temporary directory and cleans up when
your test is done.
The API currently has two modes – mixin and function – and I would
absolutely adore feedback on which mode is more useful, or other
advice about naming the methods and especially the instance var in
mixin mode.
Usage (mixin mode):
require "files"
include Files
file "hello.txt" # creates file "hello.txt" containing
“contents of hello.txt”
dir "web" do # creates directory "web"
file "snippet.html", # creates file "web/snippet.html"...
"<h1>Fix this!</h1>" # ...containing "<h1>Fix this!</h1>"
dir "img" do # creates directory "web/img"
file File.new("data/hello.png") # containing a copy
of hello.png
file “hi.png”, File.new(“data/hello.png”) # and a copy of
hello.png named hi.png
end
end
Usage (bare function mode)
require "files"
temp_dir = Files do # creates a temporary directory inside
Dir.tmpdir
file “hello.txt” # creates file “hello.txt” containing
“contents of hello.txt”
dir “web” do # creates directory “web”
file “snippet.html”, # creates file “web/snippet.html”…
“
Fix this!
” # …containing “Fix this!
”dir “img” do # creates directory “web/img”
file File.new(“data/hello.png”) # containing a
copy of hello.png
file “hi.png”, File.new(“data/hello.png”) # and a copy of
hello.png named hi.png
end
end
end # “Files” returns a string with the
path to the directory
More docs, examples and tests at https://github.com/alexch/files