diff --git a/CHANGELOG.md b/CHANGELOG.md index 087c2d3d68b..4130fca304f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. - Fixed TAR composition on Windows (#444) - Allowing `addExposedPort` to be used after ports have been specified with `withExposedPorts` (#453) - Stopping creation of temporary directory prior to creating temporary file (#443) +- Fixed temp files should be created in a temp directory (#423) ### Changed - Added `forResponsePredicate` method to HttpWaitStrategy to test response body (#441) diff --git a/core/src/main/java/org/testcontainers/utility/MountableFile.java b/core/src/main/java/org/testcontainers/utility/MountableFile.java index 06d08c2996d..71764194c1e 100644 --- a/core/src/main/java/org/testcontainers/utility/MountableFile.java +++ b/core/src/main/java/org/testcontainers/utility/MountableFile.java @@ -33,6 +33,9 @@ @Slf4j public class MountableFile implements Transferable { + private static final String TESTCONTAINERS_TMP_DIR_PREFIX = ".testcontainers-tmp-"; + private static final String OS_MAC_TMP_DIR = "/tmp"; + private final String path; @Getter(lazy = true) @@ -163,7 +166,7 @@ private String getResourcePath() { * @return the path of the temporary file/directory */ private String extractClassPathResourceToTempLocation(final String hostPath) { - File tmpLocation = new File(".testcontainers-tmp-" + Base58.randomString(5)); + File tmpLocation = createTempDirectory(); //noinspection ResultOfMethodCallIgnored tmpLocation.delete(); @@ -194,6 +197,17 @@ private String extractClassPathResourceToTempLocation(final String hostPath) { return tmpLocation.getAbsolutePath(); } + private File createTempDirectory() { + try { + if (SystemUtils.IS_OS_MAC) { + return Files.createTempDirectory(Paths.get(OS_MAC_TMP_DIR), TESTCONTAINERS_TMP_DIR_PREFIX).toFile(); + } + return Files.createTempDirectory(TESTCONTAINERS_TMP_DIR_PREFIX).toFile(); + } catch (IOException e) { + return new File(TESTCONTAINERS_TMP_DIR_PREFIX + Base58.randomString(5)); + } + } + @SuppressWarnings("ResultOfMethodCallIgnored") private void copyFromJarToLocation(final JarFile jarFile, final JarEntry entry, @@ -305,4 +319,4 @@ private int getUnixFileMode(final String pathAsString) { return mode; } } -} \ No newline at end of file +}