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

Replaced the creation of the temporary directory using the @TempDir JUnit annotation #2799

Merged
merged 3 commits into from
Mar 12, 2024
Merged
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
Expand Up @@ -51,6 +51,9 @@
*/
class DockerFileUtilTest {

@TempDir
private File tempDir;

@Test
void simple() throws Exception {
File toTest = copyToTempDir("Dockerfile_from_simple");
Expand Down Expand Up @@ -81,9 +84,8 @@ static Stream<Arguments> dockerFiles() {
}

private File copyToTempDir(String resource) throws IOException {
File dir = Files.createTempDirectory("d-m-p").toFile();
File ret = new File(dir, "Dockerfile");
try (OutputStream os = new FileOutputStream(ret)) {
File ret = new File(tempDir, "Dockerfile");
try (OutputStream os = Files.newOutputStream(ret.toPath())) {
FileUtils.copyFile(new File(getClass().getResource(resource).getPath()), os);
}
return ret;
Expand Down Expand Up @@ -273,10 +275,11 @@ void findAllArgs_fromInvalidArg_shouldBeEmpty() {
}

@Test
void readDockerConfig_fromUserDir(@TempDir Path home) throws IOException {
void readDockerConfig_fromUserDir() throws IOException {
final String original = System.getProperty("user.home");
try {
// Given
Path home = tempDir.toPath();
System.setProperty("user.home", home.toFile().getAbsolutePath());
Files.createDirectories(home.resolve(".docker"));
Files.write(home.resolve(".docker").resolve("config.json"),
Expand All @@ -292,10 +295,11 @@ void readDockerConfig_fromUserDir(@TempDir Path home) throws IOException {
}

@Test
void readDockerConfig_fromEnvVariable(@TempDir Path dockerConfig) throws IOException {
void readDockerConfig_fromEnvVariable() throws IOException {
final String original = System.getProperty("user.home");
try {
// Given
Path dockerConfig = tempDir.toPath();
final Map<String, String> env = Collections.singletonMap("DOCKER_CONFIG", dockerConfig.toFile().getAbsolutePath());
EnvUtil.overrideEnvGetter(env::get);
System.clearProperty("user.home");
Expand All @@ -311,11 +315,11 @@ void readDockerConfig_fromEnvVariable(@TempDir Path dockerConfig) throws IOExcep
}
}
@Test
void readDockerConfig_fromMissing(@TempDir Path home) {
void readDockerConfig_fromMissing() {
final String original = System.getProperty("user.home");
try {
// Given
System.setProperty("user.home", home.toFile().getAbsolutePath());
System.setProperty("user.home", tempDir.toPath().toFile().getAbsolutePath());
// When
final Map<String, Object> config = DockerFileUtil.readDockerConfig();
// Then
Expand Down