Skip to content

Commit b647935

Browse files
ghislainpiotsonartech
authored andcommitted
SONARPY-3129 Use text blocks instead of string concatenation (#339)
GitOrigin-RevId: 2f6907c148cb4b7ffacb63565afa4e0ed9226723
1 parent 5b04d3e commit b647935

69 files changed

Lines changed: 1920 additions & 1524 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

its/commons/src/test/java/com/sonar/python/it/TestsUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ public static ConcurrentOrchestratorExtension.ConcurrentOrchestratorExtensionBui
6565

6666
// Custom rules plugin
6767
.addPlugin(FileLocation.byWildcardMavenFilename(new File("../python-custom-rules-plugin/target"), "python-custom-rules-plugin-*.jar"))
68-
.addPlugin(FileLocation.byWildcardMavenFilename(new File("../../../docs/python-custom-rules-example/target"), "python-custom-rules" +
69-
"-example-*.jar"))
68+
.addPlugin(FileLocation.byWildcardMavenFilename(new File("../../../docs/python-custom-rules-example/target"), "python-custom-rules-example-*.jar"))
7069
.restoreProfileAtStartup(FileLocation.of("profiles/profile-python-custom-rules-example.xml"))
7170
.restoreProfileAtStartup(FileLocation.of("profiles/profile-python-custom-rules.xml"))
7271
.restoreProfileAtStartup(FileLocation.of("profiles/profile-python-test-rules.xml"))

its/plugin/it-python-plugin-test/src/test/java/com/sonar/python/it/plugin/SonarLintTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ static void stop() {
102102
@Test
103103
void should_raise_issues() throws IOException, InterruptedException, ExecutionException {
104104
ClientInputFile inputFile = prepareInputFile("foo.py",
105-
"def fooBar():\n"
106-
+ " `1` \n"
107-
+ " `1` #NOSONAR\n",
105+
"""
106+
def fooBar():
107+
`1`
108+
`1` #NOSONAR
109+
""",
108110
false);
109111

110112
List<Issue> issues = new ArrayList<>();

its/plugin/it-python-plugin-test/src/test/java/com/sonar/python/it/plugin/TestReportTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ void invalid_test_report() {
116116
final String projectKey = "nosetests_invalid";
117117
BuildResult result = ORCHESTRATOR.executeBuildQuietly(createBuild(projectKey, "invalid_report.xml"));
118118
assertThat(result.isSuccess()).isTrue();
119-
assertThat(result.getLogs()).contains("Cannot read report 'invalid_report.xml', the following exception occurred:" +
120-
" Unexpected character 't' (code 116) in prolog; expected '<'\n" +
121-
" at [row,col {unknown-source}]: [1,1]");
119+
assertThat(result.getLogs()).contains("""
120+
Cannot read report 'invalid_report.xml', the following exception occurred: Unexpected character 't' (code 116) in prolog; expected '<'
121+
""");
122122
}
123123

124124
private static Map<String, Integer> nullMeasures() {

python-checks-testkit/src/main/java/org/sonar/python/checks/quickfix/PythonQuickFixVerifier.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ public static void verify(Function<String, PythonVisitorContext> createVisitorCo
9595
.toList();
9696

9797
assertThat(appliedQuickFix)
98-
.as("The code with the quickfix applied is not the expected result.\n" +
99-
"Applied QuickFixes are:\n%s\nExpected result:\n%s", appliedQuickFix, Arrays.asList(codesFixed))
98+
.as("""
99+
The code with the quickfix applied is not the expected result.
100+
Applied QuickFixes are:
101+
%s
102+
Expected result:
103+
%s""", appliedQuickFix, Arrays.asList(codesFixed))
100104
.isEqualTo(Arrays.asList(codesFixed));
101105
}
102106

python-checks-testkit/src/test/java/org/sonar/python/checks/quickfix/PythonQuickFixVerifierTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ void test_verify() {
8282

8383
@Test
8484
void test_verify_invalid_after() {
85-
var expectedMsg = "[Correction of quick fixes] The code expected to be generated by the quickfix is not valid (I)Python code.\n" +
86-
"Results is :\n" +
87-
"[a!!=10]";
85+
var expectedMsg = """
86+
[Correction of quick fixes] The code expected to be generated by the quickfix is not valid (I)Python code.
87+
Results is :
88+
[a!!=10]""";
8889
var check = new SimpleCheck();
8990
assertThatThrownBy(() -> PythonQuickFixVerifier.verify(check, "a=10", "a!!=10"))
9091
.isInstanceOf(AssertionError.class)

python-checks/src/main/java/org/sonar/python/checks/FunctionUsingLoopVariableCheck.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@ private static void reportIssue(SubscriptionContext ctx, FunctionLike functionLi
9090
if (!problematicUsages.isEmpty() && !bindingUsages.isEmpty()) {
9191
PreciseIssue issue;
9292
if (functionLike.is(Tree.Kind.FUNCDEF)) {
93-
issue = ctx.addIssue(problematicUsages.get(0), String.format("Add a parameter to function \"%s\" and use variable \"%s\" as its default value;" +
94-
"The value of \"%s\" might change at the next loop iteration.", ((FunctionDef) functionLike).name().name(), symbolName, symbolName))
93+
issue = ctx.addIssue(problematicUsages.get(0), String.format("""
94+
Add a parameter to function "%s" and use variable "%s" as its default value;\
95+
The value of "%s" might change at the next loop iteration.""", ((FunctionDef) functionLike).name().name(), symbolName, symbolName))
9596
.secondary(((FunctionDef) functionLike).name(), "Function capturing the variable");
9697
} else {
9798
issue = ctx.addIssue(problematicUsages.get(0),
98-
String.format("Add a parameter to the parent lambda function and use variable \"%s\" as its default value; " +
99-
"The value of \"%s\" might change at the next loop iteration.", symbolName, symbolName))
99+
String.format("""
100+
Add a parameter to the parent lambda function and use variable "%s" as its default value; \
101+
The value of "%s" might change at the next loop iteration.""", symbolName, symbolName))
100102
.secondary(((LambdaExpression) functionLike).lambdaKeyword(), "Lambda capturing the variable");
101103
}
102104
for (Tree bindingUsage : bindingUsages) {

python-checks/src/main/java/org/sonar/python/checks/StringFormat.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ public class StringFormat {
4040

4141
// See https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
4242
private static final Pattern PRINTF_PARAMETER_PATTERN = Pattern.compile(
43-
"%" + "(?<field>(?:\\((?<mapkey>.*?)\\))?" + "(?<flags>[#\\-+0 ]*)?" + "(?<width>[0-9]*|\\*)?" +
44-
"(?:\\.(?<precision>[0-9]*|\\*))?" + "(?:[lLH])?" + "(?<type>[diueEfFgGoxXrsac]|%))?");
43+
"""
44+
%\
45+
(?<field>(?:\\((?<mapkey>.*?)\\))?\
46+
(?<flags>[#\\-+0 ]*)?\
47+
(?<width>[0-9]*|\\*)?\
48+
(?:\\.(?<precision>[0-9]*|\\*))?\
49+
(?:[lLH])?\
50+
(?<type>[diueEfFgGoxXrsac]|%))?""");
4551

4652
private static final Pattern FORMAT_FIELD_PATTERN = Pattern.compile("^(?<name>[^.\\[!:{}]+)?(?:(?:\\.[a-zA-Z0-9_]+)|(?:\\[[^]]+]))*");
4753
private static final Pattern FORMAT_NUMBER_PATTERN = Pattern.compile("^\\d+$");

python-checks/src/main/java/org/sonar/python/checks/TooManyLinesInFunctionCheck.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
@Rule(key = "S138")
3535
public class TooManyLinesInFunctionCheck extends PythonSubscriptionCheck {
3636

37-
private static final String MESSAGE = "This %1$s \"%2$s\" has %3$d lines of code, " +
38-
"which is greater than the %4$d authorized. Split it into smaller %1$ss.";
37+
private static final String MESSAGE = """
38+
This %1$s "%2$s" has %3$d lines of code, \
39+
which is greater than the %4$d authorized. Split it into smaller %1$ss.""";
3940

4041
private static final int DEFAULT = 100;
4142

python-checks/src/main/java/org/sonar/python/checks/cdk/DisabledESDomainEncryptionCheck.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737

3838
@Rule(key = "S6308")
3939
public class DisabledESDomainEncryptionCheck extends AbstractCdkResourceCheck {
40-
private static final String OMITTING_MESSAGE = "Omitting %s causes encryption of data at rest to be disabled for this %s domain." +
41-
" Make sure it is safe here.";
40+
private static final String OMITTING_MESSAGE = """
41+
Omitting %s causes encryption of data at rest to be disabled for this %s domain.\
42+
Make sure it is safe here.""";
4243
private static final String UNENCRYPTED_MESSAGE = "Make sure that using unencrypted %s domains is safe here.";
4344
private static final String ENABLED = "enabled";
4445
private static final String OPENSEARCH = "OpenSearch";

python-checks/src/main/java/org/sonar/python/checks/cdk/IamPrivilegeEscalationCheck.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
@Rule(key = "S6317")
3333
public class IamPrivilegeEscalationCheck extends AbstractIamPolicyStatementCheck {
3434

35-
private static final String ISSUE_MESSAGE_FORMAT = "This policy is vulnerable to the \"%s\" privilege escalation vector. " +
36-
"Remove permissions or restrict the set of resources they apply to.";
35+
private static final String ISSUE_MESSAGE_FORMAT = """
36+
This policy is vulnerable to the "%s" privilege escalation vector. \
37+
Remove permissions or restrict the set of resources they apply to.""";
3738
private static final String SECONDARY_MESSAGE = "Permissions are granted on all resources.";
3839
private static final Pattern SENSITIVE_RESOURCE_PATTERN = Pattern.compile("(^\\*$)|(arn:.*:(role|user|group)/\\*)");
3940

0 commit comments

Comments
 (0)