Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create temp files in a temp directory #450

Closed
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 16 additions & 2 deletions core/src/main/java/org/testcontainers/utility/MountableFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
@Slf4j
public class MountableFile implements Transferable {

private static final String TESTCONTAINERS_TMP_DIR_PREFIX = ".testcontainers-tmp-";
public static final String OS_MAC_TMP_DIR = "/tmp";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be public?


private final String path;

@Getter(lazy = true)
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain the conditions and circumstances, under which you expect this Exception to happen and how this return value solves the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example user runs java with params -Djava.io.tmpdir=<folder> on linux/windows.

In cases, the folder does not exist we will get IOException.

I think we could try to create a temp folder in the current folder as it was before.

Is it make sense?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sounds good.

return new File(TESTCONTAINERS_TMP_DIR_PREFIX + Base58.randomString(5));
}
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private void copyFromJarToLocation(final JarFile jarFile,
final JarEntry entry,
Expand Down Expand Up @@ -305,4 +319,4 @@ private int getUnixFileMode(final String pathAsString) {
return mode;
}
}
}
}