Commit d24245fc authored by Fabio Pelosin's avatar Fabio Pelosin

[PathList] Fix #relative_glob and clean up the code

parent b81d16f1
......@@ -53,6 +53,12 @@ module Pod
@dirs = dirs.map { |d| d.gsub(/\/\.\.?$/,'') }.uniq
end
#-----------------------------------------------------------------------#
public
# @!group Globbing
# @return [Array<Pathname>] Similar to {glob} but returns the absolute
# paths.
#
......@@ -89,11 +95,17 @@ module Pod
end
end.flatten
list = list.map { |path| Pathname.new(path) }
list -= relative_glob(exclude_patterns) if exclude_patterns
list.map { |path| Pathname.new(path) }
list
end
#-----------------------------------------------------------------------#
private
# @!group Private helpers
# @return [Bool] Wether a path is a directory. The result of this method
# computed without accessing the file system and is case
# insensitive.
......@@ -125,7 +137,7 @@ module Pod
# @param [String] pattern A {Dir#glob} like pattern.
#
def dir_glob_equivalent_patterns(pattern)
pattern.gsub('/**/', '{/**/,/}')
pattern = pattern.gsub('/**/', '{/**/,/}')
values_by_set = {}
pattern.scan(/\{[^}]*\}/) do |set|
values = set.gsub(/[{}]/, '').split(',')
......
require File.expand_path('../../../spec_helper', __FILE__)
describe Pod::LocalPod::PathList do
module Pod
describe LocalPod::PathList do
before do
@path_list = Pod::LocalPod::PathList.new(fixture('banana-lib'))
end
it "creates the list of all the files" do
files = @path_list.files
files.reject! do |f|
f.include?('libPusher') || f.include?('.git') || f.include?('DS_Store')
end
expected = %w[
BananaLib.podspec
Classes/Banana.h
Classes/Banana.m
Classes/BananaLib.pch
README
Resources/logo-sidebar.png
sub-dir/sub-dir-2/somefile.txt
]
files.sort.should == expected
end
it "creates theh list of the directories" do
dirs = @path_list.dirs
dirs.reject! do |f|
f.include?('libPusher') || f.include?('.git')
before do
@path_list = LocalPod::PathList.new(fixture('banana-lib'))
end
dirs.sort.should == %w| Classes Resources sub-dir sub-dir/sub-dir-2 |
end
it "detects a directory" do
@path_list.directory?('classes').should == true
end
it "doesn't reports as a directory a file" do
@path_list.directory?('Classes/Banana.m').should == false
end
it "can glob the root for a given pattern" do
paths = @path_list.relative_glob('Classes/*.{h,m}').map(&:to_s)
paths.sort.should == %w| Classes/Banana.h Classes/Banana.m |
end
it "supports the `**` glob pattern" do
paths = @path_list.relative_glob('Classes/**/*.{h,m}').map(&:to_s)
paths.sort.should == %w| Classes/Banana.h Classes/Banana.m |
end
it "supports an optional pattern for globbing directories" do
paths = @path_list.relative_glob('Classes', '*.{h,m}').map(&:to_s)
paths.sort.should == %w| Classes/Banana.h Classes/Banana.m |
end
describe "In general" do
it "creates the list of all the files" do
files = @path_list.files
files.reject! do |f|
f.include?('libPusher') || f.include?('.git') || f.include?('DS_Store')
end
expected = %w[
BananaLib.podspec
Classes/Banana.h
Classes/Banana.m
Classes/BananaLib.pch
Classes/BananaPrivate.h
README
Resources/logo-sidebar.png
preserve_me.txt
sub-dir/sub-dir-2/somefile.txt
]
files.sort.should == expected
end
it "creates the list of the directories" do
dirs = @path_list.dirs
dirs.reject! do |f|
f.include?('libPusher') || f.include?('.git')
end
dirs.sort.should == %w| Classes Resources sub-dir sub-dir/sub-dir-2 |
end
it "can return the absolute paths from glob" do
paths = @path_list.glob('Classes/*.{h,m}')
paths.all? { |p| p.absolute? }.should == true
end
it "can return the relative paths from glob" do
paths = @path_list.relative_glob('Classes/*.{h,m}')
paths.any? { |p| p.absolute? }.should == false
end
end
it "expands a pattern into all the combinations of Dir#glob literals" do
patterns = @path_list.dir_glob_equivalent_patterns('{file1,file2}.{h,m}')
patterns.sort.should == %w| file1.h file1.m file2.h file2.m |
end
#-------------------------------------------------------------------------#
describe "Globbing" do
it "can glob the root for a given pattern" do
paths = @path_list.relative_glob('Classes/*.{h,m}').map(&:to_s)
paths.sort.should == %w[
Classes/Banana.h
Classes/Banana.m
Classes/BananaPrivate.h
]
end
it "supports the `**` glob pattern" do
paths = @path_list.relative_glob('Classes/**/*.{h,m}').map(&:to_s)
paths.sort.should == %w[
Classes/Banana.h
Classes/Banana.m
Classes/BananaPrivate.h
]
end
it "supports an optional pattern for globbing directories" do
paths = @path_list.relative_glob('Classes', '*.{h,m}').map(&:to_s)
paths.sort.should == %w[
Classes/Banana.h
Classes/Banana.m
Classes/BananaPrivate.h
]
end
it "supports an optional list of patterns to exclude" do
exclude_patterns = ['**/*.m', '**/*Private*.*']
paths = @path_list.relative_glob('Classes/*', nil, exclude_patterns).map(&:to_s)
paths.sort.should == %w[
Classes/Banana.h
Classes/BananaLib.pch
]
end
it "can return the absolute paths from glob" do
paths = @path_list.glob('Classes/*.{h,m}')
paths.all? { |p| p.absolute? }.should == true
end
it "can return the relative paths from glob" do
paths = @path_list.relative_glob('Classes/*.{h,m}')
paths.any? { |p| p.absolute? }.should == false
end
end
it "returns the original pattern if there are no Dir#glob expansions" do
patterns = @path_list.dir_glob_equivalent_patterns('file*.*')
patterns.sort.should == %w| file*.* |
end
#-------------------------------------------------------------------------#
describe "Private Helpers" do
describe "#directory?" do
it "detects a directory" do
@path_list.send(:directory?, 'classes').should == true
end
it "doesn't reports as a directory a file" do
@path_list.send(:directory?, 'Classes/Banana.m').should == false
end
end
describe "#directory?" do
it "expands a pattern into all the combinations of Dir#glob literals" do
patterns = @path_list.send(:dir_glob_equivalent_patterns, '{file1,file2}.{h,m}')
patterns.sort.should == %w[ file1.h file1.m file2.h file2.m ]
end
it "returns the original pattern if there are no Dir#glob expansions" do
patterns = @path_list.send(:dir_glob_equivalent_patterns, 'file*.*')
patterns.sort.should == %w[ file*.* ]
end
it "expands `**`" do
patterns = @path_list.send(:dir_glob_equivalent_patterns, 'Classes/**/file.m')
patterns.sort.should == %w[ Classes/**/file.m Classes/file.m ]
end
it "supports a combination of `**` and literals" do
patterns = @path_list.send(:dir_glob_equivalent_patterns, 'Classes/**/file.{h,m}')
patterns.sort.should == %w[
Classes/**/file.h
Classes/**/file.m
Classes/file.h
Classes/file.m
]
end
end
end
it "expands `**`" do
patterns = @path_list.dir_glob_equivalent_patterns('Classes/**/file.m')
patterns.sort.should == %w| Classes/**/file.m Classes/file.m |
end
#-------------------------------------------------------------------------#
it "supports a combination of `**` and literals" do
patterns = @path_list.dir_glob_equivalent_patterns('Classes/**/file.{h,m}')
patterns.sort.should == %w| Classes/**/file.h Classes/**/file.m Classes/file.h Classes/file.m |
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment