Skip to content

Commit 5c57cc9

Browse files
author
demerson
committed
update to no longer strip out the hotspots section from metricfu
1 parent 02bfebf commit 5c57cc9

6 files changed

Lines changed: 12935 additions & 38 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ Our complexity sensor and code coverage sensor borrow heavily from the javascrip
5050
##Updates from Constant Contact
5151
* Updates were made for compatibility with Sonar 4.5.2 and it's api
5252
* Added support for configuring report paths for SimpleCovRcov and MetricFu via Sonar properties. They are available in the Code Coverage category and are configurable per project.
53+
* Added support to configure whether to use Saikuro or Cane for complexity metrics. May add support for obtaining complexity from flog as well
5354
* Several dependencies were also updated to more recent versions.

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Required metadata
22
sonar.projectKey=ruby-sonar-plugin-gd
33
sonar.projectName=Ruby Sonar Plugin
4-
sonar.projectVersion=1.0
4+
sonar.projectVersion=1.0.1
55

66
# Comma-separated paths to directories with sources (required)
77
sonar.sources=src/main/java

src/main/java/com/godaddy/sonar/ruby/metricfu/MetricfuComplexityYamlParserImpl.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.godaddy.sonar.ruby.metricfu;
22

33
import java.io.File;
4+
import java.io.FileInputStream;
45
import java.io.IOException;
6+
import java.io.InputStream;
57
import java.util.ArrayList;
68
import java.util.HashMap;
79
import java.util.List;
@@ -13,6 +15,8 @@
1315
import org.slf4j.LoggerFactory;
1416
import org.yaml.snakeyaml.Yaml;
1517

18+
import com.google.common.base.Throwables;
19+
1620
public class MetricfuComplexityYamlParserImpl implements
1721
MetricfuComplexityYamlParser {
1822
private static final Logger LOG = LoggerFactory
@@ -21,13 +25,17 @@ public class MetricfuComplexityYamlParserImpl implements
2125
@SuppressWarnings("unchecked")
2226
public List<RubyFunction> parseFunctions(String fileNameFromModule, File resultsFile, String complexityType) throws IOException
2327
{
24-
String fileString = FileUtils.readFileToString(resultsFile, "UTF-8");
28+
// String fileString = FileUtils.readFileToString(resultsFile, "UTF-8");
29+
InputStream resultsStream = new FileInputStream(resultsFile);
30+
2531
LOG.debug("MetricfuComplexityYamlParserImpl: Start start parse of metrics_fu YAML");
2632

2733
// remove ":hotspots:" section of the yaml so snakeyaml can parse it
2834
// correctly, snakeyaml throws an error with that section intact
2935
// Will remove if metric_fu metric filtering works for hotspots in the
3036
// future
37+
38+
// Removing this as it seems to be working correctly with current versions of dependencies
3139
// int hotSpotIndex = fileString.indexOf(":hotspots:");
3240
// if (hotSpotIndex >= 0)
3341
// {
@@ -36,19 +44,27 @@ public List<RubyFunction> parseFunctions(String fileNameFromModule, File results
3644
// }
3745

3846
Yaml yaml = new Yaml();
39-
40-
Map<String, Object> metricfuResult = (Map<String, Object>) yaml.loadAs(fileString, Map.class);
41-
Map<String, Object> saikuroResult = (Map<String, Object>) metricfuResult.get(":saikuro");
42-
Map<String, Object> caneResult = (Map<String, Object>) metricfuResult.get(":cane");
43-
44-
if ("Saikuro".equals(complexityType) && null != saikuroResult) {
45-
return analyzeSaikuro(fileNameFromModule, metricfuResult, saikuroResult);
46-
} else if ("Cane".equals(complexityType) && null != caneResult) {
47-
return analyzeCane(fileNameFromModule, metricfuResult, caneResult);
48-
} else {
49-
LOG.warn("No data found for complexity type " + complexityType);
50-
return new ArrayList<RubyFunction>();
47+
Map<String, Object> metricfuResult = new HashMap();
48+
try {
49+
// metricfuResult = (Map<String, Object>) yaml.loadAs(fileString, Map.class);
50+
metricfuResult = (Map<String, Object>) yaml.load(resultsStream);
51+
52+
Map<String, Object> saikuroResult = (Map<String, Object>) metricfuResult.get(":saikuro");
53+
Map<String, Object> caneResult = (Map<String, Object>) metricfuResult.get(":cane");
54+
55+
if ("Saikuro".equals(complexityType) && null != saikuroResult) {
56+
return analyzeSaikuro(fileNameFromModule, metricfuResult, saikuroResult);
57+
} else if ("Cane".equals(complexityType) && null != caneResult) {
58+
return analyzeCane(fileNameFromModule, metricfuResult, caneResult);
59+
} else {
60+
LOG.warn("No data found for complexity type " + complexityType);
61+
return new ArrayList<RubyFunction>();
62+
}
63+
} catch (Exception e) {
64+
LOG.error(Throwables.getStackTraceAsString(e));
65+
throw new IOException("Failure parsing YAML results", e);
5166
}
67+
5268
}
5369

5470
private List<RubyFunction> analyzeSaikuro(String fileNameFromModule, Map<String, Object> metricfuResult,

src/test/java/com/godaddy/sonar/ruby/metricfu/MetricfuComplexitySensorTest.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ public void setUp() throws Exception
6363
expect(config.getString("sonar.language", "java")).andStubReturn("ruby");
6464

6565
project = new Project("test project");
66-
Settings settings = new Settings();
67-
// settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, LanguageRuby.KEY);
68-
// project.setSettings(settings);
69-
project.setLanguage(LanguageRuby.INSTANCE);
70-
66+
project.setLanguage(LanguageRuby.INSTANCE);
7167
project.setConfiguration(config);
7268

7369
}
@@ -78,22 +74,6 @@ public void testConstructor()
7874
assertNotNull(metricfuComplexitySensor);
7975
}
8076

81-
// @Test
82-
// public void testShouldExecuteOnRubyProject()
83-
// {
84-
// Configuration config = mocksControl.createMock(Configuration.class);
85-
// expect(config.getString("sonar.language", "java")).andReturn("ruby");
86-
// mocksControl.replay();
87-
//
88-
// Project project = new Project("test project");
89-
// project.setConfiguration(config);
90-
//
91-
// RubySensor sensor = new RubySensor(moduleFileSystem);
92-
// sensor.shouldExecuteOnProject(project);
93-
//
94-
// mocksControl.verify();
95-
// }
96-
9777
@Test
9878
public void testAnalyse() throws IOException
9979
{

src/test/java/com/godaddy/sonar/ruby/metricfu/MetricfuComplexityYamlParserTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
public class MetricfuComplexityYamlParserTest extends TestCase
1111
{
12-
private final static String YML_FILE_NAME = "src/test/resources/test-data/results.yml";
12+
private final static String YML_SYNTAX_FILE_NAME = "src/test/resources/test-data/metricfu_report.yml";
13+
private final static String YML_PARSE_FILE_NAME = "src/test/resources/test-data/results.yml";
1314

1415
private MetricfuComplexityYamlParserImpl parser = null;
1516

@@ -18,11 +19,23 @@ public void setUp() throws Exception
1819
{
1920
parser = new MetricfuComplexityYamlParserImpl();
2021
}
21-
22+
23+
24+
@Test
25+
public void testParseFunctionSyntaxHandling() throws IOException
26+
{
27+
File reportFile = new File(YML_SYNTAX_FILE_NAME);
28+
List<RubyFunction> rubyFunctions = parser.parseFunctions("lib/some_path/foo_bar.rb", reportFile, "Saikuro");
29+
30+
RubyFunction rubyFunction0 = new RubyFunction("FooBar#validate_user_name", 4, 5);
31+
// assertTrue(rubyFunctions.size()==2);
32+
// assertTrue(rubyFunctions.get(0).toString().equals(rubyFunction0.toString()));
33+
}
34+
2235
@Test
2336
public void testParseFunction() throws IOException
2437
{
25-
File reportFile = new File(YML_FILE_NAME);
38+
File reportFile = new File(YML_PARSE_FILE_NAME);
2639
List<RubyFunction> rubyFunctions = parser.parseFunctions("lib/some_path/foo_bar.rb", reportFile, "Saikuro");
2740

2841
RubyFunction rubyFunction0 = new RubyFunction("FooBar#validate_user_name", 4, 5);

0 commit comments

Comments
 (0)