diff_dirs 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Carl Mercier
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = DiffDirs
2
+
3
+ Helper to diff two directories
4
+
5
+ = Example
6
+ require 'rubygems'
7
+ require 'diff_dirs'
8
+ diff_dirs "~/dir1", "~/dir2"
9
+
10
+ == Dependencies (for tests only)
11
+
12
+ - jeremymcanally-context or shoulda
13
+ - giraffesoft-zebra
14
+ - mocha
15
+ - ruby-debug
16
+
17
+ == COPYRIGHT
18
+
19
+ Copyright (c) 2009 {Carl Mercier}[http://carlmercier.com]. See LICENSE for details.
@@ -0,0 +1,52 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'lib' << 'test'
6
+ t.pattern = 'test/**/*_test.rb'
7
+ t.verbose = false
8
+ end
9
+
10
+ task :default => :test
11
+
12
+ begin
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gemspec|
15
+ gemspec.name = "diff_dirs"
16
+ gemspec.summary = "Ruby helper to diff two directories"
17
+ gemspec.email = "carl@carlmercier.com"
18
+ gemspec.homepage = "http://github.com/cmer/diff_dirs"
19
+ gemspec.description = "Ruby helper to diff two directories"
20
+ gemspec.authors = ["Carl Mercier"]
21
+ gemspec.files = FileList["[A-Z]*", "{bin,generators,lib,test}/**/*"]
22
+ gemspec.rubyforge_project = 'diffdirs'
23
+ end
24
+ rescue LoadError
25
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
26
+ end
27
+
28
+ begin
29
+ require 'rake/contrib/sshpublisher'
30
+ namespace :rubyforge do
31
+
32
+ desc "Release gem and RDoc documentation to RubyForge"
33
+ task :release => ["rubyforge:release:gem", "rubyforge:release:docs"]
34
+
35
+ namespace :release do
36
+ desc "Publish RDoc to RubyForge."
37
+ task :docs => [:rdoc] do
38
+ config = YAML.load(
39
+ File.read(File.expand_path('~/.rubyforge/user-config.yml'))
40
+ )
41
+
42
+ host = "#{config['username']}@rubyforge.org"
43
+ remote_dir = "/var/www/cmercier/diff_dirs/"
44
+ local_dir = 'rdoc'
45
+
46
+ Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
47
+ end
48
+ end
49
+ end
50
+ rescue LoadError
51
+ puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,60 @@
1
+ class DiffDirs
2
+ def self.diff_dirs(dir1, dir2)
3
+ raise(ArgumentError, "Directories must be strings.") unless dir1.is_a?(String) && dir2.is_a?(String)
4
+
5
+ dir1 = expand_path(dir1)
6
+ dir2 = expand_path(dir2)
7
+
8
+ cmd = "diff -qr #{dir1} #{dir2}"
9
+ diff_result = execute(cmd)
10
+ out = []
11
+ diff_result.each_line do |line|
12
+ line.chomp!
13
+ out << diff_result_line_parse(dir1, dir2, line)
14
+ end
15
+ out
16
+ end
17
+
18
+ protected
19
+ def self.diff_result_line_parse(dir1, dir2, line)
20
+ # New/Deleted files
21
+ match = line.match(/^Only in ([^:]*): ([^$]*)$/)
22
+ if match
23
+ if match[1] == dir1
24
+ return [:deleted, match[2]] # deleted from second dir
25
+ elsif match[1] == dir2
26
+ return [:new, match[2]] # new in second dir
27
+ else
28
+ raise "#{dir1} or #{dir2} didn't match #{match[1]}"
29
+ end
30
+
31
+ elsif line.match(/^Files\s/) && line.match(/\sdiffer$/)
32
+ # Example: Files api/Rakefile and testing/Rakefile differ
33
+ # This should be done with a nice regexp but couldn't figure out how to match everything but the word "and"
34
+ files = line.sub(/^Files\s/, "").sub(/\sdiffer$/,"").split(" and ")
35
+ return [:modified] << remove_dir_from_path(files[0], dir1)
36
+ end
37
+
38
+ raise RuntimeError, "Cannot parse: #{line}"
39
+ end
40
+
41
+ def self.execute(cmd)
42
+ `#{cmd}`
43
+ end
44
+
45
+ def self.remove_dir_from_path(path, dir)
46
+ dir += "/" unless dir[-1..-1] == "/"
47
+ path.sub(Regexp.new("^#{dir}"), "")
48
+ end
49
+
50
+ def self.expand_path(dir)
51
+ if dir[0..0] == "~"
52
+ File.expand_path(dir)
53
+ else
54
+ dir
55
+ end
56
+ end
57
+ end
58
+
59
+ public
60
+ def diff_dirs(dir1, dir2); DiffDirs::diff_dirs(dir1, dir2); end
@@ -0,0 +1,70 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require File.dirname(__FILE__) + '/../diff_dirs'
3
+
4
+ class DiffDirsTest < Test::Unit::TestCase
5
+ context "diff executable" do
6
+ should "be found by which" do
7
+ assert_not_equal "", `which diff`
8
+ end
9
+ end
10
+
11
+ context "diff_dirs method" do
12
+ should "correctly parse diff output and return proper array" do
13
+ diff_output = "Only in dir1: .git\nOnly in dir1: Capfile\nOnly in dir2: Capfile2\nFiles dir1/Rakefile and dir2/Rakefile differ\nFiles dir1/lib/format.rb and dir2/lib/format.rb differ\n"
14
+ DiffDirs.expects(:execute).with("diff -qr dir1 dir2").returns(diff_output)
15
+ assert_equal [[:deleted, ".git"],
16
+ [:deleted, "Capfile"],
17
+ [:new, "Capfile2"],
18
+ [:modified, "Rakefile"],
19
+ [:modified, "lib/format.rb"]], DiffDirs::diff_dirs("dir1", "dir2")
20
+ end
21
+
22
+ should "fail if provided with non-string arguments" do
23
+ assert_raise(ArgumentError) { diff_dirs(nil, "") }
24
+ assert_raise(ArgumentError) { diff_dirs("", nil) }
25
+ end
26
+
27
+ should "expand paths if starts with ~" do
28
+
29
+ end
30
+ end
31
+
32
+ context "diff_result_line_parse method" do
33
+ setup do
34
+ @d1 = "dir1"
35
+ @d2 = "dir2"
36
+ @diff_output = "Only in dir1: .git\nOnly in dir1: Capfile\nOnly in dir2: Capfile2\nFiles dir1/Rakefile and dir2/Rakefile differ\nFiles dir1/lib/format.rb and dir2/lib/format.rb differ\n".split("\n")
37
+ end
38
+
39
+ should "correctly identify new files" do
40
+ assert_equal [:new, "Capfile2"], DiffDirs.send(:diff_result_line_parse, @d1, @d2, @diff_output[2])
41
+ end
42
+
43
+ should "correctly identify deleted files" do
44
+ assert_equal [:deleted, ".git"], DiffDirs.send(:diff_result_line_parse, @d1, @d2, @diff_output[0])
45
+ assert_equal [:deleted, "Capfile"], DiffDirs.send(:diff_result_line_parse, @d1, @d2, @diff_output[1])
46
+ end
47
+
48
+ should "correctly identify modified files" do
49
+ assert_equal [:modified, "Rakefile"], DiffDirs.send(:diff_result_line_parse, @d1, @d2, @diff_output[3])
50
+ assert_equal [:modified, "lib/format.rb"], DiffDirs.send(:diff_result_line_parse, @d1, @d2, @diff_output[4])
51
+ end
52
+ end
53
+
54
+ context "remove_dir_from_path method" do
55
+ should "correctly remove dir not having a trailing slash" do
56
+ assert_equal "lib/format.rb", DiffDirs.send(:remove_dir_from_path, "dir1/lib/format.rb", "dir1")
57
+ end
58
+
59
+ should "correctly remove dir having a trailing slash" do
60
+ assert_equal "lib/format.rb", DiffDirs.send(:remove_dir_from_path, "dir1/lib/format.rb", "dir1/")
61
+ end
62
+ end
63
+
64
+ context "expand_path method" do
65
+ should "expand path when starting with ~" do
66
+ assert_equal "/tmp", DiffDirs.send(:expand_path, "/tmp")
67
+ assert_not_equal "~/", DiffDirs.send(:expand_path, "~/")
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'context'
4
+ require 'zebra'
5
+ require 'mocha'
6
+ require 'ruby-debug'
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: diff_dirs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Carl Mercier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-12 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Ruby helper to diff two directories
17
+ email: carl@carlmercier.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION
30
+ - lib/diff_dirs.rb
31
+ - test/diff_dirs_test.rb
32
+ - test/test_helper.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/cmer/diff_dirs
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project: diffdirs
57
+ rubygems_version: 1.3.2
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Ruby helper to diff two directories
61
+ test_files:
62
+ - test/diff_dirs_test.rb
63
+ - test/test_helper.rb