Skip to content

Commit

Permalink
Merge pull request #239 from davidy4ng/fix/unable-to-locate-test-files
Browse files Browse the repository at this point in the history
fix: location of the test source file
  • Loading branch information
gaelfoppolo authored Aug 24, 2019
2 parents e280c8b + 906037f commit a214e3b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.backelite.sonarqube.objectivec.surefire;

import org.apache.commons.lang.StringUtils;
import com.backelite.sonarqube.commons.TestFileFinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -43,18 +44,22 @@ public InputFile getUnitTestResource(FileSystem fileSystem, String classname) {
* Most xcodebuild JUnit parsers don't include the path to the class in the class field, so search for it if it
* wasn't found in the root.
*/
fp = fileSystem.predicates().and(
fileSystem.predicates().hasType(InputFile.Type.TEST),
fileSystem.predicates().matchesPathPattern("**/" + fileName.replace("_", "+")));
String lastFileNameComponents = StringUtils.substringAfterLast(fileName, "/");
if(!StringUtils.isEmpty(lastFileNameComponents)) {
fp = fileSystem.predicates().and(
fileSystem.predicates().hasType(InputFile.Type.TEST),
fileSystem.predicates().matchesPathPattern("**/" + fileName.replace("_", "+"))
);

if(fileSystem.hasFiles(fp)){
/*
* Lazily get the first file, since we wouldn't be able to determine the correct one from just the
* test class name in the event that there are multiple matches.
*/
return fileSystem.inputFiles(fp).iterator().next();
if(fileSystem.hasFiles(fp)){
/*
* Lazily get the first file, since we wouldn't be able to determine the correct one from just the
* test class name in the event that there are multiple matches.
*/
return fileSystem.inputFiles(fp).iterator().next();
}
}
LOGGER.info("Unable to locate test source file {}", fileName);
LOGGER.info("Unable to locate Objective-C test source file for classname {}. Make sure your test class name matches its filename.", classname);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.backelite.sonarqube.swift.surefire;

import org.apache.commons.lang.StringUtils;
import com.backelite.sonarqube.commons.TestFileFinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -35,29 +36,33 @@ public class SwiftTestFileFinder implements TestFileFinder {
@Override
public InputFile getUnitTestResource(FileSystem fileSystem, String classname) {
String fileName = classname.replace('.', '/') + ".swift";
String wildcardFileName = classname.replace(".", "/**/") + ".swift";
FilePredicate fp = fileSystem.predicates().hasPath(fileName);

if(fileSystem.hasFiles(fp)){
if(fileSystem.hasFiles(fp)) {
return fileSystem.inputFile(fp);
}

/*
* Most xcodebuild JUnit parsers don't include the path to the class in the class field, so search for it if it
* wasn't found in the root.
*/
fp = fileSystem.predicates().and(
fileSystem.predicates().hasType(InputFile.Type.TEST),
fileSystem.predicates().matchesPathPattern("**/" + wildcardFileName));

if(fileSystem.hasFiles(fp)){
/*
* Lazily get the first file, since we wouldn't be able to determine the correct one from just the
* test class name in the event that there are multiple matches.
*/
return fileSystem.inputFiles(fp).iterator().next();
String lastFileNameComponents = StringUtils.substringAfterLast(fileName, "/");
if(!StringUtils.isEmpty(lastFileNameComponents)) {
fp = fileSystem.predicates().and(
fileSystem.predicates().hasType(InputFile.Type.TEST),
fileSystem.predicates().matchesPathPattern("**/" + lastFileNameComponents)
);

if(fileSystem.hasFiles(fp)) {
/*
* Lazily get the first file, since we wouldn't be able to determine the correct one from just the
* test class name in the event that there are multiple matches.
*/
return fileSystem.inputFiles(fp).iterator().next();
}
}
LOGGER.info("Unable to locate test source file {}", wildcardFileName);

LOGGER.info("Unable to locate Swift test source file for classname {}. Make sure your test class name matches its filename.", classname);
return null;
}
}

0 comments on commit a214e3b

Please sign in to comment.