Skip to content

Commit

Permalink
Implement match caching in Manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
ngan committed Sep 12, 2024
1 parent 94d5305 commit e5511bb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 38 deletions.
42 changes: 27 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,35 @@ PATH
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.5.0)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
diff-lcs (1.5.1)
io-console (0.7.2)
irb (1.14.0)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
psych (5.1.2)
stringio
rake (13.0.6)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.1)
rake (13.2.1)
rdoc (6.7.0)
psych (>= 4.0.0)
reline (0.5.10)
io-console (~> 0.5)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.1)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.1)
stringio (3.1.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
stringio (3.1.1)

PLATFORMS
arm64-darwin-21
Expand All @@ -34,8 +45,9 @@ PLATFORMS

DEPENDENCIES
code_manifest!
debug
rake (~> 13.0)
rspec (~> 3.0)
rspec

BUNDLED WITH
2.5.4
3 changes: 2 additions & 1 deletion code_manifest.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ Gem::Specification.new do |spec|

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

spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'debug'
end
18 changes: 10 additions & 8 deletions lib/code_manifest/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(patterns)
@rules ||= Array(patterns).map do |pattern|
Rule.new(pattern)
end
@cache = {}
end

def files
Expand All @@ -33,14 +34,15 @@ def digest
end

def matches(paths)
result_paths = Array(paths).select do |path|
inclusion_rules.any? { |rule| rule.match?(path) }
end
result_paths.reject! do |path|
exclusion_rules.any? { |rule| rule.match?(path) }
end

result_paths.sort!
Array(paths).select do |path|
if @cache.key?(path)
@cache.fetch(path)
else
@cache[path] =
inclusion_rules.any? { |rule| rule.match?(path) } &&
exclusion_rules.none? { |rule| rule.match?(path) }
end
end.sort!
end

def matches_all?(paths)
Expand Down
61 changes: 47 additions & 14 deletions spec/lib/code_manifest/manifest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,63 @@
let(:paths) do
[
'bar/exclude',
'bar/iniclude',
'bar/include',
'foo',
'baz/baz.md'
]
end

around do |example|
root.mkpath
root.join('bar').mkpath
root.join('baz').mkpath
it 'returns matched paths' do
expect(manifest.matches(paths)).to match_array([
'bar/include',
'foo'
])
end

paths.each do |path|
FileUtils.touch(root.join(path))
context 'caching concerns' do
let(:patterns) { ['/foo', '!bar/exclude'] }
let(:paths) { ['foo'] }

it 'caches rule lookups' do
include_rule = manifest.rules[0]
exclude_rule = manifest.rules[1]

expect(include_rule).to receive(:match?).with('foo').once.and_return(true)
expect(exclude_rule).to receive(:match?).with('foo').once.and_return(false)

manifest.matches(paths)
manifest.matches(paths)
end
end
end

example.run
root.rmtree
describe '#matches_all?' do
let(:patterns) { ['/foo', 'bar/*', '!bar/exclude'] }
let(:manifest) { described_class.new(patterns) }
let(:paths) do
[
'foo',
'bar/include2',
'bar/include1',
]
end

it 'returns matched paths' do
expect(manifest.matches(paths)).to match_array([
'bar/iniclude',
'foo'
])
it 'returns true if all paths are matched' do
expect(manifest.matches_all?(paths)).to be(true)
end

context 'when not all paths are matched' do
let(:paths) do
[
'bar/include',
'bar/exclude',
'foo'
]
end

it 'returns false' do
expect(manifest.matches_all?(paths)).to be(false)
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

require 'code_manifest'
require 'fileutils'
require 'debug/prelude'

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

0 comments on commit e5511bb

Please sign in to comment.