diff --git a/README.md b/README.md index cd397cd3..ec6688d8 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/erb_lint/cache.rb b/lib/erb_lint/cache.rb index ee537e17..94dde02d 100644 --- a/lib/erb_lint/cache.rb +++ b/lib/erb_lint/cache.rb @@ -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" @@ -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 @@ -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 @@ -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 diff --git a/lib/erb_lint/cli.rb b/lib/erb_lint/cli.rb index e21e4c4f..21cc4477 100644 --- a/lib/erb_lint/cli.rb +++ b/lib/erb_lint/cli.rb @@ -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? @@ -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 diff --git a/spec/erb_lint/cache_spec.rb b/spec/erb_lint/cache_spec.rb index 93731202..5d6e5477 100644 --- a/spec/erb_lint/cache_spec.rb +++ b/spec/erb_lint/cache_spec.rb @@ -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! @@ -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)) diff --git a/spec/erb_lint/cli_spec.rb b/spec/erb_lint/cli_spec.rb index a1a23d38..edbc6026 100644 --- a/spec/erb_lint/cli_spec.rb +++ b/spec/erb_lint/cli_spec.rb @@ -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" }