Skip to content

Commit 7b97189

Browse files
miscosemickaelistria
authored andcommitted
Provide file contents when determining content type for search #3014
Ensures that describers are taken into account when deciding if a content type is binary. Adds a regression test that registers a binary content type with a describer and verifies that it only causes files with content matching the describer to be considered binary. Fixes #3014
1 parent cd33843 commit 7b97189

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

bundles/org.eclipse.search.core/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,13 @@ public IStatus search(TextSearchScope scope, IProgressMonitor monitor) {
442442
private final IContentType TEXT_TYPE = Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT);
443443

444444
private boolean hasBinaryContentType(IFile file) {
445-
IContentType[] contentTypes = Platform.getContentTypeManager().findContentTypesFor(file.getName());
445+
IContentType[] contentTypes;
446+
try (java.io.InputStream contents = file.getContents()) {
447+
contentTypes = Platform.getContentTypeManager().findContentTypesFor(contents, file.getName());
448+
} catch (IOException | CoreException e) {
449+
SearchCorePlugin.log(e);
450+
contentTypes = Platform.getContentTypeManager().findContentTypesFor(file.getName());
451+
}
446452
for (IContentType contentType : contentTypes) {
447453
if (contentType.isKindOf(TEXT_TYPE)) {
448454
return false; // is text

tests/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
import static org.junit.Assert.assertEquals;
1919

20+
import java.io.ByteArrayInputStream;
21+
import java.lang.reflect.Field;
2022
import java.util.ArrayList;
23+
import java.util.Arrays;
2124
import java.util.List;
2225
import java.util.regex.Pattern;
2326

@@ -26,7 +29,11 @@
2629
import org.junit.ClassRule;
2730
import org.junit.Test;
2831

32+
import org.eclipse.core.runtime.ContributorFactorySimple;
2933
import org.eclipse.core.runtime.CoreException;
34+
import org.eclipse.core.runtime.IContributor;
35+
import org.eclipse.core.runtime.IExtensionRegistry;
36+
import org.eclipse.core.runtime.Platform;
3037

3138
import org.eclipse.core.resources.IFile;
3239
import org.eclipse.core.resources.IFolder;
@@ -547,6 +554,70 @@ private TestResult[] performSearch(TestResultCollector collector, String[] fileN
547554

548555
}
549556

557+
@Test
558+
public void testBinaryContentTypeWithDescriberSerial() throws Exception {
559+
testBinaryContentTypeWithDescriber(new SerialTestResultCollector());
560+
}
561+
562+
@Test
563+
public void testBinaryContentTypeWithDescriberParallel() throws Exception {
564+
testBinaryContentTypeWithDescriber(new ParallelTestResultCollector());
565+
}
566+
567+
private void testBinaryContentTypeWithDescriber(TestResultCollector collector) throws Exception {
568+
IExtensionRegistry registry= Platform.getExtensionRegistry();
569+
570+
Field field= org.eclipse.core.internal.registry.ExtensionRegistry.class
571+
.getDeclaredField("masterToken");
572+
field.setAccessible(true);
573+
Object masterToken= field.get(registry);
574+
575+
IContributor contributor= ContributorFactorySimple.createContributor(this);
576+
577+
try (java.io.InputStream is= new ByteArrayInputStream("""
578+
<?xml version="1.0"?>
579+
<plugin>
580+
<extension point="org.eclipse.core.contenttype.contentTypes">
581+
<content-type
582+
id="org.eclipse.search.tests.binaryFile"
583+
name="Search Test Binary File"
584+
priority="low">
585+
<describer
586+
class="org.eclipse.core.runtime.content.BinarySignatureDescriber"
587+
plugin="org.eclipse.core.contenttype">
588+
<!-- "binary" in ASCII encoding -->
589+
<parameter name="signature" value="62 69 6E 61 72 79"/>
590+
</describer>
591+
</content-type>
592+
<file-association
593+
content-type="org.eclipse.search.tests.binaryFile"
594+
file-patterns="[^.]+"/> <!-- no file extension -->
595+
</extension>
596+
</plugin>""".getBytes())) {
597+
registry.addContribution(is, contributor, false, null, null, masterToken);
598+
try (AutoCloseable c= () -> {
599+
Arrays.stream(registry.getExtensions(contributor))
600+
.forEach(extension -> registry.removeExtension(extension, masterToken));
601+
}) {
602+
603+
IFolder folder= ResourceHelper.createFolder(fProject.getFolder("folder1"));
604+
IFile textfile= ResourceHelper.createFile(folder, "textfile", "text hello");
605+
IFile binaryfile= ResourceHelper.createFile(folder, "binaryfile", "binary hello");
606+
607+
Pattern searchPattern= PatternConstructor.createPattern("hello", true, false);
608+
609+
FileTextSearchScope scope= FileTextSearchScope.newSearchScope(new IResource[] { fProject }, (String[]) null, false);
610+
TextSearchEngine.create().search(scope, collector, searchPattern, null);
611+
612+
TestResult[] results= collector.getResults();
613+
assertEquals("Number of total results", 1, results.length);
614+
615+
assertMatches(results, 1, textfile, "text hello", "hello");
616+
assertMatches(results, 0, binaryfile, "binary hello", "hello");
617+
}
618+
}
619+
}
620+
550621

551622

552623
private void assertMatches(TestResult[] results, int expectedCount, IFile file, String fileContent, String string) {

0 commit comments

Comments
 (0)