maid 0.1.0.beta1

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.
@@ -0,0 +1,151 @@
1
+ require 'spec_helper'
2
+
3
+ module Maid
4
+ describe Maid do
5
+ before :each do
6
+ @logger = mock('Logger')
7
+ @logger.stub!(:progname=)
8
+ Logger.stub!(:new).and_return(@logger)
9
+ end
10
+
11
+ describe '.new' do
12
+ it 'should set up a logger with the default path' do
13
+ Logger.should_receive(:new).with(Maid::DEFAULTS[:log_path])
14
+ Maid.new
15
+ end
16
+
17
+ it 'should set up a logger with the given path, if provided' do
18
+ log_path = '/var/log/maid.log'
19
+ Logger.should_receive(:new).with(log_path)
20
+ Maid.new(:log_path => log_path)
21
+ end
22
+
23
+ it 'should make the log directory in case it does not exist' do
24
+ FileUtils.should_receive(:mkdir_p).with('/home/username/log')
25
+ Maid.new(:log_path => '/home/username/log/maid.log')
26
+ end
27
+
28
+ it 'should set the trash to the default path' do
29
+ maid = Maid.new
30
+ maid.trash_path.should_not be_nil
31
+ maid.trash_path.should == Maid::DEFAULTS[:trash_path]
32
+ end
33
+
34
+ it 'should set the trash to the given path, if provided' do
35
+ trash_path = '/home/username/.local/share/Trash/files/'
36
+ maid = Maid.new(:trash_path => trash_path)
37
+ maid.trash_path.should_not be_nil
38
+ maid.trash_path.should == trash_path
39
+ end
40
+
41
+ it 'should set the progname for the logger' do
42
+ @logger.should_receive(:progname=).with(Maid::DEFAULTS[:progname])
43
+ Maid.new
44
+ end
45
+
46
+ it 'should set the progname for the logger to the given name, if provided' do
47
+ @logger.should_receive(:progname=).with('Fran')
48
+ Maid.new(:progname => 'Fran')
49
+ end
50
+
51
+ it 'should set the file options to the defaults' do
52
+ Maid.new.file_options.should == Maid::DEFAULTS[:file_options]
53
+ end
54
+
55
+ it 'should set the file options to the given options, if provided' do
56
+ maid = Maid.new(:file_options => {:verbose => true})
57
+ maid.file_options.should == {:verbose => true}
58
+ end
59
+
60
+ it 'should set the rules path' do
61
+ Maid.new.rules_path.should == Maid::DEFAULTS[:rules_path]
62
+ end
63
+
64
+ it 'should set the ruels pathto the given path, if provided' do
65
+ maid = Maid.new(:rules_path => 'Maidfile')
66
+ maid.rules_path.should == 'Maidfile'
67
+ end
68
+
69
+ it 'should ignore nil options' do
70
+ maid = Maid.new(:rules_path => nil)
71
+ maid.rules_path.should == Maid::DEFAULTS[:rules_path]
72
+ end
73
+ end
74
+
75
+ describe '#clean' do
76
+ before :each do
77
+ @maid = Maid.new
78
+ @maid.stub!(:add_rules)
79
+ @logger.stub!(:info)
80
+ end
81
+
82
+ it 'should log start and finish' do
83
+ @logger.should_receive(:info).with('Started')
84
+ @logger.should_receive(:info).with('Finished')
85
+ @maid.clean
86
+ end
87
+
88
+ it 'should process the default rules' do
89
+ @maid.should_receive(:add_rules).with(Maid::DEFAULTS[:rules_path])
90
+ @maid.clean
91
+ end
92
+
93
+ it 'should follow the given rules, if provided' do
94
+ @maid.should_receive(:add_rules)
95
+ @maid.should_receive(:follow_rules)
96
+ @maid.clean
97
+ end
98
+ end
99
+
100
+ describe '#add_rules' do
101
+ before :each do
102
+ Kernel.stub!(:require)
103
+ @maid = Maid.new
104
+ end
105
+
106
+ it 'should set the Maid instance' do
107
+ ::Maid.should_receive(:with_instance).with(@maid)
108
+ @maid.add_rules('path')
109
+ end
110
+
111
+ it 'should require the path' do
112
+ path = 'rules.rb'
113
+ Kernel.should_receive(:require).with(path)
114
+ @maid.add_rules(path)
115
+ end
116
+ end
117
+
118
+ describe '#rule' do
119
+ before :each do
120
+ @maid = Maid.new
121
+ end
122
+
123
+ it 'should add a rule to the list of rules' do
124
+ @maid.rules.length.should == 0
125
+
126
+ @maid.rule 'description' do
127
+ 'instructions'
128
+ end
129
+
130
+ @maid.rules.length.should == 1
131
+ @maid.rules.first.description.should == 'description'
132
+ end
133
+ end
134
+
135
+ describe '#follow_rules' do
136
+ it 'should follow each rule' do
137
+ n = 3
138
+ maid = Maid.new
139
+ @logger.should_receive(:info).exactly(n).times
140
+ rules = (1..n).map do |n|
141
+ mock = mock("rule ##{n}", :description => 'description')
142
+ mock.should_receive(:follow)
143
+ mock
144
+ end
145
+ maid.instance_eval { @rules = rules }
146
+
147
+ maid.follow_rules
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ module Maid
4
+ describe Rule do
5
+ it 'should be able to be followed' do
6
+ rule = Rule.new 'my rule', lambda { 1 + 2 }
7
+ rule.follow.should == 3
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,118 @@
1
+ require 'spec_helper'
2
+
3
+ module Maid
4
+ describe Tools do
5
+ before :each do
6
+ @home = File.expand_path('~')
7
+ FileUtils.stub!(:mv)
8
+
9
+ Maid.ancestors.should include(Tools)
10
+ @maid = Maid.new
11
+ @logger = @maid.instance_eval { @logger }
12
+ end
13
+
14
+ describe '#move' do
15
+ before :each do
16
+ @from = '~/Downloads/foo.zip'
17
+ @to = '~/Reference/'
18
+ @options = @maid.file_options
19
+ end
20
+
21
+ it 'should move expanded paths, passing file_options' do
22
+ FileUtils.should_receive(:mv).with("#{@home}/Downloads/foo.zip", "#{@home}/Reference", @options)
23
+ @maid.move('~/Downloads/foo.zip', '~/Reference/')
24
+ end
25
+
26
+ it 'should log the move' do
27
+ @logger.should_receive(:info)
28
+ @maid.move(@from, @to)
29
+ end
30
+
31
+ it 'should not move if the target already exists' do
32
+ File.stub!(:exist?).and_return(true)
33
+ FileUtils.should_not_receive(:mv)
34
+ @logger.should_receive(:warn)
35
+
36
+ @maid.move(@from, @to)
37
+ end
38
+ end
39
+
40
+ describe '#trash' do
41
+ before :each do
42
+ @trash_path = @maid.trash_path
43
+ @path = '~/Downloads/foo.zip'
44
+ end
45
+
46
+ it 'should move the path to the trash' do
47
+ @maid.should_receive(:move).with(@path, @trash_path)
48
+ @maid.trash(@path)
49
+ end
50
+
51
+ it 'should use a safe path if the target exists' do
52
+ Timecop.freeze(Time.parse('2011-05-22T16:53:52-04:00')) do
53
+ File.stub!(:exist?).and_return(true)
54
+ @maid.should_receive(:move).with(@path, "#{@trash_path}/foo.zip 2011-05-22-16-53-52")
55
+ @maid.trash(@path)
56
+ end
57
+ end
58
+ end
59
+
60
+ describe '#dir' do
61
+ it 'should delegate to Dir#[] with an expanded path' do
62
+ Dir.should_receive(:[]).with("#@home/Downloads/*.zip")
63
+ @maid.dir('~/Downloads/*.zip')
64
+ end
65
+ end
66
+
67
+ describe '#find' do
68
+ it 'should delegate to Find.find with an expanded path' do
69
+ f = lambda { }
70
+ Find.should_receive(:find).with("#@home/Downloads/foo.zip", &f)
71
+ @maid.find('~/Downloads/foo.zip', &f)
72
+ end
73
+ end
74
+
75
+ describe '#downloaded_from' do
76
+ it 'should use Spotlight metadata to determine the download site' do
77
+ @maid.should_receive(:cmd).and_return(%Q{(\n "http://www.site.com/foo.zip",\n"http://www.site.com/"\n)})
78
+ @maid.downloaded_from('foo.zip').should == ['http://www.site.com/foo.zip', 'http://www.site.com/']
79
+ end
80
+ end
81
+
82
+ describe '#duration_s' do
83
+ it 'should use Spotlight metadata to determine audio length' do
84
+ @maid.should_receive(:cmd).and_return('235.705')
85
+ @maid.duration_s('foo.mp3').should == 235.705
86
+ end
87
+ end
88
+
89
+ describe '#locate' do
90
+ it 'should use Spotlight to locate a file by name' do
91
+ @maid.should_receive(:cmd).and_return("/a/foo.zip\n/b/foo.zip\n")
92
+ @maid.locate('foo.zip').should == ['/a/foo.zip', '/b/foo.zip']
93
+ end
94
+ end
95
+
96
+ describe '#zipfile_contents' do
97
+ it 'should use unzip to inspect the contents of a .zip file' do
98
+ @maid.should_receive(:cmd).and_return("foo/foo.exe\nfoo/README.txt\n")
99
+ @maid.zipfile_contents('foo.zip').should == ['foo/foo.exe', 'foo/README.txt']
100
+ end
101
+ end
102
+
103
+ describe '#disk_usage' do
104
+ it 'should use du to find the disk usage of a file' do
105
+ @maid.should_receive(:cmd).and_return("136 foo.zip")
106
+ @maid.disk_usage('foo.zip').should == 136
107
+ end
108
+ end
109
+
110
+ describe '#git_piston' do
111
+ it 'should pull and push the given git repository, logging the action' do
112
+ @maid.should_receive(:cmd).with(%Q{cd "#@home/code/projectname" && git pull && git push 2>&1})
113
+ @logger.should_receive(:info)
114
+ @maid.git_piston('~/code/projectname')
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Maid do
4
+ it 'should include Maid::NumericExtensions' do
5
+ 1.minute.should == 60
6
+ end
7
+ end
8
+
9
+ describe Maid, '.with_instance' do
10
+ it 'should temporarily set the instance to the given argument and execute the block' do
11
+ instance = mock('instance')
12
+ Maid.with_instance(instance) { 0 }.should == 0
13
+ Maid.instance_eval { @instance }.should be_nil
14
+ end
15
+ end
16
+
17
+ describe Maid, '.rules' do
18
+ it 'should run in the context of the Maid::Maid instance' do
19
+ instance = mock('instance')
20
+ instance.should_receive(:foo)
21
+
22
+ Maid.with_instance(instance) do
23
+ Maid.rules { foo }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'timecop'
4
+
5
+ require 'maid'
6
+
7
+ Rspec.configure do |c|
8
+ c.mock_with :rspec
9
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maid
3
+ version: !ruby/object:Gem::Version
4
+ hash: 62196401
5
+ prerelease: 6
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ - beta
11
+ - 1
12
+ version: 0.1.0.beta1
13
+ platform: ruby
14
+ authors:
15
+ - Benjamin Oakes
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-05-24 00:00:00 -04:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: thor
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ hash: 43
32
+ segments:
33
+ - 0
34
+ - 14
35
+ - 6
36
+ version: 0.14.6
37
+ type: :runtime
38
+ version_requirements: *id001
39
+ - !ruby/object:Gem::Dependency
40
+ name: rspec
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ hash: 27
48
+ segments:
49
+ - 2
50
+ - 5
51
+ - 0
52
+ version: 2.5.0
53
+ type: :development
54
+ version_requirements: *id002
55
+ - !ruby/object:Gem::Dependency
56
+ name: timecop
57
+ prerelease: false
58
+ requirement: &id003 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ hash: 25
64
+ segments:
65
+ - 0
66
+ - 3
67
+ - 5
68
+ version: 0.3.5
69
+ type: :development
70
+ version_requirements: *id003
71
+ - !ruby/object:Gem::Dependency
72
+ name: ZenTest
73
+ prerelease: false
74
+ requirement: &id004 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ hash: 43
80
+ segments:
81
+ - 4
82
+ - 4
83
+ - 2
84
+ version: 4.4.2
85
+ type: :development
86
+ version_requirements: *id004
87
+ description: Maid cleans up after you, based on rules you define.
88
+ email:
89
+ - hello@benjaminoakes.com
90
+ executables:
91
+ - maid
92
+ extensions: []
93
+
94
+ extra_rdoc_files: []
95
+
96
+ files:
97
+ - .gitignore
98
+ - .rspec
99
+ - Gemfile
100
+ - LICENSE
101
+ - README.rdoc
102
+ - Rakefile
103
+ - bin/maid
104
+ - lib/maid.rb
105
+ - lib/maid/app.rb
106
+ - lib/maid/maid.rb
107
+ - lib/maid/numeric_extensions.rb
108
+ - lib/maid/rule.rb
109
+ - lib/maid/tools.rb
110
+ - lib/maid/version.rb
111
+ - maid.gemspec
112
+ - spec/lib/maid/app_spec.rb
113
+ - spec/lib/maid/maid_spec.rb
114
+ - spec/lib/maid/rule_spec.rb
115
+ - spec/lib/maid/tools_spec.rb
116
+ - spec/lib/maid_spec.rb
117
+ - spec/spec_helper.rb
118
+ has_rdoc: true
119
+ homepage: http://www.benjaminoakes.com/
120
+ licenses: []
121
+
122
+ post_install_message:
123
+ rdoc_options: []
124
+
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: 3
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">"
140
+ - !ruby/object:Gem::Version
141
+ hash: 25
142
+ segments:
143
+ - 1
144
+ - 3
145
+ - 1
146
+ version: 1.3.1
147
+ requirements: []
148
+
149
+ rubyforge_project: maid
150
+ rubygems_version: 1.6.2
151
+ signing_key:
152
+ specification_version: 3
153
+ summary: Maid cleans up after you, based on rules you define.
154
+ test_files:
155
+ - spec/lib/maid/app_spec.rb
156
+ - spec/lib/maid/maid_spec.rb
157
+ - spec/lib/maid/rule_spec.rb
158
+ - spec/lib/maid/tools_spec.rb
159
+ - spec/lib/maid_spec.rb
160
+ - spec/spec_helper.rb