files 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/Gemfile +9 -0
- data/README.md +35 -0
- data/Rakefile +7 -0
- data/files.gemspec +24 -0
- data/lib/files.rb +58 -0
- data/lib/files/version.rb +3 -0
- data/test/data/cheez_doing_it_wrong.jpg +0 -0
- data/test/files_test.rb +30 -0
- metadata +66 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Files
|
2
|
+
|
3
|
+
*a simple DSL for creating files and directories*
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
require "files"
|
8
|
+
|
9
|
+
files = Files.create do # creates a temporary directory inside Dir.tmpdir
|
10
|
+
file "hello.txt" # creates file "hello.txt" containing "contents of hello.txt"
|
11
|
+
dir "web" do # creates directory "web"
|
12
|
+
file "snippet.html", # creates file "web/snippet.html"...
|
13
|
+
"<h1>Fix this!</h1>" # ...containing "<h1>Fix this!</h1>"
|
14
|
+
dir "img" do # creates directory "web/img"
|
15
|
+
file File.new("data/hello.png") # containing a copy of hello.png
|
16
|
+
file "hi.png", File.new("data/hello.png") # and a copy of hello.png named hi.png
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end # returns a string with the path to the directory
|
20
|
+
|
21
|
+
## Details
|
22
|
+
|
23
|
+
* the directory will be removed at exit
|
24
|
+
* unless you pass `:remove => false` to `Files.new`
|
25
|
+
|
26
|
+
## TODO
|
27
|
+
|
28
|
+
* :path option -- specifying the location of the temporary dir (default: Dir.tmpdir)
|
29
|
+
* take a hash
|
30
|
+
* take a YAML file or string
|
31
|
+
* emit a hash
|
32
|
+
* emit a YAML file or string
|
33
|
+
* symlinks (?)
|
34
|
+
* specify file mode
|
35
|
+
* copy an entire data dir
|
data/Rakefile
ADDED
data/files.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "files/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "files"
|
7
|
+
s.version = Files::VERSION
|
8
|
+
s.authors = ["Alex Chaffee"]
|
9
|
+
s.email = ["alex@stinky.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{a simple DSL for creating files and directories}
|
12
|
+
s.description = %q{Sometimes you want to create a whole bunch of files at once, like when you're testing a tool that processes a whole bunch of files. The Files gem lets you cleanly specify those files and their contents inside your test code, instead of making you 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.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "files"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
data/lib/files.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require "files/version"
|
2
|
+
|
3
|
+
|
4
|
+
module Files
|
5
|
+
def self.create options = {:remove => true}, &block
|
6
|
+
require 'tmpdir'
|
7
|
+
require 'fileutils'
|
8
|
+
|
9
|
+
called_from = File.basename caller.first.split(':').first, ".rb"
|
10
|
+
path = File.join(Dir::tmpdir, "#{called_from}_#{Time.now.to_i}_#{rand(1000)}")
|
11
|
+
|
12
|
+
files = Files.new path, block, options
|
13
|
+
|
14
|
+
files.root
|
15
|
+
end
|
16
|
+
|
17
|
+
class Files
|
18
|
+
|
19
|
+
attr_reader :root
|
20
|
+
|
21
|
+
def initialize path, block, options
|
22
|
+
@root = path
|
23
|
+
@dirs = [path]
|
24
|
+
|
25
|
+
Dir.mkdir(path)
|
26
|
+
at_exit {FileUtils.rm_rf(path) if File.exists?(path)} if options[:remove]
|
27
|
+
|
28
|
+
instance_eval &block
|
29
|
+
end
|
30
|
+
|
31
|
+
def dir name, &block
|
32
|
+
Dir.mkdir "#{current}/#{name}"
|
33
|
+
@dirs << name
|
34
|
+
instance_eval &block
|
35
|
+
end
|
36
|
+
|
37
|
+
def file name, contents = "contents of #{name}"
|
38
|
+
if name.is_a? File
|
39
|
+
FileUtils.cp name.path, current
|
40
|
+
else
|
41
|
+
path = "#{current}/#{name}"
|
42
|
+
if contents.is_a? File
|
43
|
+
FileUtils.cp contents.path, path
|
44
|
+
else
|
45
|
+
File.open(path, "w") do |f|
|
46
|
+
f.write contents
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
def current
|
54
|
+
@dirs.join('/')
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
Binary file
|
data/test/files_test.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "wrong"
|
2
|
+
include Wrong
|
3
|
+
|
4
|
+
here = File.dirname __FILE__
|
5
|
+
$LOAD_PATH.unshift File.join(here, '..', 'lib')
|
6
|
+
require "files"
|
7
|
+
|
8
|
+
dir = Files.create do # creates a temporary directory inside Dir.tmpdir
|
9
|
+
file "hello.txt" # creates file "hello.txt" containing "contents of hello.txt"
|
10
|
+
dir "web" do # creates directory "web"
|
11
|
+
file "snippet.html", # creates file "web/snippet.html"...
|
12
|
+
"<h1>File under F for fantastic!</h1>" # ...containing "<h1>File under F for fantastic!</h1>"
|
13
|
+
dir "img" do # creates directory "web/img"
|
14
|
+
file File.new("#{here}/data/cheez_doing_it_wrong.jpg") # containing a copy of cheez_doing_it_wrong.jpg
|
15
|
+
file "other.jpg", # and a different named file...
|
16
|
+
File.new("#{here}/data/cheez_doing_it_wrong.jpg") # containing the content of cheez_doing_it_wrong.jpg
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
assert { File.read("#{dir}/hello.txt") == "contents of hello.txt" }
|
22
|
+
assert { File.read("#{dir}/web/snippet.html") == "<h1>File under F for fantastic!</h1>" }
|
23
|
+
assert {
|
24
|
+
File.read("#{dir}/web/img/cheez_doing_it_wrong.jpg") ==
|
25
|
+
File.read("#{here}/data/cheez_doing_it_wrong.jpg")
|
26
|
+
}
|
27
|
+
assert {
|
28
|
+
File.read("#{dir}/web/img/other.jpg") ==
|
29
|
+
File.read("#{here}/data/cheez_doing_it_wrong.jpg")
|
30
|
+
}
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: files
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alex Chaffee
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-13 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Sometimes you want to create a whole bunch of files at once, like when
|
15
|
+
you're testing a tool that processes a whole bunch of files. The Files gem lets
|
16
|
+
you cleanly specify those files and their contents inside your test code, instead
|
17
|
+
of making you create a fixture directory and check it in to your repo. It puts them
|
18
|
+
in a temporary directory and cleans up when your test is done.
|
19
|
+
email:
|
20
|
+
- alex@stinky.com
|
21
|
+
executables: []
|
22
|
+
extensions: []
|
23
|
+
extra_rdoc_files: []
|
24
|
+
files:
|
25
|
+
- .gitignore
|
26
|
+
- Gemfile
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- files.gemspec
|
30
|
+
- lib/files.rb
|
31
|
+
- lib/files/version.rb
|
32
|
+
- test/data/cheez_doing_it_wrong.jpg
|
33
|
+
- test/files_test.rb
|
34
|
+
homepage: ''
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
hash: -2492696661746365680
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
hash: -2492696661746365680
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project: files
|
60
|
+
rubygems_version: 1.8.10
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: a simple DSL for creating files and directories
|
64
|
+
test_files:
|
65
|
+
- test/data/cheez_doing_it_wrong.jpg
|
66
|
+
- test/files_test.rb
|