files 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ .idea
data/Gemfile CHANGED
@@ -5,5 +5,5 @@ gemspec
5
5
 
6
6
  group :development do
7
7
  gem "rake"
8
- gem "wrong"
9
- end
8
+ gem "wrong", ">=0.6.2"
9
+ end
data/README.md CHANGED
@@ -22,7 +22,7 @@ The mixin mode is a fairly clean API, suitable for use in unit tests. After `inc
22
22
  end
23
23
  end
24
24
 
25
- files.root # returns the path to the temporary directory, creating it if necessary
25
+ @files.root # returns the path to the temporary directory, creating it if necessary
26
26
 
27
27
  ## Usage (bare function mode)
28
28
 
@@ -42,6 +42,13 @@ In bare function mode, you call the `Files` method, which doesn't pollute the cu
42
42
  end
43
43
  end # "Files" returns a string with the path to the directory
44
44
 
45
+ src = '/path/to/some/data' # use '/path/to/some/data/.' to only copy contents of data folder, not data folder itself.
46
+
47
+ # creates a folder called 'target/$timestamp' relative to pwd
48
+ dir = Files.create :path => "target" do
49
+ dir "foo", :src => src do # creates 'target/$timestamp/foo', and copies src into foo [but should it copy the dir or the contents?]
50
+ end
51
+ end
45
52
 
46
53
  see `test/files_test.rb` for more usage examples
47
54
 
@@ -53,14 +60,13 @@ see `test/files_test.rb` for more usage examples
53
60
  * if the first argument to `file` is a String, then a new file is made
54
61
  * the content of the new file is either a short, descriptive message, or whatever you passed as the second argument
55
62
  * if the argument to `file` is a Ruby `File` object, then it copies the named file into the new location
63
+ * To copy another directory to one created by this module, specifying `:src` to `dir` method
64
+ * [Not sure if this is implemented right... should it copy the *dir* or its *contents*?]
56
65
 
57
66
  ## TODO
58
67
 
59
- * test under Windows
60
- * :path option -- specifying the parent of the temporary dir (default: Dir.tmpdir)
61
68
  * take a hash or a YAML file or YAML string to specify the directory layout and contents
62
69
  * emit a hash or a YAML file or string to serialize the directory layout and contents for later
63
- * copy an entire data dir
64
70
  * support symlinks (?)
65
71
  * specify file write mode (?)
66
72
  * play nice with FakeFS (possibly with a :fake option)
@@ -70,6 +76,10 @@ see `test/files_test.rb` for more usage examples
70
76
 
71
77
  Written by Alex Chaffee <http://alexchaffee.com> <mailto:alex@stinky.com> <http://github.com/alexch> [@alexch](http://twitter.com/alexch)
72
78
 
79
+ Patches by
80
+
81
+ * [Seth Call](http://github.com/sethcall)
82
+
73
83
  ## License [MIT]
74
84
 
75
85
  Copyright (C) 2012 Alex Chaffee
data/Rakefile CHANGED
@@ -3,5 +3,5 @@ here = File.dirname __FILE__
3
3
 
4
4
  task :default => :test
5
5
  task :test do
6
- require "#{here}/test/files_test"
6
+ require File.join(here, "test/files_test")
7
7
  end
@@ -1,6 +1,6 @@
1
1
  require "files/version"
2
2
 
3
- module Files
3
+ module Files
4
4
 
5
5
  # class methods
6
6
 
@@ -9,7 +9,9 @@ module Files
9
9
  end
10
10
 
11
11
  def self.called_from level = 1
12
- File.basename caller[level].split(':').first, ".rb"
12
+ line = caller[level]
13
+ line.gsub!(/^.:/, '') # correct for leading Windows C:
14
+ File.basename line.split(':').first, ".rb"
13
15
  end
14
16
 
15
17
  def self.create options = default_options, &block
@@ -17,8 +19,19 @@ module Files
17
19
  require 'fileutils'
18
20
 
19
21
  name = options[:name]
20
- path = File.join(Dir::tmpdir, "#{name}_#{Time.now.to_i}_#{rand(1000)}")
22
+ root = Dir::tmpdir
21
23
 
24
+ # if the user specified a root directory (instead of default Dir.tmpdir)
25
+ if options[:path]
26
+ # then we will create their directory for them (test context-be friendly)
27
+ root = options[:path]
28
+ FileUtils::mkdir_p(root)
29
+ # if they gave relative path, this forces absolute
30
+ root = File.expand_path(root)
31
+ end
32
+
33
+ path = File.join(root, "#{name}_#{Time.now.to_i}_#{rand(1000)}")
34
+ d{path}
22
35
  Files.new path, block, options
23
36
  end
24
37
 
@@ -43,15 +56,25 @@ module Files
43
56
  def initialize path, block, options
44
57
  @root = path
45
58
  @dirs = []
46
- dir path, &block
47
- @dirs = [path]
59
+ dir nil, &block
48
60
  at_exit {remove} if options[:remove]
49
61
  end
50
62
 
51
- def dir name, &block
52
- path = "#{current}/#{name}"
63
+ # only 1 option supported: 'src'. if specified, is copied into 'name'
64
+ def dir name, options={}, &block
65
+ if name.nil?
66
+ path = current
67
+ else
68
+ path = File.join(current, name)
69
+ end
53
70
  Dir.mkdir path
54
- @dirs << name
71
+ @dirs << name if name
72
+
73
+ if options[:src]
74
+ # copy over remote dir to this one
75
+ FileUtils.cp_r(options[:src], path)
76
+ end
77
+
55
78
  Dir.chdir(path) do
56
79
  instance_eval &block if block
57
80
  end
@@ -64,7 +87,7 @@ module Files
64
87
  FileUtils.cp name.path, current
65
88
  # todo: return path
66
89
  else
67
- path = "#{current}/#{name}"
90
+ path = File.join(current, name)
68
91
  if contents.is_a? File
69
92
  FileUtils.cp contents.path, path
70
93
  else
@@ -82,7 +105,7 @@ module Files
82
105
 
83
106
  private
84
107
  def current
85
- @dirs.join('/')
108
+ File.join @root, *@dirs
86
109
  end
87
110
 
88
111
  end
@@ -1,3 +1,3 @@
1
1
  module Files
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,7 +1,16 @@
1
1
  require "wrong"
2
2
  include Wrong
3
3
 
4
- here = File.dirname __FILE__
4
+ def windows?
5
+ require 'rbconfig'
6
+ RbConfig::CONFIG["host_os"] =~
7
+ %r!(msdos|mswin|djgpp|mingw|[Ww]indows)!
8
+ end
9
+
10
+ Wrong.config.verbose
11
+ Wrong.config.color unless windows?
12
+
13
+ here = File.expand_path(File.dirname __FILE__)
5
14
  $LOAD_PATH.unshift File.join(here, '..', 'lib')
6
15
  require "files"
7
16
 
@@ -29,11 +38,11 @@ assert { dir =~ /^#{Dir::tmpdir}/}
29
38
 
30
39
  assert { File.read("#{dir}/hello.txt") == "contents of hello.txt" }
31
40
  assert { File.read("#{dir}/web/snippet.html") == "<h1>File under F for fantastic!</h1>" }
32
- assert {
41
+ assert {
33
42
  File.read("#{dir}/web/img/cheez_doing_it_wrong.jpg") ==
34
43
  File.read("#{here}/data/cheez_doing_it_wrong.jpg")
35
44
  }
36
- assert {
45
+ assert {
37
46
  File.read("#{dir}/web/img/other.jpg") ==
38
47
  File.read("#{here}/data/cheez_doing_it_wrong.jpg")
39
48
  }
@@ -55,10 +64,8 @@ assert { File.read("#{dir}/hello.txt") == "contents of hello.txt" }
55
64
  assert { File.read("#{dir}/web/hello.html") == "contents of hello.html" }
56
65
  assert { dir.split('/').last =~ /^files_test/ }
57
66
 
58
-
59
67
  assert { Files.called_from(0) == "files_test" }
60
68
 
61
-
62
69
  dir = Files do
63
70
  dir "foo" do
64
71
  file "foo.txt"
@@ -79,6 +86,18 @@ assert { File.read("#{dir}/bar/bar.txt") == "contents of bar.txt" }
79
86
  assert { File.read("#{dir}/bar/baz/baz.txt") == "contents of baz.txt" }
80
87
  assert { File.read("#{dir}/bar/baf/baf.txt") == "contents of baf.txt" }
81
88
 
89
+ # test for data directory copy
90
+ src = File.expand_path("#{here}/data")
91
+
92
+ files = Files.create do
93
+ dir "foo", :src => src do
94
+ # note: I'm not sure if this is desired behavior...
95
+ # shouldn't it put the *contents* of data into foo?
96
+ assert { File.exist?(File.join(Dir.pwd, 'data/cheez_doing_it_wrong.jpg'))}
97
+ end
98
+ end
99
+
100
+ # todo: test :target option
82
101
 
83
102
  dir = Files()
84
103
  assert { File.exist? dir and File.directory? dir}
@@ -130,15 +149,23 @@ class FilesMixinTest
130
149
  assert("the current directory is set inside a nested dir block") { File.read("sub.txt") == "contents of sub.txt" }
131
150
  end
132
151
  end
133
- assert("a file created inside the dir block exists under the root dir") {
134
- File.read("#{@files.root}/bar/bar.txt") == "contents of bar.txt"
152
+ assert("a file created inside the dir block exists under the root dir") {
153
+ File.read("#{@files.root}/bar/bar.txt") == "contents of bar.txt"
135
154
  }
136
-
155
+
137
156
  subdir = dir "baz"
138
157
  assert("the dir method creates the dir") { File.exist?("#{@files.root}/baz")}
139
158
  assert("the dir method returns the created dir") { subdir == "#{@files.root}/baz"}
140
159
  assert { File.directory?("#{@files.root}/baz")}
141
-
160
+
161
+ # this behavior is kind of a bug
162
+ begin
163
+ @content = "breakfast"
164
+ dir "stuff" do
165
+ assert("instance variables are *not* preserved in a dir block") { @content.nil? }
166
+ end
167
+ end
168
+
142
169
  end
143
170
  end
144
171
  FilesMixinTest.new.go
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: files
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-25 00:00:00.000000000Z
12
+ date: 2012-09-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Ever want to create a whole bunch of files at once? Like when you're
15
15
  writing tests for a tool that processes files? The Files gem lets you cleanly specify
@@ -45,7 +45,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
45
45
  version: '0'
46
46
  segments:
47
47
  - 0
48
- hash: -4160850693345767944
48
+ hash: -3557681526261230673
49
49
  required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
@@ -54,10 +54,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
54
  version: '0'
55
55
  segments:
56
56
  - 0
57
- hash: -4160850693345767944
57
+ hash: -3557681526261230673
58
58
  requirements: []
59
59
  rubyforge_project: files
60
- rubygems_version: 1.8.6
60
+ rubygems_version: 1.8.24
61
61
  signing_key:
62
62
  specification_version: 3
63
63
  summary: a simple DSL for creating temporary files and directories