Refactor TODOFormatter spec

This commit is contained in:
Sijawusz Pur Rahnama 2022-11-22 19:51:48 +01:00
parent 2af58cabd4
commit 9f670c09b5
2 changed files with 66 additions and 54 deletions

View file

@ -2,42 +2,51 @@ require "../../spec_helper"
require "file_utils"
module Ameba
private def with_formatter
io = IO::Memory.new
formatter = Formatter::TODOFormatter.new(io)
yield formatter, io
end
private def create_todo
formatter = Formatter::TODOFormatter.new IO::Memory.new
s = Source.new "a = 1", "source.cr"
s.add_issue DummyRule.new, {1, 2}, "message"
file = formatter.finished([s])
file ? File.read(file.path) : ""
with_formatter do |formatter|
s = Source.new "a = 1", "source.cr"
s.add_issue DummyRule.new, {1, 2}, "message"
file = formatter.finished([s])
file ? File.read(file.path) : ""
end
end
describe Formatter::TODOFormatter do
::Spec.after_each do
FileUtils.rm(Ameba::Config::PATH) if File.exists?(Ameba::Config::PATH)
FileUtils.rm_rf(Ameba::Config::PATH)
end
context "problems not found" do
it "does not create file" do
formatter = Formatter::TODOFormatter.new IO::Memory.new
file = formatter.finished [Source.new ""]
file.should be_nil
with_formatter do |formatter|
file = formatter.finished [Source.new ""]
file.should be_nil
end
end
it "reports a message saying file is not created" do
io = IO::Memory.new
formatter = Formatter::TODOFormatter.new io
formatter.finished [Source.new ""]
io.to_s.should contain "No issues found. File is not generated"
with_formatter do |formatter, io|
formatter.finished [Source.new ""]
io.to_s.should contain "No issues found. File is not generated"
end
end
end
context "problems found" do
it "prints a message saying file is created" do
io = IO::Memory.new
formatter = Formatter::TODOFormatter.new io
s = Source.new "a = 1", "source.cr"
s.add_issue DummyRule.new, {1, 2}, "message"
formatter.finished([s])
io.to_s.should contain "Created .ameba.yml"
with_formatter do |formatter, io|
s = Source.new "a = 1", "source.cr"
s.add_issue DummyRule.new, {1, 2}, "message"
formatter.finished([s])
io.to_s.should contain "Created .ameba.yml"
end
end
it "creates a valid YAML document" do
@ -77,49 +86,52 @@ module Ameba
end
context "with multiple issues" do
formatter = Formatter::TODOFormatter.new IO::Memory.new
it "does generate todo file" do
with_formatter do |formatter|
s1 = Source.new "a = 1", "source1.cr"
s2 = Source.new "a = 1", "source2.cr"
s1.add_issue DummyRule.new, {1, 2}, "message1"
s1.add_issue NamedRule.new, {1, 2}, "message1"
s1.add_issue DummyRule.new, {2, 2}, "message1"
s2.add_issue DummyRule.new, {2, 2}, "message2"
s1 = Source.new "a = 1", "source1.cr"
s2 = Source.new "a = 1", "source2.cr"
s1.add_issue DummyRule.new, {1, 2}, "message1"
s1.add_issue NamedRule.new, {1, 2}, "message1"
s1.add_issue DummyRule.new, {2, 2}, "message1"
s2.add_issue DummyRule.new, {2, 2}, "message2"
file = formatter.finished([s1, s2]).should_not be_nil
content = File.read(file.path)
content.should contain <<-CONTENT
# Problems found: 3
# Run `ameba --only Ameba/DummyRule` for details
Ameba/DummyRule:
Description: Dummy rule that does nothing.
Excluded:
- source1.cr
- source2.cr
Enabled: true
Severity: Convention
CONTENT
file = formatter.finished([s1, s2]).should_not be_nil
content = File.read(file.path)
content.should contain <<-CONTENT
# Problems found: 3
# Run `ameba --only Ameba/DummyRule` for details
Ameba/DummyRule:
Description: Dummy rule that does nothing.
Excluded:
- source1.cr
- source2.cr
Enabled: true
Severity: Convention
CONTENT
end
end
end
context "when invalid syntax" do
it "does generate todo file" do
formatter = Formatter::TODOFormatter.new IO::Memory.new
s = Source.new "def invalid_syntax"
s.add_issue Rule::Lint::Syntax.new, {1, 2}, "message"
with_formatter do |formatter|
s = Source.new "def invalid_syntax"
s.add_issue Rule::Lint::Syntax.new, {1, 2}, "message"
file = formatter.finished [s]
file.should be_nil
file = formatter.finished [s]
file.should be_nil
end
end
it "prints an error message" do
io = IO::Memory.new
formatter = Formatter::TODOFormatter.new io
s = Source.new "def invalid_syntax"
s.add_issue Rule::Lint::Syntax.new, {1, 2}, "message"
with_formatter do |formatter, io|
s = Source.new "def invalid_syntax"
s.add_issue Rule::Lint::Syntax.new, {1, 2}, "message"
formatter.finished [s]
io.to_s.should contain "Unable to generate TODO file"
io.to_s.should contain "Please fix syntax issues"
formatter.finished [s]
io.to_s.should contain "Unable to generate TODO file"
io.to_s.should contain "Please fix syntax issues"
end
end
end
end

View file

@ -74,14 +74,14 @@ module Ameba
# A rule that always raises an error
class RaiseRule < Rule::Base
property should_raise = false
property? should_raise = false
properties do
description "Internal rule that always raises"
end
def test(source)
should_raise && raise "something went wrong"
should_raise? && raise "something went wrong"
end
end