diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PartitionSmokeInventoryTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PartitionSmokeInventoryTest.java index f134af747e..0e2029b58c 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/PartitionSmokeInventoryTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/PartitionSmokeInventoryTest.java @@ -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 mintingPackages() { Set packages = new TreeSet<>(); try (Stream dirs = Files.list(SERVICES)) { @@ -96,7 +153,7 @@ class PartitionSmokeInventoryTest { private static boolean mints(Path dir) throws IOException { try (Stream 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; } }