Skip to content

DRY analyzer specs #46

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

Merged
merged 1 commit into from
Nov 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 2 additions & 25 deletions spec/cc/engine/analyzers/javascript/main_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@
require 'flay'
require 'tmpdir'

RSpec.describe CC::Engine::Analyzers::Javascript::Main do
around do |example|
Dir.mktmpdir do |directory|
@code = directory

Dir.chdir(directory) do
example.run
end
end
end
RSpec.describe CC::Engine::Analyzers::Javascript::Main, in_tmpdir: true do
include AnalyzerSpecHelpers

describe "#run" do
it "prints an issue" do
Expand Down Expand Up @@ -68,21 +60,6 @@
expect(issues.length).to eq 1
end

def create_source_file(path, content)
File.write(File.join(@code, path), content)
end

def run_engine(config = nil)
io = StringIO.new

engine = ::CC::Engine::Analyzers::Javascript::Main.new(engine_config: config)
reporter = ::CC::Engine::Analyzers::Reporter.new(double(concurrency: 2), engine, io)

reporter.run

io.string
end

def engine_conf
CC::Engine::Analyzers::EngineConfig.new({
'config' => {
Expand Down
27 changes: 2 additions & 25 deletions spec/cc/engine/analyzers/php/main_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@
require 'flay'
require 'tmpdir'

RSpec.describe CC::Engine::Analyzers::Php::Main do
around do |example|
Dir.mktmpdir do |directory|
@code = directory

Dir.chdir(directory) do
example.run
end
end
end
RSpec.describe CC::Engine::Analyzers::Php::Main, in_tmpdir: true do
include AnalyzerSpecHelpers

describe "#run" do
it "prints an issue" do
Expand Down Expand Up @@ -58,21 +50,6 @@
end
end

def create_source_file(path, content)
File.write(File.join(@code, path), content)
end

def run_engine(config = nil)
io = StringIO.new

engine = ::CC::Engine::Analyzers::Php::Main.new(engine_config: config)
reporter = ::CC::Engine::Analyzers::Reporter.new(double(concurrency: 2), engine, io)

reporter.run

io.string
end

def printed_issue
issue = {"type":"issue","check_name":"Identical code","description":"Similar code found in 1 other location","categories":["Duplication"],"location":{"path":"foo.php","lines":{"begin":2,"end":6}},"remediation_points":176000,"other_locations":[{"path":"foo.php","lines":{"begin":10,"end":14}}],"content":{"body": read_up}}
issue.to_json + "\0\n"
Expand Down
27 changes: 2 additions & 25 deletions spec/cc/engine/analyzers/python/main_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@
require "flay"
require "tmpdir"

RSpec.describe CC::Engine::Analyzers::Python::Main do
around do |example|
Dir.mktmpdir do |directory|
@code = directory

Dir.chdir(directory) do
example.run
end
end
end
RSpec.describe CC::Engine::Analyzers::Python::Main, in_tmpdir: true do
include AnalyzerSpecHelpers

describe "#run" do
it "prints an issue" do
Expand Down Expand Up @@ -46,21 +38,6 @@
end
end

def create_source_file(path, content)
File.write(File.join(@code, path), content)
end

def run_engine(config = nil)
io = StringIO.new

engine = ::CC::Engine::Analyzers::Python::Main.new(engine_config: config)
reporter = ::CC::Engine::Analyzers::Reporter.new(double(concurrency: 2), engine, io)

reporter.run

io.string
end

def engine_conf
CC::Engine::Analyzers::EngineConfig.new({
"config" => {
Expand Down
32 changes: 6 additions & 26 deletions spec/cc/engine/analyzers/ruby/main_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@
require 'flay'
require 'tmpdir'

RSpec.describe CC::Engine::Analyzers::Ruby::Main do
around do |example|
Dir.mktmpdir do |directory|
@code = directory

Dir.chdir(directory) do
example.run
end
end
end
RSpec.describe CC::Engine::Analyzers::Ruby::Main, in_tmpdir: true do
include AnalyzerSpecHelpers

describe "#run" do
it "prints an issue" do
Expand All @@ -35,7 +27,7 @@
end
EORUBY

result = run_engine.strip
result = run_engine(engine_conf).strip
json = JSON.parse(result)

expect(json["type"]).to eq("issue")
Expand All @@ -60,24 +52,12 @@
EORUBY

expect {
expect(run_engine).to eq("")
expect(run_engine(engine_conf)).to eq("")
}.to output(/Skipping file/).to_stderr
end
end

def create_source_file(path, content)
File.write(File.join(@code, path), content)
end

def run_engine(config = {})
io = StringIO.new

config = CC::Engine::Analyzers::EngineConfig.new(config)
engine = ::CC::Engine::Analyzers::Ruby::Main.new(engine_config: config)
reporter = ::CC::Engine::Analyzers::Reporter.new(double(concurrency: 2), engine, io)

reporter.run

io.string
def engine_conf
CC::Engine::Analyzers::EngineConfig.new({})
end
end
10 changes: 10 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
mocks.verify_partial_doubles = true
end

config.around(:example, :in_tmpdir) do |example|
Dir.mktmpdir do |directory|
@code = directory

Dir.chdir(directory) do
example.run
end
end
end

config.order = :random
config.disable_monkey_patching!
end
16 changes: 16 additions & 0 deletions spec/support/helpers/analyzer_spec_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module AnalyzerSpecHelpers
def create_source_file(path, content)
File.write(File.join(@code, path), content)
end

def run_engine(config = nil)
io = StringIO.new

engine = described_class.new(engine_config: config)
reporter = ::CC::Engine::Analyzers::Reporter.new(double(concurrency: 2), engine, io)

reporter.run

io.string
end
end