Skip to content

Commit 32dab65

Browse files
committed
Add JSON support to profdata file format
2 parents d3bece9 + 8558508 commit 32dab65

13 files changed

+69
-40
lines changed

lib/slather/coverage_file.rb

+9-5
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,13 @@ def all_lines
6868
end
6969
end
7070

71-
def line_number_in_line(line)
72-
line.split(':')[1].strip.to_i
71+
def cleaned_gcov_data
72+
data = gcov_data.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '').gsub(/^function(.*) called [0-9]+ returned [0-9]+% blocks executed(.*)$\r?\n/, '')
73+
data.gsub(/^branch(.*)$\r?\n/, '')
74+
end
75+
76+
def raw_data
77+
self.gcov_data
7378
end
7479

7580
def line_coverage_data
@@ -84,9 +89,8 @@ def line_coverage_data
8489
end
8590
end
8691

87-
def cleaned_gcov_data
88-
data = gcov_data.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '').gsub(/^function(.*) called [0-9]+ returned [0-9]+% blocks executed(.*)$\r?\n/, '')
89-
data.gsub(/^branch(.*)$\r?\n/, '')
92+
def line_number_in_line(line)
93+
line.split(':')[1].strip.to_i
9094
end
9195

9296
def coverage_for_line(line)

lib/slather/coverage_service/gutter_json_output.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ def post
1616
symbols = {}
1717

1818
coverage_files.each do |coverage_file|
19-
next unless coverage_file.gcov_data
19+
next unless coverage_file.raw_data
2020

2121
filename = coverage_file.source_file_pathname.to_s
2222
filename = filename.sub(Pathname.pwd.to_s, '').reverse.chomp("/").reverse
2323

2424
coverage_file.all_lines.each do |line|
25-
data = line.split(':')
2625

27-
line_number = data[1].to_i
26+
line_number = coverage_file.line_number_in_line(line)
2827
next unless line_number > 0
2928

30-
coverage = data[0].strip
29+
coverage = coverage_file.coverage_for_line(line)
30+
short_text = coverage != nil ? coverage.to_s : "-"
3131

3232
symbol = { 'line' => line_number,
3333
'long_text' => '',
34-
'short_text' => coverage }
34+
'short_text' => short_text }
3535

36-
if coverage != '-'
36+
if coverage != nil
3737
symbol['background_color'] = coverage.to_i > 0 ? '0x35CC4B' : '0xFC635E'
3838
end
3939

lib/slather/coverage_service/hardcover.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module CoverageService
33
module Hardcover
44

55
def coverage_file_class
6-
Slather::CoverallsCoverageFile
6+
Slather::CoverageFile
77
end
88
private :coverage_file_class
99

lib/slather/profdata_coverage_file.rb

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ def all_lines
4545
@all_lines
4646
end
4747

48+
def raw_data
49+
self.source
50+
end
51+
4852
def line_number_in_line(line)
4953
line =~ /^(\s*)(\d*)\|(\s*)(\d+)\|/
5054
if $4 != nil

lib/slather/project.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def profdata_coverage_files
6868
files.map do |source|
6969
coverage_file = coverage_file_class.new(self, source)
7070
!coverage_file.ignored? ? coverage_file : nil
71-
end
71+
end.compact
7272
end
7373
private :profdata_coverage_files
7474

spec/fixtures/cobertura.xml

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?xml version="1.0"?>
22
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
3-
<coverage line-rate="0.6603773584905660" branch-rate="0.1190476190476190" lines-covered="35" lines-valid="53" branches-covered="5" branches-valid="42" complexity="0.0" timestamp="1428118535" version="Slather 1.6.0">
3+
<coverage line-rate="0.6666666666666666" branch-rate="0.1190476190476190" lines-covered="34" lines-valid="51" branches-covered="5" branches-valid="42" complexity="0.0" timestamp="1428118535" version="Slather 1.6.0">
44
<sources>
55
<source>/Users/marklarsen/programming/slather</source>
66
</sources>
77
<packages>
8-
<package name="spec.fixtures.fixtures.more_files" line-rate="0.3846153846153846" branch-rate="0.3333333333333333" complexity="0.0">
8+
<package name="spec.fixtures.fixtures.more_files" line-rate="0.3750000000000000" branch-rate="0.3333333333333333" complexity="0.0">
99
<classes>
10-
<class name="Branches" filename="spec/fixtures/fixtures/more_files/Branches.m" line-rate="0.5000000000000000" branch-rate="0.4000000000000000" complexity="0.0">
10+
<class name="Branches" filename="spec/fixtures/fixtures/more_files/Branches.m" line-rate="0.4736842105263158" branch-rate="0.4000000000000000" complexity="0.0">
1111
<methods/>
1212
<lines>
13-
<line number="13" branch="false" hits="2"/>
1413
<line number="15" branch="true" hits="2" condition-coverage="100% (2/2)">
1514
<conditions>
1615
<condition number="0" type="jump" coverage="100%"/>
@@ -55,7 +54,6 @@
5554
<class name="peekaview" filename="spec/fixtures/fixtures/more_files/peekaview.m" line-rate="0.0000000000000000" branch-rate="0.0000000000000000" complexity="0.0">
5655
<methods/>
5756
<lines>
58-
<line number="13" branch="false" hits="0"/>
5957
<line number="15" branch="false" hits="0"/>
6058
<line number="16" branch="true" hits="0" condition-coverage="0% (0/2)">
6159
<conditions>

spec/fixtures/fixtures/Fixtures.swift

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
import UIKit
3+
4+
@UIApplicationMain
5+
class AppDelegate: UIResponder, UIApplicationDelegate {
6+
7+
var window: UIWindow?
8+
9+
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
10+
return true
11+
}
12+
13+
func applicationWillResignActive(application: UIApplication) {
14+
}
15+
}

spec/fixtures/gutter.json

+1-1
Large diffs are not rendered by default.

spec/slather/coverage_service/coveralls_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
describe "#coverage_file_class" do
1111
it "should return CoverallsCoverageFile" do
12-
expect(fixtures_project.send(:coverage_file_class)).to eq(Slather::CoverallsCoverageFile)
12+
expect(fixtures_project.send(:coverage_file_class)).to eq(Slather::CoverageFile)
1313
end
1414
end
1515

spec/slather/coverage_service/hardcover_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
describe "#coverage_file_class" do
1919
it "should return CoverallsCoverageFile" do
20-
expect(fixtures_project.send(:coverage_file_class)).to eq(Slather::CoverallsCoverageFile)
20+
expect(fixtures_project.send(:coverage_file_class)).to eq(Slather::CoverageFile)
2121
end
2222
end
2323

spec/slather/coverage_service/simple_output_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
describe '#post' do
1717
it "should print out the coverage for each file, and then total coverage" do
1818
["spec/fixtures/fixtures/fixtures.m: 2 of 4 lines (50.00%)",
19-
"spec/fixtures/fixtures/more_files/peekaview.m: 0 of 6 lines (0.00%)",
19+
"spec/fixtures/fixtures/more_files/peekaview.m: 0 of 5 lines (0.00%)",
2020
"spec/fixtures/fixtures/fixtures_cpp.cpp: 0 of 0 lines (100.00%)",
2121
"spec/fixtures/fixtures/fixtures_mm.mm: 0 of 0 lines (100.00%)",
2222
"spec/fixtures/fixtures/fixtures_m.m: 0 of 0 lines (100.00%)",
23-
"spec/fixtures/fixtures/more_files/Branches.m: 10 of 20 lines (50.00%)",
23+
"spec/fixtures/fixtures/more_files/Branches.m: 9 of 19 lines (47.37%)",
2424
"spec/fixtures/fixtures/more_files/Empty.m: 0 of 0 lines (100.00%)",
2525
"spec/fixtures/fixturesTests/fixturesTests.m: 7 of 7 lines (100.00%)",
2626
"spec/fixtures/fixturesTests/peekaviewTests.m: 6 of 6 lines (100.00%)",
2727
"spec/fixtures/fixturesTests/BranchesTests.m: 10 of 10 lines (100.00%)",
28-
"Test Coverage: 66.04%"].each do |line|
28+
"Test Coverage: 66.67%"].each do |line|
2929
expect(fixtures_project).to receive(:puts).with(line)
3030
end
3131

spec/slather/project_spec.rb

+23-16
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,36 @@ class SpecXcode7CoverageFile < Slather::ProfdataCoverageFile
8989
before(:each) do
9090
Dir.stub(:[]).and_call_original
9191
Dir.stub(:[]).with("#{fixtures_project.build_directory}/**/Coverage.profdata").and_return(["/some/path/Coverage.profdata"])
92-
fixtures_project.stub(:profdata_llvm_cov_output).and_return("/Users/venmo/ExampleProject/AppDelegate.swift:
93-
| 8|
94-
| 9|import UIKit
95-
| 10|
96-
| 11|@UIApplicationMain
97-
| 12|class AppDelegate: UIResponder, UIApplicationDelegate {
98-
| 13|
99-
| 14| var window: UIWindow?
100-
| 16|
101-
1| 17| func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
102-
1| 19| return true
103-
1| 20| }
104-
| 21|
105-
0| 22| func applicationWillResignActive(application: UIApplication) {
106-
0| 25| }")
92+
fixtures_project.stub(:profdata_llvm_cov_output).and_return("#{FIXTURES_SWIFT_FILE_PATH}:
93+
| 0|
94+
| 1|import UIKit
95+
| 2|
96+
| 3|@UIApplicationMain
97+
| 4|class AppDelegate: UIResponder, UIApplicationDelegate {
98+
| 5|
99+
| 6| var window: UIWindow?
100+
| 7|
101+
1| 8| func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
102+
1| 9| return true
103+
1| 10| }
104+
| 11|
105+
0| 12| func applicationWillResignActive(application: UIApplication) {
106+
0| 13| }
107+
0| 14|}")
107108
fixtures_project.stub(:coverage_file_class).and_return(SpecXcode7CoverageFile)
108109
fixtures_project.stub(:ignore_list).and_return([])
109110
end
110111

111112
it "should return Coverage.profdata file objects" do
112113
profdata_coverage_files = fixtures_project.send(:profdata_coverage_files)
113114
profdata_coverage_files.each { |cf| expect(cf.kind_of?(SpecXcode7CoverageFile)).to be_truthy }
114-
expect(profdata_coverage_files.map { |cf| cf.source_file_pathname.basename.to_s }).to eq(["AppDelegate.swift"])
115+
expect(profdata_coverage_files.map { |cf| cf.source_file_pathname.basename.to_s }).to eq(["Fixtures.swift"])
116+
end
117+
118+
it "should ignore files from the ignore list" do
119+
fixtures_project.stub(:ignore_list).and_return(["**/Fixtures.swift"])
120+
profdata_coverage_files = fixtures_project.send(:profdata_coverage_files)
121+
expect(profdata_coverage_files.count).to eq(0)
115122
end
116123
end
117124

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
FIXTURES_XML_PATH = File.join(File.dirname(__FILE__), 'fixtures/cobertura.xml')
1111
FIXTURES_JSON_PATH = File.join(File.dirname(__FILE__), 'fixtures/gutter.json')
1212
FIXTURES_PROJECT_PATH = File.join(File.dirname(__FILE__), 'fixtures/fixtures.xcodeproj')
13+
FIXTURES_SWIFT_FILE_PATH = File.join(File.dirname(__FILE__), 'fixtures/fixtures/Fixtures.swift')
1314

1415
RSpec.configure do |config|
1516
config.before(:suite) do

0 commit comments

Comments
 (0)