[tests] temp. file handling in GeneralCommandLineTest

This commit is contained in:
Roman Shevchenko
2017-11-20 18:03:06 +01:00
parent 1f6040f776
commit 2f9ce8adea
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed 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.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.execution;
import com.intellij.execution.configurations.GeneralCommandLine;
@@ -24,10 +10,12 @@ import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.testFramework.rules.TempDirectory;
import com.intellij.util.containers.ContainerUtil;
import org.intellij.lang.annotations.MagicConstant;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
@@ -134,6 +122,8 @@ public class GeneralCommandLineTest {
}
}
@Rule public TempDirectory tempDir = new TempDirectory();
protected GeneralCommandLine createCommandLine(String... command) {
return new GeneralCommandLine(command);
}
@@ -174,42 +164,25 @@ public class GeneralCommandLineTest {
@Test(timeout = 60000)
public void unicodePath() throws Exception {
String mark = String.valueOf(new Random().nextInt());
File script = createTempScript("spaces 'and quotes' and " + UNICODE_RU + "_" + UNICODE_EU + " ",
"@echo " + mark + "\n",
"#!/bin/sh\n" + "echo " + mark + "\n");
String command = SystemInfo.isWindows ? "@echo " + mark + '\n' : "#!/bin/sh\necho " + mark + '\n';
File script = ExecUtil.createTempExecutableScript("spaces 'and quotes' and " + UNICODE_RU + "_" + UNICODE_EU + " ", ".cmd", command);
try {
String output = execAndGetOutput(createCommandLine(script.getPath()));
assertEquals(mark + "\n", StringUtil.convertLineSeparators(output));
assertEquals(mark + '\n', StringUtil.convertLineSeparators(output));
}
finally {
FileUtil.delete(script);
}
}
@NotNull
private static File createTempScript(@NotNull String scriptNamePrefix,
@NotNull String winScriptContent,
@NotNull String unixScriptContent) throws IOException, ExecutionException {
if (SystemInfo.isWindows) {
return ExecUtil.createTempExecutableScript(scriptNamePrefix, ".cmd", winScriptContent);
}
return ExecUtil.createTempExecutableScript(scriptNamePrefix, ".sh", unixScriptContent);
}
@Test(timeout = 60000)
public void unicodeClassPath() throws Exception {
assumeTrue(UNICODE != null);
File dir = FileUtil.createTempDirectory("spaces 'and quotes' and " + UNICODE, ".tmp");
try {
Pair<GeneralCommandLine, File> command = makeHelperCommand(dir, CommandTestHelper.ARG, "test");
String output = execHelper(command);
assertEquals("test\n", StringUtil.convertLineSeparators(output));
}
finally {
FileUtil.delete(dir);
}
File dir = tempDir.newFolder("spaces 'and quotes' and " + UNICODE);
Pair<GeneralCommandLine, File> command = makeHelperCommand(dir, CommandTestHelper.ARG, "test");
String output = execHelper(command);
assertEquals("test\n", StringUtil.convertLineSeparators(output));
}
@Test(timeout = 60000)
@@ -298,8 +271,7 @@ public class GeneralCommandLineTest {
@Test(timeout = 60000)
public void passingArgumentsToCygwinPrintf() throws Exception {
assumeTrue(SystemInfo.isWindows);
File cygwinPrintf = FileUtil.findFirstThatExist("C:\\cygwin\\bin\\printf.exe",
"C:\\cygwin64\\bin\\printf.exe");
File cygwinPrintf = FileUtil.findFirstThatExist("C:\\cygwin\\bin\\printf.exe", "C:\\cygwin64\\bin\\printf.exe");
assumeNotNull(cygwinPrintf);
for (String argument : ARGUMENTS) {
@@ -361,11 +333,10 @@ public class GeneralCommandLineTest {
@Test(timeout = 60000)
public void redirectInput() throws Exception {
String content = "Line 1\nLine 2\n";
File input = FileUtil.createTempFile("input", null);
File input = tempDir.newFile("input");
FileUtil.writeToFile(input, content);
File script = createTempScript("print-stdin",
"@echo off\n" + "findstr \"^\"\n",
"#!/bin/sh\n" + "cat\n");
String command = SystemInfo.isWindows ? "@echo off\nfindstr \"^\"\n" : "#!/bin/sh\ncat\n";
File script = ExecUtil.createTempExecutableScript("print-stdin", ".cmd", command);
try {
GeneralCommandLine commandLine = createCommandLine(script.getPath()).withInput(input);
String output = execAndGetOutput(commandLine);
@@ -373,7 +344,6 @@ public class GeneralCommandLineTest {
}
finally {
FileUtil.delete(script);
FileUtil.delete(input);
}
}
@@ -471,7 +441,7 @@ public class GeneralCommandLineTest {
commandLine.addParameter(className);
File out = FileUtil.createTempFile("test.", ".out");
File out = tempDir.newFile("test_output");
commandLine.addParameters(mode, CommandTestHelper.OUT, out.getPath());
commandLine.addParameters(args);
@@ -480,13 +450,8 @@ public class GeneralCommandLineTest {
}
private String execHelper(Pair<GeneralCommandLine, File> pair) throws IOException, ExecutionException {
try {
execAndGetOutput(pair.first);
return FileUtil.loadFile(pair.second, CommandTestHelper.ENC);
}
finally {
FileUtil.delete(pair.second);
}
execAndGetOutput(pair.first);
return FileUtil.loadFile(pair.second, CommandTestHelper.ENC);
}
private static void checkParamPassing(String output, String... expected) {
@@ -529,4 +494,4 @@ public class GeneralCommandLineTest {
fail("% missed: " + pctMissed + ", missed: " + missed + ", passed: " + lines);
}
}
}
}