Skip to content
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

Add File Suffixes #84

Merged
merged 3 commits into from
Mar 6, 2024
Merged
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 @@ -18,17 +18,34 @@
package fr.insideapp.sonarqube.objc;

import fr.insideapp.sonarqube.apple.commons.tests.LanguageTestFile;
import org.apache.commons.lang3.StringUtils;
import org.sonar.api.config.Configuration;
import org.sonar.api.resources.AbstractLanguage;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public final class ObjectiveC extends AbstractLanguage implements LanguageTestFile {

public ObjectiveC() {
public static final List<String> FILE_SUFFIXES = List.of("h", "m", "mm");

private Configuration configuration;

public ObjectiveC(final Configuration configuration) {
super("objc", "Objective-C");
this.configuration = configuration;
}

@Override
public String[] getFileSuffixes() {
return new String[]{"h", "m", "mm"};
final List<String> providedFilesSuffixes = Arrays.asList(configuration.getStringArray(ObjectiveCExtensionProvider.FILE_SUFFIXES_KEY))
.stream()
.map(String::trim)
.filter(StringUtils::isNotBlank)
.collect(Collectors.toList());
final List<String> filesSuffixes = providedFilesSuffixes.isEmpty() ? FILE_SUFFIXES : providedFilesSuffixes;
return filesSuffixes.stream().toArray(String[]::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,34 @@
import fr.insideapp.sonarqube.objc.antlr.ObjectiveCHighlighterVisitor;
import fr.insideapp.sonarqube.objc.antlr.ObjectiveCSourceLinesVisitor;
import fr.insideapp.sonarqube.objc.issues.ObjectiveCProfile;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.scanner.ScannerSide;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@ScannerSide
public class ObjectiveCExtensionProvider implements ExtensionProvider {

public static final String FILE_SUFFIXES_KEY = "sonar.objc.file.suffixes";

private static final PropertyDefinition FILE_SUFFIXES = PropertyDefinition
.builder(FILE_SUFFIXES_KEY)
.name("File Suffixes")
.description("List of suffixes of Objective-C files to analyze.")
.multiValues(true)
.category(APPLE_CATEGORY)
.subCategory("Objective-C")
.onQualifiers(Qualifiers.PROJECT)
.defaultValue(ObjectiveC.FILE_SUFFIXES.stream().collect(Collectors.joining(",")))
.build();

public List<Object> extensions() {
return Arrays.asList(
ObjectiveC.class,
FILE_SUFFIXES,
ObjectiveCAntlrContext.class,
ObjectiveCSourceLinesVisitor.class,
ObjectiveCHighlighterVisitor.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void prepare() {

@Test
public void extensions() {
assertThat(provider.extensions()).hasSize(7);
assertThat(provider.extensions()).hasSize(8);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.config.Configuration;

import java.io.File;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public final class ObjectiveCSensorTest {

private ObjectiveCSensor sensor;
private SensorContextTester context;
private Configuration configuration;
private ObjectiveC objectiveC;

@Before
public void prepare() {
objectiveC = new ObjectiveC();
configuration = mock(Configuration.class);
objectiveC = new ObjectiveC(configuration);
context = SensorContextTester.create(new File("."));
sensor = new ObjectiveCSensor(
objectiveC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,66 @@

import org.junit.Before;
import org.junit.Test;
import org.sonar.api.config.Configuration;
import org.sonar.api.resources.Language;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public final class ObjectiveCTest {

private Language language;
private Configuration configuration;

@Before
public void prepare() {
language = new ObjectiveC();
configuration = mock(Configuration.class);
language = new ObjectiveC(configuration);
}

@Test
public void definition() {
assertThat(language.getKey()).isEqualTo("objc");
assertThat(language.getName()).isEqualTo("Objective-C");
assertThat(language.getFileSuffixes()).containsOnly("h", "m", "mm");
public void fileSuffixes_default() {
// prepare
when(configuration.getStringArray(ObjectiveCExtensionProvider.FILE_SUFFIXES_KEY))
.thenReturn(new String[]{});
// test
final String[] fileSuffixes = language.getFileSuffixes();
// prepare
assertThat(fileSuffixes).containsAll(ObjectiveC.FILE_SUFFIXES);
}

@Test
public void fileSuffixes_custom() {
// prepare
when(configuration.getStringArray(ObjectiveCExtensionProvider.FILE_SUFFIXES_KEY))
.thenReturn(new String[]{"foo", "bar"});
// test
final String[] fileSuffixes = language.getFileSuffixes();
// prepare
assertThat(fileSuffixes).containsExactlyInAnyOrder("foo", "bar");
}

@Test
public void fileSuffixes_custom_but_not_good() {
// prepare
when(configuration.getStringArray(ObjectiveCExtensionProvider.FILE_SUFFIXES_KEY))
.thenReturn(new String[]{"", " "});
// test
final String[] fileSuffixes = language.getFileSuffixes();
// prepare
assertThat(fileSuffixes).containsAll(ObjectiveC.FILE_SUFFIXES);
}

@Test
public void fileSuffixes_custom_with_spaces() {
// prepare
when(configuration.getStringArray(ObjectiveCExtensionProvider.FILE_SUFFIXES_KEY))
.thenReturn(new String[]{"foo ", " bar", "", " "});
// test
final String[] fileSuffixes = language.getFileSuffixes();
// prepare
assertThat(fileSuffixes).containsExactlyInAnyOrder("foo", "bar");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.measure.Measure;
import org.sonar.api.config.Configuration;
import org.sonar.api.measures.CoreMetrics;

import java.io.File;
Expand All @@ -34,6 +35,7 @@
import java.nio.file.Paths;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public class ObjectiveCCyclomaticComplexityVisitorTest {

Expand All @@ -49,14 +51,17 @@ public Container(String fileName, int complexity) {

private static final String BASE_DIR = "src/test/resources/objc/cyclomatic_complexity";
private SensorContextTester sensorContext;
private Configuration configuration;

private ObjectiveC objectiveC;
private ObjectiveCAntlrContext antlrContext;
private CustomTreeVisitor customTreeVisitor;

@Before
public void prepare() {
sensorContext = SensorContextTester.create(new File(BASE_DIR));
objectiveC = new ObjectiveC();
configuration = mock(Configuration.class);
objectiveC = new ObjectiveC(configuration);
antlrContext = new ObjectiveCAntlrContext();
customTreeVisitor = new CustomTreeVisitor(new ObjectiveCCyclomaticComplexityVisitor());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.measure.Measure;
import org.sonar.api.config.Configuration;
import org.sonar.api.measures.CoreMetrics;

import java.io.File;
Expand All @@ -33,6 +34,7 @@
import java.nio.file.Paths;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public class ObjectiveCSourceLinesVisitorTest {

Expand All @@ -51,53 +53,54 @@ public Container(String fileName, int linesOfCode, int linesOfComment) {

private static final String BASE_DIR = "src/test/resources/objc/source_lines_visitor";
private SensorContextTester sensorContext;

private Configuration configuration;

private ObjectiveC objectiveC;
private ObjectiveCAntlrContext antlrContext;
private ObjectiveCSourceLinesVisitor visitor;

@Before
public void prepare() {
sensorContext = SensorContextTester.create(new File(BASE_DIR));
objectiveC = new ObjectiveC();
configuration = mock(Configuration.class);
objectiveC = new ObjectiveC(configuration);
antlrContext = new ObjectiveCAntlrContext();
visitor = new ObjectiveCSourceLinesVisitor();
}

@Test
public void testNoComment() throws IOException {
assertContainer(new Container("NoComment", 7, 0));
assertContainer(new Container("NoComment.m", 7, 0));
}

@Test
public void testNoCode() throws IOException {
assertContainer(new Container("NoCode", 0, 1));
assertContainer(new Container("NoCode.m", 0, 1));
}

@Test
public void testEmpty() throws IOException {
assertContainer(new Container("Empty", 0, 0));
assertContainer(new Container("Empty.m", 0, 0));
}

@Test
public void testLineWithMixedCodeComment() throws IOException {
assertContainer(new Container("LineWithMixedCodeComment", 3, 0));
assertContainer(new Container("LineWithMixedCodeComment.m", 3, 0));
}

@Test
public void testWhiteLineIgnored() throws IOException {
assertContainer(new Container("WhiteLineIgnored", 7, 1));
assertContainer(new Container("WhiteLineIgnored.m", 7, 1));
}

private void assertContainer(Container container) throws IOException {

final String completeFileName = container.fileName + ".m";

// Real file
File file = new File(BASE_DIR, completeFileName);
File file = new File(BASE_DIR, container.fileName);

// Mock file for test purpose
// Setting it up with the real file properties
InputFile inputFile = new TestInputFileBuilder("", completeFileName)
InputFile inputFile = new TestInputFileBuilder("", container.fileName)
.setLanguage(objectiveC.getKey())
.setModuleBaseDir(Paths.get(BASE_DIR))
.setContents(FileUtils.readFileToString(file, Charset.defaultCharset()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
import fr.insideapp.sonarqube.objc.issues.warnings.XcodeWarningObjectiveCRulesDefinition;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.config.Configuration;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public final class ObjectiveCProfileTest {

private Configuration configuration;
private ObjectiveC objectiveC;

private ObjectiveCProfile profile;
Expand All @@ -38,7 +41,8 @@ public final class ObjectiveCProfileTest {

@Before
public void prepare() {
objectiveC = new ObjectiveC();
configuration = mock(Configuration.class);
objectiveC = new ObjectiveC(configuration);
context = new BuiltInQualityProfilesDefinition.Context();
profile = new ObjectiveCProfile(
objectiveC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@
import fr.insideapp.sonarqube.objc.ObjectiveC;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.config.Configuration;
import org.sonar.api.server.rule.RulesDefinition;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public final class MobSFScanObjectiveCRulesDefinitionTest {

private MobSFScanRulesDefinition rulesDefinition;
private Configuration configuration;
private ObjectiveC language;
private RulesDefinition.Context context;

@Before
public void prepare() {
language = new ObjectiveC();
configuration = mock(Configuration.class);
language = new ObjectiveC(configuration);
rulesDefinition = new MobSFScanObjectiveCRulesDefinition(language);
context = new RulesDefinition.Context();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@
import fr.insideapp.sonarqube.objc.ObjectiveC;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.config.Configuration;
import org.sonar.api.server.rule.RulesDefinition;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public final class OCLintRulesDefinitionTest {

private JSONRulesDefinition rulesDefinition;
private Configuration configuration;
private ObjectiveC language;
private RulesDefinition.Context context;

@Before
public void prepare() {
language = new ObjectiveC();
configuration = mock(Configuration.class);
language = new ObjectiveC(configuration);
rulesDefinition = new OCLintRulesDefinition(language);
context = new RulesDefinition.Context();
}
Expand Down
Loading