maid 0.1.2 → 0.1.3.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.rspec +3 -0
- data/.travis.yml +1 -0
- data/Guardfile +8 -0
- data/README.md +103 -66
- data/Rakefile +4 -7
- data/Vagrantfile +11 -0
- data/lib/maid.rb +7 -1
- data/lib/maid/app.rb +53 -0
- data/lib/maid/maid.rb +32 -10
- data/lib/maid/numeric_extensions.rb +111 -73
- data/lib/maid/platform.rb +17 -0
- data/lib/maid/rules.sample.rb +44 -35
- data/lib/maid/tools.rb +179 -25
- data/lib/maid/trash_migration.rb +38 -0
- data/lib/maid/version.rb +1 -1
- data/maid.gemspec +16 -5
- data/resources/OneThingWell.png +0 -0
- data/resources/ruby5.gif +0 -0
- data/script/provision +22 -0
- data/spec/lib/maid/app_spec.rb +4 -6
- data/spec/lib/maid/maid_spec.rb +51 -26
- data/spec/lib/maid/numeric_extensions_spec.rb +15 -1
- data/spec/lib/maid/platform_spec.rb +44 -0
- data/spec/lib/maid/tools_spec.rb +233 -31
- data/spec/lib/maid/trash_migration_spec.rb +76 -0
- data/spec/lib/maid_spec.rb +4 -1
- data/spec/spec_helper.rb +9 -1
- metadata +147 -47
- data/CONTRIBUTING.rdoc +0 -25
- data/Gemfile.lock +0 -32
- data/configure +0 -7
- data/script/micro-maid.sh +0 -18
@@ -0,0 +1,38 @@
|
|
1
|
+
# Migrate trash to correct directory on Linux due to a configuration bug in previous releases.
|
2
|
+
#
|
3
|
+
# It used to be that the default trash path was the same on every platform, so everything used to go to `~/.Trash` regardless of OS. (For what it's worth, that used to be the correct trash path on older releases of Ubuntu.)
|
4
|
+
module Maid
|
5
|
+
module TrashMigration
|
6
|
+
class << self
|
7
|
+
def incorrect_trash
|
8
|
+
File.expand_path('~/.Trash') + '/'
|
9
|
+
end
|
10
|
+
|
11
|
+
def correct_trash
|
12
|
+
Maid.new.trash_path
|
13
|
+
end
|
14
|
+
|
15
|
+
def needed?
|
16
|
+
Platform.linux? &&
|
17
|
+
File.directory?(incorrect_trash) &&
|
18
|
+
!ENV['MAID_NO_MIGRATE_TRASH']
|
19
|
+
end
|
20
|
+
|
21
|
+
def perform
|
22
|
+
maid = ::Maid::Maid.new(:trash_path => correct_trash)
|
23
|
+
# Use local variable so it's available in the closure used by `instance_eval`
|
24
|
+
path = incorrect_trash
|
25
|
+
|
26
|
+
# Might as well use Maid itself for this :)
|
27
|
+
maid.instance_eval do
|
28
|
+
rule 'Migrate Linux trash to correct path' do
|
29
|
+
trash(dir("#{ path }/*"))
|
30
|
+
trash(path)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
maid.clean
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/maid/version.rb
CHANGED
data/maid.gemspec
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
$:.push File.expand_path("../lib", __FILE__)
|
3
3
|
require "maid/version"
|
4
|
+
require "maid/platform"
|
4
5
|
|
5
6
|
Gem::Specification.new do |s|
|
6
7
|
s.name = "maid"
|
@@ -14,11 +15,21 @@ Gem::Specification.new do |s|
|
|
14
15
|
|
15
16
|
s.rubyforge_project = "maid"
|
16
17
|
|
17
|
-
s.add_dependency('thor', '~> 0.
|
18
|
-
s.
|
19
|
-
s.
|
20
|
-
s.add_development_dependency('
|
21
|
-
s.add_development_dependency('
|
18
|
+
s.add_dependency('thor', '~> 0.16.0')
|
19
|
+
s.add_dependency('deprecated', '~> 3.0.1')
|
20
|
+
s.add_dependency('xdg', '~> 2.2.2') # Only used on Linux, but still required/tested on OSX
|
21
|
+
s.add_development_dependency('guard', '~> 1.4.0')
|
22
|
+
s.add_development_dependency('guard-rspec', '~> 2.1.0')
|
23
|
+
s.add_development_dependency('rake', '~> 0.9.2.2')
|
24
|
+
s.add_development_dependency('rspec', '~> 2.11.0')
|
25
|
+
s.add_development_dependency('timecop', '~> 0.5.2')
|
26
|
+
s.add_development_dependency('fakefs', '~> 0.4.0')
|
27
|
+
|
28
|
+
if Maid::Platform.linux?
|
29
|
+
s.add_development_dependency('rb-inotify', '~> 0.8.8')
|
30
|
+
elsif Maid::Platform.osx?
|
31
|
+
s.add_development_dependency('rb-fsevent', '~> 0.9.2')
|
32
|
+
end
|
22
33
|
|
23
34
|
s.files = `git ls-files`.split("\n")
|
24
35
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
Binary file
|
data/resources/ruby5.gif
ADDED
Binary file
|
data/script/provision
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
# A simple shell-based provisioner for Vagrant.
|
3
|
+
#
|
4
|
+
# Documentation: [Shell Provisioner](http://vagrantup.com/v1/docs/provisioners/shell.html)
|
5
|
+
set -o errexit
|
6
|
+
|
7
|
+
sudo apt-get update
|
8
|
+
|
9
|
+
# ## Dependencies
|
10
|
+
sudo apt-get install -y ruby1.9.1
|
11
|
+
|
12
|
+
# ## Development dependencies
|
13
|
+
#
|
14
|
+
# For building `maid-x.y.z.gem`
|
15
|
+
sudo apt-get install -y git-core
|
16
|
+
# For building `ffi` for `guard`'s soft dependency on `rb-inotify`
|
17
|
+
sudo apt-get install -y make ruby1.9.1-dev libffi-dev
|
18
|
+
|
19
|
+
# ## RubyGems
|
20
|
+
cd /vagrant
|
21
|
+
sudo gem install bundler
|
22
|
+
bundle install
|
data/spec/lib/maid/app_spec.rb
CHANGED
@@ -26,10 +26,13 @@ module Maid
|
|
26
26
|
@app.stub!(:maid_options)
|
27
27
|
@app.stub!(:say)
|
28
28
|
|
29
|
+
TrashMigration.stub(:needed?) { false }
|
30
|
+
|
29
31
|
# NOTE It's pretty important that this is stubbed, unless you want your rules to be run over and over when you test!
|
30
32
|
@maid = mock('Maid')
|
31
33
|
@maid.stub!(:clean)
|
32
34
|
@maid.stub!(:log_device)
|
35
|
+
@maid.stub!(:load_rules)
|
33
36
|
Maid.stub!(:new).and_return(@maid)
|
34
37
|
end
|
35
38
|
|
@@ -63,12 +66,6 @@ module Maid
|
|
63
66
|
end
|
64
67
|
end
|
65
68
|
|
66
|
-
describe App, 'sample rules' do
|
67
|
-
it 'should be able to run' do
|
68
|
-
lambda { App.start(%w[clean --silent --noop --rules=lib/maid/rules.sample.rb]) }.should_not raise_error(SyntaxError)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
69
|
describe App, '#version' do
|
73
70
|
it 'should print out the gem version' do
|
74
71
|
app = App.new
|
@@ -85,6 +82,7 @@ module Maid
|
|
85
82
|
it 'should log to STDOUT for testing purposes when given noop' do
|
86
83
|
opts = @app.maid_options('noop' => true)
|
87
84
|
opts[:file_options][:noop].should be_true
|
85
|
+
opts[:logger].should be_false
|
88
86
|
opts[:log_device].should == STDOUT
|
89
87
|
opts[:log_formatter].call(nil, nil, nil, 'hello').should == "hello\n"
|
90
88
|
end
|
data/spec/lib/maid/maid_spec.rb
CHANGED
@@ -3,8 +3,7 @@ require 'spec_helper'
|
|
3
3
|
module Maid
|
4
4
|
describe Maid do
|
5
5
|
before :each do
|
6
|
-
@logger =
|
7
|
-
@logger.stub!(:progname=)
|
6
|
+
@logger = double('Logger').as_null_object
|
8
7
|
Logger.stub!(:new).and_return(@logger)
|
9
8
|
FileUtils.stub(:mkdir_p)
|
10
9
|
end
|
@@ -26,16 +25,55 @@ module Maid
|
|
26
25
|
Maid.new(:log_device => '/home/username/log/maid.log')
|
27
26
|
end
|
28
27
|
|
29
|
-
it 'should
|
30
|
-
|
31
|
-
|
32
|
-
maid
|
33
|
-
|
34
|
-
|
28
|
+
it 'should take a logger object during intialization' do
|
29
|
+
Logger.unstub!(:new)
|
30
|
+
maid = Maid.new(:logger => @logger)
|
31
|
+
maid.logger.should == @logger
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'platform-specific behavior' do
|
35
|
+
before do
|
36
|
+
Platform.stub(:linux?)
|
37
|
+
Platform.stub(:osx?)
|
38
|
+
@home = File.expand_path('~')
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when running on Linux' do
|
42
|
+
before do
|
43
|
+
Platform.stub(:linux?) { true }
|
44
|
+
XDG.stub(:[]).with('DATA_HOME') { "#{ @home }/.local/share" }
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should set the trash to the correct default path' do
|
48
|
+
trash_path = "#{ @home }/.local/share/Trash/files/"
|
49
|
+
FileUtils.should_receive(:mkdir_p).with(trash_path).once
|
50
|
+
maid = Maid.new
|
51
|
+
maid.trash_path.should == trash_path
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when running on OS X' do
|
56
|
+
before do
|
57
|
+
Platform.stub(:osx?) { true }
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should set the trash to the correct default path' do
|
61
|
+
trash_path = "#{ @home }/.Trash/"
|
62
|
+
FileUtils.should_receive(:mkdir_p).with(trash_path).once
|
63
|
+
maid = Maid.new
|
64
|
+
maid.trash_path.should == trash_path
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'when running on an unsupported platform' do
|
69
|
+
it 'does not implement trashing files' do
|
70
|
+
lambda { Maid.new }.should raise_error(NotImplementedError)
|
71
|
+
end
|
72
|
+
end
|
35
73
|
end
|
36
74
|
|
37
75
|
it 'should set the trash to the given path, if provided' do
|
38
|
-
trash_path = '/home/username
|
76
|
+
trash_path = '/home/username/tmp/my_trash/'
|
39
77
|
FileUtils.should_receive(:mkdir_p).with(trash_path).once
|
40
78
|
maid = Maid.new(:trash_path => trash_path)
|
41
79
|
maid.trash_path.should_not be_nil
|
@@ -79,7 +117,6 @@ module Maid
|
|
79
117
|
describe '#clean' do
|
80
118
|
before :each do
|
81
119
|
@maid = Maid.new
|
82
|
-
@maid.stub!(:add_rules)
|
83
120
|
@logger.stub!(:info)
|
84
121
|
end
|
85
122
|
|
@@ -89,19 +126,13 @@ module Maid
|
|
89
126
|
@maid.clean
|
90
127
|
end
|
91
128
|
|
92
|
-
it 'should
|
93
|
-
@maid.should_receive(:add_rules).with(Maid::DEFAULTS[:rules_path])
|
94
|
-
@maid.clean
|
95
|
-
end
|
96
|
-
|
97
|
-
it 'should follow the given rules, if provided' do
|
98
|
-
@maid.should_receive(:add_rules)
|
129
|
+
it 'should follow the given rules' do
|
99
130
|
@maid.should_receive(:follow_rules)
|
100
131
|
@maid.clean
|
101
132
|
end
|
102
133
|
end
|
103
134
|
|
104
|
-
describe '#
|
135
|
+
describe '#load_rules' do
|
105
136
|
before :each do
|
106
137
|
Kernel.stub!(:load)
|
107
138
|
@maid = Maid.new
|
@@ -109,19 +140,13 @@ module Maid
|
|
109
140
|
|
110
141
|
it 'should set the Maid instance' do
|
111
142
|
::Maid.should_receive(:with_instance).with(@maid)
|
112
|
-
@maid.
|
113
|
-
end
|
114
|
-
|
115
|
-
it 'should load the path' do
|
116
|
-
path = 'rules.rb'
|
117
|
-
Kernel.should_receive(:load).with(path)
|
118
|
-
@maid.add_rules(path)
|
143
|
+
@maid.load_rules
|
119
144
|
end
|
120
145
|
|
121
146
|
it 'should give an error on STDERR if there is a LoadError' do
|
122
147
|
Kernel.stub!(:load).and_raise(LoadError)
|
123
148
|
STDERR.should_receive(:puts)
|
124
|
-
@maid.
|
149
|
+
@maid.load_rules
|
125
150
|
end
|
126
151
|
end
|
127
152
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Maid::NumericExtensions, '#since?' do
|
3
|
+
describe Maid::NumericExtensions::Time, '#since?' do
|
4
4
|
it 'should tell you that 1 week ago happened after 2 weeks ago' do
|
5
5
|
(1.week.since? 2.weeks.ago).should be_true
|
6
6
|
end
|
@@ -9,3 +9,17 @@ describe Maid::NumericExtensions, '#since?' do
|
|
9
9
|
(2.week.since? 1.weeks.ago).should be_false
|
10
10
|
end
|
11
11
|
end
|
12
|
+
|
13
|
+
describe Maid::NumericExtensions::SizeToKb do
|
14
|
+
it 'should tell you that 1 megabyte equals 1024 kilobytes' do
|
15
|
+
(1.megabyte == 1024.kilobytes).should be_true
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should tell you that 1 gigabyte equals 1024 megabytes' do
|
19
|
+
(1.gigabyte == 1024.megabytes).should be_true
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should tell you that 1 terabyte equals 1024 gigabytes' do
|
23
|
+
(1.terabyte == 1024.gigabytes).should be_true
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Maid
|
4
|
+
describe Platform do
|
5
|
+
def stub_host_os(value)
|
6
|
+
RbConfig::CONFIG.stub(:[]).with('host_os') { value }
|
7
|
+
end
|
8
|
+
|
9
|
+
describe 'determining the host OS' do
|
10
|
+
it 'delegates to RbConfig' do
|
11
|
+
stub_host_os('foo')
|
12
|
+
subject.host_os.should == 'foo'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when running on Ubuntu' do
|
17
|
+
before do
|
18
|
+
stub_host_os('linux-gnu')
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'is identified as Linux' do
|
22
|
+
subject.linux?.should be_true
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'is not identified as OS X' do
|
26
|
+
subject.osx?.should be_false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'when running on Mac OS X' do
|
31
|
+
before do
|
32
|
+
stub_host_os('darwin10.0')
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'is not identified as Linux' do
|
36
|
+
subject.linux?.should be_false
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'is identified as OS X' do
|
40
|
+
subject.osx?.should be_true
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/lib/maid/tools_spec.rb
CHANGED
@@ -1,75 +1,192 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
module Maid
|
4
|
-
|
4
|
+
# NOTE: Please use FakeFS instead of mocking and stubbing specific calls which happen to modify the filesystem.
|
5
|
+
#
|
6
|
+
# More info:
|
7
|
+
#
|
8
|
+
# * [FakeFS](https://github.com/defunkt/fakefs)
|
9
|
+
describe Tools, :fakefs => true do
|
5
10
|
before :each do
|
6
11
|
@home = File.expand_path('~')
|
7
|
-
|
12
|
+
@now = Time.now
|
8
13
|
|
9
14
|
Maid.ancestors.should include(Tools)
|
10
|
-
|
11
|
-
@logger
|
15
|
+
|
16
|
+
@logger = double('Logger').as_null_object
|
17
|
+
@maid = Maid.new(:logger => @logger)
|
18
|
+
|
19
|
+
# Prevent warnings from showing when testing deprecated methods:
|
20
|
+
@maid.stub(:__deprecated_run_action__)
|
21
|
+
|
22
|
+
# For safety, stub `cmd` to prevent running commands:
|
23
|
+
@maid.stub(:cmd)
|
12
24
|
end
|
13
25
|
|
14
26
|
describe '#move' do
|
15
27
|
before :each do
|
16
|
-
@
|
17
|
-
@
|
18
|
-
@
|
28
|
+
@src_file = (@src_dir = '~/Source/') + (@file_name = 'foo.zip')
|
29
|
+
FileUtils.mkdir_p(@src_dir)
|
30
|
+
FileUtils.touch(@src_file)
|
31
|
+
FileUtils.mkdir_p(@dst_dir = '~/Destination/')
|
19
32
|
end
|
20
33
|
|
21
34
|
it 'should move expanded paths, passing file_options' do
|
22
|
-
|
23
|
-
|
35
|
+
@maid.move(@src_file, @dst_dir)
|
36
|
+
File.exists?(@dst_dir + @file_name).should be_true
|
24
37
|
end
|
25
38
|
|
26
39
|
it 'should log the move' do
|
27
40
|
@logger.should_receive(:info)
|
28
|
-
@maid.move(@
|
41
|
+
@maid.move(@src_file, @dst_dir)
|
29
42
|
end
|
30
43
|
|
31
44
|
it 'should not move if the target already exists' do
|
32
|
-
|
33
|
-
FileUtils.should_not_receive(:mv)
|
45
|
+
FileUtils.touch(@dst_dir + @file_name)
|
34
46
|
@logger.should_receive(:warn)
|
35
47
|
|
36
|
-
@maid.move(@
|
48
|
+
@maid.move(@src_file, @dst_dir)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should handle multiple from paths' do
|
52
|
+
second_src_file = @src_dir + (second_file_name = 'bar.zip')
|
53
|
+
FileUtils.touch(second_src_file)
|
54
|
+
src_files = [@src_file, second_src_file]
|
55
|
+
|
56
|
+
@maid.move(src_files, @dst_dir)
|
57
|
+
File.exist?(@dst_dir + @file_name).should be_true
|
58
|
+
File.exist?(@dst_dir + second_file_name).should be_true
|
37
59
|
end
|
38
60
|
end
|
39
61
|
|
40
62
|
describe '#trash' do
|
41
63
|
before :each do
|
42
64
|
@trash_path = @maid.trash_path
|
43
|
-
@
|
65
|
+
@src_file = (@src_dir = '~/Source/') + (@file_name = 'foo.zip')
|
66
|
+
FileUtils.mkdir_p(@src_dir)
|
67
|
+
FileUtils.touch(@src_file)
|
68
|
+
|
69
|
+
@trash_file = File.join(@trash_path, @file_name)
|
44
70
|
end
|
45
71
|
|
46
72
|
it 'should move the path to the trash' do
|
47
|
-
@maid.
|
48
|
-
|
73
|
+
@maid.trash(@src_file)
|
74
|
+
File.exist?(@trash_file).should be_true
|
49
75
|
end
|
50
76
|
|
51
77
|
it 'should use a safe path if the target exists' do
|
52
78
|
# Without an offset, ISO8601 parses to local time, which is what we want here.
|
53
79
|
Timecop.freeze(Time.parse('2011-05-22T16:53:52')) do
|
54
|
-
|
55
|
-
@maid.
|
56
|
-
|
80
|
+
FileUtils.touch(@trash_file)
|
81
|
+
@maid.trash(@src_file)
|
82
|
+
new_trash_file = File.join(@trash_path, @file_name + ' 2011-05-22-16-53-52')
|
83
|
+
File.exist?(new_trash_file).should be_true
|
57
84
|
end
|
58
85
|
end
|
86
|
+
|
87
|
+
it 'should handle multiple paths' do
|
88
|
+
second_src_file = @src_dir + (second_file_name = 'bar.zip')
|
89
|
+
FileUtils.touch(second_src_file)
|
90
|
+
@src_files = [@src_file, second_src_file]
|
91
|
+
@maid.trash(@src_files)
|
92
|
+
|
93
|
+
second_trash_file = File.join(@trash_path, second_file_name)
|
94
|
+
File.exist?(@trash_file).should be_true
|
95
|
+
File.exist?(second_trash_file).should be_true
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'should remove files greater then the remove option size' do
|
99
|
+
@maid.stub!(:disk_usage).and_return(1025)
|
100
|
+
@maid.trash(@src_file, :remove_over => 1.mb)
|
101
|
+
File.exist?(@src_file).should_not be_true
|
102
|
+
File.exist?(@trash_file).should_not be_true
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'should trash files less then the remove option size' do
|
106
|
+
@maid.stub!(:disk_usage).and_return(1023)
|
107
|
+
@maid.trash(@src_file, :remove_over => 1.mb)
|
108
|
+
File.exist?(@trash_file).should be_true
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe '#remove' do
|
113
|
+
before :each do
|
114
|
+
@src_file = (@src_dir = '~/Source/') + (@file_name = 'foo.zip')
|
115
|
+
FileUtils.mkdir_p(@src_dir)
|
116
|
+
FileUtils.touch(@src_file)
|
117
|
+
@src_file_expand_path = File.expand_path(@src_file)
|
118
|
+
@options = @maid.file_options
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'should remove expanded paths, passing options' do
|
122
|
+
@maid.remove(@src_file)
|
123
|
+
File.exist?(@src_file).should be_false
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'should log the remove' do
|
127
|
+
@logger.should_receive(:info)
|
128
|
+
@maid.remove(@src_file)
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'should set the secure option' do
|
132
|
+
@options = @options.merge(:secure => true)
|
133
|
+
FileUtils.should_receive(:rm_r).with(@src_file_expand_path, @options)
|
134
|
+
@maid.remove(@src_file, :secure => true)
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'should set the force option' do
|
138
|
+
@options = @options.merge(:force => true)
|
139
|
+
FileUtils.should_receive(:rm_r).with(@src_file_expand_path, @options)
|
140
|
+
@maid.remove(@src_file, :force => true)
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'should handle multiple paths' do
|
144
|
+
second_src_file = @src_dir + (second_file_name = 'bar.zip')
|
145
|
+
FileUtils.touch(second_src_file)
|
146
|
+
@src_files = [@src_file, second_src_file]
|
147
|
+
|
148
|
+
@maid.remove(@src_files)
|
149
|
+
File.exist?(@src_file).should be_false
|
150
|
+
File.exist?(second_src_file).should be_false
|
151
|
+
end
|
59
152
|
end
|
60
153
|
|
61
154
|
describe '#dir' do
|
62
|
-
|
63
|
-
|
64
|
-
|
155
|
+
before :each do
|
156
|
+
@file = (dir = "#@home/Downloads/") + 'foo.zip'
|
157
|
+
FileUtils.mkdir_p(dir)
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'lists files in a directory' do
|
161
|
+
FileUtils.touch(@file)
|
162
|
+
@maid.dir('~/Downloads/*.zip').should == [@file]
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe '#mkdir' do
|
167
|
+
it 'should create a directory successfully' do
|
168
|
+
@maid.mkdir('~/Downloads/Music/Pink.Floyd')
|
169
|
+
File.exist?("#@home/Downloads/Music/Pink.Floyd").should be_true
|
65
170
|
end
|
66
171
|
end
|
67
172
|
|
68
173
|
describe '#find' do
|
174
|
+
before :each do
|
175
|
+
@file = (@dir = '~/Source/') + (@file_name = 'foo.zip')
|
176
|
+
FileUtils.mkdir_p(@dir)
|
177
|
+
FileUtils.touch(@file)
|
178
|
+
@dir_expand_path = File.expand_path(@dir)
|
179
|
+
@file_expand_path = File.expand_path(@file)
|
180
|
+
end
|
181
|
+
|
69
182
|
it 'should delegate to Find.find with an expanded path' do
|
70
183
|
f = lambda { }
|
71
|
-
Find.should_receive(:find).with(
|
72
|
-
@maid.find(
|
184
|
+
Find.should_receive(:find).with(@file_expand_path, &f)
|
185
|
+
@maid.find(@file, &f)
|
186
|
+
end
|
187
|
+
|
188
|
+
it "should return an array of all the files' names when no block is given" do
|
189
|
+
@maid.find(@dir).should == [@dir_expand_path, @file_expand_path]
|
73
190
|
end
|
74
191
|
end
|
75
192
|
|
@@ -82,7 +199,7 @@ module Maid
|
|
82
199
|
|
83
200
|
describe '#downloaded_from' do
|
84
201
|
it 'should determine the download site' do
|
85
|
-
@maid.should_receive(:cmd).and_return(%
|
202
|
+
@maid.should_receive(:cmd).and_return(%((\n "http://www.site.com/foo.zip",\n"http://www.site.com/"\n)))
|
86
203
|
@maid.downloaded_from('foo.zip').should == ['http://www.site.com/foo.zip', 'http://www.site.com/']
|
87
204
|
end
|
88
205
|
end
|
@@ -103,25 +220,110 @@ module Maid
|
|
103
220
|
|
104
221
|
describe '#disk_usage' do
|
105
222
|
it 'should give the disk usage of a file' do
|
106
|
-
@maid.should_receive(:cmd).and_return(
|
223
|
+
@maid.should_receive(:cmd).and_return('136 foo.zip')
|
107
224
|
@maid.disk_usage('foo.zip').should == 136
|
108
225
|
end
|
226
|
+
|
227
|
+
context 'when the file does not exist' do
|
228
|
+
it 'raises an error' do
|
229
|
+
@maid.should_receive(:cmd).and_return('du: cannot access `foo.zip\': No such file or directory')
|
230
|
+
lambda { @maid.disk_usage('foo.zip') }.should raise_error(RuntimeError)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
describe '#created_at' do
|
236
|
+
before do
|
237
|
+
@path = "~/test.txt"
|
238
|
+
end
|
239
|
+
|
240
|
+
it 'should give the created time of the file' do
|
241
|
+
Timecop.freeze(@now) do
|
242
|
+
FileUtils.touch(File.expand_path(@path))
|
243
|
+
@maid.created_at(@path).should == Time.now
|
244
|
+
end
|
245
|
+
end
|
109
246
|
end
|
110
247
|
|
111
|
-
describe '#
|
248
|
+
describe '#accessed_at' do
|
249
|
+
# FakeFS does not implement atime.
|
112
250
|
it 'should give the last accessed time of the file' do
|
113
|
-
|
114
|
-
|
115
|
-
|
251
|
+
File.should_receive(:atime).with("#@home/foo.zip").and_return(@now)
|
252
|
+
@maid.accessed_at('~/foo.zip').should == @now
|
253
|
+
end
|
254
|
+
|
255
|
+
it 'should trigger deprecation warning when last_accessed is used, but still run' do
|
256
|
+
File.should_receive(:atime).with("#@home/foo.zip").and_return(@now)
|
257
|
+
@maid.should have_deprecated_method(:last_accessed)
|
258
|
+
@maid.last_accessed('~/foo.zip').should == @now
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
describe '#modified_at' do
|
263
|
+
before do
|
264
|
+
@path = "~/test.txt"
|
265
|
+
FileUtils.touch(File.expand_path(@path))
|
266
|
+
end
|
267
|
+
|
268
|
+
it 'should give the modified time of the file' do
|
269
|
+
Timecop.freeze(@now) do
|
270
|
+
File.open(@path, 'w') { |f| f.write('Test') }
|
271
|
+
end
|
272
|
+
|
273
|
+
# use to_i to ignore milliseconds during test
|
274
|
+
@maid.modified_at(@path).to_i.should == @now.to_i
|
116
275
|
end
|
117
276
|
end
|
118
277
|
|
119
278
|
describe '#git_piston' do
|
279
|
+
it 'is deprecated' do
|
280
|
+
@maid.should have_deprecated_method(:git_piston)
|
281
|
+
@maid.git_piston('~/code/projectname')
|
282
|
+
end
|
283
|
+
|
120
284
|
it 'should pull and push the given git repository, logging the action' do
|
121
|
-
@maid.should_receive(:cmd).with(%
|
285
|
+
@maid.should_receive(:cmd).with(%(cd "#@home/code/projectname" && git pull && git push 2>&1))
|
122
286
|
@logger.should_receive(:info)
|
123
287
|
@maid.git_piston('~/code/projectname')
|
124
288
|
end
|
125
289
|
end
|
290
|
+
|
291
|
+
describe '#sync' do
|
292
|
+
before :each do
|
293
|
+
@src_dir = '~/Downloads/'
|
294
|
+
@dst_dir = '~/Reference'
|
295
|
+
end
|
296
|
+
|
297
|
+
it 'should sync the expanded paths, retaining backslash' do
|
298
|
+
@maid.should_receive(:cmd).with(%(rsync -a -u "#@home/Downloads/" "#@home/Reference" 2>&1))
|
299
|
+
@maid.sync(@src_dir, @dst_dir)
|
300
|
+
end
|
301
|
+
|
302
|
+
it 'should log the action' do
|
303
|
+
@logger.should_receive(:info)
|
304
|
+
@maid.sync(@src_dir, @dst_dir)
|
305
|
+
end
|
306
|
+
|
307
|
+
it 'should have no options' do
|
308
|
+
@maid.should_receive(:cmd).with(%(rsync "#@home/Downloads/" "#@home/Reference" 2>&1))
|
309
|
+
@maid.sync(@src_dir, @dst_dir, :archive => false, :update => false)
|
310
|
+
end
|
311
|
+
|
312
|
+
it 'should add all options' do
|
313
|
+
@maid.should_receive(:cmd).with(%(rsync -a -v -u -m --exclude=".git" --delete "#@home/Downloads/" "#@home/Reference" 2>&1))
|
314
|
+
@maid.sync(@src_dir, @dst_dir, :archive => true, :update => true, :delete => true, :verbose => true, :prune_empty => true, :exclude => '.git')
|
315
|
+
end
|
316
|
+
|
317
|
+
it 'should add multiple exlcude options' do
|
318
|
+
@maid.should_receive(:cmd).with(%(rsync -a -u --exclude=".git" --exclude=".rvmrc" "#@home/Downloads/" "#@home/Reference" 2>&1))
|
319
|
+
@maid.sync(@src_dir, @dst_dir, :exclude => ['.git', '.rvmrc'])
|
320
|
+
end
|
321
|
+
|
322
|
+
it 'should add noop option' do
|
323
|
+
@maid.file_options[:noop] = true
|
324
|
+
@maid.should_receive(:cmd).with(%(rsync -a -u -n "#@home/Downloads/" "#@home/Reference" 2>&1))
|
325
|
+
@maid.sync(@src_dir, @dst_dir)
|
326
|
+
end
|
327
|
+
end
|
126
328
|
end
|
127
329
|
end
|