use of org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot in project n4js by eclipse.
the class TestDiscoveryHelper method isTestable.
/**
* Checks if the resource at the given location can be executed as a test. The location may point to an N4JS project
* or a folder or file within an N4JS project.
* <p>
* This method is intended as a very first check to decide if the user should be given the option to launch a test
* or not; for example, to enable or disable a "Run as test" context menu item). This method does not check all
* details because many checks are better handled at later stages when meaningful error messages can be provided to
* the user.
*/
public boolean isTestable(N4JSWorkspaceConfigSnapshot ws, final URI location) {
if (null == location) {
return false;
}
if (isProject(ws, location)) {
// location points an N4JS project
// --> must contain at least 1 source container of type "test"
// (do *not* check if the folder contains test classes (for performance reasons and
// to show better error messages; same behavior as in JUnit support in Eclipse))
final N4JSProjectConfigSnapshot p = ws.findProjectByPath(location);
return p.getSourceFolders().stream().anyMatch(N4JSSourceFolderSnapshot::isTest);
} else {
// then extract the fragment and check the location for the module.
if (location.hasFragment()) {
return isTestable(ws, location.trimFragment());
}
// (2) location must lie in a source container of type "test"
final N4JSSourceFolderSnapshot c = ws.findSourceFolderContaining(location);
if (c == null || !c.isTest())
return false;
// (3) if the location points to an n4js-file, it must contain at least one test class
final ResourceSet resourceSet = workspaceAccess.createResourceSet();
final IResourceDescriptions index = workspaceAccess.getXtextIndex(resourceSet).get();
final IResourceDescription rdesc = index.getResourceDescription(location);
if (rdesc != null) {
return stream(rdesc.getExportedObjectsByType(T_CLASS)).anyMatch(desc -> hasTestMethods(resourceSet, desc));
} else {
// to show better error messages; same behavior as in JUnit support in Eclipse))
return true;
}
}
}
use of org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot in project n4js by eclipse.
the class N4JSLanguageHelper method isDirectoryWithPackageJson.
private boolean isDirectoryWithPackageJson(IResourceDescriptions index, TModule targetModule, QualifiedName targetQN) {
// NOTE: the following approach would be a more elegant implementation of this method, but would require a
// different computation of FQNs for package.json files in source folders (in N4JSQualifiedNameProvider):
// @formatter:off
// Iterable<IEObjectDescription> matchingPackageJsonDesc = index.getExportedObjects(
// JSONPackage.Literals.JSON_DOCUMENT,
// targetQN.append(N4JSQualifiedNameProvider.PACKAGE_JSON_SEGMENT), false);
// if (matchingPackageJsonDesc.iterator().hasNext()) {
// return true;
// }
// @formatter:on
N4JSProjectConfigSnapshot targetProject = replaceDefinitionProjectByDefinedProject(targetModule, workspaceAccess.findProjectContaining(targetModule), true);
if (targetProject == null) {
return false;
}
int segCount = targetQN.getSegments().size();
String[] segments = new String[segCount + 1];
for (int i = 0; i < segCount; i++) {
segments[i] = targetQN.getSegments().get(i);
}
segments[segCount] = N4JSGlobals.PACKAGE_JSON;
for (N4JSSourceFolderSnapshot srcFolder : targetProject.getSourceFolders()) {
if (srcFolder instanceof N4JSSourceFolderSnapshotForPackageJson) {
continue;
}
FileURI packageJsonURI = srcFolder.getPathAsFileURI().appendSegments(segments);
if (index.getResourceDescription(packageJsonURI.toURI()) != null) {
return true;
}
}
return false;
}
use of org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot in project n4js by eclipse.
the class StaticPolyfillHelper method findStaticPolyfiller.
/**
* Find the corresponding static-polyfill to this {@code @@PolyfillAware} resource in the same project. returns null
* if not found or this resource has no {@code @@PolyfillAware} annotation.
*/
public SafeURI<?> findStaticPolyfiller(Resource resource) {
// ensure right resource
if (resource instanceof N4JSResource) {
final N4JSResource res = (N4JSResource) resource;
if (!isContainedInStaticPolyfillAware(res.getScript()))
return null;
final QualifiedName qnFilled = qualifiedNameConverter.toQualifiedName(res.getModule().getQualifiedName());
final N4JSProjectConfigSnapshot project = projectResolver.resolveProject(res, res.getURI());
final QualifiedName fqn = qnFilled;
// see Req.155#4: "Both extensions are equal."
final Optional<String> fileExtension = Optional.of(URIUtils.fileExtension(res.getURI()));
final N4JSSourceFolderSnapshot filledSrcContainer = workspaceAccess.findSourceFolderContaining(res, res.getURI());
for (N4JSSourceFolderSnapshot srcConti : project.getSourceFolders()) {
if (!Objects.equals(filledSrcContainer, srcConti)) {
final SafeURI<?> uri = findArtifactHelper.findArtifact(srcConti, fqn, fileExtension);
if (uri != null) {
return uri;
}
}
}
}
return null;
}
Aggregations