Skip to content

Commit e5511bb

Browse files
committed
Implement match caching in Manifest
1 parent 94d5305 commit e5511bb

File tree

5 files changed

+87
-38
lines changed

5 files changed

+87
-38
lines changed

Gemfile.lock

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,35 @@ PATH
77
GEM
88
remote: https://rubygems.org/
99
specs:
10-
diff-lcs (1.5.0)
10+
debug (1.9.2)
11+
irb (~> 1.10)
12+
reline (>= 0.3.8)
13+
diff-lcs (1.5.1)
14+
io-console (0.7.2)
15+
irb (1.14.0)
16+
rdoc (>= 4.0.0)
17+
reline (>= 0.4.2)
1118
psych (5.1.2)
1219
stringio
13-
rake (13.0.6)
14-
rspec (3.11.0)
15-
rspec-core (~> 3.11.0)
16-
rspec-expectations (~> 3.11.0)
17-
rspec-mocks (~> 3.11.0)
18-
rspec-core (3.11.0)
19-
rspec-support (~> 3.11.0)
20-
rspec-expectations (3.11.1)
20+
rake (13.2.1)
21+
rdoc (6.7.0)
22+
psych (>= 4.0.0)
23+
reline (0.5.10)
24+
io-console (~> 0.5)
25+
rspec (3.13.0)
26+
rspec-core (~> 3.13.0)
27+
rspec-expectations (~> 3.13.0)
28+
rspec-mocks (~> 3.13.0)
29+
rspec-core (3.13.1)
30+
rspec-support (~> 3.13.0)
31+
rspec-expectations (3.13.3)
2132
diff-lcs (>= 1.2.0, < 2.0)
22-
rspec-support (~> 3.11.0)
23-
rspec-mocks (3.11.1)
33+
rspec-support (~> 3.13.0)
34+
rspec-mocks (3.13.1)
2435
diff-lcs (>= 1.2.0, < 2.0)
25-
rspec-support (~> 3.11.0)
26-
rspec-support (3.11.1)
27-
stringio (3.1.0)
36+
rspec-support (~> 3.13.0)
37+
rspec-support (3.13.1)
38+
stringio (3.1.1)
2839

2940
PLATFORMS
3041
arm64-darwin-21
@@ -34,8 +45,9 @@ PLATFORMS
3445

3546
DEPENDENCIES
3647
code_manifest!
48+
debug
3749
rake (~> 13.0)
38-
rspec (~> 3.0)
50+
rspec
3951

4052
BUNDLED WITH
4153
2.5.4

code_manifest.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ Gem::Specification.new do |spec|
2121

2222
spec.add_dependency "psych", ">= 4.0.0"
2323

24-
spec.add_development_dependency 'rspec', '~> 3.0'
24+
spec.add_development_dependency 'rspec'
25+
spec.add_development_dependency 'debug'
2526
end

lib/code_manifest/manifest.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def initialize(patterns)
1313
@rules ||= Array(patterns).map do |pattern|
1414
Rule.new(pattern)
1515
end
16+
@cache = {}
1617
end
1718

1819
def files
@@ -33,14 +34,15 @@ def digest
3334
end
3435

3536
def matches(paths)
36-
result_paths = Array(paths).select do |path|
37-
inclusion_rules.any? { |rule| rule.match?(path) }
38-
end
39-
result_paths.reject! do |path|
40-
exclusion_rules.any? { |rule| rule.match?(path) }
41-
end
42-
43-
result_paths.sort!
37+
Array(paths).select do |path|
38+
if @cache.key?(path)
39+
@cache.fetch(path)
40+
else
41+
@cache[path] =
42+
inclusion_rules.any? { |rule| rule.match?(path) } &&
43+
exclusion_rules.none? { |rule| rule.match?(path) }
44+
end
45+
end.sort!
4446
end
4547

4648
def matches_all?(paths)

spec/lib/code_manifest/manifest_spec.rb

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,63 @@
107107
let(:paths) do
108108
[
109109
'bar/exclude',
110-
'bar/iniclude',
110+
'bar/include',
111111
'foo',
112112
'baz/baz.md'
113113
]
114114
end
115115

116-
around do |example|
117-
root.mkpath
118-
root.join('bar').mkpath
119-
root.join('baz').mkpath
116+
it 'returns matched paths' do
117+
expect(manifest.matches(paths)).to match_array([
118+
'bar/include',
119+
'foo'
120+
])
121+
end
120122

121-
paths.each do |path|
122-
FileUtils.touch(root.join(path))
123+
context 'caching concerns' do
124+
let(:patterns) { ['/foo', '!bar/exclude'] }
125+
let(:paths) { ['foo'] }
126+
127+
it 'caches rule lookups' do
128+
include_rule = manifest.rules[0]
129+
exclude_rule = manifest.rules[1]
130+
131+
expect(include_rule).to receive(:match?).with('foo').once.and_return(true)
132+
expect(exclude_rule).to receive(:match?).with('foo').once.and_return(false)
133+
134+
manifest.matches(paths)
135+
manifest.matches(paths)
123136
end
137+
end
138+
end
124139

125-
example.run
126-
root.rmtree
140+
describe '#matches_all?' do
141+
let(:patterns) { ['/foo', 'bar/*', '!bar/exclude'] }
142+
let(:manifest) { described_class.new(patterns) }
143+
let(:paths) do
144+
[
145+
'foo',
146+
'bar/include2',
147+
'bar/include1',
148+
]
127149
end
128150

129-
it 'returns matched paths' do
130-
expect(manifest.matches(paths)).to match_array([
131-
'bar/iniclude',
132-
'foo'
133-
])
151+
it 'returns true if all paths are matched' do
152+
expect(manifest.matches_all?(paths)).to be(true)
153+
end
154+
155+
context 'when not all paths are matched' do
156+
let(:paths) do
157+
[
158+
'bar/include',
159+
'bar/exclude',
160+
'foo'
161+
]
162+
end
163+
164+
it 'returns false' do
165+
expect(manifest.matches_all?(paths)).to be(false)
166+
end
134167
end
135168
end
136169
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
require 'code_manifest'
2020
require 'fileutils'
21+
require 'debug/prelude'
2122

2223
RSpec.configure do |config|
2324
# rspec-expectations config goes here. You can use an alternate

0 commit comments

Comments
 (0)