Copilot commented on code in PR #37470: URL: https://github.com/apache/shardingsphere/pull/37470#discussion_r2642285579
########## proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/util/ExportUtilsTest.java: ########## @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.proxy.backend.util; + +import org.apache.shardingsphere.infra.exception.generic.FileIOException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.UUID; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ExportUtilsTest { + + @TempDir + private Path tempDir; + + @Test + void assertExportToFileCreatesParentAndWritesContent() throws IOException { + Path parentPath = tempDir.resolve("child"); + Path filePath = parentPath.resolve("config.yaml"); + String exportedData = "foo: bar"; + ExportUtils.exportToFile(filePath.toString(), exportedData); + assertTrue(Files.exists(parentPath)); Review Comment: The assertion only verifies that the parent path exists, but doesn't verify that it's actually a directory. Consider using Files.isDirectory(parentPath) instead of or in addition to Files.exists(parentPath) to ensure the test validates that the parent directory was correctly created. ```suggestion assertTrue(Files.isDirectory(parentPath)); ``` ########## proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/util/ExportUtilsTest.java: ########## @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.proxy.backend.util; + +import org.apache.shardingsphere.infra.exception.generic.FileIOException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.UUID; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ExportUtilsTest { + + @TempDir + private Path tempDir; + + @Test + void assertExportToFileCreatesParentAndWritesContent() throws IOException { + Path parentPath = tempDir.resolve("child"); + Path filePath = parentPath.resolve("config.yaml"); + String exportedData = "foo: bar"; + ExportUtils.exportToFile(filePath.toString(), exportedData); + assertTrue(Files.exists(parentPath)); + assertThat(new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8), is(exportedData)); + } + + @Test + void assertExportToFileThrowsFileIOExceptionWhenPathIsDirectory() throws IOException { + Path directoryPath = Files.createDirectory(tempDir.resolve("targetDir")); + FileIOException actualException = assertThrows(FileIOException.class, () -> ExportUtils.exportToFile(directoryPath.toString(), "any")); + assertThat(actualException.getMessage(), is(String.format("File access failed, file is: %s", directoryPath.toFile().getAbsolutePath()))); + } + + @Test + void assertExportToFileWithoutParentDirectory() throws IOException { + Path filePath = Paths.get(String.format("export-%s.tmp", UUID.randomUUID())); + String exportedData = "baz"; + try { + ExportUtils.exportToFile(filePath.toString(), exportedData); + assertThat(new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8), is(exportedData)); + } finally { + Files.deleteIfExists(filePath); + } + } Review Comment: This test creates a file in the current working directory (outside of the @TempDir), which can pollute the working directory and may cause issues in CI/CD environments or when tests run in parallel. Consider using tempDir.resolve() instead of Paths.get() to ensure the file is created within the temporary directory and automatically cleaned up. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
