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

Ensure Files.createTempDirectory honors java.io.tmpdir system property #16832

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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,66 @@
/*
* 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.assertNotNull;
import static org.junit.Assert.assertTrue;

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,107 @@
/*
* 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.opensearch.tools.java_version_checker.SuppressForbidden;
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 org.mockito.MockedStatic;

import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;

@SuppressForbidden(reason = "modifies system properties to test different O.S")
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));
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));
}
}
}
Loading