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 14, 2024
1 parent b359dd8 commit 493b175
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 7 deletions.
3 changes: 3 additions & 0 deletions distribution/tools/launchers/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ 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}"
testImplementation "net.bytebuddy:byte-buddy:1.15.4"
testImplementation "net.bytebuddy:byte-buddy-agent:1.15.4"
}

base {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class Launchers {
* @param message the message to print
*/
@SuppressForbidden(reason = "System#out")
static void outPrintln(final String message) {
public static void outPrintln(final String message) {
System.out.println(message);
}

Expand All @@ -60,7 +60,7 @@ static void outPrintln(final String message) {
* @param message the message to print
*/
@SuppressForbidden(reason = "System#err")
static void errPrintln(final String message) {
public static void errPrintln(final String message) {
System.err.println(message);
}

Expand All @@ -70,13 +70,12 @@ static void errPrintln(final String message) {
* @param status the status
*/
@SuppressForbidden(reason = "System#exit")
static void exit(final int status) {
public static void exit(final int status) {
System.exit(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,65 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.tools.launchers;


import com.carrotsearch.randomizedtesting.RandomizedTest;
import org.junit.After;
import org.junit.Test;

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

import static org.junit.Assert.*;

public class LaunchersTests extends LaunchersTestCase {

private Path tempDirectory;

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

@Test
public void testCreateTempDirectory() throws IOException {
String tempDirPrefix = "opensearch-test-" + RandomizedTest.randomAsciiLettersOfLength(7);
tempDirectory = Launchers.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), tempDirPrefix);

assertNotNull("Temporary directory should not be null", tempDirectory);
assertTrue("Temporary directory should exist", Files.exists(tempDirectory));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.tools.launchers;

import com.carrotsearch.randomizedtesting.RandomizedTest;
import org.junit.After;
import org.junit.Test;
import org.mockito.MockedStatic;

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 {

private Path createdTempDir;

@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));

try (MockedStatic<Launchers> mockedLaunchers = mockStatic(Launchers.class)) {
mockedLaunchers.when(() -> Launchers.outPrintln(any(String.class))).thenAnswer(invocation -> null);

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

createdTempDir = Paths.get(System.getProperty("java.io.tmpdir"), "opensearch");
mockedLaunchers.verify(() -> Launchers.outPrintln(createdTempDir.toString()), times(1));
System.out.println("Test passed. Temp Directory exists: " + createdTempDir);
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);

try (MockedStatic<Launchers> mockedLaunchers = mockStatic(Launchers.class)) {
Path mockedTempDir = Paths.get(System.getProperty("java.io.tmpdir"), "opensearch-" + RandomizedTest.randomAsciiLettersOfLength(7));
mockedLaunchers.when(() -> Launchers.createTempDirectory(any(Path.class), eq("opensearch-")))
.thenReturn(mockedTempDir);
mockedLaunchers.when(() -> Launchers.outPrintln(any(String.class))).thenAnswer(invocation -> null);

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

mockedLaunchers.verify(() -> Launchers.createTempDirectory(any(Path.class), eq("opensearch-")), times(1));
mockedLaunchers.verify(() -> Launchers.outPrintln(mockedTempDir.toString()), times(1));
}
}
}

0 comments on commit 493b175

Please sign in to comment.