for (Map.Entry<Path, DefaultInputFile> e : changedFiles.entrySet()) {
DefaultInputFile inputFile = e.getValue();
- Set<Integer> changedLines = pathSetMap.getOrDefault(e.getKey(), Collections.emptySet());
+ Set<Integer> changedLines = pathSetMap.get(e.getKey());
- // detect unchanged last empty line
- if (changedLines.size() + 1 == inputFile.lines() && inputFile.lineLength(inputFile.lines()) == 0) {
- changedLines.add(inputFile.lines());
- }
-
- if (changedLines.isEmpty()) {
+ if (changedLines == null) {
LOG.warn("File '{}' was detected as changed but without having changed lines", e.getKey().toAbsolutePath());
+ // assume that no line was changed
+ writeChangedLines(writer, e.getValue().scannerId(), Collections.emptySet());
+ } else {
+ // detect unchanged last empty line
+ if (changedLines.size() + 1 == inputFile.lines() && inputFile.lineLength(inputFile.lines()) == 0) {
+ changedLines.add(inputFile.lines());
+ }
+ count++;
+ writeChangedLines(writer, e.getValue().scannerId(), changedLines);
}
- count++;
- writeChangedLines(writer, e.getValue().scannerId(), changedLines);
}
return count;
}
*/
package org.sonar.scanner.report;
+import com.google.common.collect.ImmutableMap;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.scm.ScmProvider;
+import org.sonar.api.utils.log.LogTester;
import org.sonar.scanner.fs.InputModuleHierarchy;
import org.sonar.scanner.protocol.output.ScannerReportReader;
import org.sonar.scanner.protocol.output.ScannerReportWriter;
import org.sonar.scanner.scm.ScmConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assumptions.assumeThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
@Rule
public TemporaryFolder temp = new TemporaryFolder();
+ @Rule
+ public LogTester logTester = new LogTester();
+
private ChangedLinesPublisher publisher = new ChangedLinesPublisher(scmConfiguration, project, inputComponentStore, branchConfiguration);
@Before
@Test
public void write_changed_files() {
DefaultInputFile fileWithChangedLines = createInputFile("path1", "l1\nl2\nl3\n");
- DefaultInputFile fileWithoutChangedLines = createInputFile("path2", "l1\nl2\nl3\n");
- Set<Path> paths = new HashSet<>(Arrays.asList(BASE_DIR.resolve("path1"), BASE_DIR.resolve("path2")));
+ DefaultInputFile fileNotReturned = createInputFile("path2", "l1\nl2\nl3\n");
+ DefaultInputFile fileWithoutChangedLines = createInputFile("path3", "l1\nl2\nl3\n");
+
+ Set<Path> paths = new HashSet<>(Arrays.asList(BASE_DIR.resolve("path1"), BASE_DIR.resolve("path2"), BASE_DIR.resolve("path3")));
Set<Integer> lines = new HashSet<>(Arrays.asList(1, 10));
- when(provider.branchChangedLines(TARGET_BRANCH, BASE_DIR, paths)).thenReturn(Collections.singletonMap(BASE_DIR.resolve("path1"), lines));
- when(inputComponentStore.allChangedFilesToPublish()).thenReturn(Arrays.asList(fileWithChangedLines, fileWithoutChangedLines));
+ when(provider.branchChangedLines(TARGET_BRANCH, BASE_DIR, paths))
+ .thenReturn(ImmutableMap.of(BASE_DIR.resolve("path1"), lines, BASE_DIR.resolve("path3"), Collections.emptySet()));
+ when(inputComponentStore.allChangedFilesToPublish()).thenReturn(Arrays.asList(fileWithChangedLines, fileNotReturned, fileWithoutChangedLines));
publisher.publish(writer);
assertPublished(fileWithChangedLines, new HashSet<>(Arrays.asList(1, 10)));
assertPublished(fileWithoutChangedLines, Collections.emptySet());
+ assertPublished(fileNotReturned, Collections.emptySet());
+
+ assumeThat(logTester.logs()).contains("File '/root/path2' was detected as changed but without having changed lines");
}
@Test
assertThat(reader.readComponentChangedLines(file.scannerId()).getLineList()).containsExactlyElementsOf(lines);
}
- private void assertNotPublished(DefaultInputFile file) {
- assertThat(new File(temp.getRoot(), "changed-lines-" + file.scannerId() + ".pb")).doesNotExist();
- }
-
private void assertNotPublished() {
assertThat(temp.getRoot().list()).isEmpty();
}