Skip to content

Commit

Permalink
Ensure Files.createTempDirectory honors java.io.tmpdir system property
Browse files Browse the repository at this point in the history
Signed-off-by: Arun Gopalpuri <[email protected]>
  • Loading branch information
arun0009 committed Dec 11, 2024
1 parent 5aa6509 commit 0ef02ed
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions distribution/tools/launchers/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
compileOnly project(':distribution:tools:java-version-checker')
testImplementation "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"
testImplementation "junit:junit:${versions.junit}"
testImplementation "org.mockito:mockito-core:${versions.mockito}"
testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ static void exit(final int status) {
}

@SuppressForbidden(reason = "Files#createTempDirectory(String, FileAttribute...)")
static Path createTempDirectory(final String prefix, final FileAttribute<?>... attrs) throws IOException {
return Files.createTempDirectory(prefix, attrs);
static Path createTempDirectory(final Path rootDir, final String prefix, final FileAttribute<?>... attrs) throws IOException {
return Files.createTempDirectory(rootDir, prefix, attrs);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void main(final String[] args) throws IOException {
path = Paths.get(System.getProperty("java.io.tmpdir"), "opensearch");
Files.createDirectories(path);
} else {
path = Launchers.createTempDirectory("opensearch-");
path = Launchers.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), "opensearch-");
}
Launchers.outPrintln(path.toString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.opensearch.tools.launchers;

import com.carrotsearch.randomizedtesting.RandomizedTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

public class TempDirectoryTests extends LaunchersTestCase {

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

private Path createdTempDir;

@Before
public void setUp() throws IOException{
System.setProperty("java.io.tmpdir", tempFolder.newFolder().toString());
}

@After
public void tearDown() throws IOException {
if (createdTempDir != null && Files.exists(createdTempDir)) {
Files.delete(createdTempDir);
}
}

@Test
public void testMainWithArguments() {
String[] args = {RandomizedTest.randomAsciiLettersOfLengthBetween(1, 10)};
assertThrows(IllegalArgumentException.class, () -> TempDirectory.main(args));
}

@Test
public void testMainOnWindows() throws IOException {
System.setProperty("os.name", "Windows " + RandomizedTest.randomIntBetween(7, 10));
Launchers mockLaunchers = mock(Launchers.class);
doNothing().when(mockLaunchers).outPrintln(any(String.class));

TempDirectory.main(new String[]{});

createdTempDir = Paths.get(System.getProperty("java.io.tmpdir"), "opensearch");
verify(mockLaunchers, times(1)).outPrintln(createdTempDir.toString());
assert Files.exists(createdTempDir);
}

@Test
public void testMainOnNonWindows() throws IOException {
String [] osNames = {"Linux", "Mac OS X", "Unix"};
String osName = RandomizedTest.randomFrom(osNames);
System.setProperty("os.name", osName);
Launchers mockLaunchers = mock(Launchers.class);
doNothing().when(mockLaunchers).outPrintln(any(String.class));
when(mockLaunchers.createTempDirectory(any(Path.class), any(String.class))).thenReturn(tempFolder.newFolder().toPath().resolve("opensearch-" + RandomizedTest.randomAsciiLettersOfLength(3)));

TempDirectory.main(new String[]{});

createdTempDir = tempFolder.newFolder().toPath().resolve("opensearch-" + RandomizedTest.randomAsciiLettersOfLength(3));
verify(mockLaunchers, times(1)).outPrintln(createdTempDir.toString());
assert Files.exists(createdTempDir);
}
}

0 comments on commit 0ef02ed

Please sign in to comment.