105 lines
2.9 KiB
Java
105 lines
2.9 KiB
Java
package com.r11.tests;
|
|
|
|
import org.eclipse.jgit.api.Git;
|
|
import org.eclipse.jgit.api.Status;
|
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
|
import org.eclipse.jgit.lib.Ref;
|
|
import org.eclipse.jgit.lib.Repository;
|
|
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
|
|
import org.junit.jupiter.api.*;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.util.List;
|
|
|
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
|
public class GitRepositoryTests {
|
|
|
|
private static Git git;
|
|
private static Status status;
|
|
private static Repository repository;
|
|
|
|
private final List<String> unwantedFileTypes = List.of("application/java-archive", "application/java-vm");
|
|
|
|
@BeforeAll
|
|
static void beforeAll() throws IOException, GitAPIException {
|
|
FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
|
|
repositoryBuilder.findGitDir();
|
|
|
|
git = Git.wrap(repositoryBuilder.build());
|
|
|
|
status = git.status().call();
|
|
repository = git.getRepository();
|
|
}
|
|
|
|
@Test
|
|
@Order(1)
|
|
void checkHasUncommittedChangesTest() {
|
|
Assertions.assertFalse(status.hasUncommittedChanges());
|
|
}
|
|
|
|
@Test
|
|
@Order(2)
|
|
void checkNotPushedCommitsTest() throws GitAPIException, IOException {
|
|
String localHeadHash = repository.resolve("HEAD").getName();
|
|
|
|
for (Ref ref : git.lsRemote().call()) {
|
|
String refHash = ref.getObjectId().getName();
|
|
if (refHash.equals(localHeadHash)) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
Assertions.fail("The local branch repository is not compatible with the branch repository on the server.");
|
|
}
|
|
|
|
@Test
|
|
@Order(3)
|
|
void checkUnwantedFilesInUncommittedChangesTest() {
|
|
status.getUncommittedChanges().forEach(filePath -> {
|
|
if (this.isUnwantedFile(filePath)) {
|
|
Assertions.fail("Unwanted uncommitted file: " + filePath);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the file type.
|
|
* @param filePath The file path.
|
|
* @return The file type.
|
|
*/
|
|
private String getFileTypeFromPath(String filePath) {
|
|
File file = new File(filePath);
|
|
return this.getFileProbeContentType(file);
|
|
}
|
|
|
|
/**
|
|
* Get the file type by probe content.
|
|
* @param file The file.
|
|
* @return The file type.
|
|
*/
|
|
private String getFileProbeContentType(File file) {
|
|
try {
|
|
return Files.probeContentType(file.toPath());
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return "Undetermined";
|
|
}
|
|
|
|
/**
|
|
* Check if the file is unwanted.
|
|
* @param filePath The file path.
|
|
* @return True if the file is unwanted.
|
|
*/
|
|
private boolean isUnwantedFile(String filePath) {
|
|
String type = this.getFileTypeFromPath(filePath);
|
|
if (type == null) {
|
|
return true;
|
|
}
|
|
|
|
return this.unwantedFileTypes.contains(type);
|
|
}
|
|
}
|