-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Implement file cache #268
Merged
etiennebarrie
merged 23 commits into
Shopify:main
from
zachfeldman:zfeldman/cbruckmayer/implement-file-cache
Oct 26, 2022
Merged
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
fbf26a2
Implement file cache
ChrisBr 17ce887
Initial cleaning implementation
zachfeldman b5aeb01
Rename clean to prune, implement clearing, add autocorrect error message
zachfeldman 95b5e22
Lints and cops
zachfeldman 7032a73
Add integration specs
zachfeldman 6012ae8
Fix cache implementation, clean up code
zachfeldman ad47b98
Specs for cache and associated changes
zachfeldman fbbcd2a
Clean up
zachfeldman a292ef0
Include ERBLint version in cache key
zachfeldman be11370
Fix nonsensical error message
zachfeldman deeb4f8
Spacing fixes and removing unneeded code
zachfeldman 1a5db05
Fix tests by mocking returned checksum
zachfeldman 8d4eaa8
Move cache pruning to the cache class
zachfeldman cdf089c
Do less file reads and stats
zachfeldman b331799
Attempt a new implementation for storing offenses in a CachedOffense …
zachfeldman ce8d6da
Update lib/erb_lint/cached_offense.rb to avoid errors on nil severity
zachfeldman acba4a9
Support caching with json and compact reporters
eterry1388 6d492e4
Merge pull request #1 from eterry1388/eterry1388/zfeldman/cbruckmayer…
zachfeldman 9ec34e3
Update lib/erb_lint/cli.rb to use safe navigation operator
zachfeldman 877c28c
Rename with-cache to cache
zachfeldman 7ea048c
Prune cache by default
zachfeldman a32816d
Add README for cache
zachfeldman e082855
Reduce output of pruning process
zachfeldman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module ERBLint | ||
# A Cached version of an Offense with only essential information represented as strings | ||
class CachedOffense | ||
attr_reader :line_number, :message, :severity | ||
|
||
def initialize(message, line_number, severity) | ||
@message = message | ||
@line_number = line_number | ||
@severity = severity | ||
end | ||
|
||
def self.new_from_offense(offense) | ||
new( | ||
offense.message, | ||
offense.line_number.to_s, | ||
offense.severity | ||
) | ||
end | ||
|
||
def to_json_format | ||
{ | ||
message: message, | ||
line_number: line_number, | ||
severity: severity, | ||
} | ||
end | ||
|
||
def self.from_json(parsed_json) | ||
parsed_json.transform_keys!(&:to_sym) | ||
new( | ||
parsed_json[:message], | ||
parsed_json[:line_number], | ||
parsed_json[:severity]&.to_sym | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -39,7 +39,7 @@ def run(args = ARGV) | |||||||
|
||||||||
load_config | ||||||||
|
||||||||
@cache = Cache.new(@config, file_loader) if with_cache? || clear_cache? | ||||||||
@cache = Cache.new(@config, file_loader, prune_cache?) if with_cache? || clear_cache? | ||||||||
|
||||||||
if clear_cache? | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made my own commit to fix these newline suggestions! |
||||||||
if cache.cache_dir_exists? | ||||||||
|
@@ -93,7 +93,7 @@ def run(args = ARGV) | |||||||
end | ||||||||
end | ||||||||
|
||||||||
cache.prune if prune_cache? | ||||||||
cache.close if with_cache? || clear_cache? | ||||||||
zachfeldman marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
reporter.show | ||||||||
|
||||||||
|
@@ -133,13 +133,11 @@ def run_on_file(runner, filename) | |||||||
end | ||||||||
|
||||||||
def run_using_cache(runner, filename, file_content) | ||||||||
if cache.include?(filename) | ||||||||
runner.restore_offenses(cache.get(filename, file_content)) | ||||||||
cache.add_hit(filename) if prune_cache? | ||||||||
if (cache_result_offenses = cache.get(filename, file_content)) | ||||||||
runner.restore_offenses(cache_result_offenses) | ||||||||
else | ||||||||
run_with_corrections(runner, filename, file_content) | ||||||||
cache[filename] = runner.offenses.map(&:to_json_format).to_json | ||||||||
cache.add_new_result(filename) if prune_cache? | ||||||||
cache.set(filename, file_content, runner.offenses.map(&:to_cached_offense_json_format).to_json) | ||||||||
end | ||||||||
end | ||||||||
|
||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
[{"linter_config":{"enabled":true,"exclude":[]},"linter_config_class":"ERBLint::LinterConfig","linter_class":"ERBLint::Linters::SpaceAroundErbTag","source_range":{"begin_pos":2,"end_pos":2,"source_buffer_name":"/home/zach/dev/fake-app/app/components/elements/image_component/image_component.html.erb"},"message":"Use 1 space after `<%` instead of 0 space.","context":" ","severity":null},{"linter_config":{"enabled":true,"exclude":[]},"linter_config_class":"ERBLint::LinterConfig","linter_class":"ERBLint::Linters::SpaceAroundErbTag","source_range":{"begin_pos":172,"end_pos":172,"source_buffer_name":"/home/zach/dev/fake-app/app/components/elements/image_component/image_component.html.erb"},"message":"Use 1 space before `%>` instead of 0 space.","context":" ","severity":null}] | ||
[{"message":"Layout/InitialIndentation: Indentation of first line in file detected.","line_number":"1","severity":"convention"},{"message":"Layout/TrailingEmptyLines: Final newline missing.","line_number":"1","severity":"convention"}] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how this is handled in the rest of the library but maybe we should pass in an io or logger so we could silence these outputs?
Something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems like a nice addition, the rest of the library though uses
puts
to output to$stdout
though. I did try the code you recommended, but I'm not really sure if that allows an easy way to silence output (at least I couldn't Google it, and couldn't make it happen running the command without output unless I redirect to> /dev/null
). Do you have a good example of this pattern in another lib? I could also go for a full blown logger instance withdebug
levels for most of the stuff in this file but then I feel like I'd want to apply that to allputs
statements in the project.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the rest of the library uses puts this is fine.
To silence it in tests you can pass in a
StringIO
like