mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-07-03 12:18:09 +02:00
Expose content type resolver api to plugins (#1752)
Expose an api which makes it easy to detect the content type of files. The api is based on the spotter api, but does not expose spotter classes. Co-authored-by: René Pfeuffer <rene.pfeuffer@cloudogu.com>
This commit is contained in:
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.api.v2;
|
||||
|
||||
import com.github.sdorra.spotter.ContentType;
|
||||
import com.github.sdorra.spotter.Language;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ContentSearchableTypeResolverTest {
|
||||
|
||||
@Test
|
||||
void shouldResolveMarkdown() {
|
||||
String content = String.join("\n",
|
||||
"% Markdown content",
|
||||
"% Which does not start with markdown"
|
||||
);
|
||||
ContentType contentType = ContentTypeResolver.resolve("somedoc.md", content.getBytes(StandardCharsets.UTF_8));
|
||||
assertThat(contentType.getLanguage()).contains(Language.MARKDOWN);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResolveMarkdownWithoutContent() {
|
||||
ContentType contentType = ContentTypeResolver.resolve("somedoc.md");
|
||||
assertThat(contentType.getLanguage()).contains(Language.MARKDOWN);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResolveMarkdownEvenWithDotsInFilename() {
|
||||
ContentType contentType = ContentTypeResolver.resolve("somedoc.1.1.md");
|
||||
assertThat(contentType.getLanguage()).contains(Language.MARKDOWN);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResolveDockerfile() {
|
||||
ContentType contentType = ContentTypeResolver.resolve("Dockerfile");
|
||||
assertThat(contentType.getLanguage()).contains(Language.DOCKERFILE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,10 +29,10 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import sonia.scm.NotFoundException;
|
||||
import sonia.scm.io.DefaultContentTypeResolver;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.api.CatCommandBuilder;
|
||||
import sonia.scm.repository.api.RepositoryService;
|
||||
@@ -68,13 +68,14 @@ public class ContentResourceTest {
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private RepositoryServiceFactory repositoryServiceFactory;
|
||||
|
||||
@InjectMocks
|
||||
private ContentResource contentResource;
|
||||
|
||||
private CatCommandBuilder catCommand;
|
||||
|
||||
@Before
|
||||
public void initService() throws Exception {
|
||||
contentResource = new ContentResource(repositoryServiceFactory, new DefaultContentTypeResolver());
|
||||
|
||||
NamespaceAndName existingNamespaceAndName = new NamespaceAndName(NAMESPACE, REPO_NAME);
|
||||
RepositoryService repositoryService = repositoryServiceFactory.create(existingNamespaceAndName);
|
||||
catCommand = repositoryService.getCatCommand();
|
||||
@@ -169,7 +170,7 @@ public class ContentResourceTest {
|
||||
@Test
|
||||
public void shouldNotReadCompleteFileForHead() throws Exception {
|
||||
FailingAfterSomeBytesStream stream = new FailingAfterSomeBytesStream();
|
||||
doAnswer(invocation -> stream).when(catCommand).getStream(eq("readHeadOnly"));
|
||||
doAnswer(invocation -> stream).when(catCommand).getStream("readHeadOnly");
|
||||
|
||||
Response response = contentResource.metadata(NAMESPACE, REPO_NAME, REV, "readHeadOnly");
|
||||
assertEquals(200, response.getStatus());
|
||||
@@ -201,7 +202,7 @@ public class ContentResourceTest {
|
||||
outputStream.close();
|
||||
return null;
|
||||
}).when(catCommand).retriveContent(any(), eq(path));
|
||||
doAnswer(invocation -> new ByteArrayInputStream(content)).when(catCommand).getStream(eq(path));
|
||||
doAnswer(invocation -> new ByteArrayInputStream(content)).when(catCommand).getStream(path);
|
||||
}
|
||||
|
||||
private ByteArrayOutputStream readOutputStream(Response response) throws IOException {
|
||||
|
||||
@@ -28,6 +28,7 @@ import de.otto.edison.hal.Link;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.io.DefaultContentTypeResolver;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.api.DiffFile;
|
||||
import sonia.scm.repository.api.DiffLine;
|
||||
@@ -52,7 +53,7 @@ class DiffResultToDiffResultDtoMapperTest {
|
||||
private static final Repository REPOSITORY = new Repository("1", "git", "space", "X");
|
||||
|
||||
ResourceLinks resourceLinks = ResourceLinksMock.createMock(create("/scm/api/v2"));
|
||||
DiffResultToDiffResultDtoMapper mapper = new DiffResultToDiffResultDtoMapper(resourceLinks);
|
||||
DiffResultToDiffResultDtoMapper mapper = new DiffResultToDiffResultDtoMapper(resourceLinks, new DefaultContentTypeResolver());
|
||||
|
||||
@Test
|
||||
void shouldMapDiffResult() {
|
||||
@@ -62,8 +63,8 @@ class DiffResultToDiffResultDtoMapperTest {
|
||||
assertAddedFile(files.get(0), "A.java", "abc", "java");
|
||||
assertModifiedFile(files.get(1), "B.ts", "abc", "def", "typescript");
|
||||
assertDeletedFile(files.get(2), "C.go", "ghi", "golang");
|
||||
assertRenamedFile(files.get(3), "typo.ts", "okay.ts", "def", "fixed", "typescript");
|
||||
assertCopiedFile(files.get(4), "good.ts", "better.ts", "def", "fixed", "typescript");
|
||||
assertRenamedFile(files.get(3), "typo.ts", "okay.ts", "def", "fixed", "typescript");
|
||||
assertCopiedFile(files.get(4), "good.ts", "better.ts", "def", "fixed", "typescript");
|
||||
|
||||
DiffResultDto.HunkDto hunk = files.get(1).getHunks().get(0);
|
||||
assertHunk(hunk, "@@ -3,4 1,2 @@", 1, 2, 3, 4);
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.github.sdorra.spotter.Language;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ProgrammingLanguagesTest {
|
||||
|
||||
@Test
|
||||
void shouldReturnAceModeIfPresent() {
|
||||
assertThat(ProgrammingLanguages.getValue(Language.GO)).isEqualTo("golang");
|
||||
assertThat(ProgrammingLanguages.getValue(Language.JAVA)).isEqualTo("java");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnCodemirrorIfAceModeIsMissing() {
|
||||
assertThat(ProgrammingLanguages.getValue(Language.HTML_ECR)).isEqualTo("htmlmixed");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnTextIfNoModeIsPresent() {
|
||||
assertThat(ProgrammingLanguages.getValue(Language.HXML)).isEqualTo("text");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.io;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
class DefaultContentTypeResolverTest {
|
||||
|
||||
private final DefaultContentTypeResolver contentTypeResolver = new DefaultContentTypeResolver();
|
||||
|
||||
@Test
|
||||
void shouldReturnPrimaryPart() {
|
||||
ContentType contentType = contentTypeResolver.resolve("hog.pdf");
|
||||
assertThat(contentType.getPrimary()).isEqualTo("application");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnSecondaryPart() {
|
||||
ContentType contentType = contentTypeResolver.resolve("hog.pdf");
|
||||
assertThat(contentType.getSecondary()).isEqualTo("pdf");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnRaw() {
|
||||
ContentType contentType = contentTypeResolver.resolve("hog.pdf");
|
||||
assertThat(contentType.getRaw()).isEqualTo("application/pdf");
|
||||
}
|
||||
|
||||
@Nested
|
||||
class IsTextTests {
|
||||
|
||||
@ParameterizedTest(name = "shouldReturnIsTextFor: {argumentsWithNames}")
|
||||
@ValueSource(strings = {"App.java", "Dockerfile", "Playbook.yml", "README.md", "LICENSE.txt"})
|
||||
void shouldReturnIsTextFor(String path) {
|
||||
ContentType contentType = contentTypeResolver.resolve(path);
|
||||
assertThat(contentType.isText()).isTrue();
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "shouldReturnIsNotTextFor: {argumentsWithNames}")
|
||||
@ValueSource(strings = {"scan.exe", "hog.pdf", "library.so", "awesome.dll", "something.dylib"})
|
||||
void shouldReturnIsNotTextFor(String path) {
|
||||
ContentType contentType = contentTypeResolver.resolve(path);
|
||||
assertThat(contentType.isText()).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class LanguageTests {
|
||||
|
||||
@Test
|
||||
void shouldResolveMarkdown() {
|
||||
String content = String.join("\n",
|
||||
"% Markdown content",
|
||||
"% Which does not start with markdown"
|
||||
);
|
||||
ContentType contentType = contentTypeResolver.resolve("somedoc.md", content.getBytes(StandardCharsets.UTF_8));
|
||||
Assertions.assertThat(contentType.getLanguage()).contains("markdown");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResolveMarkdownWithoutContent() {
|
||||
ContentType contentType = contentTypeResolver.resolve("somedoc.md");
|
||||
Assertions.assertThat(contentType.getLanguage()).contains("markdown");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResolveMarkdownEvenWithDotsInFilename() {
|
||||
ContentType contentType = contentTypeResolver.resolve("somedoc.1.1.md");
|
||||
Assertions.assertThat(contentType.getLanguage()).contains("markdown");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResolveDockerfile() {
|
||||
ContentType contentType = contentTypeResolver.resolve("Dockerfile");
|
||||
Assertions.assertThat(contentType.getLanguage()).contains("dockerfile");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void shouldReturnAceModeIfPresent() {
|
||||
assertThat(contentTypeResolver.resolve("app.go").getLanguage()).contains("golang"); // codemirror is just go
|
||||
assertThat(contentTypeResolver.resolve("App.java").getLanguage()).contains("java"); // codemirror is clike
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnCodemirrorIfAceModeIsMissing() {
|
||||
assertThat(contentTypeResolver.resolve("index.ecr").getLanguage()).contains("htmlmixed");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnTextIfNoModeIsPresent() {
|
||||
assertThat(contentTypeResolver.resolve("index.hxml").getLanguage()).contains("text");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user