Skip to content

Commit

Permalink
Prevent the unit tests from using ~/.lemminx or ~/cache
Browse files Browse the repository at this point in the history
  • Loading branch information
datho7561 committed Sep 6, 2022
1 parent b095903 commit 1fb2a20
Show file tree
Hide file tree
Showing 95 changed files with 322 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ public class CacheResourcesManager {
private static final String CACHE_PATH = "cache";
private static final Logger LOGGER = Logger.getLogger(CacheResourcesManager.class.getName());

private static final Path TEMP_DOWNLOAD_DIR;

static {
Path tempDownloadDir = null;
try {
tempDownloadDir = Files.createTempDirectory("lemminx-temp");
} catch (Exception e) {
}
TEMP_DOWNLOAD_DIR = tempDownloadDir;
}

private final Map<String, CompletableFuture<Path>> resourcesLoading;
private boolean useCache;

Expand Down Expand Up @@ -222,7 +233,7 @@ private CompletableFuture<Path> downloadResource(final String resourceURI, Path
}

// Download resource in a temporary file
Path path = Files.createTempFile(resourceCachePath.getFileName().toString(), ".lemminx");
Path path = Files.createTempFile(TEMP_DOWNLOAD_DIR, resourceCachePath.getFileName().toString(), ".lemminx");
try (ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
FileOutputStream fos = new FileOutputStream(path.toFile())) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
Expand Down Expand Up @@ -369,7 +380,7 @@ public boolean isUseCache() {

/**
* Returns true if the external resources can be downloaded and false otherwise.
*
*
* @return true if the external resources can be downloaded and false otherwise.
*/
public boolean isDownloadExternalResources() {
Expand All @@ -378,7 +389,7 @@ public boolean isDownloadExternalResources() {

/**
* Set true if the external resources can be downloaded and false otherwise.
*
*
* @param downloadExternalResources the external resources
*/
public void setDownloadExternalResources(boolean downloadExternalResources) {
Expand Down Expand Up @@ -463,7 +474,7 @@ private void addDefaultProtocolsForCache() {

/**
* Force the given <code>url</code> to download.
*
*
* @param url the url to download.
*/
public void forceDownloadExternalResource(String url) {
Expand All @@ -473,7 +484,7 @@ public void forceDownloadExternalResource(String url) {
/**
* Returns true if the given <code>url</code> can be downloaded and false
* otherwise.
*
*
* @param url the url to download.
* @return true if the given <code>url</code> can be downloaded and false
* otherwise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,51 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import com.google.common.io.MoreFiles;
import com.google.common.io.RecursiveDeleteOption;
import java.util.UUID;

import org.eclipse.lemminx.utils.FilesUtils;
import org.eclipse.lemminx.utils.ProjectUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;

import com.google.common.io.MoreFiles;
import com.google.common.io.RecursiveDeleteOption;

/**
* AbstractCacheBasedTest
*/
public abstract class AbstractCacheBasedTest {

protected static Path TEST_WORK_DIRECTORY = ProjectUtils.getProjectDirectory().resolve("target/test-cache");
protected Path testWorkDirectory = null;

private final String uuid = UUID.randomUUID().toString();

private static Path parentDir;

static {
try {
parentDir = Files.createTempDirectory("lemminx");
} catch (IOException e) {
parentDir = null;
}
}

@BeforeEach
public final void setupCache() throws Exception {
System.out.println(this.getClass().getName() + ": " + uuid);
clearCache();
System.setProperty(FilesUtils.LEMMINX_WORKDIR_KEY, TEST_WORK_DIRECTORY.toAbsolutePath().toString());
FilesUtils.resetDeployPath();
Assertions.assertNotNull(parentDir);
Path childDir = parentDir.resolve(uuid);
testWorkDirectory = Files.createDirectory(childDir);
System.setProperty(FilesUtils.LEMMINX_WORKDIR_KEY, testWorkDirectory.toAbsolutePath().toString());
}

@AfterEach
public final void clearCache() throws IOException {
if (Files.exists(TEST_WORK_DIRECTORY)) {
MoreFiles.deleteDirectoryContents(TEST_WORK_DIRECTORY,RecursiveDeleteOption.ALLOW_INSECURE);
if (testWorkDirectory != null && Files.exists(testWorkDirectory)) {
MoreFiles.deleteDirectoryContents(testWorkDirectory,RecursiveDeleteOption.ALLOW_INSECURE);
Files.delete(testWorkDirectory);
}
System.clearProperty(FilesUtils.LEMMINX_WORKDIR_KEY);
FilesUtils.resetDeployPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import static org.eclipse.lemminx.XMLAssert.d;

import org.eclipse.lemminx.AbstractCacheBasedTest;
import org.eclipse.lemminx.XMLAssert;
import org.junit.jupiter.api.Test;

public class XMLCatalogDiagnosticsTest {
public class XMLCatalogDiagnosticsTest extends AbstractCacheBasedTest {
@Test
public void testCatalogWithValidFile() {
String xml = "<?xml version=\"1.0\"?>\n" + //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
import static org.eclipse.lemminx.XMLAssert.r;
import static org.eclipse.lemminx.XMLAssert.testDocumentLinkFor;

import org.eclipse.lemminx.AbstractCacheBasedTest;
import org.junit.jupiter.api.Test;

/**
* Tests for the document links in XML catalog files
*/
public class XMLCatalogDocumentLinkTest {
public class XMLCatalogDocumentLinkTest extends AbstractCacheBasedTest {

private static String CATALOG_PATH = "src/test/resources/catalog.xml";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.eclipse.lemminx.XMLAssert.c;
import static org.eclipse.lemminx.XMLAssert.d;

import org.eclipse.lemminx.AbstractCacheBasedTest;
import org.eclipse.lemminx.XMLAssert;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.extensions.contentmodel.participants.XMLSchemaErrorCode;
Expand All @@ -25,7 +26,7 @@
* Schema.
*
*/
public class XMLCatalogExtensionTest {
public class XMLCatalogExtensionTest extends AbstractCacheBasedTest {

@Test
public void completion() throws BadLocationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collections;
import java.util.function.Consumer;

import org.eclipse.lemminx.AbstractCacheBasedTest;
import org.eclipse.lemminx.client.CodeLensKind;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager;
Expand All @@ -31,7 +32,7 @@
* Associate grammar codelens tests
*
*/
public class AssociateGrammarCodeLensExtensionsTest {
public class AssociateGrammarCodeLensExtensionsTest extends AbstractCacheBasedTest {

// Codelens for Binding

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,16 @@
package org.eclipse.lemminx.extensions.contentmodel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.eclipse.lemminx.AbstractCacheBasedTest;

import com.google.common.io.MoreFiles;
import com.google.common.io.RecursiveDeleteOption;

public class BaseFileTempTest {

private static final Path tempDirPath = Paths.get("target/temp/");
protected static final URI tempDirUri = tempDirPath.toAbsolutePath().toUri();

@BeforeAll
public static void setup() throws FileNotFoundException, IOException {
deleteTempDirIfExists();
createTempDir();
}

@AfterAll
public static void tearDown() throws IOException {
deleteTempDirIfExists();
}

private static void deleteTempDirIfExists() throws IOException {
File tempDir = new File(tempDirUri);
if (tempDir.exists()) {
MoreFiles.deleteRecursively(tempDir.toPath(), RecursiveDeleteOption.ALLOW_INSECURE);
}
}

private static void createTempDir() {
File tempDir = new File(tempDirUri);
tempDir.mkdir();
}
public abstract class BaseFileTempTest extends AbstractCacheBasedTest {

protected static void createFile(String fileName, String contents) throws IOException {
URI fileURI = new File(fileName).toURI();
Expand Down Expand Up @@ -81,8 +50,8 @@ protected static void updateFile(URI fileURI, String contents) throws IOExceptio
}
createFile(fileURI, contents);
}
protected static Path getTempDirPath() {
return tempDirPath;

protected Path getTempDirPath() {
return testWorkDirectory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@

/**
* Content model manager cache test
*
*
* @author Angelo ZERR
*
*/
public class ContentModelManagerCacheTest extends BaseFileTempTest {

@Test
public void testXSDCache() throws IOException, BadLocationException {
String xsdPath = tempDirUri.getPath() + "/tag.xsd";
String xmlPath = tempDirUri.toString() + "/tag.xml";
String xsdPath = getTempDirPath().toString() + "/tag.xsd";
String xmlPath = getTempDirPath().toString() + "/tag.xml";

// Create a XSD file in the temp directory
String xsd = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n" + //
Expand Down Expand Up @@ -87,13 +87,13 @@ public void testXSDCache() throws IOException, BadLocationException {
MoreFiles.deleteRecursively(new File(xsdPath).toPath(), RecursiveDeleteOption.ALLOW_INSECURE);
// Completion must be empty
XMLAssert.testCompletionFor(xml, null, xmlPath, 4 /* region, endregion, cdata, comment */);

// recreate the XSD file
createFile(xsdPath, xsd);
XMLAssert.testCompletionFor(xml, null, xmlPath, 5 /* region, endregion, cdata, comment, label */,
c("label", "<label></label>"));
XMLAssert.testCompletionFor(xml, null, xmlPath, 5 /* region, endregion, cdata, comment, label */,
c("label", "<label></label>"));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.eclipse.lemminx.AbstractCacheBasedTest;
import org.eclipse.lemminx.commons.TextDocument;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMParser;
Expand All @@ -28,11 +29,11 @@

/**
* Test with {@link ContentModelManager#dependsOnGrammar(DOMDocument, String)}
*
*
* @author Angelo ZERR
*
*/
public class ContentModelManagerDependsOnGrammarTest {
public class ContentModelManagerDependsOnGrammarTest extends AbstractCacheBasedTest {

private ContentModelManager modelManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.eclipse.lemminx.XMLAssert.c;
import static org.eclipse.lemminx.XMLAssert.te;

import org.eclipse.lemminx.AbstractCacheBasedTest;
import org.eclipse.lemminx.XMLAssert;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.services.XMLLanguageService;
Expand All @@ -28,7 +29,7 @@
import org.eclipse.lsp4j.MarkupKind;
import org.junit.jupiter.api.Test;

public class DTDCompletionExtensionsTest {
public class DTDCompletionExtensionsTest extends AbstractCacheBasedTest {

@Test
public void completionInRoot() throws BadLocationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

/**
* Test with DTD completion and cache.
*
*
* @author Angelo ZERR
*
*/
Expand All @@ -45,7 +45,7 @@ public void dtdCache() throws Exception {
};

// Copy the svg.dtd in the cache folder
Path expectedLocation = TEST_WORK_DIRECTORY
Path expectedLocation = testWorkDirectory
.resolve("cache/http/www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd");
// Download resource in a temporary file
Files.createDirectories(expectedLocation.getParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.xerces.impl.XMLEntityManager;
import org.apache.xerces.util.URI.MalformedURIException;
import org.eclipse.lemminx.AbstractCacheBasedTest;
import org.eclipse.lemminx.XMLAssert;
import org.eclipse.lemminx.extensions.contentmodel.participants.DTDErrorCode;
import org.eclipse.lemminx.extensions.contentmodel.participants.XMLSyntaxErrorCode;
Expand All @@ -39,7 +40,7 @@
* DTD diagnostics services tests
*
*/
public class DTDDiagnosticsTest {
public class DTDDiagnosticsTest extends AbstractCacheBasedTest {

@Test
public void MSG_ELEMENT_NOT_DECLARED() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import static org.eclipse.lemminx.XMLAssert.d;

import org.eclipse.lemminx.AbstractCacheBasedTest;
import org.eclipse.lemminx.XMLAssert;
import org.eclipse.lemminx.extensions.contentmodel.participants.DTDErrorCode;
import org.eclipse.lemminx.extensions.contentmodel.participants.XMLSyntaxErrorCode;
Expand All @@ -26,7 +27,7 @@
* DTD doctype diagnostics services tests
*
*/
public class DTDDoctypeDiagnosticsTest {
public class DTDDoctypeDiagnosticsTest extends AbstractCacheBasedTest {

@Test
public void MSG_ELEMENT_TYPE_REQUIRED_IN_ELEMENTDECL() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.eclipse.lemminx.XMLAssert.dl;
import static org.eclipse.lemminx.XMLAssert.r;

import org.eclipse.lemminx.AbstractCacheBasedTest;
import org.eclipse.lemminx.XMLAssert;
import org.eclipse.lemminx.commons.BadLocationException;
import org.junit.jupiter.api.Test;
Expand All @@ -22,7 +23,7 @@
* DTD document links tests
*
*/
public class DTDDocumentLinkTest {
public class DTDDocumentLinkTest extends AbstractCacheBasedTest {

@Test
public void docTypeSYSTEM() throws BadLocationException {
Expand Down
Loading

0 comments on commit 1fb2a20

Please sign in to comment.