Skip to content

Commit

Permalink
Merge pull request #282 from Shopify/ac-move-cached-dir
Browse files Browse the repository at this point in the history
Allow custom cache directory via --cache-dir option
  • Loading branch information
adrianna-chang-shopify authored Oct 27, 2022
2 parents 3f14d38 + 4e28007 commit 3deee16
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,10 @@ File names pruned from the cache will be logged
No errors were found in ERB files
```

When the cache is on, lint results are stored in the `.erb-lint-cache` directory, in files with a filename computed with a hash of information about the file and `erb-lint` that should change when necessary. These files store instance attributes of the `CachedOffense` object, which only contain the `Offense` attributes necessary to restore the results of running `erb-lint` for output. The cache also automatically prunes outdated files each time it's run.
Cached lint results are stored in the `.erb-lint-cache` directory by default, though a custom directory can be provided
via the `--cache-dir` option. Cache filenames are computed with a hash of information about the file and `erb-lint` settings.
These files store instance attributes of the `CachedOffense` object, which only contain the `Offense` attributes
necessary to restore the results of running `erb-lint` for output. The cache also automatically prunes outdated files each time it's run.

You can also use the --clear-cache option to delete the cache file directory.

Expand Down
18 changes: 9 additions & 9 deletions lib/erb_lint/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module ERBLint
class Cache
CACHE_DIRECTORY = ".erb-lint-cache"

def initialize(config, file_loader = nil)
def initialize(config, cache_dir = nil)
@config = config
@file_loader = file_loader
@cache_dir = cache_dir || CACHE_DIRECTORY
@hits = []
@new_results = []
puts "Cache mode is on"
Expand All @@ -16,7 +16,7 @@ def get(filename, file_content)
file_checksum = checksum(filename, file_content)
begin
cache_file_contents_as_offenses = JSON.parse(
File.read(File.join(CACHE_DIRECTORY, file_checksum))
File.read(File.join(@cache_dir, file_checksum))
).map do |offense_hash|
ERBLint::CachedOffense.new(offense_hash)
end
Expand All @@ -31,9 +31,9 @@ def set(filename, file_content, offenses_as_json)
file_checksum = checksum(filename, file_content)
@new_results.push(file_checksum)

FileUtils.mkdir_p(CACHE_DIRECTORY)
FileUtils.mkdir_p(@cache_dir)

File.open(File.join(CACHE_DIRECTORY, file_checksum), "wb") do |f|
File.open(File.join(@cache_dir, file_checksum), "wb") do |f|
f.write(offenses_as_json)
end
end
Expand All @@ -48,23 +48,23 @@ def prune_cache
return
end

cache_files = Dir.new(CACHE_DIRECTORY).children
cache_files = Dir.new(@cache_dir).children
cache_files.each do |cache_file|
next if hits.include?(cache_file) || new_results.include?(cache_file)

File.delete(File.join(CACHE_DIRECTORY, cache_file))
File.delete(File.join(@cache_dir, cache_file))
end
end

def cache_dir_exists?
File.directory?(CACHE_DIRECTORY)
File.directory?(@cache_dir)
end

def clear
return unless cache_dir_exists?

puts "Clearing cache by deleting cache directory"
FileUtils.rm_r(CACHE_DIRECTORY)
FileUtils.rm_r(@cache_dir)
end

private
Expand Down
7 changes: 6 additions & 1 deletion lib/erb_lint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def run(args = ARGV)

load_config

@cache = Cache.new(@config, file_loader) if cache? || clear_cache?
cache_dir = @options[:cache_dir]
@cache = Cache.new(@config, cache_dir) if cache? || clear_cache?

if clear_cache?
if cache.cache_dir_exists?
Expand Down Expand Up @@ -338,6 +339,10 @@ def option_parser
@options[:cache] = config
end

opts.on("--cache-dir DIR", "Set the cache directory") do |dir|
@options[:cache_dir] = dir
end

opts.on("--clear-cache", "Clear cache") do |config|
@options[:clear_cache] = config
end
Expand Down
5 changes: 3 additions & 2 deletions spec/erb_lint/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
include FakeFS::SpecHelpers

let(:linter_config) { ERBLint::LinterConfig.new }
let(:cache) { described_class.new(linter_config) }
let(:cache) { described_class.new(linter_config, cache_dir) }
let(:linted_file_path) { "app/components/elements/image_component/image_component.html.erb" }
let(:checksum) { "2dc3e17183b87889cc783b0157723570d4bbb90a" }
let(:cache_dir) { ERBLint::Cache::CACHE_DIRECTORY }
let(:cache_dir) { "tmp/erb_lint" }
let(:rubocop_yml) { %(SpaceAroundErbTag:\n Enabled: true\n) }
let(:cache_file_content) do
FakeFS.deactivate!
Expand Down Expand Up @@ -74,6 +74,7 @@
it "returns true if the cache dir exists" do
expect(cache.cache_dir_exists?).to(be(true))
end

it "returns false if the cache dir does not exist" do
FileUtils.rm_rf(cache_dir)
expect(cache.cache_dir_exists?).to(be(false))
Expand Down
13 changes: 13 additions & 0 deletions spec/erb_lint/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ def run(processed_source)
end
end

context "with custom --cache-dir" do
let(:args) { ["--lint-all", "--enable-linter", "linter_with_errors", "--clear-cache", "--cache-dir", cache_dir] }
let(:cache_dir) { "tmp/erb_lint" }

before do
FileUtils.mkdir_p(cache_dir)
end

it "uses the specified directory" do
expect { subject }.to(output(/cache directory cleared/).to_stdout)
end
end

context "with file as argument" do
context "when file does not exist" do
let(:linted_file) { "/path/to/myfile.html.erb" }
Expand Down

0 comments on commit 3deee16

Please sign in to comment.