|
21 | 21 |
|
22 | 22 | import java.io.File; |
23 | 23 | import java.nio.charset.StandardCharsets; |
24 | | -import java.util.List; |
25 | | -import java.util.concurrent.atomic.AtomicBoolean; |
26 | | -import org.junit.Before; |
27 | 24 | import org.junit.Test; |
28 | | -import org.sonar.api.batch.fs.internal.DefaultFileSystem; |
| 25 | +import org.sonar.api.batch.fs.InputFile; |
29 | 26 | import org.sonar.api.batch.fs.internal.DefaultInputFile; |
30 | 27 | import org.sonar.api.batch.fs.internal.FileMetadata; |
31 | | -import org.sonar.api.batch.rule.ActiveRules; |
32 | 28 | import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; |
| 29 | +import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; |
33 | 30 | import org.sonar.api.batch.sensor.internal.SensorContextTester; |
34 | 31 | import org.sonar.api.config.MapSettings; |
35 | | -import org.sonar.api.config.Settings; |
36 | 32 | import org.sonar.api.rule.RuleKey; |
37 | 33 | import org.sonar.plugins.python.Python; |
38 | 34 |
|
39 | 35 | import static org.assertj.core.api.Assertions.assertThat; |
40 | | -import static org.mockito.Mockito.mock; |
41 | 36 |
|
42 | 37 | public class PylintImportSensorTest { |
43 | 38 |
|
44 | | - private File baseDir = new File("src/test/resources/org/sonar/plugins/python/pylint"); |
45 | | - private Settings settings; |
46 | | - private DefaultFileSystem fileSystem; |
47 | | - private ActiveRules activeRules; |
48 | | - private DefaultInputFile inputFile; |
49 | | - |
50 | | - @Before |
51 | | - public void init() { |
52 | | - settings = new MapSettings(); |
53 | | - settings.setProperty(PylintImportSensor.REPORT_PATH_KEY, "pylint-report.txt"); |
| 39 | + @Test |
| 40 | + public void parse_report() { |
| 41 | + File baseDir = new File("src/test/resources/org/sonar/plugins/python/pylint"); |
| 42 | + SensorContextTester context = SensorContextTester.create(baseDir); |
54 | 43 |
|
55 | | - fileSystem = new DefaultFileSystem(baseDir); |
| 44 | + context.settings().setProperty(PylintImportSensor.REPORT_PATH_KEY, "pylint-report.txt"); |
56 | 45 |
|
57 | 46 | File file = new File(baseDir, "src/file1.py"); |
58 | | - inputFile = new DefaultInputFile("", "src/file1.py") |
| 47 | + DefaultInputFile inputFile = new DefaultInputFile("", "src/file1.py") |
59 | 48 | .setLanguage(Python.KEY) |
60 | 49 | .initMetadata(new FileMetadata().readMetadata(file, StandardCharsets.UTF_8)); |
61 | | - fileSystem.add(inputFile); |
62 | | - activeRules = (new ActiveRulesBuilder()) |
63 | | - .create(RuleKey.of(PylintRuleRepository.REPOSITORY_KEY, "C0103")) |
64 | | - .setName("Invalid name") |
65 | | - .activate() |
66 | | - .create(RuleKey.of(PylintRuleRepository.REPOSITORY_KEY, "C0111")) |
67 | | - .setName("Missing docstring") |
68 | | - .activate() |
69 | | - .build(); |
70 | | - } |
71 | | - |
72 | | - @Test |
73 | | - public void shouldExecuteOnlyWhenNecessary() { |
74 | | - ActiveRules emptyProfile = mock(ActiveRules.class); |
75 | | - Settings settingsWithoutProperty = new MapSettings(); |
76 | | - |
77 | | - assertThat(shouldExecute(activeRules, settings)).isTrue(); |
78 | | - assertThat(shouldExecute(emptyProfile, settings)).isFalse(); |
79 | | - |
80 | | - assertThat(shouldExecute(activeRules, settingsWithoutProperty)).isFalse(); |
81 | | - assertThat(shouldExecute(emptyProfile, settingsWithoutProperty)).isFalse(); |
82 | | - } |
| 50 | + context.fileSystem().add(inputFile); |
83 | 51 |
|
84 | | - private boolean shouldExecute(ActiveRules activeRules, Settings settings) { |
85 | | - SensorContextTester ctx = SensorContextTester.create(baseDir); |
86 | | - ctx.setActiveRules(activeRules); |
87 | | - ctx.setSettings(settings); |
88 | | - ctx.setFileSystem(fileSystem); |
89 | | - AtomicBoolean executed = new AtomicBoolean(false); |
90 | | - PylintImportSensor sensor = new PylintImportSensor(settings) { |
91 | | - @Override |
92 | | - protected void processReports(org.sonar.api.batch.sensor.SensorContext context, List<File> reports) { |
93 | | - super.processReports(context, reports); |
94 | | - executed.set(true); |
95 | | - } |
96 | | - }; |
97 | | - sensor.execute(ctx); |
98 | | - return executed.get(); |
99 | | - } |
| 52 | + context.setActiveRules((new ActiveRulesBuilder()) |
| 53 | + .create(RuleKey.of(PylintRuleRepository.REPOSITORY_KEY, "C0103")) |
| 54 | + .setName("Invalid name") |
| 55 | + .activate() |
| 56 | + .create(RuleKey.of(PylintRuleRepository.REPOSITORY_KEY, "C0111")) |
| 57 | + .setName("Missing docstring") |
| 58 | + .activate() |
| 59 | + .build()); |
100 | 60 |
|
101 | | - @Test |
102 | | - public void parse_report() { |
103 | | - SensorContextTester context = SensorContextTester.create(baseDir); |
104 | | - context.setActiveRules(activeRules); |
105 | | - context.setFileSystem(fileSystem); |
106 | | - |
107 | | - PylintImportSensor sensor = new PylintImportSensor(settings); |
| 61 | + PylintImportSensor sensor = new PylintImportSensor(context.settings()); |
108 | 62 | sensor.execute(context); |
109 | 63 | assertThat(context.allIssues()).hasSize(3); |
110 | 64 | assertThat(context.allIssues()).extracting(issue -> issue.primaryLocation().inputComponent().key()) |
111 | 65 | .containsOnly(inputFile.key()); |
112 | 66 | } |
113 | 67 |
|
| 68 | + @Test |
| 69 | + public void sensor_descriptor() { |
| 70 | + DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor(); |
| 71 | + new PylintImportSensor(new MapSettings()).describe(descriptor); |
| 72 | + assertThat(descriptor.name()).isEqualTo("PylintImportSensor"); |
| 73 | + assertThat(descriptor.languages()).containsOnly("py"); |
| 74 | + assertThat(descriptor.type()).isEqualTo(InputFile.Type.MAIN); |
| 75 | + assertThat(descriptor.ruleRepositories()).containsExactly(PylintRuleRepository.REPOSITORY_KEY); |
| 76 | + assertThat(descriptor.properties()).containsOnly(PylintImportSensor.REPORT_PATH_KEY); |
| 77 | + } |
| 78 | + |
114 | 79 | } |
0 commit comments