From e1f3fc1cf97802ca6215f23c1e04885fcfe162eb Mon Sep 17 00:00:00 2001 From: Michael Grosser Date: Fri, 20 Mar 2015 20:54:55 +0100 Subject: [PATCH 1/2] do not load rubygems, it is not necessary in post ruby 1.9 --- bin/cc-tddium-post-worker | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/cc-tddium-post-worker b/bin/cc-tddium-post-worker index dffa4ce..70e8db8 100755 --- a/bin/cc-tddium-post-worker +++ b/bin/cc-tddium-post-worker @@ -1,6 +1,5 @@ #!/usr/bin/env ruby -require 'rubygems' require 'codeclimate-test-reporter' require 'tmpdir' From 20de39a39307349b9e406849978c1c527c8f5ef3 Mon Sep 17 00:00:00 2001 From: Michael Grosser Date: Fri, 20 Mar 2015 21:59:19 +0100 Subject: [PATCH 2/2] simple test for current batch_post_results + use webmock --- codeclimate-test-reporter.gemspec | 2 +- spec/lib/ci_spec.rb | 1 - spec/lib/client_spec.rb | 16 +++++++++++ spec/lib/formatter_spec.rb | 16 ++++------- spec/spec_helper.rb | 44 +++++++++---------------------- 5 files changed, 34 insertions(+), 45 deletions(-) diff --git a/codeclimate-test-reporter.gemspec b/codeclimate-test-reporter.gemspec index 0b9a085..d7e04f0 100644 --- a/codeclimate-test-reporter.gemspec +++ b/codeclimate-test-reporter.gemspec @@ -19,6 +19,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake" spec.add_development_dependency "rspec" - spec.add_development_dependency "artifice" + spec.add_development_dependency "webmock" spec.add_development_dependency "pry" end diff --git a/spec/lib/ci_spec.rb b/spec/lib/ci_spec.rb index 99f7d6e..19ad55b 100644 --- a/spec/lib/ci_spec.rb +++ b/spec/lib/ci_spec.rb @@ -2,7 +2,6 @@ module CodeClimate::TestReporter describe Ci do - describe '.service_data' do before :each do @env = { diff --git a/spec/lib/client_spec.rb b/spec/lib/client_spec.rb index c6abc51..2a9d31c 100644 --- a/spec/lib/client_spec.rb +++ b/spec/lib/client_spec.rb @@ -20,5 +20,21 @@ module CodeClimate::TestReporter Client.new.post_results("") end + + describe "#batch_post_results" do + let(:uuid) { "my-uuid" } + let(:token) { ENV["CODECLIMATE_REPO_TOKEN"] } + + before { expect(SecureRandom).to receive(:uuid).and_return uuid } + around { |test| Dir.mktmpdir { |dir| Dir.chdir(dir, &test) } } + + it "posts a single file" do + File.write("a", "Something") + requests = capture_requests(stub_request(:post, "http://cc.dev/test_reports/batch")) + Client.new.batch_post_results(["a"]) + + expect(requests.first.body).to eq "--#{uuid}\r\nContent-Disposition: form-data; name=\"repo_token\"\r\n\r\n#{token}\r\n--#{uuid}\r\nContent-Disposition: form-data; name=\"coverage_reports[0]\"; filename=\"a\"\r\nContent-Type: application/json\r\n\r\nSomething\r\n--#{uuid}--\r\n" + end + end end end diff --git a/spec/lib/formatter_spec.rb b/spec/lib/formatter_spec.rb index 9e10f66..05d501f 100644 --- a/spec/lib/formatter_spec.rb +++ b/spec/lib/formatter_spec.rb @@ -94,22 +94,16 @@ module CodeClimate::TestReporter it "sends an http request with all the coverage information" do allow(CodeClimate::TestReporter).to receive(:run?).and_return(true) - app = FakeCodeClimateEndpoint.new - Artifice.activate_with(app) do - formatter.format(simplecov_result) - end - - expect(app.path_info).to eq("/test_reports") - expect(app.content_type).to eq("application/json") - expect(app.http_content_encoding).to eq("gzip") + stub = stub_request(:post, "http://cc.dev/test_reports"). + with(:headers => {'Content-Encoding'=>'gzip', 'Content-Type'=>'application/json', 'User-Agent'=>"Code Climate (Ruby Test Reporter v#{CodeClimate::TestReporter::VERSION})"}) + requests = capture_requests(stub) - uncompressed = inflate(app.request_body) + formatter.format(simplecov_result) + uncompressed = inflate(requests.first.body) expected_request.merge!("ci_service" => Ci.service_data) expected_json = JSON.parse(expected_request.to_json, symbolize_names: true) - expect(JSON.parse(uncompressed, symbolize_names: true)).to eq(expected_json) - expect(app.http_user_agent).to include("v#{CodeClimate::TestReporter::VERSION}") end describe '#short_filename' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4fc783a..7cd56db 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,46 +1,26 @@ -require 'rubygems' require 'bundler/setup' -require 'artifice' require 'pry' require 'codeclimate-test-reporter' +require 'webmock/rspec' ENV['CODECLIMATE_REPO_TOKEN'] = "172754c1bf9a3c698f7770b9fb648f1ebb214425120022d0b2ffc65b97dff531" ENV['CODECLIMATE_API_HOST'] = "http://cc.dev" -def inflate(string) - reader = Zlib::GzipReader.new(StringIO.new(string)) - reader.read -end - -class FakeCodeClimateEndpoint - def call(env) - @env = env - [ - 200, - {"Content-Type" => 'text/plain'}, - ["Received"] - ] - end - - def path_info - @env["PATH_INFO"] - end - - def request_body - @env["rack.input"].string - end - - def content_type - @env["CONTENT_TYPE"] +module TestHelper + def inflate(string) + reader = Zlib::GzipReader.new(StringIO.new(string)) + reader.read end - def http_content_encoding - @env["HTTP_CONTENT_ENCODING"] + def capture_requests(stub) + requests = [] + stub.to_return { |r| requests << r; {body: "hello"} } + requests end +end - def http_user_agent - @env["HTTP_USER_AGENT"] - end +RSpec.configure do |c| + c.include TestHelper end