Skip to content

Commit 932a86e

Browse files
joke1196ghislainpiot
authored andcommitted
SONARPY-2001:Create GeneratedIPythonFile to hold offset data (#1859)
1 parent 1f51905 commit 932a86e

File tree

5 files changed

+190
-13
lines changed

5 files changed

+190
-13
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2011-2024 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.plugins.python;
21+
22+
import java.io.IOException;
23+
import org.sonar.api.batch.fs.InputFile;
24+
25+
public class GeneratedIPythonFile implements PythonInputFile {
26+
27+
InputFile wrappedFile;
28+
29+
private String pythonContent;
30+
31+
public GeneratedIPythonFile(InputFile wrappedFile, String pythonContent) {
32+
this.wrappedFile = wrappedFile;
33+
this.pythonContent = pythonContent;
34+
}
35+
36+
@Override
37+
public InputFile wrappedFile() {
38+
return wrappedFile;
39+
}
40+
41+
@Override
42+
public Kind kind() {
43+
return Kind.IPYTHON;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return wrappedFile.toString();
49+
}
50+
51+
@Override
52+
public String contents() throws IOException {
53+
return pythonContent;
54+
}
55+
56+
}

sonar-python-plugin/src/main/java/org/sonar/plugins/python/PythonInputFile.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,24 @@
1919
*/
2020
package org.sonar.plugins.python;
2121

22+
import java.io.IOException;
2223
import org.sonar.api.batch.fs.InputFile;
2324

2425
public interface PythonInputFile {
2526

2627
InputFile wrappedFile();
2728

29+
default String contents() throws IOException {
30+
return wrappedFile().contents();
31+
}
32+
33+
default Kind kind() {
34+
return Kind.PYTHON;
35+
}
36+
37+
enum Kind {
38+
PYTHON,
39+
IPYTHON
40+
}
41+
2842
}

sonar-python-plugin/src/main/java/org/sonar/plugins/python/PythonInputFileImpl.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,13 @@ public PythonInputFileImpl(InputFile wrappedFile) {
2828
this.wrappedFile = wrappedFile;
2929
}
3030

31+
@Override
3132
public InputFile wrappedFile() {
32-
return this.wrappedFile;
33+
return wrappedFile;
3334
}
3435

3536
@Override
3637
public String toString() {
3738
return wrappedFile.toString();
3839
}
39-
40-
41-
@Override
42-
public boolean equals(Object obj) {
43-
return wrappedFile.equals(obj);
44-
}
45-
46-
47-
@Override
48-
public int hashCode() {
49-
return wrappedFile.hashCode();
50-
}
5140
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2011-2024 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.plugins.python;
21+
22+
import java.io.IOException;
23+
import org.junit.jupiter.api.Test;
24+
import org.sonar.api.batch.fs.InputFile;
25+
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
26+
import org.sonar.plugins.python.PythonInputFile.Kind;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
class GeneratedIPythonFileTest {
31+
32+
@Test
33+
void shouldHaveIPythonKind() {
34+
PythonInputFile inputFile = new GeneratedIPythonFile(createWrappedFile(), "");
35+
assertThat(inputFile.kind()).isEqualTo(Kind.IPYTHON);
36+
}
37+
38+
@Test
39+
void shouldReturnTheWrappedFile() {
40+
InputFile wrappedFile = createWrappedFile();
41+
GeneratedIPythonFile inputFile = new GeneratedIPythonFile(wrappedFile, "");
42+
assertThat(inputFile.wrappedFile()).isEqualTo(wrappedFile);
43+
}
44+
45+
@Test
46+
void shouldHaveTheWrappedFileToString() {
47+
InputFile wrappedFile = createWrappedFile();
48+
PythonInputFile inputFile = new GeneratedIPythonFile(wrappedFile, "");
49+
assertThat(inputFile).hasToString(wrappedFile.toString());
50+
}
51+
52+
@Test
53+
void shouldHaveTheContentPassed() throws IOException {
54+
InputFile wrappedFile = createWrappedFile();
55+
PythonInputFile inputFile = new GeneratedIPythonFile(wrappedFile, "test");
56+
assertThat(inputFile.contents()).isEqualTo("test");
57+
58+
}
59+
60+
private InputFile createWrappedFile() {
61+
return TestInputFileBuilder.create("moduleKey", "name").setContents("Some content").build();
62+
}
63+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2011-2024 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.plugins.python;
21+
22+
import java.io.IOException;
23+
import org.junit.jupiter.api.Test;
24+
import org.sonar.api.batch.fs.InputFile;
25+
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
26+
import org.sonar.plugins.python.PythonInputFile.Kind;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
class PythonInputFileImplTest {
31+
32+
@Test
33+
void shouldHavePythonKind() {
34+
PythonInputFile inputFile = new PythonInputFileImpl(createWrappedFile());
35+
assertThat(inputFile.kind()).isEqualTo(Kind.PYTHON);
36+
}
37+
38+
@Test
39+
void shouldHaveTheWrappedFileToString() {
40+
InputFile wrappedFile = createWrappedFile();
41+
PythonInputFile inputFile = new PythonInputFileImpl(wrappedFile);
42+
assertThat(inputFile).hasToString(wrappedFile.toString());
43+
}
44+
45+
@Test
46+
void shouldReturnTheContentOfTheWrappedFile() throws IOException {
47+
InputFile wrappedFile = createWrappedFile();
48+
PythonInputFile inputFile = new PythonInputFileImpl(wrappedFile);
49+
assertThat(inputFile.contents()).isEqualTo(wrappedFile.contents());
50+
}
51+
52+
private InputFile createWrappedFile() {
53+
return TestInputFileBuilder.create("moduleKey", "name").setContents("Test").build();
54+
}
55+
}

0 commit comments

Comments
 (0)