1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 01:15:29 +02:00

Bug 515374: Stop using user.home as a temporary directory in tests

Change-Id: I20533ba4910ae9d691f51533183b4f475eaa5bbc
This commit is contained in:
Jonah Graham 2017-04-18 17:14:11 +01:00 committed by Gerrit Code Review @ Eclipse.org
parent a06ad70d0a
commit 0fc24a0c78
4 changed files with 126 additions and 193 deletions

View file

@ -177,106 +177,4 @@ public class StandardBuildTestHelper {
}
return buff;
}
static public IPath copyFilesToTempDir(IPath srcDir, IPath tmpSubDir, IPath[] files) {
IPath tmpSrcDir = null;
String userDirStr = System.getProperty("user.home");
if (userDirStr != null) {
IPath userDir = Path.fromOSString(userDirStr);
tmpSrcDir = userDir.append(tmpSubDir);
if (userDir.toString().equalsIgnoreCase(tmpSrcDir.toString())) {
Assert.fail("Temporary sub-directory cannot be the empty string.");
} else {
File tmpSrcDirFile = tmpSrcDir.toFile();
if (tmpSrcDirFile.exists()) {
// Make sure that this is the expected directory before we delete it...
if (tmpSrcDir.lastSegment().equals(tmpSubDir.lastSegment())) {
deleteDirectory(tmpSrcDirFile);
} else {
Assert.fail("Temporary directory " + tmpSrcDirFile.toString() + " already exists.");
}
}
boolean succeed = tmpSrcDirFile.mkdir();
if (succeed) {
for (int i=0; i<files.length; i++) {
IPath file = files[i];
IPath srcFile = srcDir.append(file);
FileReader srcReader = null;
try {
srcReader = new FileReader(srcFile.toFile());
} catch (Exception e) {
Assert.fail("File " + file.toString() + " could not be read.");
}
if (file.segmentCount() > 1) {
IPath newDir = tmpSrcDir;
do {
IPath dir = file.uptoSegment(1);
newDir = newDir.append(dir);
file = file.removeFirstSegments(1);
succeed = newDir.toFile().mkdir();
} while (file.segmentCount() > 1);
}
IPath destFile = tmpSrcDir.append(files[i]);
FileWriter writer = null;
try {
writer = new FileWriter(destFile.toFile());
} catch (Exception e) {
Assert.fail("File " + files[i].toString() + " could not be written.");
}
try {
int c;
do {
c = srcReader.read();
if (c == -1) break;
writer.write(c);
} while (c != -1);
srcReader.close();
writer.close();
} catch (Exception e) {
Assert.fail("File " + file.toString() + " could not be copied.");
}
}
}
}
}
return tmpSrcDir;
}
static public void deleteTempDir(IPath tmpSubDir, IPath[] files) {
IPath tmpSrcDir = null;
String userDirStr = System.getProperty("user.home");
if (userDirStr != null) {
IPath userDir = Path.fromOSString(userDirStr);
tmpSrcDir = userDir.append(tmpSubDir);
if (userDir.toString().equalsIgnoreCase(tmpSrcDir.toString())) {
Assert.fail("Temporary sub-directory cannot be the empty string.");
} else {
File tmpSrcDirFile = tmpSrcDir.toFile();
if (!tmpSrcDirFile.exists()) {
Assert.fail("Temporary directory " + tmpSrcDirFile.toString() + " does not exist.");
} else {
for (int i=0; i<files.length; i++) {
// Delete the file
IPath thisFile = tmpSrcDir.append(files[i]);
thisFile.toFile().delete();
}
// Delete the dir
tmpSrcDirFile.delete();
}
}
}
}
static private void deleteDirectory(File dir) {
File[] toDelete = dir.listFiles();
for (int i=0; i<toDelete.length; i++) {
File fileToDelete = toDelete[i];
if (fileToDelete.isDirectory()) {
deleteDirectory(fileToDelete);
}
fileToDelete.delete();
}
dir.delete();
}
}

View file

@ -872,73 +872,70 @@ public class ManagedBuildTestHelper {
return buff;
}
static public IPath copyFilesToTempDir(IPath srcDir, IPath tmpSubDir, IPath[] files) {
static public IPath copyFilesToTempDir(IPath srcDir, IPath tmpRootDir, IPath tmpSubDir, IPath[] files) {
IPath tmpSrcDir = null;
String userDirStr = System.getProperty("user.home");
if (userDirStr != null) {
IPath userDir = Path.fromOSString(userDirStr);
tmpSrcDir = userDir.append(tmpSubDir);
if (userDir.toString().equalsIgnoreCase(tmpSrcDir.toString())) {
Assert.fail("Temporary sub-directory cannot be the empty string.");
} else {
File tmpSrcDirFile = tmpSrcDir.toFile();
if (tmpSrcDirFile.exists()) {
// Make sure that this is the expected directory before we delete it...
if (tmpSrcDir.lastSegment().equals(tmpSubDir.lastSegment())) {
deleteDirectory(tmpSrcDirFile);
} else {
Assert.fail("Temporary directory " + tmpSrcDirFile.toString() + " already exists.");
}
tmpSrcDir = tmpRootDir.append(tmpSubDir);
if (tmpRootDir.toString().equalsIgnoreCase(tmpSrcDir.toString())) {
Assert.fail("Temporary sub-directory cannot be the empty string.");
} else {
File tmpSrcDirFile = tmpSrcDir.toFile();
if (tmpSrcDirFile.exists()) {
// Make sure that this is the expected directory before we delete it...
if (tmpSrcDir.lastSegment().equals(tmpSubDir.lastSegment())) {
deleteDirectory(tmpSrcDirFile);
} else {
Assert.fail("Temporary directory " + tmpSrcDirFile.toString() + " already exists.");
}
tmpSrcDirFile.mkdir();
if (!tmpSrcDirFile.exists()) {
Assert.fail("Can't create temporary directory " + tmpSrcDirFile.toString());
}
tmpSrcDirFile.mkdir();
if (!tmpSrcDirFile.exists()) {
Assert.fail("Can't create temporary directory " + tmpSrcDirFile.toString());
}
for (int i=0; i<files.length; i++) {
IPath file = files[i];
IPath srcFile = srcDir.append(file);
FileReader srcReader = null;
try {
srcReader = new FileReader(srcFile.toFile());
} catch (Exception e) {
Assert.fail("File " + file.toString() + " could not be read.");
return null;
}
for (int i=0; i<files.length; i++) {
IPath file = files[i];
IPath srcFile = srcDir.append(file);
FileReader srcReader = null;
try {
srcReader = new FileReader(srcFile.toFile());
} catch (Exception e) {
Assert.fail("File " + file.toString() + " could not be read.");
return null;
}
if (file.segmentCount() > 1) {
IPath newDir = tmpSrcDir;
do {
IPath dir = file.uptoSegment(1);
newDir = newDir.append(dir);
file = file.removeFirstSegments(1);
newDir.toFile().mkdir();
if (!newDir.toFile().exists()) {
Assert.fail("Can't create temporary directory " + tmpSrcDirFile.toString());
}
} while (file.segmentCount() > 1);
}
IPath destFile = tmpSrcDir.append(files[i]);
FileWriter writer = null;
try {
writer = new FileWriter(destFile.toFile());
} catch (Exception e) {
Assert.fail("File " + files[i].toString() + " could not be written.");
return null;
}
try {
int c;
do {
c = srcReader.read();
if (c == -1) break;
writer.write(c);
} while (c != -1);
srcReader.close();
writer.close();
} catch (Exception e) {
Assert.fail("File " + file.toString() + " could not be copied.");
}
if (file.segmentCount() > 1) {
IPath newDir = tmpSrcDir;
do {
IPath dir = file.uptoSegment(1);
newDir = newDir.append(dir);
file = file.removeFirstSegments(1);
newDir.toFile().mkdir();
if (!newDir.toFile().exists()) {
Assert.fail("Can't create temporary directory " + tmpSrcDirFile.toString());
}
} while (file.segmentCount() > 1);
}
IPath destFile = tmpSrcDir.append(files[i]);
FileWriter writer = null;
try {
writer = new FileWriter(destFile.toFile());
} catch (Exception e) {
Assert.fail("File " + files[i].toString() + " could not be written.");
return null;
}
try {
int c;
do {
c = srcReader.read();
if (c == -1) break;
writer.write(c);
} while (c != -1);
srcReader.close();
writer.close();
} catch (Exception e) {
Assert.fail("File " + file.toString() + " could not be copied.");
}
}
}
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
for (IFile rc : root.findFilesForLocation(tmpSrcDir)) {
@ -951,29 +948,28 @@ public class ManagedBuildTestHelper {
return tmpSrcDir;
}
static public void deleteTempDir(IPath tmpSubDir, IPath[] files) {
static public void deleteTempDir(IPath tmpRootDir, IPath tmpSubDir, IPath[] files) {
IPath tmpSrcDir = null;
String userDirStr = System.getProperty("user.home");
if (userDirStr != null) {
IPath userDir = Path.fromOSString(userDirStr);
tmpSrcDir = userDir.append(tmpSubDir);
if (userDir.toString().equalsIgnoreCase(tmpSrcDir.toString())) {
Assert.fail("Temporary sub-directory cannot be the empty string.");
} else {
File tmpSrcDirFile = tmpSrcDir.toFile();
if (tmpSrcDirFile.exists()) {
for (int i=0; i<files.length; i++) {
// Delete the file
IPath thisFile = tmpSrcDir.append(files[i]);
thisFile.toFile().delete();
}
// Delete the dir
tmpSrcDirFile.delete();
tmpSrcDir = tmpRootDir.append(tmpSubDir);
if (tmpRootDir.toString().equalsIgnoreCase(tmpSrcDir.toString())) {
Assert.fail("Temporary sub-directory cannot be the empty string.");
} else {
File tmpSrcDirFile = tmpSrcDir.toFile();
if (tmpSrcDirFile.exists()) {
for (int i=0; i<files.length; i++) {
// Delete the file
IPath thisFile = tmpSrcDir.append(files[i]);
thisFile.toFile().delete();
}
// Delete the dir
tmpSrcDirFile.delete();
}
tmpRootDir.toFile().delete();
}
}
/*
* Cloned from core CProjectHelper
*/
@ -1209,4 +1205,5 @@ public class ManagedBuildTestHelper {
return false;
}
}

View file

@ -19,6 +19,8 @@ package org.eclipse.cdt.managedbuilder.core.tests;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import junit.framework.Test;
@ -47,7 +49,7 @@ public class ManagedProject21MakefileTests extends TestCase {
private IPath resourcesLocation = new Path(CTestPlugin.getFileInPlugin(new Path("resources/test21Projects/")).getAbsolutePath());
public static final String MBS_TEMP_DIR = "MBSTemp";
static boolean pathVariableCreated = false;
boolean pathVariableCreated = false;
public ManagedProject21MakefileTests(String name) {
super(name);
@ -68,6 +70,12 @@ public class ManagedProject21MakefileTests extends TestCase {
return suite;
}
@Override
protected void tearDown() throws Exception {
removePathVariables();
super.tearDown();
}
private IProject[] createProject(String projName, IPath location, String projectTypeId, boolean containsZip){
Path path = new Path("resources/test21Projects/" + projName);
File testDir = CTestPlugin.getFileInPlugin(path);
@ -198,6 +206,13 @@ public class ManagedProject21MakefileTests extends TestCase {
} catch (Exception e) {fail("could not create the path variable " + name);}
}
private void removePathVariables() throws CoreException {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
workspace = ResourcesPlugin.getWorkspace();
IPathVariableManager pathMan = workspace.getPathVariableManager();
pathMan.setValue(MBS_TEMP_DIR, null);
}
private void createFileLink(IProject project, IPath tmpDir, String linkName, String fileName) {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
String name = MBS_TEMP_DIR;
@ -265,7 +280,7 @@ public class ManagedProject21MakefileTests extends TestCase {
/* (non-Javadoc)
* tests 2.1 style tool integration for linked files
*/
public void testLinkedLib(){
public void testLinkedLib() throws IOException{
boolean succeeded = false;
IPath[] makefiles = {
Path.fromOSString("makefile"),
@ -278,8 +293,10 @@ public class ManagedProject21MakefileTests extends TestCase {
Path.fromOSString("test_ar.h")};
File srcDirFile = CTestPlugin.getFileInPlugin(new Path("resources/test21Projects/linkedLib/"));
IPath srcDir = Path.fromOSString(srcDirFile.toString());
IPath tmpRootDir = Path.fromOSString(
Files.createTempDirectory("testLinkedLib").toAbsolutePath().toString());
IPath tmpSubDir = Path.fromOSString("CDTMBSTest");
IPath tmpDir = ManagedBuildTestHelper.copyFilesToTempDir(srcDir, tmpSubDir, linkedFiles);
IPath tmpDir = ManagedBuildTestHelper.copyFilesToTempDir(srcDir, tmpRootDir, tmpSubDir, linkedFiles);
try {
IProject[] projects = createProjects("linkedLib", null, "cdt.managedbuild.target.testgnu21.lib", true);
// There should be only one project. Add our linked files to it.
@ -291,14 +308,14 @@ public class ManagedProject21MakefileTests extends TestCase {
succeeded = buildProjects("linkedLib", projects, makefiles);
} finally {
if (succeeded)
ManagedBuildTestHelper.deleteTempDir(tmpSubDir, linkedFiles);
ManagedBuildTestHelper.deleteTempDir(tmpRootDir, tmpSubDir, linkedFiles);
}
}
/* (non-Javadoc)
* tests 2.1 style tool integration for a linked folder
*/
public void testLinkedFolder(){
public void testLinkedFolder() throws IOException{
boolean succeeded = false;
IPath[] makefiles = {
Path.fromOSString("makefile"),
@ -316,7 +333,9 @@ public class ManagedProject21MakefileTests extends TestCase {
File srcDirFile = CTestPlugin.getFileInPlugin(new Path("resources/test21Projects/linkedFolder/"));
IPath srcDir = Path.fromOSString(srcDirFile.toString());
IPath tmpSubDir = Path.fromOSString("CDTMBSTest");
IPath tmpDir = ManagedBuildTestHelper.copyFilesToTempDir(srcDir, tmpSubDir, linkedFiles);
IPath tmpRootDir = Path.fromOSString(
Files.createTempDirectory("testLinkedFolder").toAbsolutePath().toString());
IPath tmpDir = ManagedBuildTestHelper.copyFilesToTempDir(srcDir, tmpRootDir, tmpSubDir, linkedFiles);
if (!pathVariableCreated) {
createPathVariable(tmpDir);
pathVariableCreated = true;

View file

@ -17,6 +17,8 @@ package org.eclipse.cdt.managedbuilder.core.tests;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import org.eclipse.cdt.managedbuilder.core.IAdditionalInput;
@ -55,7 +57,7 @@ import junit.framework.TestSuite;
public class ManagedProject30MakefileTests extends TestCase {
public static final String MBS_TEMP_DIR = "MBSTemp";
static boolean pathVariableCreated = false;
boolean pathVariableCreated = false;
private IPath resourcesLocation = new Path(CTestPlugin.getFileInPlugin(new Path("resources/test30Projects/")).getAbsolutePath());
@ -63,6 +65,12 @@ public class ManagedProject30MakefileTests extends TestCase {
super(name);
}
@Override
protected void tearDown() throws Exception {
removePathVariables();
super.tearDown();
}
public static Test suite() {
TestSuite suite = new TestSuite(ManagedProject30MakefileTests.class.getName());
@ -238,6 +246,13 @@ public class ManagedProject30MakefileTests extends TestCase {
} catch (Exception e) {fail("could not create the path variable " + name);}
}
private void removePathVariables() throws CoreException {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
workspace = ResourcesPlugin.getWorkspace();
IPathVariableManager pathMan = workspace.getPathVariableManager();
pathMan.setValue(MBS_TEMP_DIR, null);
}
private void createFileLink(IProject project, IPath tmpDir, String linkName, String fileName) {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
String name = MBS_TEMP_DIR;
@ -309,7 +324,7 @@ public class ManagedProject30MakefileTests extends TestCase {
/* (non-Javadoc)
* tests 3.0 style tool integration for linked files
*/
public void test30LinkedLib(){
public void test30LinkedLib() throws IOException{
IPath[] makefiles = {
Path.fromOSString("makefile"),
Path.fromOSString("objects.mk"),
@ -321,8 +336,10 @@ public class ManagedProject30MakefileTests extends TestCase {
Path.fromOSString("test_ar_30.h")};
File srcDirFile = CTestPlugin.getFileInPlugin(new Path("resources/test30Projects/linkedLib30/"));
IPath srcDir = Path.fromOSString(srcDirFile.toString());
IPath tmpRootDir = Path.fromOSString(
Files.createTempDirectory("test30LinkedLib").toAbsolutePath().toString());
IPath tmpSubDir = Path.fromOSString("CDTMBSTest");
IPath tmpDir = ManagedBuildTestHelper.copyFilesToTempDir(srcDir, tmpSubDir, linkedFiles);
IPath tmpDir = ManagedBuildTestHelper.copyFilesToTempDir(srcDir, tmpRootDir, tmpSubDir, linkedFiles);
try {
IProject[] projects = createProjects("linkedLib30", null, "cdt.managedbuild.target.gnu30.lib", true);
// There should be only one project. Add our linked files to it.
@ -332,13 +349,13 @@ public class ManagedProject30MakefileTests extends TestCase {
createFileLink(project, tmpDir, "test_ar_30.h", "test_ar_30.h");
// Build the project
buildProjects(projects, makefiles);
} finally {ManagedBuildTestHelper.deleteTempDir(tmpSubDir, linkedFiles);}
} finally {ManagedBuildTestHelper.deleteTempDir(tmpRootDir, tmpSubDir, linkedFiles);}
}
/* (non-Javadoc)
* tests 3.0 style tool integration for a linked folder
*/
public void test30LinkedFolder(){
public void test30LinkedFolder() throws IOException{
IPath[] makefiles = {
Path.fromOSString("makefile"),
Path.fromOSString("objects.mk"),
@ -354,8 +371,10 @@ public class ManagedProject30MakefileTests extends TestCase {
Path.fromOSString("Benchmarks/sources.mk")};
File srcDirFile = CTestPlugin.getFileInPlugin(new Path("resources/test30Projects/linkedFolder/"));
IPath srcDir = Path.fromOSString(srcDirFile.toString());
IPath tmpRootDir = Path.fromOSString(
Files.createTempDirectory("test30LinkedFolder").toAbsolutePath().toString());
IPath tmpSubDir = Path.fromOSString("CDTMBSTest");
IPath tmpDir = ManagedBuildTestHelper.copyFilesToTempDir(srcDir, tmpSubDir, linkedFiles);
IPath tmpDir = ManagedBuildTestHelper.copyFilesToTempDir(srcDir, tmpRootDir, tmpSubDir, linkedFiles);
if (!pathVariableCreated) {
createPathVariable(tmpDir);
pathVariableCreated = true;
@ -365,7 +384,7 @@ public class ManagedProject30MakefileTests extends TestCase {
IProject[] projects = createProjects("linkedFolder", location, "cdt.managedbuild.target.gnu30.lib", false);
// Build the project
buildProjects(projects, makefiles);
} finally {ManagedBuildTestHelper.deleteTempDir(tmpSubDir, linkedFiles);}
} finally {ManagedBuildTestHelper.deleteTempDir(tmpRootDir, tmpSubDir, linkedFiles);}
}
/* (non-Javadoc)