Skip to content

Commit

Permalink
Implemented SwiftFileSystem to prepare new API refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilles Grousset committed Aug 18, 2017
1 parent 19d5e67 commit 0d7ed80
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 36 deletions.
4 changes: 2 additions & 2 deletions build-and-deploy.sh
Expand Up @@ -26,10 +26,10 @@ if [ "$?" != 0 ]; then
exit $?
fi

# Run shell tests
# Run shell surefire
#shelltest src/test/shell --execdir --diff
#if [ "$?" != 0 ]; then
# echo "ERROR - Shell tests failed!" 1>&2
# echo "ERROR - Shell surefire failed!" 1>&2
# exit $?
#fi

Expand Down
4 changes: 2 additions & 2 deletions sonar-project.properties
Expand Up @@ -36,7 +36,7 @@ sonar.sources=SourceDir
# Path to test directories (comment if no test)
sonar.tests=TestDir

# Destination Simulator to run tests
# Destination Simulator to run surefire
# As string expected in destination argument of xcodebuild command
# Example = sonar.swift.simulator=platform=iOS Simulator,name=iPhone 6,OS=9.2
sonar.swift.simulator=platform=iOS Simulator,name=iPhone 6,OS=9.2
Expand Down Expand Up @@ -91,7 +91,7 @@ sonar.sourceEncoding=UTF-8
# Change it only if you generate the file on your own
# sonar.swift.tailor.report=sonar-reports/*tailor.txt

# Paths to exclude from coverage report (tests, 3rd party libraries etc.)
# Paths to exclude from coverage report (surefire, 3rd party libraries etc.)
# sonar.swift.excludedPathsFromCoverage=pattern1,pattern2
sonar.swift.excludedPathsFromCoverage=.*Tests.*

Expand Down
Expand Up @@ -22,7 +22,6 @@
import org.sonar.api.Properties;
import org.sonar.api.Property;
import org.sonar.api.SonarPlugin;
import org.sonar.plugins.swift.colorizer.SwiftCodeColorizerFormat;
import org.sonar.plugins.swift.complexity.LizardSensor;
import org.sonar.plugins.swift.coverage.SwiftCoberturaSensor;
import org.sonar.plugins.swift.cpd.SwiftCpdMapping;
Expand All @@ -36,7 +35,7 @@
import org.sonar.plugins.swift.issues.tailor.TailorRulesDefinition;
import org.sonar.plugins.swift.issues.tailor.TailorSensor;
import org.sonar.plugins.swift.lang.core.Swift;
import org.sonar.plugins.swift.tests.SwiftSurefireSensor;
import org.sonar.plugins.swift.surefire.SwiftSurefireSensor;

import com.google.common.collect.ImmutableList;

Expand Down Expand Up @@ -112,7 +111,7 @@ public List getExtensions() {
// code
SwiftSquidSensor.class,

// tests
// surefire
SwiftSurefireSensor.class,
SwiftCoberturaSensor.class,

Expand Down
@@ -0,0 +1,81 @@
/**
* backelite-sonar-swift-plugin - Enables analysis of Swift projects into SonarQube.
* Copyright © 2015 Backelite (${email})
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sonar.plugins.swift.lang;

import org.sonar.api.batch.BatchSide;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.plugins.swift.lang.core.Swift;

import javax.annotation.CheckForNull;
import java.io.File;
import java.util.ArrayList;
import java.util.List;


@BatchSide
public class SwiftFileSystem {

private final FileSystem fileSystem;
private final FilePredicates predicates;
private final FilePredicate isSwiftLanguage;
private final FilePredicate isMainTypeFile;

public SwiftFileSystem(FileSystem fileSystem) {
this.fileSystem = fileSystem;
this.predicates = fileSystem.predicates();
this.isSwiftLanguage = predicates.hasLanguage(Swift.KEY);
this.isMainTypeFile = predicates.hasType(InputFile.Type.MAIN);
}

public boolean hasSwiftFiles() {
return fileSystem.hasFiles(isSwiftLanguage);
}

public List<File> sourceFiles() {
Iterable<File> files = fileSystem.files(predicates.and(isSwiftLanguage, isMainTypeFile));
List<File> list = new ArrayList<>();
files.iterator().forEachRemaining(list::add);
return list;
}

public List<InputFile> swiftInputFiles() {
Iterable<InputFile> inputFiles = fileSystem.inputFiles(isSwiftLanguage);
List<InputFile> list = new ArrayList<>();
inputFiles.iterator().forEachRemaining(list::add);
return list;
}

public List<InputFile> sourceInputFiles() {
Iterable<InputFile> inputFiles = fileSystem.inputFiles(predicates.and(isSwiftLanguage, isMainTypeFile));
List<InputFile> list = new ArrayList<>();
inputFiles.iterator().forEachRemaining(list::add);
return list;
}

@CheckForNull
public InputFile sourceInputFileFromRelativePath(String relativePath) {
return fileSystem.inputFile(predicates.and(predicates.matchesPathPattern("**/" + relativePath), isSwiftLanguage, isMainTypeFile));
}

public File baseDir() {
return fileSystem.baseDir();
}
}
Expand Up @@ -15,10 +15,9 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sonar.plugins.swift.tests;
package org.sonar.plugins.swift.surefire;

import com.google.common.collect.ImmutableList;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -27,25 +26,18 @@
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.component.ResourcePerspectives;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Resource;
import org.sonar.api.test.MutableTestPlan;
import org.sonar.api.test.TestCase;
import org.sonar.api.utils.ParsingUtils;
import org.sonar.api.utils.StaxParser;
import org.sonar.api.utils.XmlParserException;
import org.sonar.plugins.swift.tests.surefire.SurefireStaxHandler;
import org.sonar.plugins.swift.tests.surefire.UnitTestClassReport;
import org.sonar.plugins.swift.tests.surefire.UnitTestIndex;
import org.sonar.plugins.swift.tests.surefire.UnitTestResult;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.sonar.plugins.swift.surefire.data.SurefireStaxHandler;
import org.sonar.plugins.swift.surefire.data.UnitTestClassReport;
import org.sonar.plugins.swift.surefire.data.UnitTestIndex;
import org.sonar.plugins.swift.surefire.data.UnitTestResult;

import javax.xml.stream.XMLStreamException;
import javax.xml.transform.TransformerException;
import java.io.File;
import java.io.FilenameFilter;
import java.util.List;
Expand Down Expand Up @@ -138,7 +130,7 @@ private void save(UnitTestIndex index) {
}
}
if (negativeTimeTestNumber > 0) {
LOGGER.warn("There is {} test(s) reported with negative time by surefire, total duration may not be accurate.", negativeTimeTestNumber);
LOGGER.warn("There is {} test(s) reported with negative time by data, total duration may not be accurate.", negativeTimeTestNumber);
}
}

Expand Down
Expand Up @@ -15,21 +15,18 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sonar.plugins.swift.tests;
package org.sonar.plugins.swift.surefire;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.CoverageExtension;
import org.sonar.api.batch.DependsUpon;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.component.ResourcePerspectives;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.plugins.swift.lang.core.Swift;

import java.io.File;

Expand Down
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sonar.plugins.swift.tests.surefire;
package org.sonar.plugins.swift.surefire.data;

import org.apache.commons.lang.StringUtils;
import org.codehaus.staxmate.in.ElementFilter;
Expand Down Expand Up @@ -96,7 +96,7 @@ private static UnitTestResult parseTestResult(SMInputCursor testCaseCursor) thro
String elementName = childNode.getLocalName();
if ("skipped".equals(elementName)) {
status = UnitTestResult.STATUS_SKIPPED;
// bug with surefire reporting wrong time for skipped tests
// bug with data reporting wrong time for skipped surefire
duration = 0L;

} else if ("failure".equals(elementName)) {
Expand Down
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sonar.plugins.swift.tests.surefire;
package org.sonar.plugins.swift.surefire.data;

import com.google.common.collect.Lists;

Expand Down
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sonar.plugins.swift.tests.surefire;
package org.sonar.plugins.swift.surefire.data;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
Expand Down
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sonar.plugins.swift.tests.surefire;
package org.sonar.plugins.swift.surefire.data;

public final class UnitTestResult {
public static final String STATUS_OK = "ok";
Expand Down
8 changes: 4 additions & 4 deletions sonar-swift-plugin/src/main/shell/run-sonar-swift.sh
Expand Up @@ -254,7 +254,7 @@ if [ "$vflag" = "on" ]; then
echo "Destination simulator is: $destinationSimulator"
echo "Excluded paths from coverage are: $excludedPathsFromCoverage"
else
echo "Unit tests are disabled"
echo "Unit surefire are disabled"
fi
fi

Expand All @@ -275,13 +275,13 @@ rm -rf sonar-reports
mkdir sonar-reports

if [ "$unittests" = "on" ]; then
# Unit tests and coverage
# Unit surefire and coverage

# Put default xml files with no tests and no coverage...
# Put default xml files with no surefire and no coverage...
echo "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><testsuites name='AllTestUnits'></testsuites>" > sonar-reports/TEST-report.xml
echo "<?xml version='1.0' ?><!DOCTYPE coverage SYSTEM 'http://cobertura.sourceforge.net/xml/coverage-03.dtd'><coverage><sources></sources><packages></packages></coverage>" > sonar-reports/coverage-swift.xml

echo -n 'Running tests'
echo -n 'Running surefire'
buildCmd=(xcodebuild clean build test)
if [[ ! -z "$workspaceFile" ]]; then
buildCmd+=(-workspace "$workspaceFile")
Expand Down
Expand Up @@ -43,7 +43,7 @@ public void setUp() {
}

/**
* this method tests that the sensor should be executed when a project is a root project and uses objective c
* this method surefire that the sensor should be executed when a project is a root project and uses objective c
*/
@Test
public void shouldExecuteOnProjectShouldBeTrueWhenProjectIsObjc() {
Expand Down

0 comments on commit 0d7ed80

Please sign in to comment.