spwn 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ spec/tmp
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in spwn.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+ #
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # Spwn
2
+ Spwn is a testing library for random file production. It basically
3
+ allows you to quickly throw some files up, test your methods on
4
+ them, and quickly clean up your filestructure.
5
+
6
+ ## Installation
7
+
8
+ Throw `spwn` in your Gemfile:
9
+
10
+ ```ruby
11
+ gem 'spwn'
12
+ ```
13
+
14
+ and bundle
15
+
16
+ ```ruby
17
+ $ bundle
18
+ ```
19
+
20
+ Or install it yourself as:
21
+
22
+ ```bash
23
+ $ gem install spwn
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ ### Returning something useful
29
+
30
+ > This currently is the source of a major issue. Each time a File object
31
+ > is created a file pointer is used. The user woudl need to manually
32
+ > `f.close` each file. I am currently building a seperate library to
33
+ > create a `File` replacement that doesn't immediately open a file
34
+ > buffer and has a more robust set of useful istance methods.
35
+
36
+ In almost all the following methods, you will find either a [Dir][]
37
+ or [File][] object returned to you. Why do this instead of return a
38
+ path to the file? Easy! You can do cool stuff like this now:
39
+ [Dir]: http://www.ruby-doc.org/core-1.9.3/Dir.html
40
+ [File]: http://www.ruby-doc.org/core-1.9.3/File.html
41
+
42
+ ```ruby
43
+ # Look! You can still get the path...
44
+ Uber.pwd.path #=> "/home/michael/code"
45
+ # ...but you can also iterate through files as well now!
46
+ Uber.pwd.each { |item| puts 'item' }
47
+ ```
48
+ which would produce
49
+ ```ruby
50
+ .
51
+ ..
52
+ migrateServer
53
+ superSecretProject
54
+ ```
55
+ or with `File` ...
56
+ ```ruby
57
+ # return the file size in bytes
58
+ Uber.file('Gemfile').size #=> 171
59
+ # change file permissions (I believe this needs root permissions)
60
+ Uber.file('Gemfile').chmod('0644')
61
+ ```
62
+ You can also use the returned File or Dir class in that respective
63
+ class's class methods!
64
+ ```ruby
65
+ gemfile = Uber.file('Gemfile')
66
+ File.executable?(gemfile) #=> false
67
+ ```
68
+
69
+ ### Spawning Files
70
+ ```ruby
71
+ Spwn.file #=> <File:file1.html>
72
+ Spwn.file #=> <File:file2.md>
73
+ ```
74
+
75
+ ```ruby
76
+ Spwn.file( ext: 'md' ) #=> <File:file3.rb>
77
+ Spwn.file( 'index.html' ) #=> <File:index.html>
78
+ Spwn.file( count: 3 ) #=> [<File:file4.md>,<File:file5.txt>,<File:file6.rb>]
79
+ ```
80
+
81
+ ```ruby
82
+ Spwn.dir #=> <Dir:dir1>
83
+ Spwn.dir( 'spec' ) #=> <Dir:spec>
84
+ ```
85
+
86
+ ```ruby
87
+ #spawn a dir and then cd inside the dir to do stuff
88
+ Spwn.dir( 'scripts' ) do
89
+ Spwn.dir( 'vendor' )
90
+ Spwn.file( 'app.js' )
91
+ Dir.pwd() #=> '.../mySite/scripts'
92
+ end
93
+ ```
94
+
95
+ ### removing spawned files
96
+ All done testing and need to clean up those files? Just run
97
+
98
+ ```ruby
99
+ Spwn.clean( 'spec/tmp' )
100
+ ```
101
+
102
+ to empty the directory completely.
103
+
104
+ ## Contributing
105
+ 1. Fork it
106
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
107
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
108
+ 4. Push to the branch (`git push origin my-new-feature`)
109
+ 5. Create new Pull Request
110
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/spwn.rb ADDED
@@ -0,0 +1,104 @@
1
+ require "spwn/version"
2
+
3
+ class Spwn
4
+ class << self
5
+ attr_accessor :extensions
6
+ attr_reader :dirs, :files
7
+ end
8
+ @extensions = [
9
+ 'html',
10
+ 'htm',
11
+ 'txt',
12
+ 'rb',
13
+ 'sh',
14
+ 'md',
15
+ 'markdown'
16
+ ]
17
+ @dirs = []
18
+ @files = []
19
+ #@last_dir_inc = {}
20
+ #@last_file_inc = {}
21
+
22
+
23
+ # Spwn.file( name , options={} )
24
+ def self.file( *args )
25
+ options = args.last.is_a?(Hash) ? args.last : {}
26
+ return spawn_multiples( :file, *args ) unless options[:count].nil?
27
+ name = args.first.is_a?(String) ? args.first : 'file'
28
+ ext = options[:ext] || @extensions.sample
29
+ @files << new_file = File.new( next_filename(name, ext), 'w')
30
+ new_file
31
+ end
32
+
33
+ # Spwn.dir( name , options={} )
34
+ def self.dir( *args, &block )
35
+ options = args.last.is_a?(Hash) ? args.last : {}
36
+ return spawn_multiples( :dir, *args, &block ) unless options[:count].nil?
37
+ name = args.first.is_a?(String) ? args.first : 'dir'
38
+
39
+ digit = @dirs.empty? ? '1' : next_digit( @dirs.last )
40
+ path = File.join(Dir.pwd, "#{name}#{digit}")
41
+ Dir.mkdir(path)
42
+ @dirs << new_dir = Dir.new(path)
43
+ if block_given?
44
+ origin = Dir.pwd
45
+ Dir.chdir(new_dir)
46
+ yield new_dir
47
+ Dir.chdir(origin)
48
+ end
49
+ new_dir
50
+ end
51
+
52
+ def self.clean( path )
53
+ # commenting out until I make a replacement for the File object
54
+ #
55
+ # if path.nil?
56
+ # #clean all known files you have spawn
57
+ # @files.each { |file| File.delete(file) }
58
+ # @dirs.each { |dir| Dir.rmdir(dir) }
59
+ # else
60
+ #empty the given directory
61
+ path = File.expand_path(path)
62
+
63
+ #delete all files and directories
64
+ Dir.glob(File.join(path,'**','*')).reverse_each do |file|
65
+ if File.file?(file)
66
+ File.delete(file)
67
+ elsif File.directory?(file)
68
+ Dir.rmdir(file)
69
+ end
70
+ end
71
+ # end
72
+ @files = []
73
+ @dirs = []
74
+ end
75
+
76
+ private
77
+ def self.spawn_multiples( method, *args, &block )
78
+ raise ArgumentError "missing options hash" unless args.last.is_a? Hash
79
+ count = args.last[:count]
80
+ raise ArgumentError "count can't be less than 1" if count < 1
81
+ args.last[:count] = nil
82
+ items = []
83
+ count.times do
84
+ items << self.send( method, *args, &block )
85
+ end
86
+ items
87
+ end
88
+ def self.next_digit(item)
89
+ File.basename(item).match(/\d+/)[0].next
90
+ end
91
+ def self.next_filename(name, ext)
92
+ digit = @files.empty? ? '1' : next_digit( @files.last )
93
+ "#{name}#{digit}.#{ext}"
94
+ end
95
+ def self.split_filename(fullname)
96
+ #fullname = string.match(/\A\.?\w+/).to_s
97
+ split = [] << name = fullname.match(/\A\.?[a-zA-Z_]+/)
98
+ split << digit = fullname.match(/\d+\Z/)
99
+ #split << ext = string.match(/(?<=[^\A]\.)\w+\Z/)
100
+ split.each_index { |i| split[i] = split[i].to_s if split[i].is_a? MatchData }
101
+ split[1] = split[1].to_i unless split[1].nil?
102
+ return split
103
+ end
104
+ end
@@ -0,0 +1,3 @@
1
+ class Spwn
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require "pry"
8
+ ROOT = Dir.pwd
9
+ TESTDIR = File.join(ROOT,'spec/tmp')
10
+
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+
16
+ # Run specs in random order to surface order dependencies. If you find an
17
+ # order dependency and want to debug it, you can fix the order by providing
18
+ # the seed, which is printed after each run.
19
+ # --seed 1234
20
+ config.order = 'random'
21
+ end
data/spec/spwn_spec.rb ADDED
@@ -0,0 +1,183 @@
1
+ require 'spec_helper'
2
+ require 'spwn'
3
+
4
+ describe Spwn do
5
+ before(:all) { Dir.chdir(TESTDIR) }
6
+ after(:each) { Spwn.clean(TESTDIR) }
7
+ after(:all) { Dir.chdir(ROOT) }
8
+
9
+ it "should have accessible extensions" do
10
+ old_exts = Spwn.extensions
11
+ new_exts = ['haml', 'slim', 'yaml', 'json']
12
+ Spwn.extensions = new_exts
13
+ Spwn.extensions.should == new_exts
14
+ Spwn.extensions = old_exts
15
+ end
16
+
17
+ context '#file' do
18
+ it "should be instance of File" do
19
+ Spwn.file.should be_an_instance_of File
20
+ end
21
+ it "should spawn a file" do
22
+ file = Spwn.file
23
+ Dir.entries(Dir.pwd).should include file.path
24
+ end
25
+ it "should be included in the @files array" do
26
+ Spwn.files.should include Spwn.file
27
+ end
28
+ it "should have a common file name" do
29
+ File.basename(Spwn.file).should match /\Afile\d+/
30
+ end
31
+ it "should increment the file name" do
32
+ pattern = /\Afile(\d+)(?:.\w*)?/
33
+ Spwn.file
34
+ expect {
35
+ 3.times { Spwn.file }
36
+ }.to change {
37
+ # the digit in the filename for the last spawned file
38
+ File.basename(Spwn.files.last).match(pattern)[1].to_i
39
+ }.from(1).to(4)
40
+ end
41
+ it "should allow you to define a name" do
42
+ file = Spwn.file('candy', ext: 'html')
43
+ File.basename(file).should == 'candy1.html'
44
+ end
45
+ it "should use a random extension" do
46
+ ext = File.extname(Spwn.file).delete '.'
47
+ Spwn.extensions.should include ext
48
+ end
49
+ context "options[:ext]" do
50
+ it "should allow the user to define an extension" do
51
+ ext = File.extname(Spwn.file( ext: 'rb' ))
52
+ ext.should == '.rb'
53
+ end
54
+ end
55
+ context "options[:count]" do
56
+ it "should return an array" do
57
+ Spwn.file(count: 3).should be_an_instance_of Array
58
+ end
59
+ it "should spawn that many files" do
60
+ expect { Spwn.file(count: 3) }.to change {
61
+ Dir.entries(Dir.pwd).length
62
+ }.by(3)
63
+ end
64
+ end
65
+ end
66
+
67
+ context 'dir' do
68
+ subject(:dir) { Spwn.dir }
69
+ it { should be_instance_of(Dir) }
70
+ it "should spawn a dir" do
71
+ dir()
72
+ Dir.entries(Dir.pwd).should include File.basename(dir)
73
+ end
74
+ it "should be included in the @dirs array" do
75
+ Spwn.dirs.should include dir
76
+ end
77
+ it "should have a common dir name" do
78
+ File.basename(dir).should match /\Adir\d+/
79
+ end
80
+ it "should increment the dir name" do
81
+ pattern = /\Adir(\d+)/
82
+ Spwn.dir
83
+ expect {
84
+ 3.times { Spwn.dir }
85
+ }.to change {
86
+ # the digit in the filename for the last spawned file
87
+ File.basename(Spwn.dirs.last).match(pattern)[1].to_i
88
+ }.from(1).to(4)
89
+ end
90
+ it "should cd into the dir if a block is given" do
91
+ Spwn.dir do |directory|
92
+ Dir.pwd.should == directory.path
93
+ end
94
+ end
95
+ context "options[:count]" do
96
+ it "should trigger spawn_multiples" do
97
+ Spwn.should_receive(:spawn_multiples).with(:dir, count:3)
98
+ Spwn.dir(count: 3)
99
+ end
100
+ it "can accept multiple arguments"do
101
+ Spwn.should_receive(:spawn_multiples).with(:dir, 'candy', count: 3)
102
+ Spwn.dir('candy', count: 3) do
103
+ Spwn.file
104
+ end
105
+ end
106
+ it "should accept a block" do
107
+ Spwn.should_receive(:file).exactly(3).times
108
+ Spwn.dir(count: 3) do |dir|
109
+ Spwn.file
110
+ end
111
+ end
112
+ it "should return an Array" do
113
+ Spwn.dir(count: 3).should be_an_instance_of Array
114
+ end
115
+ it "should spawn multiple dirs" do
116
+ expect { Spwn.dir(count: 3) }.to change{
117
+ Dir.entries(Dir.pwd).length
118
+ }.by(3)
119
+ end
120
+ end
121
+ end
122
+
123
+ context "clean" do
124
+ before(:each) do
125
+ Spwn.dir(count: 4) do
126
+ Spwn.file(count: 3)
127
+ end
128
+ clean
129
+ end
130
+ context "with directory" do
131
+ subject(:clean) { Spwn.clean(TESTDIR) }
132
+ it "can remove everything under a directory" do
133
+ testdir = Dir.entries(TESTDIR) - ['.','..']
134
+ testdir.should be_empty
135
+ end
136
+ it "should empty the @dirs array" do
137
+ Spwn.dirs.should be_empty
138
+ end
139
+ it "should emtpy the @files array" do
140
+ Spwn.files.should be_empty
141
+ end
142
+ end
143
+ #context "without directory" do
144
+ # subject(:clean) { Spwn.clean() }
145
+ # it "can remove everything it remembers" do
146
+ # testdir = Dir.entries(TESTDIR) - ['.','..']
147
+ # testdir.should be_empty
148
+ # end
149
+ # it "should empty the @dirs array" do
150
+ # Spwn.dirs.should be_empty
151
+ # end
152
+ # it "should emtpy the @files array" do
153
+ # Spwn.files.should be_empty
154
+ # end
155
+ #end
156
+ end
157
+ context "split_filename" do
158
+ context "with a normal file" do
159
+ subject() { Spwn.split_filename("super_file21") }
160
+ it "should return an Array" do
161
+ subject.should be_an_instance_of Array
162
+ end
163
+ it "should return a file name" do
164
+ subject[0].should == "super_file"
165
+ end
166
+ it "should return a file number" do
167
+ subject[1].should == 21
168
+ end
169
+ end
170
+ context "with a dot file" do
171
+ subject() { Spwn.split_filename(".gitignore") }
172
+ it "should return an Array" do
173
+ subject.should be_an_instance_of Array
174
+ end
175
+ it "should return a file name" do
176
+ subject[0].should == ".gitignore"
177
+ end
178
+ it "should not return a digit" do
179
+ subject[1].should be_nil
180
+ end
181
+ end
182
+ end
183
+ end
data/spwn.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/spwn/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Michael Mullins"]
6
+ gem.email = ["mcmullins@mail.umhb.edu"]
7
+ gem.summary = %q{Spwn is a small tool for producing random files and directories for testing purposes}
8
+ gem.description = %q{Spwn is a small tool for producing random files and directories for testing purposes}
9
+ gem.homepage = "http://www.github.com/mcmullins/spwn"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "spwn"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Spwn::VERSION
17
+ gem.add_development_dependency 'guard'
18
+ gem.add_development_dependency 'guard-rspec'
19
+ gem.add_development_dependency 'rspec'
20
+ gem.add_development_dependency 'rb-inotify'
21
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spwn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Mullins
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: guard
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: guard-rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rb-inotify
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Spwn is a small tool for producing random files and directories for testing
79
+ purposes
80
+ email:
81
+ - mcmullins@mail.umhb.edu
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .rspec
88
+ - Gemfile
89
+ - Guardfile
90
+ - LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - lib/spwn.rb
94
+ - lib/spwn/version.rb
95
+ - spec/spec_helper.rb
96
+ - spec/spwn_spec.rb
97
+ - spwn.gemspec
98
+ homepage: http://www.github.com/mcmullins/spwn
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.24
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Spwn is a small tool for producing random files and directories for testing
122
+ purposes
123
+ test_files:
124
+ - spec/spec_helper.rb
125
+ - spec/spwn_spec.rb