Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/http-client-java"
---

Allow generated test and sample filenames to use the Azure SDK for Java pipeline's relaxed path-length limit.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

public final class ClassNameUtil {

static final int MAX_PATH_LENGTH = 260;
private static final int MAX_DIRECTORY_LENGTH = 248;
static final int BASE_PATH_LENGTH = 30;

/**
* Truncate class name to avoid path too long.
*
Expand All @@ -24,8 +28,6 @@ public static String truncateClassName(String namespace, String directory, Strin
String classNameSuffix) {
// see
// https://github.com/Azure/azure-sdk-for-java/blob/main/eng/common/pipelines/templates/steps/verify-path-length.yml
final int maxPathLength = 260;
final int basePathLength = 38;

// directory layout in Java SDK repository is:
// sdk/<group>/<artifact>/<directory>/<package_name>/<class_name><class_name_suffix>.java
Expand All @@ -43,7 +45,7 @@ public static String truncateClassName(String namespace, String directory, Strin

final int minRemainLength = 5; // we still need some char for class name

final int remainLength = maxPathLength - basePathLength - groupLength - artifactLength - directoryLength
final int remainLength = MAX_PATH_LENGTH - BASE_PATH_LENGTH - groupLength - artifactLength - directoryLength
- packageLength - classNameSuffixLength - extraLength;

if (remainLength < className.length() && remainLength >= minRemainLength) {
Expand All @@ -68,10 +70,10 @@ public static String getDirectoryNameForGraalVmConfig(String groupId, String art
? artifactIdSegments[2]
: artifactIdSegments[artifactIdSegments.length - 1]);
final int parentDirectoryLength = ("sdk/" + group + "/" + artifactId + "/").length();
final int fileNameLength = "/reflect-config.json".length();
final int fileNameLength = "/resource-config.json".length();

if (parentDirectoryLength + metaInfPath.length() > (248 - 38)
|| parentDirectoryLength + metaInfPath.length() + fileNameLength > (260 - 38)) {
if (parentDirectoryLength + metaInfPath.length() > (MAX_DIRECTORY_LENGTH - BASE_PATH_LENGTH)
|| parentDirectoryLength + metaInfPath.length() + fileNameLength > (MAX_PATH_LENGTH - BASE_PATH_LENGTH)) {
Comment thread
weidongxu-microsoft marked this conversation as resolved.
// see
// https://github.com/Azure/azure-sdk-for-java/blob/main/eng/common/pipelines/templates/steps/verify-path-length.yml
String shortenedArtifactId = artifactId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,24 @@ public class ClassNameUtilTests {

@Test
public void testTruncateClassName() {
final int maxFileLength = 260 - 38;
final int maxFileLength = ClassNameUtil.MAX_PATH_LENGTH - ClassNameUtil.BASE_PATH_LENGTH;

// truncate class name
// names that previously required truncation now fit
String name = ClassNameUtil.truncateClassName("com.azure.resourcemanager.deviceprovisioningservices",
"src/samples/java", "com.azure.resourcemanager.deviceprovisioningservices.generated",
"IotDpsResourceCheckProvisioningServiceNameAvailability", "Samples");
Assertions.assertEquals(maxFileLength,
("sdk/deviceprovisioningservices/azure-resourcemanager-deviceprovisioningservices/src/samples/java/com/azure/resourcemanager/deviceprovisioningservices/generated/"
+ name + ".java").length());
Assertions.assertEquals("IotDpsResourceCheckProvisioningServiceNameAvailabilitySamples", name);

name = ClassNameUtil.truncateClassName("com.azure.resourcemanager.recoveryservicessiterecovery",
"src/test/java", "com.azure.resourcemanager.recoveryservicessiterecovery.generated",
"InMageRcmUpdateApplianceForReplicationProtectedItemInput", "Tests");
Assertions.assertEquals(maxFileLength,
("sdk/recoveryservicessiterecovery/azure-resourcemanager-recoveryservicessiterecovery/src/test/java/com/azure/resourcemanager/recoveryservicessiterecovery/generated/"
+ name + ".java").length());
Assertions.assertEquals("InMageRcmUpdateApplianceForReplicationProtectedItemInputTests", name);

// truncate a name that exceeds the relaxed limit
name = ClassNameUtil.truncateClassName(
"com.azure.resourcemanager." + KUBERNETES_CONFIGURATION + ".extensiontypes", "src/test/java",
"com.azure.resourcemanager." + KUBERNETES_CONFIGURATION + ".extensiontypes.generated",
"ExtensionTypesLocationGetWithResponse", "MockTests");
"ExtensionTypesLocationGetWithResponseAsync", "MockTests");
Assertions.assertEquals(maxFileLength,
("sdk/" + KUBERNETES_CONFIGURATION + "/azure-resourcemanager-" + KUBERNETES_CONFIGURATION
+ "-extensiontypes/src/test/java/com/azure/resourcemanager/" + KUBERNETES_CONFIGURATION
Expand All @@ -53,12 +50,12 @@ public void testTruncateClassName() {

@Test
public void testGetDirectoryNameForGraalVmConfig() {
// directory length over 210
// directory length over 218
String directoryName = ClassNameUtil.getDirectoryNameForGraalVmConfig("com.azure.resourcemanager",
"azure-resourcemanager-" + KUBERNETES_CONFIGURATION + "-extensiontypes");
"azure-resourcemanager-" + KUBERNETES_CONFIGURATION + "-extensiontypesabc");
Assertions.assertFalse(directoryName.contains("azure-resourcemanager-"));

// directory length not over 210, but full filename length over 222
// reflect-config.json fits exactly at 230, but resource-config.json exceeds the limit by one
directoryName = ClassNameUtil.getDirectoryNameForGraalVmConfig("com.azure.resourcemanager",
"azure-resourcemanager-" + "recovery" + "services" + "data" + "replication");
Assertions.assertFalse(directoryName.contains("azure-resourcemanager-"));
Expand Down
Loading