spec/teaspoon/instrumentation_spec.rb in teaspoon-0.7.9 vs spec/teaspoon/instrumentation_spec.rb in teaspoon-0.8.0

- old
+ new

@@ -5,36 +5,59 @@ describe Teaspoon::Instrumentation do subject { Teaspoon::Instrumentation } - let(:asset) { double('asset', source: nil, pathname: 'path/to/instrument.js') } + let(:asset) { double(source: source, pathname: 'path/to/instrument.js') } + let(:source) { "function add(a, b) { return a + b } // ☃ " } let(:response) { [200, {"Content-Type" => "application/javascript"}, asset] } let(:env) { {"QUERY_STRING" => "instrument=true"} } - before do - Teaspoon::Instrumentation.stub(:istanbul).and_return("/path/to/istanbul") - Teaspoon::Instrumentation.instance_variable_set(:@executable, nil) - end + describe ".add_to" do - after do - Teaspoon::Instrumentation.instance_variable_set(:@executable, nil) - end + before do + Teaspoon::Instrumentation.stub(:executable).and_return("/path/to/istanbul") + end - describe ".executable" do + before do + Teaspoon::Instrumentation.stub(:add?).and_return(true) + asset.should_receive(:clone).and_return(asset) - it "returns the executable" do - expect(subject.executable).to eq("/path/to/istanbul") - expect(subject.instance_variable_get(:@executable)).to eq("/path/to/istanbul") + File.stub(:open) + subject.any_instance.stub(:instrument).and_return(source + " // instrumented") + + path = nil + Dir.mktmpdir { |p| path = p } + Dir.stub(:mktmpdir).and_yield(path) + @output = File.join(path, "instrument.js") end + it "writes the file to a tmp path" do + file = double('file') + File.should_receive(:open).with(@output, "w").and_yield(file) + file.should_receive(:write).with("function add(a, b) { return a + b } // ☃ ") + subject.add_to(response, env) + end + + it "instruments the javascript file" do + subject.any_instance.should_receive(:instrument).with(@output).and_return("_instrumented_") + subject.add_to(response, env) + end + + it "replaces the response array with the appropriate information" do + response = [666, {"Content-Type" => "application/javascript"}, asset] + expected = [666, {"Content-Type" => "application/javascript", "Content-Length" => "59"}, asset] + + expect(subject.add_to(response, env)).to eq(expected) + end + end describe ".add?" do before do - subject.stub(:executable).and_return("/path/to/istanbul") + Teaspoon::Instrumentation.stub(:executable).and_return("/path/to/istanbul") end it "returns true when everything is good" do expect(subject.add?(response, {"QUERY_STRING" => "instrument=true"})).to be(true) expect(subject.add?(response, {"QUERY_STRING" => "instrument=1"})).to be(true) @@ -62,45 +85,22 @@ expect(subject.add?([404, {"Content-Type" => "application/javascript"}, []], env)).to_not be(true) end end - describe ".add_to" do + describe "integration" do - let(:asset) { double(source: source, pathname: 'path/to/instrument.js') } - let(:source) { "function add(a, b) { return a + b } // ☃ " } + let(:asset) { Rails.application.assets.find_asset('instrumented1.coffee') } before do - Teaspoon::Instrumentation.stub(:add?).and_return(true) - asset.should_receive(:clone).and_return(asset) - - File.stub(:open) - subject.any_instance.stub(:instrument).and_return(source + " // instrumented") - - path = nil - Dir.mktmpdir { |p| path = p } - Dir.stub(:mktmpdir).and_yield(path) - @output = File.join(path, "instrument.js") + pending("needs istanbul to be installed") unless Teaspoon::Instrumentation.executable end - it "writes the file to a tmp path" do - file = double('file') - File.should_receive(:open).with(@output, "w").and_yield(file) - file.should_receive(:write).with("function add(a, b) { return a + b } // ☃ ") - subject.add_to(response, env) - end - - it "instruments the javascript file" do - subject.any_instance.should_receive(:instrument).with(@output).and_return("_instrumented_") - subject.add_to(response, env) - end - - it "replaces the response array with the appropriate information" do - response = [666, {"Content-Type" => "application/javascript"}, asset] - expected = [666, {"Content-Type" => "application/javascript", "Content-Length" => "59"}, asset] - - subject.add_to(response, env) - expect(response).to eq(expected) + it "instruments a file" do + status, headers, asset = subject.add_to(response, {"QUERY_STRING" => "instrument=true"}) + expect(status).to eq(200) + expect(headers).to include("Content-Type" => "application/javascript") + expect(asset.source).to match(/var __cov_.+ = \(Function\('return this'\)\)\(\);/) end end end