Skip to content

Created SwiftSensor class and reworked reportDirectory into reportsInRoot #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,27 @@
description = "Relative to projects' root. Ant patterns are accepted",
global = false,
project = true),
@Property(
key = SwiftCoberturaSensor.REPORTS_IN_ROOT_KEY,
defaultValue = "false",
name = "Whether reports are located in the root directory",
description = "Determines where to find reports in modular analysis",
global = false,
project = true),
@Property(
key = SwiftLintSensor.REPORT_PATH_KEY,
defaultValue = SwiftLintSensor.DEFAULT_REPORT_PATH,
name = "Path to SwiftLint report",
description = "Relative to projects' root.",
global = false,
project = true),
@Property(
key = SwiftLintSensor.REPORTS_IN_ROOT_KEY,
defaultValue = "false",
name = "Whether the report is located in the root directory",
description = "Determines where to find the report in modular analysis",
global = false,
project = true),
@Property(
key = TailorSensor.REPORT_PATH_KEY,
defaultValue = TailorSensor.DEFAULT_REPORT_PATH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.measures.CoverageMeasuresBuilder;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.utils.ParsingUtils;
import org.sonar.api.utils.StaxParser;
Expand All @@ -46,20 +45,20 @@ final class CoberturaReportParser {
private static final Logger LOGGER = LoggerFactory.getLogger(CoberturaReportParser.class);

private final FileSystem fileSystem;
private final Project project;
private final SensorContext context;
private final String rootDirectory;

private CoberturaReportParser(FileSystem fileSystem, Project project, SensorContext context) {
private CoberturaReportParser(FileSystem fileSystem, SensorContext context, String rootDirectory) {
this.fileSystem = fileSystem;
this.project = project;
this.context = context;
this.rootDirectory = rootDirectory;
}

/**
* Parse a Cobertura xml report and create measures accordingly
*/
public static void parseReport(File xmlFile, FileSystem fileSystem, Project project, SensorContext context) {
new CoberturaReportParser(fileSystem, project, context).parse(xmlFile);
public static void parseReport(File xmlFile, FileSystem fileSystem, SensorContext sensorContext, String rootDirectory) {
new CoberturaReportParser(fileSystem, sensorContext, rootDirectory).parse(xmlFile);
}

private void parse(File xmlFile) {
Expand All @@ -84,11 +83,7 @@ private void collectPackageMeasures(SMInputCursor pack) throws XMLStreamExceptio
collectFileMeasures(pack.descendantElementCursor("class"), builderByFilename);
for (Map.Entry<String, CoverageMeasuresBuilder> entry : builderByFilename.entrySet()) {
String filePath = entry.getKey();
filePath = getAdjustedPathIfProjectIsModule(filePath);
if (filePath == null) {
continue;
}
File file = new File(fileSystem.baseDir(), filePath);
File file = new File(rootDirectory, filePath);
InputFile inputFile = fileSystem.inputFile(fileSystem.predicates().hasAbsolutePath(file.getAbsolutePath()));

if (inputFile == null) {
Expand All @@ -111,18 +106,6 @@ private boolean resourceExists(Resource file) {
return context.getResource(file) != null;
}

private String getAdjustedPathIfProjectIsModule(String filePath) {
if (project.isModule()) {
// the file doesn't belong to the module we're analyzing
if (!filePath.startsWith(project.path())) {
return null;
}
// fileSystem.baseDir() will include the module path, so we need to get rid of it here
return filePath.substring(project.path().length());
}
return filePath;
}

private static void collectFileMeasures(SMInputCursor clazz,
Map<String, CoverageMeasuresBuilder> builderByFilename) throws XMLStreamException {
while (clazz.getNext() != null) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,78 +19,28 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.plugins.swift.SwiftPlugin;
import org.sonar.plugins.swift.lang.core.Swift;
import org.sonar.plugins.swift.generic.SwiftSensor;

import java.io.File;
import java.util.List;


public final class SwiftCoberturaSensor implements Sensor {
public class SwiftCoberturaSensor extends SwiftSensor {

private static final Logger LOGGER = LoggerFactory.getLogger(SwiftCoberturaSensor.class);

public static final String REPORT_PATTERN_KEY = SwiftPlugin.PROPERTY_PREFIX + ".coverage.reportPattern";
public static final String DEFAULT_REPORT_PATTERN = "sonar-reports/coverage-swift*.xml";
public static final String REPORT_DIRECTORY_KEY = SwiftPlugin.PROPERTY_PREFIX + ".coverage.reportDirectory";

private final ReportFilesFinder reportFilesFinder;

private final Settings settings;
private final FileSystem fileSystem;
private final PathResolver pathResolver;
private Project project;

public SwiftCoberturaSensor(final FileSystem fileSystem, final PathResolver pathResolver, final Settings settings) {

this.settings = settings;
this.fileSystem = fileSystem;
this.pathResolver = pathResolver;

reportFilesFinder = new ReportFilesFinder(settings, REPORT_PATTERN_KEY, DEFAULT_REPORT_PATTERN, REPORT_DIRECTORY_KEY);
}

public boolean shouldExecuteOnProject(final Project project) {

this.project = project;

return fileSystem.languages().contains(Swift.KEY);
}

public void analyse(final Project project, final SensorContext context) {

final String projectBaseDir = fileSystem.baseDir().getPath();
LOGGER.info("Analyzing directory: {}", projectBaseDir);

List<File> reports;
if (project.isRoot()) {
reports = reportFilesFinder.reportsIn(projectBaseDir);
}
else {
final String module = project.getName();
final String rootDir = getRootDirectory(project);
reports = reportFilesFinder.reportsIn(module, rootDir, projectBaseDir);
}
public static final String REPORTS_IN_ROOT_KEY = SwiftPlugin.PROPERTY_PREFIX + ".coverage.reportsInRoot";

for (final File report : reports) {
LOGGER.info("Processing coverage report {}", report);
CoberturaReportParser.parseReport(report, fileSystem, project, context);
}
public SwiftCoberturaSensor(FileSystem fileSystem, Settings settings) {
super(fileSystem, settings, REPORT_PATTERN_KEY, DEFAULT_REPORT_PATTERN, REPORTS_IN_ROOT_KEY, true);
}

private String getRootDirectory(Project project) {
final String projectBaseDir = fileSystem.baseDir().getPath();
if (project.isRoot()) {
return projectBaseDir;
} else {
final String modulePath = project.path();
return projectBaseDir.substring(0, projectBaseDir.length() - modulePath.length());
}
public void parseReport(File report, Project project, SensorContext context) {
CoberturaReportParser.parseReport(report, fileSystem, context, getRootDirectory(project));
}
}
Loading