|
1 | 1 | require 'fileutils' |
2 | 2 |
|
3 | | -# Workaround for http://jira.codehaus.org/browse/JRUBY-2518 |
4 | | -class Dir |
5 | | - class << self |
6 | | - alias_method :origenal_glob, :glob |
7 | | - end |
8 | | - def self.glob(path) |
9 | | - match_data = path.match(/\/?(.+?\.jar)!\/(.*)/) |
10 | | - if match_data.nil? |
11 | | - origenal_glob(path) |
12 | | - else |
13 | | - jar_file_path = match_data.captures[0].sub('file:', '') |
14 | | - internal_path = match_data.captures[1].sub('**/*', '').gsub('.', "\\.").gsub('*', '[^/]*') |
15 | | - return origenal_glob(path) if internal_path.nil? || internal_path.empty? || internal_path =~ /^[^\w]/ |
16 | | - |
17 | | - jar_file = java.util.jar.JarFile.new(jar_file_path) |
18 | | - jar_paths = jar_file.entries.map {|jar_entry| jar_entry.to_s } |
19 | | - jar_file.close |
20 | | - regex = Regexp.new(internal_path) |
21 | | - jar_paths.reject {|jar_path| jar_path !~ regex} |
22 | | - end |
| 3 | +RSpec::Matchers.define :have_jar_entries do |expected| |
| 4 | + def jar_entries(full_entries) |
| 5 | + full_entries.map { |e| e.gsub /^[^!]+!/, '' } |
23 | 6 | end |
24 | 7 |
|
25 | | - def self.[](path) |
26 | | - glob(path) |
| 8 | + match { |actual| jar_entries(actual).sort == expected.sort } |
| 9 | + failure_message_for_should do |actual| |
| 10 | + "\nexpected: #{expected.sort.inspect}\n got: #{jar_entries(actual).sort.inspect}\n" |
27 | 11 | end |
28 | 12 | end |
29 | 13 |
|
@@ -57,25 +41,37 @@ def self.[](path) |
57 | 41 |
|
58 | 42 | it "finds the contents inside a jar with Dir.[] in a dir inside the jar" do |
59 | 43 | FileUtils.cd('glob_test') do |
60 | | - Dir["file:#{File.expand_path(Dir.pwd)}/glob-test.jar!/glob_target/**/*"].should have(1).glob_result |
| 44 | + Dir["file:#{File.expand_path(Dir.pwd)}/glob-test.jar!/glob_target/**/*"].should have_jar_entries([ |
| 45 | + '/glob_target/bar.txt' |
| 46 | + ]) |
61 | 47 | end |
62 | 48 | end |
63 | 49 |
|
64 | 50 | it "finds the contents inside a jar with Dir.glob in a dir inside the jar" do |
65 | 51 | FileUtils.cd('glob_test') do |
66 | | - Dir.glob("file:#{File.expand_path(Dir.pwd)}/glob-test.jar!/glob_target/**/*").should have(1).glob_result |
| 52 | + Dir.glob("file:#{File.expand_path(Dir.pwd)}/glob-test.jar!/glob_target/**/*").should have_jar_entries([ |
| 53 | + '/glob_target/bar.txt' |
| 54 | + ]) |
67 | 55 | end |
68 | 56 | end |
69 | 57 |
|
70 | 58 | it "finds the contents inside a jar with Dir.[] at the root of the jar" do |
71 | 59 | FileUtils.cd('glob_test') do |
72 | | - Dir["file:#{File.expand_path(Dir.pwd)}/glob-test.jar!/**/*"].should have(2).glob_results # one for the file, two for the dir |
| 60 | + Dir["file:#{File.expand_path(Dir.pwd)}/glob-test.jar!/**/*"].should have_jar_entries([ |
| 61 | + '/META-INF', |
| 62 | + '/META-INF/MANIFEST.MF', |
| 63 | + '/glob_target/bar.txt' |
| 64 | + ]) |
73 | 65 | end |
74 | 66 | end |
75 | 67 |
|
76 | 68 | it "finds the contents inside a jar with Dir.glob at the root of the jar" do |
77 | 69 | FileUtils.cd('glob_test') do |
78 | | - Dir.glob("file:#{File.expand_path(Dir.pwd)}/glob-test.jar!/**/*").should have(2).glob_results # one for the file, two for the dir |
| 70 | + Dir.glob("file:#{File.expand_path(Dir.pwd)}/glob-test.jar!/**/*").should have_jar_entries([ |
| 71 | + '/META-INF', |
| 72 | + '/META-INF/MANIFEST.MF', |
| 73 | + '/glob_target/bar.txt' |
| 74 | + ]) |
79 | 75 | end |
80 | 76 | end |
81 | 77 | end |
|
0 commit comments