|
11 | 11 | import datadog.trace.api.civisibility.telemetry.tag.CoverageErrorType; |
12 | 12 | import datadog.trace.civisibility.coverage.ConcurrentCoverageStore; |
13 | 13 | import datadog.trace.civisibility.source.SourcePathResolver; |
14 | | -import datadog.trace.civisibility.source.SourceResolutionException; |
15 | 14 | import datadog.trace.civisibility.source.Utils; |
16 | 15 | import java.io.InputStream; |
17 | 16 | import java.util.ArrayList; |
@@ -53,88 +52,84 @@ private LineCoverageStore( |
53 | 52 | @Nullable |
54 | 53 | @Override |
55 | 54 | protected TestReport report( |
56 | | - DDTraceId testSessionId, Long testSuiteId, long testSpanId, Collection<LineProbes> probes) |
57 | | - throws SourceResolutionException { |
58 | | - try { |
59 | | - Map<Class<?>, ExecutionDataAdapter> combinedExecutionData = new IdentityHashMap<>(); |
60 | | - Collection<String> combinedNonCodeResources = new HashSet<>(); |
61 | | - |
62 | | - for (LineProbes probe : probes) { |
63 | | - for (Map.Entry<Class<?>, ExecutionDataAdapter> e : probe.getExecutionData().entrySet()) { |
64 | | - combinedExecutionData.merge(e.getKey(), e.getValue(), ExecutionDataAdapter::merge); |
65 | | - } |
66 | | - combinedNonCodeResources.addAll(probe.getNonCodeResources()); |
67 | | - } |
| 55 | + DDTraceId testSessionId, Long testSuiteId, long testSpanId, Collection<LineProbes> probes) { |
| 56 | + Map<Class<?>, ExecutionDataAdapter> combinedExecutionData = new IdentityHashMap<>(); |
| 57 | + Collection<String> combinedNonCodeResources = new HashSet<>(); |
68 | 58 |
|
69 | | - if (combinedExecutionData.isEmpty() && combinedNonCodeResources.isEmpty()) { |
70 | | - return null; |
| 59 | + for (LineProbes probe : probes) { |
| 60 | + for (Map.Entry<Class<?>, ExecutionDataAdapter> e : probe.getExecutionData().entrySet()) { |
| 61 | + combinedExecutionData.merge(e.getKey(), e.getValue(), ExecutionDataAdapter::merge); |
71 | 62 | } |
| 63 | + combinedNonCodeResources.addAll(probe.getNonCodeResources()); |
| 64 | + } |
72 | 65 |
|
73 | | - Map<String, BitSet> coveredLinesBySourcePath = new HashMap<>(); |
74 | | - for (Map.Entry<Class<?>, ExecutionDataAdapter> e : combinedExecutionData.entrySet()) { |
75 | | - ExecutionDataAdapter executionDataAdapter = e.getValue(); |
76 | | - String className = executionDataAdapter.getClassName(); |
77 | | - |
78 | | - Class<?> clazz = e.getKey(); |
79 | | - String sourcePath = sourcePathResolver.getSourcePath(clazz); |
80 | | - if (sourcePath == null) { |
81 | | - log.debug( |
82 | | - "Skipping coverage reporting for {} because source path could not be determined", |
83 | | - className); |
84 | | - metrics.add(CiVisibilityCountMetric.CODE_COVERAGE_ERRORS, 1, CoverageErrorType.PATH); |
85 | | - continue; |
86 | | - } |
87 | | - |
88 | | - try (InputStream is = Utils.getClassStream(clazz)) { |
89 | | - BitSet coveredLines = |
90 | | - coveredLinesBySourcePath.computeIfAbsent(sourcePath, key -> new BitSet()); |
91 | | - ExecutionDataStore store = new ExecutionDataStore(); |
92 | | - store.put(executionDataAdapter.toExecutionData()); |
93 | | - |
94 | | - // TODO optimize this part to avoid parsing |
95 | | - // the same class multiple times for different test cases |
96 | | - Analyzer analyzer = new Analyzer(store, new SourceAnalyzer(coveredLines)); |
97 | | - analyzer.analyzeClass(is, null); |
98 | | - |
99 | | - } catch (Exception exception) { |
100 | | - log.debug( |
101 | | - "Skipping coverage reporting for {} ({}) because of error", |
102 | | - className, |
103 | | - sourcePath, |
104 | | - exception); |
105 | | - metrics.add(CiVisibilityCountMetric.CODE_COVERAGE_ERRORS, 1); |
106 | | - } |
107 | | - } |
| 66 | + if (combinedExecutionData.isEmpty() && combinedNonCodeResources.isEmpty()) { |
| 67 | + return null; |
| 68 | + } |
108 | 69 |
|
109 | | - List<TestReportFileEntry> fileEntries = new ArrayList<>(coveredLinesBySourcePath.size()); |
110 | | - for (Map.Entry<String, BitSet> e : coveredLinesBySourcePath.entrySet()) { |
111 | | - String sourcePath = e.getKey(); |
112 | | - BitSet coveredLines = e.getValue(); |
113 | | - fileEntries.add(new TestReportFileEntry(sourcePath, coveredLines)); |
| 70 | + Map<String, BitSet> coveredLinesBySourcePath = new HashMap<>(); |
| 71 | + for (Map.Entry<Class<?>, ExecutionDataAdapter> e : combinedExecutionData.entrySet()) { |
| 72 | + ExecutionDataAdapter executionDataAdapter = e.getValue(); |
| 73 | + String className = executionDataAdapter.getClassName(); |
| 74 | + |
| 75 | + Class<?> clazz = e.getKey(); |
| 76 | + Collection<String> sourcePaths = sourcePathResolver.getSourcePaths(clazz); |
| 77 | + if (sourcePaths.size() != 1) { |
| 78 | + log.debug( |
| 79 | + "Skipping coverage reporting for {} because source path could not be determined", |
| 80 | + className); |
| 81 | + metrics.add(CiVisibilityCountMetric.CODE_COVERAGE_ERRORS, 1, CoverageErrorType.PATH); |
| 82 | + continue; |
114 | 83 | } |
115 | | - |
116 | | - for (String nonCodeResource : combinedNonCodeResources) { |
117 | | - String resourcePath = sourcePathResolver.getResourcePath(nonCodeResource); |
118 | | - if (resourcePath == null) { |
119 | | - log.debug( |
120 | | - "Skipping coverage reporting for {} because resource path could not be determined", |
121 | | - nonCodeResource); |
122 | | - metrics.add(CiVisibilityCountMetric.CODE_COVERAGE_ERRORS, 1, CoverageErrorType.PATH); |
123 | | - continue; |
124 | | - } |
125 | | - fileEntries.add(new TestReportFileEntry(resourcePath, null)); |
| 84 | + String sourcePath = sourcePaths.iterator().next(); |
| 85 | + |
| 86 | + try (InputStream is = Utils.getClassStream(clazz)) { |
| 87 | + BitSet coveredLines = |
| 88 | + coveredLinesBySourcePath.computeIfAbsent(sourcePath, key -> new BitSet()); |
| 89 | + ExecutionDataStore store = new ExecutionDataStore(); |
| 90 | + store.put(executionDataAdapter.toExecutionData()); |
| 91 | + |
| 92 | + // TODO optimize this part to avoid parsing |
| 93 | + // the same class multiple times for different test cases |
| 94 | + Analyzer analyzer = new Analyzer(store, new SourceAnalyzer(coveredLines)); |
| 95 | + analyzer.analyzeClass(is, null); |
| 96 | + |
| 97 | + } catch (Exception exception) { |
| 98 | + log.debug( |
| 99 | + "Skipping coverage reporting for {} ({}) because of error", |
| 100 | + className, |
| 101 | + sourcePath, |
| 102 | + exception); |
| 103 | + metrics.add(CiVisibilityCountMetric.CODE_COVERAGE_ERRORS, 1); |
126 | 104 | } |
| 105 | + } |
127 | 106 |
|
128 | | - TestReport report = new TestReport(testSessionId, testSuiteId, testSpanId, fileEntries); |
129 | | - metrics.add( |
130 | | - CiVisibilityDistributionMetric.CODE_COVERAGE_FILES, |
131 | | - report.getTestReportFileEntries().size()); |
132 | | - return report; |
| 107 | + List<TestReportFileEntry> fileEntries = new ArrayList<>(coveredLinesBySourcePath.size()); |
| 108 | + for (Map.Entry<String, BitSet> e : coveredLinesBySourcePath.entrySet()) { |
| 109 | + String sourcePath = e.getKey(); |
| 110 | + BitSet coveredLines = e.getValue(); |
| 111 | + fileEntries.add(new TestReportFileEntry(sourcePath, coveredLines)); |
| 112 | + } |
133 | 113 |
|
134 | | - } catch (Exception e) { |
135 | | - metrics.add(CiVisibilityCountMetric.CODE_COVERAGE_ERRORS, 1); |
136 | | - throw e; |
| 114 | + for (String nonCodeResource : combinedNonCodeResources) { |
| 115 | + Collection<String> resourcePaths = sourcePathResolver.getResourcePaths(nonCodeResource); |
| 116 | + if (resourcePaths.isEmpty()) { |
| 117 | + log.debug( |
| 118 | + "Skipping coverage reporting for {} because resource path could not be determined", |
| 119 | + nonCodeResource); |
| 120 | + metrics.add(CiVisibilityCountMetric.CODE_COVERAGE_ERRORS, 1, CoverageErrorType.PATH); |
| 121 | + continue; |
| 122 | + } |
| 123 | + for (String resourcePath : resourcePaths) { |
| 124 | + fileEntries.add(new TestReportFileEntry(resourcePath, null)); |
| 125 | + } |
137 | 126 | } |
| 127 | + |
| 128 | + TestReport report = new TestReport(testSessionId, testSuiteId, testSpanId, fileEntries); |
| 129 | + metrics.add( |
| 130 | + CiVisibilityDistributionMetric.CODE_COVERAGE_FILES, |
| 131 | + report.getTestReportFileEntries().size()); |
| 132 | + return report; |
138 | 133 | } |
139 | 134 |
|
140 | 135 | public static final class Factory implements CoverageStore.Factory { |
|
0 commit comments