|
29 | 29 | import java.util.Arrays;
|
30 | 30 | import java.util.List;
|
31 | 31 | import java.util.Map;
|
| 32 | +import java.util.Set; |
| 33 | +import java.util.TreeSet; |
32 | 34 | import java.util.stream.Stream;
|
33 | 35 |
|
34 | 36 | import javax.xml.parsers.ParserConfigurationException;
|
35 | 37 |
|
| 38 | +import org.apache.commons.lang3.StringUtils; |
36 | 39 | import org.sonar.api.CoreProperties;
|
37 | 40 | import org.sonar.api.batch.fs.FilePredicates;
|
38 | 41 | import org.sonar.api.batch.fs.FileSystem;
|
|
42 | 45 | import org.sonar.api.batch.sensor.SensorDescriptor;
|
43 | 46 | import org.sonar.api.batch.sensor.coverage.CoverageType;
|
44 | 47 | import org.sonar.api.batch.sensor.coverage.NewCoverage;
|
45 |
| -import org.sonar.api.internal.apachecommons.lang.StringUtils; |
46 | 48 | import org.sonar.api.utils.log.Logger;
|
47 | 49 | import org.sonar.api.utils.log.Loggers;
|
48 |
| -import org.xml.sax.SAXException; |
49 | 50 |
|
50 | 51 | import fr.univartois.sonargo.core.language.GoLanguage;
|
51 | 52 | import fr.univartois.sonargo.core.settings.GoProperties;
|
52 | 53 |
|
53 | 54 | public class CoverageSensor implements Sensor {
|
54 | 55 |
|
55 |
| - private static final Logger LOGGER = Loggers.get(CoverageSensor.class); |
| 56 | + private static final Logger LOGGER = Loggers.get(CoverageSensor.class); |
56 | 57 |
|
57 |
| - @Override |
58 |
| - public void describe(SensorDescriptor descriptor) { |
59 |
| - descriptor.name("Go Coverage").onlyOnFileType(InputFile.Type.MAIN).onlyOnLanguage(GoLanguage.KEY); |
60 |
| - } |
| 58 | + @Override |
| 59 | + public void describe(SensorDescriptor descriptor) { |
| 60 | + descriptor.name("Go Coverage").onlyOnFileType(InputFile.Type.MAIN).onlyOnLanguage(GoLanguage.KEY); |
| 61 | + } |
61 | 62 |
|
62 |
| - private List<String> getExcludedPath(SensorContext context) { |
| 63 | + private Set<String> getExcludedPath(SensorContext context) { |
63 | 64 |
|
64 |
| - String globalExcludedPath = context.settings().getString(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY); |
| 65 | + String globalExcludedPath = context.settings().getString(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY); |
65 | 66 |
|
66 |
| - List<String> listExcludedPath = Arrays.asList(StringUtils.split(globalExcludedPath, ",")); |
| 67 | + Set<String> listExcludedPath = new TreeSet<>(Arrays.asList(StringUtils.split(globalExcludedPath, ","))); |
67 | 68 |
|
68 |
| - return listExcludedPath; |
69 |
| - } |
| 69 | + return listExcludedPath; |
| 70 | + } |
70 | 71 |
|
71 |
| - private boolean isNotAnExcludedPath(Path p, SensorContext context) { |
72 |
| - List<String> listExcludedPath = getExcludedPath(context); |
| 72 | + private boolean isNotAnExcludedPath(Path p, SensorContext context) { |
| 73 | + return !getExcludedPath(context).contains(p.toFile().getPath()); |
| 74 | + } |
73 | 75 |
|
74 |
| - for (String s : listExcludedPath) { |
75 |
| - if (s.equals(p.toFile().getPath())) { |
76 |
| - return false; |
77 |
| - } |
78 |
| - } |
79 |
| - return true; |
| 76 | + public Stream<Path> createStream(SensorContext context) throws IOException { |
| 77 | + final String fullPath = context.fileSystem().baseDir().getPath(); |
80 | 78 |
|
81 |
| - } |
| 79 | + return Files.walk(Paths.get(fullPath)) |
| 80 | + .filter(p -> !p.getParent().toString().contains(".git") && !p.getParent().toString().contains(".sonar") |
| 81 | + && !p.getParent().toString().contains(".scannerwork") |
| 82 | + && !p.getFileName().toString().startsWith(".") && isNotAnExcludedPath(p, context)); |
82 | 83 |
|
83 |
| - public Stream<Path> createStream(SensorContext context) throws IOException { |
84 |
| - final String fullPath = context.fileSystem().baseDir().getPath(); |
| 84 | + } |
85 | 85 |
|
86 |
| - return Files.walk(Paths.get(fullPath)) |
87 |
| - .filter(p -> !p.getParent().toString().contains(".git") && !p.getParent().toString().contains(".sonar") |
88 |
| - && !p.getParent().toString().contains(".scannerwork") |
89 |
| - && !p.getFileName().toString().startsWith(".") && isNotAnExcludedPath(p, context)); |
| 86 | + @Override |
| 87 | + public void execute(SensorContext context) { |
90 | 88 |
|
91 |
| - } |
| 89 | + try (Stream<Path> paths = createStream(context)) { |
| 90 | + paths.forEach(filePath -> { |
| 91 | + if (Files.isDirectory(filePath)) { |
| 92 | + final String reportPath = context.settings().getString(GoProperties.COVERAGE_REPORT_PATH_KEY); |
| 93 | + final File f = new File(filePath + File.separator + reportPath); |
| 94 | + if (f.exists()) { |
| 95 | + LOGGER.info("Analyse for " + f.getPath()); |
92 | 96 |
|
93 |
| - @Override |
94 |
| - public void execute(SensorContext context) { |
95 |
| - |
96 |
| - try (Stream<Path> paths = createStream(context)) { |
97 |
| - paths.forEach(filePath -> { |
98 |
| - if (Files.isDirectory(filePath)) { |
99 |
| - final String reportPath = context.settings().getString(GoProperties.COVERAGE_REPORT_PATH_KEY); |
100 |
| - final File f = new File(filePath + File.separator + reportPath); |
101 |
| - if (f.exists()) { |
102 |
| - LOGGER.info("Analyse for " + f.getPath()); |
103 |
| - |
104 |
| - final CoverageParser coverParser = new CoverageParser(context); |
105 |
| - |
106 |
| - try { |
107 |
| - coverParser.parse(f.getPath()); |
108 |
| - save(context, coverParser.getCoveragePerFile()); |
109 |
| - } catch (ParserConfigurationException | SAXException | IOException e) { |
110 |
| - LOGGER.error("Exception: ", e); |
111 |
| - } |
112 |
| - |
113 |
| - } else { |
114 |
| - LOGGER.info("no coverage file in package " + f.getPath()); |
115 |
| - } |
116 |
| - } |
117 |
| - }); |
118 |
| - } catch (final IOException e) { |
119 |
| - LOGGER.error("IO Exception " + context.fileSystem().baseDir().getPath()); |
120 |
| - } |
121 |
| - } |
| 97 | + final CoverageParser coverParser = new CoverageParser(context); |
122 | 98 |
|
123 |
| - public static void save(SensorContext context, Map<String, List<LineCoverage>> coveragePerFile) { |
124 |
| - for (Map.Entry<String, List<LineCoverage>> entry : coveragePerFile.entrySet()) { |
125 |
| - final String filePath = entry.getKey(); |
126 |
| - final List<LineCoverage> lines = entry.getValue(); |
127 |
| - final FileSystem fileSystem = context.fileSystem(); |
128 |
| - final FilePredicates predicates = fileSystem.predicates(); |
129 |
| - final InputFile inputFile = fileSystem.inputFile( |
130 |
| - predicates.or(predicates.hasRelativePath(filePath), predicates.hasAbsolutePath(filePath))); |
131 |
| - |
132 |
| - if (inputFile == null) { |
133 |
| - LOGGER.warn("unable to create InputFile object: " + filePath); |
134 |
| - return; |
| 99 | + try { |
| 100 | + coverParser.parse(f.getPath()); |
| 101 | + save(context, coverParser.getCoveragePerFile()); |
| 102 | + } catch (ParserConfigurationException | IOException e) { |
| 103 | + LOGGER.error("Exception: ", e); |
135 | 104 | }
|
136 | 105 |
|
137 |
| - final NewCoverage coverage = context.newCoverage().onFile(inputFile); |
138 |
| - |
139 |
| - for (final LineCoverage line : lines) { |
140 |
| - try { |
141 |
| - coverage.lineHits(line.getLineNumber(), line.getHits()); |
142 |
| - } catch (final Exception ex) { |
143 |
| - LOGGER.error(ex.getMessage() + line); |
144 |
| - } |
145 |
| - } |
146 |
| - coverage.ofType(CoverageType.UNIT); |
147 |
| - coverage.save(); |
| 106 | + } else { |
| 107 | + LOGGER.info("no coverage file in package " + f.getPath()); |
| 108 | + } |
| 109 | + } |
| 110 | + }); |
| 111 | + } catch (final IOException e) { |
| 112 | + LOGGER.error("IO Exception " + context.fileSystem().baseDir().getPath()); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public static void save(SensorContext context, Map<String, List<LineCoverage>> coveragePerFile) { |
| 117 | + for (Map.Entry<String, List<LineCoverage>> entry : coveragePerFile.entrySet()) { |
| 118 | + final String filePath = entry.getKey(); |
| 119 | + final List<LineCoverage> lines = entry.getValue(); |
| 120 | + final FileSystem fileSystem = context.fileSystem(); |
| 121 | + final FilePredicates predicates = fileSystem.predicates(); |
| 122 | + final InputFile inputFile = fileSystem.inputFile( |
| 123 | + predicates.or(predicates.hasRelativePath(filePath), predicates.hasAbsolutePath(filePath))); |
| 124 | + |
| 125 | + if (inputFile == null) { |
| 126 | + LOGGER.warn("unable to create InputFile object: " + filePath); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + final NewCoverage coverage = context.newCoverage().onFile(inputFile); |
| 131 | + |
| 132 | + for (final LineCoverage line : lines) { |
| 133 | + try { |
| 134 | + coverage.lineHits(line.getLineNumber(), line.getHits()); |
| 135 | + } catch (final Exception ex) { |
| 136 | + LOGGER.error(ex.getMessage() + line); |
148 | 137 | }
|
| 138 | + } |
| 139 | + coverage.ofType(CoverageType.UNIT); |
| 140 | + coverage.save(); |
149 | 141 | }
|
| 142 | + } |
150 | 143 |
|
151 | 144 | }
|
0 commit comments