Buildr C0 Coverage Information - RCov

lib/buildr/packaging/tar.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
lib/buildr/packaging/tar.rb 189 96
15.34%
18.75%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with this
3 # work for additional information regarding copyright ownership.  The ASF
4 # licenses this file to you under the Apache License, Version 2.0 (the
5 # "License"); you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #    http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13 # License for the specific language governing permissions and limitations under
14 # the License.
15 
16 
17 require 'buildr/packaging/archive'
18 gem 'minitar' ; autoload :Archive, 'archive/tar/minitar'
19 
20 
21 module Buildr
22 
23   # The TarTask creates a new Tar file. You can include any number of files and and directories,
24   # use exclusion patterns, and include files into specific directories.
25   #
26   # To create a GZipped Tar, either set the gzip option to true, or use the .tgz or .gz suffix.
27   #
28   # For example:
29   #   tar("test.tgz").tap do |task|
30   #     task.include "srcs"
31   #     task.include "README", "LICENSE"
32   #   end
33   #
34   # See Buildr#tar and ArchiveTask.
35   class TarTask < ArchiveTask
36 
37     # To create a GZipped Tar, either set this option to true, or use the .tgz/.gz suffix.
38     attr_accessor :gzip
39     # Permission mode for files contained in the Tar.  Defaults to 0755.
40     attr_accessor :mode
41 
42     def initialize(*args, &block) #:nodoc:
43       super
44       self.gzip = name =~ /\.t?gz$/
45       self.mode = '0755'
46     end
47 
48     # :call-seq:
49     #   entry(name) => Entry
50     #
51     # Returns a Tar file entry. You can use this to check if the entry exists and its contents,
52     # for example:
53     #   package(:tar).entry("src/LICENSE").should contain(/Apache Software License/)
54     def entry(entry_name)
55       Buildr::TarEntry.new(self, entry_name)
56     end
57 
58     def entries() #:nodoc:
59       tar_entries = nil
60       with_uncompressed_tar { |tar| tar_entries = tar.entries }
61       tar_entries
62     end
63 
64     # :call-seq:
65     #   with_uncompressed_tar { |tar_entries| ... }
66     #
67     # Yields an Archive::Tar::Minitar::Input object to the provided block.
68     # Opening, closing and Gzip-decompressing is automatically taken care of.
69     def with_uncompressed_tar &block
70       if gzip
71         Zlib::GzipReader.open(name) { |tar| Archive::Tar::Minitar.open(tar, &block) }
72       else
73         Archive::Tar::Minitar.open(name, &block)
74       end
75     end
76 
77   private
78 
79     def create_from(file_map)
80       if gzip
81         StringIO.new.tap do |io|
82           create_tar io, file_map
83           io.seek 0
84           Zlib::GzipWriter.open(name) { |gzip| gzip.write io.read }
85         end
86       else
87         File.open(name, 'wb') { |file| create_tar file, file_map }
88       end
89     end
90 
91     def create_tar(out, file_map)
92       Archive::Tar::Minitar::Writer.open(out) do |tar|
93         options = { :mode=>mode || '0755', :mtime=>Time.now }
94 
95         file_map.each do |path, content|
96           if content.respond_to?(:call)
97             tar.add_file(path, options) { |os, opts| content.call os }
98           elsif content.nil?
99           elsif File.directory?(content.to_s)
100             stat = File.stat(content.to_s)
101             tar.mkdir(path, options.merge(:mode=>stat.mode, :mtime=>stat.mtime, :uid=>stat.uid, :gid=>stat.gid))
102           else
103             File.open content.to_s, 'rb' do |is|
104               tar.add_file path, options.merge(:mode=>is.stat.mode, :mtime=>is.stat.mtime, :uid=>is.stat.uid, :gid=>is.stat.gid) do |os, opts|
105                 while data = is.read(4096)
106                   os.write(data)
107                 end
108               end
109             end
110           end
111         end
112       end
113     end
114 
115   end
116 
117 
118   class TarEntry #:nodoc:
119 
120     def initialize(tar_task, entry_name)
121       @tar_task = tar_task
122       @entry_name = entry_name
123     end
124 
125     # :call-seq:
126     #   contain?(*patterns) => boolean
127     #
128     # Returns true if this Tar file entry matches against all the arguments. An argument may be
129     # a string or regular expression.
130     def contain?(*patterns)
131       content = read_content_from_tar
132       patterns.map { |pattern| Regexp === pattern ? pattern : Regexp.new(Regexp.escape(pattern.to_s)) }.
133         all? { |pattern| content =~ pattern }
134     end
135 
136     # :call-seq:
137     #   empty?() => boolean
138     #
139     # Returns true if this entry is empty.
140     def empty?()
141       read_content_from_tar.nil?
142     end
143 
144     # :call-seq:
145     #   exist() => boolean
146     #
147     # Returns true if this entry exists.
148     def exist?()
149       exist = false
150       @tar_task.with_uncompressed_tar { |tar| exist = tar.any? { |entry| entry.name == @entry_name } }
151       exist
152     end
153 
154     def to_s #:nodoc:
155       @entry_name
156     end
157 
158     private
159 
160     def read_content_from_tar
161       content = Errno::ENOENT.new("No such file or directory - #{@entry_name}")
162       @tar_task.with_uncompressed_tar do |tar|
163         content = tar.inject(content) { |content, entry| entry.name == @entry_name ? entry.read : content }
164       end
165       raise content if Exception === content
166       content
167     end
168   end
169 
170 end
171 
172 
173 # :call-seq:
174 #    tar(file) => TarTask
175 #
176 # The TarTask creates a new Tar file. You can include any number of files and
177 # and directories, use exclusion patterns, and include files into specific
178 # directories.
179 #
180 # To create a GZipped Tar, either set the gzip option to true, or use the .tgz or .gz suffix.
181 #
182 # For example:
183 #   tar("test.tgz").tap do |tgz|
184 #     tgz.include "srcs"
185 #     tgz.include "README", "LICENSE"
186 #   end
187 def tar(file)
188   TarTask.define_task(file)
189 end

Generated on 2011-07-06 23:35:38 -0700 with rcov 0.9.8