test: strip comments before the partition smoke inventory matches (#6553)

PartitionSmokeInventoryTest matched MINTS against raw source, so a
comment between "arn:" + and PARTITION_REGEX defeated the exclusion
for a pattern that only recognises ARNs, and a call shape in a comment
or javadoc counted as minting. Either could make a package need a smoke
case or an exemption it does not. The scan now blanks comments and text
blocks first, keeping string literals, the way code_text in
tools/partition/partition_literals.py does, so the two scanners agree.

Follow-up to Greptile's review on #6482.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-25 16:53:16 -04:00
1 parent 7c9c8ee398
commit 796f203b76
1 file changed
+58 -1
@@ -16,6 +16,7 @@ import java.util.regex.Pattern;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
@@ -77,6 +78,62 @@ class PartitionSmokeInventoryTest {
assertTrue(unknown.isEmpty(), "COVERED_PACKAGES names packages that do not mint ARNs or hosts: " + unknown);
}
@Test
void commentsNeitherMintNorHideAPattern() {
assertFalse(mintsIn("Pattern p = Pattern.compile(\"arn:\" + /* partition */ AwsArnUtils.PARTITION_REGEX"
+ " + \":iam::\\\\d{12}:role/.+\");"));
assertFalse(mintsIn("// String arn = \"arn:\" + partition;\n/** see Arn.of( */ int x;"));
assertTrue(mintsIn("String arn = \"arn:\" + /* the request's */ partition + \":iam::\" + account;"));
assertTrue(mintsIn("String url = \"https://\" + host; Arn.of(service, region, account, resource);"));
}
private static boolean mintsIn(String source) {
return MINTS.matcher(codeText(source)).find();
}
/**
* The source with every comment and text block blanked and string literals kept, as
* {@code code_text} in {@code tools/partition/partition_literals.py} does: a {@code //} inside a
* string is a URL, not a comment, and a call shape in a comment or javadoc never counts.
*/
static String codeText(String text) {
StringBuilder out = new StringBuilder(text.length());
int n = text.length();
int i = 0;
while (i < n) {
if (text.startsWith("//", i)) {
int end = text.indexOf('\n', i);
end = end < 0 ? n : end;
out.append(" ".repeat(end - i));
i = end;
} else if (text.startsWith("/*", i) || text.startsWith("\"\"\"", i)) {
String closer = text.charAt(i + 1) == '*' ? "*/" : "\"\"\"";
int j = i + closer.length();
while (j < n && !text.startsWith(closer, j)) {
j += closer.equals("\"\"\"") && text.charAt(j) == '\\' ? 2 : 1;
}
int end = Math.min(j + closer.length(), n);
for (int k = i; k < end; k++) {
out.append(text.charAt(k) == '\n' ? '\n' : ' ');
}
i = end;
} else if (text.charAt(i) == '"' || text.charAt(i) == '\'') {
char quote = text.charAt(i);
int j = i + 1;
while (j < n && text.charAt(j) != quote && text.charAt(j) != '\n') {
j += text.charAt(j) == '\\' ? 2 : 1;
}
int end = j < n && text.charAt(j) == quote ? j + 1 : Math.min(j, n);
out.append(text, i, end);
i = end;
} else {
out.append(text.charAt(i));
i++;
}
}
return out.toString();
}
private static Set<String> mintingPackages() {
Set<String> packages = new TreeSet<>();
try (Stream<Path> dirs = Files.list(SERVICES)) {
@@ -96,7 +153,7 @@ class PartitionSmokeInventoryTest {
private static boolean mints(Path dir) throws IOException {
try (Stream<Path> files = Files.walk(dir)) {
for (Path file : files.filter(f -> f.toString().endsWith(".java")).toList()) {
if (MINTS.matcher(Files.readString(file, StandardCharsets.UTF_8)).find()) {
if (mintsIn(Files.readString(file, StandardCharsets.UTF_8))) {
return true;
}
}