From 1b342e825769b6a82c4e572db94b56459ad8b6a7 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Mon, 6 Nov 2023 08:53:22 +0100 Subject: [PATCH] Make `TODOFormatter`'s configuration file path configurable Fixes the case where formatter specs were deleting project's `.ameba.yml` file --- spec/ameba/formatter/todo_formatter_spec.cr | 8 +++++--- src/ameba/formatter/todo_formatter.cr | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/spec/ameba/formatter/todo_formatter_spec.cr b/spec/ameba/formatter/todo_formatter_spec.cr index 99ad3949..cc7dd32c 100644 --- a/spec/ameba/formatter/todo_formatter_spec.cr +++ b/spec/ameba/formatter/todo_formatter_spec.cr @@ -1,10 +1,12 @@ require "../../spec_helper" require "file_utils" +CONFIG_PATH = Path[Dir.tempdir] / Ameba::Config::FILENAME + module Ameba private def with_formatter(&) io = IO::Memory.new - formatter = Formatter::TODOFormatter.new(io) + formatter = Formatter::TODOFormatter.new(io, CONFIG_PATH) yield formatter, io end @@ -20,7 +22,7 @@ module Ameba describe Formatter::TODOFormatter do ::Spec.after_each do - FileUtils.rm_rf(Ameba::Config::DEFAULT_PATH) + FileUtils.rm_rf(CONFIG_PATH) end context "problems not found" do @@ -45,7 +47,7 @@ module Ameba s = Source.new "a = 1", "source.cr" s.add_issue DummyRule.new, {1, 2}, "message" formatter.finished([s]) - io.to_s.should contain "Created #{Config::DEFAULT_PATH}" + io.to_s.should contain "Created #{CONFIG_PATH}" end end diff --git a/src/ameba/formatter/todo_formatter.cr b/src/ameba/formatter/todo_formatter.cr index 8bcc3d5c..3e6efee3 100644 --- a/src/ameba/formatter/todo_formatter.cr +++ b/src/ameba/formatter/todo_formatter.cr @@ -3,7 +3,7 @@ module Ameba::Formatter # Basically, it takes all issues reported and disables corresponding rules # or excludes failed sources from these rules. class TODOFormatter < DotFormatter - def initialize(@output = STDOUT) + def initialize(@output = STDOUT, @config_path : Path = Config::DEFAULT_PATH) end def finished(sources) @@ -26,7 +26,7 @@ module Ameba::Formatter end private def generate_todo_config(issues) - file = File.new(Config::DEFAULT_PATH, mode: "w") + file = File.new(@config_path, mode: "w") file << header rule_issues_map(issues).each do |rule, rule_issues| file << "\n# Problems found: #{rule_issues.size}"