Skip to content

Commit

Permalink
Relax source file attribute check
Browse files Browse the repository at this point in the history
The purpose of the check is to alert users that they must configure the
compiler to include source file debug info.

Previously all classes were checked to ensure the source file was
present, however it appears there may be some scenarios in which the
source file is omitted when compiling kotlin.

To allow analysis of the remaining classes, the check has been relaxed
so that the info must be present in at least one file, rather than all
files.
  • Loading branch information
Henry Coles committed Nov 11, 2022
1 parent d151089 commit 347dde1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import java.util.Collection;
import java.util.function.Consumer;
import java.util.List;
import java.util.function.Predicate;

import org.pitest.classinfo.ClassInfo;
Expand All @@ -29,11 +29,11 @@ public class DefaultBuildVerifier implements BuildVerifier {

@Override
public void verify(final CodeSource code) {
final Collection<ClassInfo> codeClasses = FCollection.filter(code.getCode(), isNotSynthetic());
final List<ClassInfo> codeClasses = FCollection.filter(code.getCode(), isNotSynthetic());

if (hasMutableCode(codeClasses)) {
checkAtLeastOneClassHasLineNumbers(codeClasses);
codeClasses.forEach(throwErrorIfHasNoSourceFile());
checkAtLeastOneClassHasSourceFile(codeClasses);
}
}

Expand All @@ -54,6 +54,14 @@ private void checkAtLeastOneClassHasLineNumbers(
}
}

private void checkAtLeastOneClassHasSourceFile(List<ClassInfo> codeClasses) {
// perform only a weak check for line numbers as
// some jvm languages are not guaranteed to include a source file for all classes
if (!FCollection.contains(codeClasses, a -> a.getSourceFileName() != null)) {
throw new PitHelpError(Help.NO_SOURCE_FILE, codeClasses.get(0).getName().asJavaName());
}
}

private static Predicate<ClassInfo> aConcreteClass() {
return a -> !a.isInterface();
}
Expand All @@ -62,14 +70,6 @@ private static Predicate<ClassInfo> aClassWithLineNumbers() {
return a -> a.getNumberOfCodeLines() != 0;
}

private Consumer<ClassInfo> throwErrorIfHasNoSourceFile() {
return a -> {
if (a.getSourceFileName() == null) {
throw new PitHelpError(Help.NO_SOURCE_FILE, a.getName().asJavaName());
}
};
}

private static Predicate<ClassInfo> isNotSynthetic() {
return a -> !a.isSynthetic();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package org.pitest.mutationtest.verify;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -105,6 +106,12 @@ public void shouldNotThrowAnErrorWhenOnlyInterfacesPresent() {
}
}

@Test
public void doesNotErrorWhenNoClassesProvided() {
when(this.code.getCode()).thenReturn(Collections.emptyList());
assertThatCode(() -> this.testee.verify(this.code)).doesNotThrowAnyException();
}

private void setupClassPath(final Class<?> clazz) {
this.setupClassPath(
new ClassloaderByteArraySource(IsolationUtils.getContextClassLoader()),
Expand Down

0 comments on commit 347dde1

Please sign in to comment.