Author: lacton
Date: Sun Oct 19 07:09:44 2008
New Revision: 706017
URL: http://svn.apache.org/viewvc?rev=706017&view=rev
Log:
BUILDR-159 Improved 'check' to accept both tar and tgz archives
Modified:
incubator/buildr/trunk/CHANGELOG
incubator/buildr/trunk/doc/pages/testing.textile
incubator/buildr/trunk/lib/buildr/packaging/tar.rb
incubator/buildr/trunk/spec/core/checks_spec.rb
Modified: incubator/buildr/trunk/CHANGELOG
URL:
http://svn.apache.org/viewvc/incubator/buildr/trunk/CHANGELOG?rev=706017&r1=706016&r2=706017&view=diff
==============================================================================
--- incubator/buildr/trunk/CHANGELOG (original)
+++ incubator/buildr/trunk/CHANGELOG Sun Oct 19 07:09:44 2008
@@ -1,4 +1,5 @@
1.3.4 (Pending)
+* Added: BUILDR-159 Improved 'check' to accept both tar and tgz archives.
* Change: Upgraded to use Rake 0.8.3, RSpec 1.1.8.
* Change: Introduced new options from Rake 0.8.3: -I (libdir), -R (rakelib),
--rules, --no-search, --silent.
Modified: incubator/buildr/trunk/doc/pages/testing.textile
URL:
http://svn.apache.org/viewvc/incubator/buildr/trunk/doc/pages/testing.textile?rev=706017&r1=706016&r2=706017&view=diff
==============================================================================
--- incubator/buildr/trunk/doc/pages/testing.textile (original)
+++ incubator/buildr/trunk/doc/pages/testing.textile Sun Oct 19 07:09:44 2008
@@ -195,7 +195,7 @@
| @empty@ | Given a file task checks that the file (or directory) is empty. |
| @contain@ | Given a file task referencing a file, checks its contents, using
string or regular expression. For a file task referencing a directory, checks
that it contains the specified files; global patterns using @*@ and @**@ are
allowed. |
-All these matchers operate against a file task. If you run them against a
ZipTask (including JAR, WAR, etc) they can also check the contents of the ZIP
file. And as you can see in the examples above, you can also run them against
a path in a ZIP file, checking its contents as if it was a directory, or
against an entry in a ZIP file, checking the content of that file.
+All these matchers operate against a file task. If you run them against a
ZipTask (including JAR, WAR, etc) or a TarTask, they can also check the
contents of the archive. And as you can see in the examples above, you can
also run them against a path in an archive, checking its contents as if it was
a directory, or against an entry in an archive, checking the content of that
file.
p(note). The @package@ method returns a package task based on packaging type,
identifier, group, version and classifier. The last four are inferred, but if
you create a package with different specifications (for example, you specify a
classifier) your checks must call @package@ with the same qualifying arguments
to return the very same package task.
Modified: incubator/buildr/trunk/lib/buildr/packaging/tar.rb
URL:
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/packaging/tar.rb?rev=706017&r1=706016&r2=706017&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/packaging/tar.rb (original)
+++ incubator/buildr/trunk/lib/buildr/packaging/tar.rb Sun Oct 19 07:09:44 2008
@@ -45,6 +45,35 @@
self.mode = '0755'
end
+ # :call-seq:
+ # entry(name) => Entry
+ #
+ # Returns a Tar file entry. You can use this to check if the entry exists
and its contents,
+ # for example:
+ # package(:tar).entry("src/LICENSE").should contain(/Apache Software
License/)
+ def entry(entry_name)
+ Buildr::TarEntry.new(self, entry_name)
+ end
+
+ def entries() #:nodoc:
+ tar_entries = nil
+ with_uncompressed_tar { |tar| tar_entries = tar.entries }
+ tar_entries
+ end
+
+ # :call-seq:
+ # with_uncompressed_tar { |tar_entries| ... }
+ #
+ # Yields an Archive::Tar::Minitar::Input object to the provided block.
+ # Opening, closing and Gzip-decompressing is automatically taken care of.
+ def with_uncompressed_tar &block
+ if gzip
+ Zlib::GzipReader.open(name) { |tar| Archive::Tar::Minitar.open(tar,
&block) }
+ else
+ Archive::Tar::Minitar.open(name, &block)
+ end
+ end
+
private
def create_from(file_map)
@@ -81,7 +110,60 @@
end
end
-
+
+
+ class TarEntry #:nodoc:
+
+ def initialize(tar_task, entry_name)
+ @tar_task = tar_task
+ @entry_name = entry_name
+ end
+
+ # :call-seq:
+ # contain?(*patterns) => boolean
+ #
+ # Returns true if this Tar file entry matches against all the arguments.
An argument may be
+ # a string or regular expression.
+ def contain?(*patterns)
+ content = read_content_from_tar
+ patterns.map { |pattern| Regexp === pattern ? pattern :
Regexp.new(Regexp.escape(pattern.to_s)) }.
+ all? { |pattern| content =~ pattern }
+ end
+
+ # :call-seq:
+ # empty?() => boolean
+ #
+ # Returns true if this entry is empty.
+ def empty?()
+ read_content_from_tar.nil?
+ end
+
+ # :call-seq:
+ # exist() => boolean
+ #
+ # Returns true if this entry exists.
+ def exist?()
+ exist = false
+ @tar_task.with_uncompressed_tar { |tar| tar.any? { |entry| exist =
entry.name == @entry_name } }
+ exist
+ end
+
+ def to_s #:nodoc:
+ @entry_name
+ end
+
+ private
+
+ def read_content_from_tar
+ content = Errno::ENOENT.new("No such file or directory - [EMAIL
PROTECTED]")
+ @tar_task.with_uncompressed_tar do |tar|
+ content = tar.inject(content) { |content, entry| entry.name ==
@entry_name ? entry.read : content }
+ end
+ raise content if Exception === content
+ content
+ end
+ end
+
end
Modified: incubator/buildr/trunk/spec/core/checks_spec.rb
URL:
http://svn.apache.org/viewvc/incubator/buildr/trunk/spec/core/checks_spec.rb?rev=706017&r1=706016&r2=706017&view=diff
==============================================================================
--- incubator/buildr/trunk/spec/core/checks_spec.rb (original)
+++ incubator/buildr/trunk/spec/core/checks_spec.rb Sun Oct 19 07:09:44 2008
@@ -181,43 +181,6 @@
end
lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
end
-
- it "should pass if ZIP path exists" do
- write "resources/test"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).path("resources")) { it.should exist }
- end
- lambda { project("foo").task("package").invoke }.should_not raise_error
- end
-
- it "should fail if ZIP path does not exist" do
- mkpath "resources"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar)) { it.path("not-resources").should exist }
- end
- lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
- end
-
- it "should pass if ZIP entry exists" do
- write "resources/test"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).entry("resources/test")) { it.should exist }
- check(package(:jar).path("resources").entry("test")) { it.should exist }
- end
- lambda { project("foo").task("package").invoke }.should_not raise_error
- end
-
- it "should fail if ZIP path does not exist" do
- mkpath "resources"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).entry("resources/test")) { it.should exist }
- end
- lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
- end
end
@@ -261,52 +224,6 @@
end
lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
end
-
- it "should pass if ZIP path is empty" do
- mkpath "resources"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).path("resources")) { it.should be_empty }
- end
- lambda { project("foo").task("package").invoke }.should_not raise_error
- end
-
- it "should fail if ZIP path has any entries" do
- write "resources/test"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).path("resources")) { it.should be_empty }
- end
- lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
- end
-
- it "should pass if ZIP entry has no content" do
- write "resources/test"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).entry("resources/test")) { it.should be_empty }
- check(package(:jar).path("resources").entry("test")) { it.should
be_empty }
- end
- lambda { project("foo").task("package").invoke }.should_not raise_error
- end
-
- it "should fail if ZIP entry has content" do
- write "resources/test", "something"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).entry("resources/test")) { it.should be_empty }
- end
- lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
- end
-
- it "should fail if ZIP entry does not exist" do
- mkpath "resources"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).entry("resources/test")) { it.should be_empty }
- end
- lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
- end
end
@@ -412,126 +329,253 @@
end
-describe Buildr::Checks::Expectation, " contain(zip.entry)" do
-
- it "should pass if ZIP entry content matches string" do
- write "resources/test", "something"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).entry("resources/test")) { it.should
contain("thing") }
- #check(package(:jar)) { it.entry("resources/test").should
contain("thing") }
- end
- lambda { project("foo").task("package").invoke }.should_not raise_error
- end
-
- it "should pass if ZIP entry content matches pattern" do
- write "resources/test", "something\nor\another"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).entry("resources/test")) { it.should contain(/or/) }
- #check(package(:jar)) { it.entry("resources/test").should contain(/or/) }
- end
- lambda { project("foo").task("package").invoke }.should_not raise_error
- end
-
- it "should pass if ZIP entry content matches all arguments" do
- write "resources/test", "something\nor\nanother"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).entry("resources/test")) { it.should contain(/or/,
/other/) }
- #check(package(:jar)) { it.entry("resources/test").should contain(/or/,
/other/) }
- end
- lambda { project("foo").task("package").invoke }.should_not raise_error
- end
-
- it "should fail unless ZIP path contains all arguments" do
- write "resources/test", "something"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).entry("resources/test")) { it.should contain(/some/,
/other/) }
- #check(package(:jar)) { it.entry("resources/test").should
contain(/some/, /other/) }
- end
- lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
- end
-
- it "should fail if ZIP entry content does not match" do
- write "resources/test", "something"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).entry("resources/test")) { it.should
contain(/other/) }
- #check(package(:jar)) { it.entry("resources/test").should
contain(/other/) }
- end
- lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
- end
-
- it "should fail if ZIP entry does not exist" do
- mkpath "resources"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).entry("resources/test")) { it.should
contain(/anything/) }
- #check(package(:jar)) { it.entry("resources/test").should
contain(/anything/) }
- end
- lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
- end
-end
-
-
-describe Buildr::Checks::Expectation, " contain(zip.path)" do
-
- it "should pass if ZIP path contains file" do
- write "resources/test"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).path("resources")) { it.should contain("test") }
- end
- lambda { project("foo").task("package").invoke }.should_not raise_error
+describe Buildr::Checks::Expectation do
+
+ shared_examples_for "all archive types" do
+
+ describe '#exist' do
+ it "should pass if archive path exists" do
+ archive = @archive
+ write "resources/test"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).path("resources")) { it.should exist }
+ end
+ lambda { project("foo").task("package").invoke }.should_not raise_error
+ end
+
+ it "should fail if archive path does not exist" do
+ archive = @archive
+ mkpath "resources"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive)) { it.path("not-resources").should exist }
+ end
+ lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
+ end
+
+ it "should pass if archive entry exists" do
+ archive = @archive
+ write "resources/test"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).entry("resources/test")) { it.should exist }
+ check(package(archive).path("resources").entry("test")) { it.should
exist }
+ end
+ lambda { project("foo").task("package").invoke }.should_not raise_error
+ end
+
+ it "should fail if archive path does not exist" do
+ archive = @archive
+ mkpath "resources"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).entry("resources/test")) { it.should exist }
+ end
+ lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
+ end
+ end
+
+ describe '#be_empty' do
+ it "should pass if archive path is empty" do
+ archive = @archive
+ mkpath "resources"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).path("resources")) { it.should be_empty }
+ end
+ lambda { project("foo").task("package").invoke }.should_not raise_error
+ end
+
+ it "should fail if archive path has any entries" do
+ archive = @archive
+ write "resources/test"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).path("resources")) { it.should be_empty }
+ end
+ lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
+ end
+
+ it "should pass if archive entry has no content" do
+ archive = @archive
+ write "resources/test"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).entry("resources/test")) { it.should be_empty
}
+ check(package(archive).path("resources").entry("test")) { it.should
be_empty }
+ end
+ lambda { project("foo").task("package").invoke }.should_not raise_error
+ end
+
+ it "should fail if archive entry has content" do
+ archive = @archive
+ write "resources/test", "something"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).entry("resources/test")) { it.should be_empty
}
+ end
+ lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
+ end
+
+ it "should fail if archive entry does not exist" do
+ archive = @archive
+ mkpath "resources"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).entry("resources/test")) { it.should be_empty
}
+ end
+ lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
+ end
+ end
+
+ describe "#contain(entry)" do
+
+ it "should pass if archive entry content matches string" do
+ archive = @archive
+ write "resources/test", "something"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).entry("resources/test")) { it.should
contain("thing") }
+ #check(package(archive)) { it.entry("resources/test").should
contain("thing") }
+ end
+ lambda { project("foo").task("package").invoke }.should_not raise_error
+ end
+
+ it "should pass if archive entry content matches pattern" do
+ archive = @archive
+ write "resources/test", "something\nor\another"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).entry("resources/test")) { it.should
contain(/or/) }
+ #check(package(archive)) { it.entry("resources/test").should
contain(/or/) }
+ end
+ lambda { project("foo").task("package").invoke }.should_not raise_error
+ end
+
+ it "should pass if archive entry content matches all arguments" do
+ archive = @archive
+ write "resources/test", "something\nor\nanother"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).entry("resources/test")) { it.should
contain(/or/, /other/) }
+ #check(package(archive)) { it.entry("resources/test").should
contain(/or/, /other/) }
+ end
+ lambda { project("foo").task("package").invoke }.should_not raise_error
+ end
+
+ it "should fail unless archive path contains all arguments" do
+ archive = @archive
+ write "resources/test", "something"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).entry("resources/test")) { it.should
contain(/some/, /other/) }
+ #check(package(archive)) { it.entry("resources/test").should
contain(/some/, /other/) }
+ end
+ lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
+ end
+
+ it "should fail if archive entry content does not match" do
+ archive = @archive
+ write "resources/test", "something"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).entry("resources/test")) { it.should
contain(/other/) }
+ #check(package(archive)) { it.entry("resources/test").should
contain(/other/) }
+ end
+ lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
+ end
+
+ it "should fail if archive entry does not exist" do
+ archive = @archive
+ mkpath "resources"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).entry("resources/test")) { it.should
contain(/anything/) }
+ #check(package(archive)) { it.entry("resources/test").should
contain(/anything/) }
+ end
+ lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
+ end
+ end
+
+ describe "#contain(path)" do
+
+ it "should pass if archive path contains file" do
+ archive = @archive
+ write "resources/test"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).path("resources")) { it.should
contain("test") }
+ end
+ lambda { project("foo").task("package").invoke }.should_not raise_error
+ end
+
+ it "should handle deep nesting" do
+ archive = @archive
+ write "resources/test/test2.efx"
+ define "foo", :version=>"1.0" do
+ package(archive).include("*")
+ check(package(archive)) { it.should
contain("resources/test/test2.efx") }
+ check(package(archive).path("resources")) { it.should
contain("test/test2.efx") }
+ check(package(archive).path("resources/test")) { it.should
contain("test2.efx") }
+ end
+ lambda { project("foo").task("package").invoke }.should_not raise_error
+ end
+
+ it "should pass if archive path contains pattern" do
+ archive = @archive
+ write "resources/with/test"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).path("resources")) { it.should
contain("**/t*st") }
+ end
+ lambda { project("foo").task("package").invoke }.should_not raise_error
+ end
+
+ it "should pass if archive path contains all arguments" do
+ archive = @archive
+ write "resources/with/test"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).path("resources")) { it.should
contain("**/test", "**/*") }
+ end
+ lambda { project("foo").task("package").invoke }.should_not raise_error
+ end
+
+ it "should fail unless archive path contains all arguments" do
+ archive = @archive
+ write "resources/test"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).path("resources")) { it.should
contain("test", "or-not") }
+ end
+ lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
+ end
+
+ it "should fail if archive path is empty" do
+ archive = @archive
+ mkpath "resources"
+ define "foo", :version=>"1.0" do
+ package(archive).include("resources")
+ check(package(archive).path("resources")) { it.should
contain("test") }
+ end
+ lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
+ end
+ end
+ end
+
+ describe 'ZIP' do
+ it_should_behave_like 'all archive types'
+ before { @archive = :jar }
+ end
+
+ describe 'tar' do
+ it_should_behave_like 'all archive types'
+ before { @archive = :tar }
+ end
+
+ describe 'tgz' do
+ it_should_behave_like 'all archive types'
+ before { @archive = :tgz }
end
-
- it "should handle deep nesting" do
- write "resources/test/test2.efx"
- define "foo", :version=>"1.0" do
- package(:jar).include("*")
- check(package(:jar)) { it.should contain("resources/test/test2.efx") }
- check(package(:jar).path("resources")) { it.should
contain("test/test2.efx") }
- check(package(:jar).path("resources/test")) { it.should
contain("test2.efx") }
- end
- lambda { project("foo").task("package").invoke }.should_not raise_error
- end
-
-
- it "should pass if ZIP path contains pattern" do
- write "resources/with/test"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).path("resources")) { it.should contain("**/t*st") }
- end
- lambda { project("foo").task("package").invoke }.should_not raise_error
- end
-
- it "should pass if ZIP path contains all arguments" do
- write "resources/with/test"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).path("resources")) { it.should contain("**/test",
"**/*") }
- end
- lambda { project("foo").task("package").invoke }.should_not raise_error
- end
-
- it "should fail unless ZIP path contains all arguments" do
- write "resources/test"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).path("resources")) { it.should contain("test",
"or-not") }
- end
- lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
- end
-
- it "should fail if ZIP path is empty" do
- mkpath "resources"
- define "foo", :version=>"1.0" do
- package(:jar).include("resources")
- check(package(:jar).path("resources")) { it.should contain("test") }
- end
- lambda { project("foo").task("package").invoke }.should
raise_error(RuntimeError, /Checks failed/)
- end
-end
+end
\ No newline at end of file