pathspec 0.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/pathspec.rb CHANGED
@@ -3,15 +3,14 @@ require 'pathspec/regexspec'
3
3
  require 'find'
4
4
  require 'pathname'
5
5
 
6
+ # Main PathSpec class, provides interfaces to various spec implementations
6
7
  class PathSpec
7
8
  attr_reader :specs
8
9
 
9
- def initialize(lines=nil, type=:git)
10
+ def initialize(lines = nil, type = :git)
10
11
  @specs = []
11
12
 
12
- if lines
13
- add(lines, type)
14
- end
13
+ add(lines, type) if lines
15
14
 
16
15
  self
17
16
  end
@@ -26,9 +25,7 @@ class PathSpec
26
25
 
27
26
  def specs_matching(path)
28
27
  @specs.select do |spec|
29
- if spec.match(path)
30
- spec
31
- end
28
+ spec if spec.match(path)
32
29
  end
33
30
  end
34
31
 
@@ -41,58 +38,52 @@ class PathSpec
41
38
  Find.find(root) do |path|
42
39
  relpath = Pathname.new(path).relative_path_from(rootpath).to_s
43
40
  relpath += '/' if File.directory? path
44
- if match(relpath)
45
- matching << path
46
- end
41
+ matching << path if match(relpath)
47
42
  end
48
43
 
49
44
  matching
50
45
  end
51
46
 
52
- def match_path(path, root='/')
47
+ def match_path(path, root = '/')
53
48
  rootpath = Pathname.new(drive_letter_to_path(root))
54
49
  relpath = Pathname.new(drive_letter_to_path(path)).relative_path_from(rootpath).to_s
55
- relpath = relpath + '/' if path[-1].chr == '/'
50
+ relpath += '/' if path[-1].chr == '/'
56
51
 
57
52
  match(relpath)
58
53
  end
59
54
 
60
- def match_paths(paths, root='/')
55
+ def match_paths(paths, root = '/')
61
56
  matching = []
62
57
 
63
58
  paths.each do |path|
64
- if match_path(path, root)
65
- matching << path
66
- end
59
+ matching << path if match_path(path, root)
67
60
  end
68
61
 
69
62
  matching
70
63
  end
71
64
 
72
65
  def drive_letter_to_path(path)
73
- path.gsub(/^([a-zA-Z]):\//, '/\1/')
66
+ path.gsub(%r{^([a-zA-Z]):/}, '/\1/')
74
67
  end
75
68
 
76
69
  # Generate specs from a filename, such as a .gitignore
77
- def self.from_filename(filename, type=:git)
78
- File.open(filename, 'r') { |io| self.from_lines(io, type) }
70
+ def self.from_filename(filename, type = :git)
71
+ File.open(filename, 'r') { |io| from_lines(io, type) }
79
72
  end
80
73
 
81
- def self.from_lines(lines, type=:git)
82
- self.new lines, type
74
+ def self.from_lines(lines, type = :git)
75
+ new lines, type
83
76
  end
84
77
 
85
78
  # Generate specs from lines of text
86
- def add(obj, type=:git)
79
+ def add(obj, type = :git)
87
80
  spec_class = spec_type(type)
88
81
 
89
82
  if obj.respond_to?(:each_line)
90
83
  obj.each_line do |l|
91
84
  spec = spec_class.new(l.rstrip)
92
85
 
93
- if !spec.regex.nil? && !spec.inclusive?.nil?
94
- @specs << spec
95
- end
86
+ @specs << spec if !spec.regex.nil? && !spec.inclusive?.nil?
96
87
  end
97
88
  elsif obj.respond_to?(:each)
98
89
  obj.each do |l|
@@ -110,9 +101,10 @@ class PathSpec
110
101
  end
111
102
 
112
103
  def spec_type(type)
113
- if type == :git
104
+ case type
105
+ when :git
114
106
  GitIgnoreSpec
115
- elsif type == :regex
107
+ when :regex
116
108
  RegexSpec
117
109
  else
118
110
  raise "Unknown spec type #{type}"
@@ -0,0 +1,2 @@
1
+ !**/important.txt
2
+ abc/**
@@ -0,0 +1,51 @@
1
+ # Source: https://github.com/github/gitignore/blob/master/Ruby.gitignore
2
+ *.gem
3
+ *.rbc
4
+ /.config
5
+ /coverage/
6
+ /InstalledFiles
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/examples.txt
10
+ /test/tmp/
11
+ /test/version_tmp/
12
+ /tmp/
13
+
14
+ # Used by dotenv library to load environment variables.
15
+ # .env
16
+
17
+ ## Specific to RubyMotion:
18
+ .dat*
19
+ .repl_history
20
+ build/
21
+ *.bridgesupport
22
+ build-iPhoneOS/
23
+ build-iPhoneSimulator/
24
+
25
+ ## Specific to RubyMotion (use of CocoaPods):
26
+ #
27
+ # We recommend against adding the Pods directory to your .gitignore. However
28
+ # you should judge for yourself, the pros and cons are mentioned at:
29
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
30
+ #
31
+ # vendor/Pods/
32
+
33
+ ## Documentation cache and generated files:
34
+ /.yardoc/
35
+ /_yardoc/
36
+ /doc/
37
+ /rdoc/
38
+
39
+ ## Environment normalization:
40
+ /.bundle/
41
+ /vendor/bundle
42
+ /lib/bundler/man/
43
+
44
+ # for a library or gem, you might want to ignore these files since the code is
45
+ # intended to run in multiple environments; otherwise, check them in:
46
+ # Gemfile.lock
47
+ # .ruby-version
48
+ # .ruby-gemset
49
+
50
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
51
+ .rvmrc
@@ -0,0 +1 @@
1
+ *.md
@@ -0,0 +1 @@
1
+ .*\.md
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,6 @@
1
1
  begin
2
2
  require 'simplecov'
3
3
  SimpleCov.start
4
-
5
- require 'codecov'
6
- SimpleCov.formatter = SimpleCov::Formatter::Codecov
7
- rescue
4
+ rescue StandardError
8
5
  puts 'SimpleCov failed to start, most likely this due to running Ruby 1.8.7'
9
6
  end
@@ -1,20 +1,20 @@
1
1
  require 'spec_helper'
2
2
  require 'pathspec/gitignorespec'
3
3
 
4
- describe GitIgnoreSpec do
4
+ describe PathSpec::GitIgnoreSpec do
5
5
  # Original specification by http://git-scm.com/docs/gitignore
6
6
 
7
7
  # A blank line matches no files, so it can serve as a separator for
8
8
  # readability.
9
9
  describe 'does nothing for newlines' do
10
- subject { GitIgnoreSpec.new "\n" }
10
+ subject { PathSpec::GitIgnoreSpec.new "\n" }
11
11
  it { is_expected.to_not match('foo.tmp') }
12
12
  it { is_expected.to_not match(' ') }
13
13
  it { is_expected.to_not be_inclusive }
14
14
  end
15
15
 
16
16
  describe 'does nothing for blank strings' do
17
- subject { GitIgnoreSpec.new '' }
17
+ subject { PathSpec::GitIgnoreSpec.new '' }
18
18
  it { is_expected.to_not match 'foo.tmp' }
19
19
  it { is_expected.to_not match ' ' }
20
20
  it { is_expected.to_not be_inclusive }
@@ -23,21 +23,21 @@ describe GitIgnoreSpec do
23
23
  # A line starting with # serves as a comment. Put a backslash ("\") in front
24
24
  # of the first hash for patterns that begin with a hash.
25
25
  describe 'does nothing for comments' do
26
- subject { GitIgnoreSpec.new '# this is a gitignore style comment' }
26
+ subject { PathSpec::GitIgnoreSpec.new '# this is a gitignore style comment' }
27
27
  it { is_expected.to_not match('foo.tmp') }
28
28
  it { is_expected.to_not match(' ') }
29
29
  it { is_expected.to_not be_inclusive }
30
30
  end
31
31
 
32
32
  describe 'ignores comment char with a slash' do
33
- subject { GitIgnoreSpec.new '\#averystrangefile' }
33
+ subject { PathSpec::GitIgnoreSpec.new '\#averystrangefile' }
34
34
  it { is_expected.to match('#averystrangefile') }
35
35
  it { is_expected.to_not match('foobar') }
36
36
  it { is_expected.to be_inclusive }
37
37
  end
38
38
 
39
39
  describe 'escapes characters with slashes' do
40
- subject { GitIgnoreSpec.new 'twinkletwinkle\*' }
40
+ subject { PathSpec::GitIgnoreSpec.new 'twinkletwinkle\*' }
41
41
  it { is_expected.to match('twinkletwinkle*') }
42
42
  it { is_expected.to_not match('twinkletwinkletwinkle') }
43
43
  it { is_expected.to be_inclusive }
@@ -45,7 +45,7 @@ describe GitIgnoreSpec do
45
45
 
46
46
  # Trailing spaces are ignored unless they are quoted with backlash ("\").
47
47
  describe 'ignores trailing spaces' do
48
- subject { GitIgnoreSpec.new 'foo ' }
48
+ subject { PathSpec::GitIgnoreSpec.new 'foo ' }
49
49
  it { is_expected.to match('foo') }
50
50
  it { is_expected.to_not match('foo ') }
51
51
  it { is_expected.to be_inclusive }
@@ -62,7 +62,7 @@ describe GitIgnoreSpec do
62
62
  # backslash ("\") in front of the first "!" for patterns that begin with a
63
63
  # literal "!", for example, "\!important!.txt".
64
64
  describe 'is exclusive of !' do
65
- subject { GitIgnoreSpec.new '!important.txt' }
65
+ subject { PathSpec::GitIgnoreSpec.new '!important.txt' }
66
66
  it { is_expected.to match('important.txt') }
67
67
  it { is_expected.to_not be_inclusive }
68
68
  it { is_expected.to_not match('!important.txt') }
@@ -74,7 +74,7 @@ describe GitIgnoreSpec do
74
74
  # will not match a regular file or a symbolic link foo (this is consistent
75
75
  # with the way how pathspec works in general in Git).
76
76
  describe 'trailing slashes match directories and their contents but not regular files or symlinks' do
77
- subject { GitIgnoreSpec.new 'foo/' }
77
+ subject { PathSpec::GitIgnoreSpec.new 'foo/' }
78
78
  it { is_expected.to match('foo/') }
79
79
  it { is_expected.to match('foo/bar') }
80
80
  it { is_expected.to match('baz/foo/bar') }
@@ -87,7 +87,7 @@ describe GitIgnoreSpec do
87
87
  # of the .gitignore file (relative to the toplevel of the work tree if not
88
88
  # from a .gitignore file).
89
89
  describe 'handles basic globbing' do
90
- subject { GitIgnoreSpec.new '*.tmp' }
90
+ subject { PathSpec::GitIgnoreSpec.new '*.tmp' }
91
91
  it { is_expected.to match('foo.tmp') }
92
92
  it { is_expected.to match('foo/bar.tmp') }
93
93
  it { is_expected.to match('foo/bar.tmp/baz') }
@@ -96,7 +96,7 @@ describe GitIgnoreSpec do
96
96
  end
97
97
 
98
98
  describe 'handles inner globs' do
99
- subject { GitIgnoreSpec.new 'foo-*-bar' }
99
+ subject { PathSpec::GitIgnoreSpec.new 'foo-*-bar' }
100
100
  it { is_expected.to match('foo--bar') }
101
101
  it { is_expected.to match('foo-hello-bar') }
102
102
  it { is_expected.to match('a/foo-hello-bar') }
@@ -106,7 +106,7 @@ describe GitIgnoreSpec do
106
106
  end
107
107
 
108
108
  describe 'handles postfix globs' do
109
- subject { GitIgnoreSpec.new '~temp-*' }
109
+ subject { PathSpec::GitIgnoreSpec.new '~temp-*' }
110
110
  it { is_expected.to match('~temp-') }
111
111
  it { is_expected.to match('~temp-foo') }
112
112
  it { is_expected.to match('foo/~temp-bar') }
@@ -115,14 +115,14 @@ describe GitIgnoreSpec do
115
115
  end
116
116
 
117
117
  describe 'handles multiple globs' do
118
- subject { GitIgnoreSpec.new '*.middle.*' }
118
+ subject { PathSpec::GitIgnoreSpec.new '*.middle.*' }
119
119
  it { is_expected.to match('hello.middle.rb') }
120
120
  it { is_expected.to_not match('foo.rb') }
121
121
  it { is_expected.to be_inclusive }
122
122
  end
123
123
 
124
124
  describe 'handles dir globs' do
125
- subject { GitIgnoreSpec.new 'dir/*' }
125
+ subject { PathSpec::GitIgnoreSpec.new 'dir/*' }
126
126
  it { is_expected.to match('dir/foo') }
127
127
  it { is_expected.to_not match('foo/') }
128
128
  it { is_expected.to be_inclusive }
@@ -134,14 +134,14 @@ describe GitIgnoreSpec do
134
134
  # "Documentation/git.html" but not "Documentation/ppc/ppc.html" or
135
135
  # "tools/perf/Documentation/perf.html".
136
136
  describe 'handles dir globs' do
137
- subject { GitIgnoreSpec.new 'dir/*' }
137
+ subject { PathSpec::GitIgnoreSpec.new 'dir/*' }
138
138
  it { is_expected.to match('dir/foo') }
139
139
  it { is_expected.to_not match('foo/') }
140
140
  it { is_expected.to be_inclusive }
141
141
  end
142
142
 
143
143
  describe 'handles globs inside of dirs' do
144
- subject { GitIgnoreSpec.new 'Documentation/*.html' }
144
+ subject { PathSpec::GitIgnoreSpec.new 'Documentation/*.html' }
145
145
  it { is_expected.to match('Documentation/git.html') }
146
146
  it { is_expected.to_not match('Documentation/ppc/ppc.html') }
147
147
  it { is_expected.to_not match('tools/perf/Documentation/perf.html') } # TODO: Or is it? Git 2 weirdness?
@@ -149,14 +149,14 @@ describe GitIgnoreSpec do
149
149
  end
150
150
 
151
151
  describe 'handles wildcards' do
152
- subject { GitIgnoreSpec.new 'jokeris????' }
152
+ subject { PathSpec::GitIgnoreSpec.new 'jokeris????' }
153
153
  it { is_expected.to match('jokeriswild') }
154
154
  it { is_expected.to_not match('jokerisfat') }
155
155
  it { is_expected.to be_inclusive }
156
156
  end
157
157
 
158
158
  describe 'handles brackets' do
159
- subject { GitIgnoreSpec.new '*[eu][xl]*' }
159
+ subject { PathSpec::GitIgnoreSpec.new '*[eu][xl]*' }
160
160
  it { is_expected.to match('youknowregex') }
161
161
  it { is_expected.to match('youknowregularexpressions') }
162
162
  it { is_expected.to_not match('youknownothing') }
@@ -164,19 +164,19 @@ describe GitIgnoreSpec do
164
164
  end
165
165
 
166
166
  describe 'handles unmatched brackets' do
167
- subject { GitIgnoreSpec.new '*[*[*' }
167
+ subject { PathSpec::GitIgnoreSpec.new '*[*[*' }
168
168
  it { is_expected.to match('bracket[oh[wow') }
169
169
  it { is_expected.to be_inclusive }
170
170
  end
171
171
 
172
172
  describe 'handles brackets with carats' do
173
- subject { GitIgnoreSpec.new '*[^]' }
173
+ subject { PathSpec::GitIgnoreSpec.new '*[^]' }
174
174
  it { is_expected.to match('myfavorite^') }
175
175
  it { is_expected.to be_inclusive }
176
176
  end
177
177
 
178
178
  describe 'handles brackets for brackets' do
179
- subject { GitIgnoreSpec.new '*[]]' }
179
+ subject { PathSpec::GitIgnoreSpec.new '*[]]' }
180
180
  it { is_expected.to match('yodawg[]]') }
181
181
  it { is_expected.to be_inclusive }
182
182
  end
@@ -189,7 +189,7 @@ describe GitIgnoreSpec do
189
189
  end
190
190
 
191
191
  describe 'handles negated brackets' do
192
- subject { GitIgnoreSpec.new 'ab[!cd]ef' }
192
+ subject { PathSpec::GitIgnoreSpec.new 'ab[!cd]ef' }
193
193
  it { is_expected.to_not match('abcef') }
194
194
  it { is_expected.to match('abzef') }
195
195
  it { is_expected.to be_inclusive }
@@ -198,14 +198,14 @@ describe GitIgnoreSpec do
198
198
  # A leading slash matches the beginning of the pathname. For example, "/*.c"
199
199
  # matches "cat-file.c" but not "mozilla-sha1/sha1.c".
200
200
  describe 'handles leading / as relative to base directory' do
201
- subject { GitIgnoreSpec.new '/*.c' }
201
+ subject { PathSpec::GitIgnoreSpec.new '/*.c' }
202
202
  it { is_expected.to match('cat-file.c') }
203
203
  it { is_expected.to_not match('mozilla-sha1/sha1.c') }
204
204
  it { is_expected.to be_inclusive }
205
205
  end
206
206
 
207
207
  describe 'handles simple single paths' do
208
- subject { GitIgnoreSpec.new 'spam' }
208
+ subject { PathSpec::GitIgnoreSpec.new 'spam' }
209
209
  it { is_expected.to match('spam') }
210
210
  it { is_expected.to match('spam/') }
211
211
  it { is_expected.to match('foo/spam') }
@@ -222,7 +222,7 @@ describe GitIgnoreSpec do
222
222
  # pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is
223
223
  # directly under directory "foo".
224
224
  describe 'handles prefixed ** as searching any location' do
225
- subject { GitIgnoreSpec.new '**/foo' }
225
+ subject { PathSpec::GitIgnoreSpec.new '**/foo' }
226
226
  it { is_expected.to match('foo') }
227
227
  it { is_expected.to match('bar/foo') }
228
228
  it { is_expected.to match('baz/bar/foo') }
@@ -231,7 +231,7 @@ describe GitIgnoreSpec do
231
231
  end
232
232
 
233
233
  describe 'handles prefixed ** with a directory as searching a file under a directory in any location' do
234
- subject { GitIgnoreSpec.new '**/foo/bar' }
234
+ subject { PathSpec::GitIgnoreSpec.new '**/foo/bar' }
235
235
  it { is_expected.to_not match('foo') }
236
236
  it { is_expected.to match('foo/bar') }
237
237
  it { is_expected.to match('baz/foo/bar') }
@@ -245,7 +245,7 @@ describe GitIgnoreSpec do
245
245
  # all files inside directory "abc", relative to the location of the .gitignore
246
246
  # file, with infinite depth.
247
247
  describe 'handles leading /** as all files inside a directory' do
248
- subject { GitIgnoreSpec.new 'abc/**' }
248
+ subject { PathSpec::GitIgnoreSpec.new 'abc/**' }
249
249
  it { is_expected.to match('abc/') }
250
250
  it { is_expected.to match('abc/def') }
251
251
  it { is_expected.to_not match('123/abc/def') }
@@ -257,7 +257,7 @@ describe GitIgnoreSpec do
257
257
  # more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b"
258
258
  # and so on.
259
259
  describe 'handles /** in the middle of a path' do
260
- subject { GitIgnoreSpec.new 'a/**/b' }
260
+ subject { PathSpec::GitIgnoreSpec.new 'a/**/b' }
261
261
  it { is_expected.to match('a/b') }
262
262
  it { is_expected.to match('a/x/b') }
263
263
  it { is_expected.to match('a/x/y/b') }
@@ -267,7 +267,7 @@ describe GitIgnoreSpec do
267
267
  end
268
268
 
269
269
  describe 'matches all paths when given **' do
270
- subject { GitIgnoreSpec.new '**' }
270
+ subject { PathSpec::GitIgnoreSpec.new '**' }
271
271
 
272
272
  it { is_expected.to match('a/b') }
273
273
  it { is_expected.to match('a/x/b') }
@@ -278,7 +278,7 @@ describe GitIgnoreSpec do
278
278
 
279
279
  # Other consecutive asterisks are considered invalid.
280
280
  describe 'considers other consecutive asterisks invalid' do
281
- subject { GitIgnoreSpec.new 'a/***/b' }
281
+ subject { PathSpec::GitIgnoreSpec.new 'a/***/b' }
282
282
  it { is_expected.to_not match('a/b') }
283
283
  it { is_expected.to_not match('a/x/b') }
284
284
  it { is_expected.to_not match('a/x/y/b') }
@@ -288,18 +288,16 @@ describe GitIgnoreSpec do
288
288
  end
289
289
 
290
290
  describe 'does not match single absolute paths' do
291
- subject { GitIgnoreSpec.new "/" }
291
+ subject { PathSpec::GitIgnoreSpec.new '/' }
292
292
  it { is_expected.to_not match('foo.tmp') }
293
293
  it { is_expected.to_not match(' ') }
294
294
  it { is_expected.to_not match('a/b') }
295
295
  end
296
296
 
297
297
  describe 'nested paths are relative to the file' do
298
- subject { GitIgnoreSpec.new 'foo/spam' }
298
+ subject { PathSpec::GitIgnoreSpec.new 'foo/spam' }
299
299
  it { is_expected.to match('foo/spam') }
300
300
  it { is_expected.to match('foo/spam/bar') }
301
301
  it { is_expected.to_not match('bar/foo/spam') }
302
302
  end
303
-
304
-
305
303
  end
@@ -1,12 +1,12 @@
1
1
  require 'spec_helper'
2
2
  require 'pathspec/spec'
3
3
 
4
- describe Spec do
5
- subject { Spec.new }
4
+ describe PathSpec::Spec do
5
+ subject { PathSpec::Spec.new }
6
6
 
7
- it "does not allow matching" do
8
- expect { subject.match "anything" }.to raise_error
7
+ it 'does not allow matching' do
8
+ expect { subject.match 'anything' }.to raise_error(/Unimplemented/)
9
9
  end
10
10
 
11
- it { is_expected.to be_inclusive }
11
+ it { is_expected.to be_inclusive }
12
12
  end