Skip to content

Commit

Permalink
added test for a merge commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Duda committed May 17, 2024
1 parent c8da484 commit 5ee3960
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 7 deletions.
13 changes: 12 additions & 1 deletion src/Git/GitRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,18 @@ export class GitRepository {
}

public isMergeCommit(commit: Commit): boolean {
return commit.parentcount() > 1;
if (commit.parentcount() > 1) {
return true;
}

const trimmedCommitMessage = commit.summary();

// Ugly, but works
if (trimmedCommitMessage.includes("Merge")) {
return true;
}

return false;
}

private async _getRepository(): Promise<Repository> {
Expand Down
2 changes: 1 addition & 1 deletion test/_repo
Submodule _repo updated 1 files
+2 −2 README.md
23 changes: 21 additions & 2 deletions test/_utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ export const cloneMockRepositoryToFolder = (parentFolder: string): string => {
return clonedRepoPath;
}

export const mergeBranchToCurrent = (repositoryPath: string, branchName: string): void => {
const { execSync } = require('child_process');

execSync(
`cd ${repositoryPath} && git merge --no-ff origin/${branchName}`,
(err: any, stdout: any, stderr: any) => {
if (err) {
console.debug(err);
return;
}
console.debug(`stdout: ${stdout}`);
console.debug(`stderr: ${stderr}`);
});
}

export const getRandomUUID = () => uuidv4();

export const appendSomeTextToFile = (filePath: string) => {
Expand Down Expand Up @@ -94,13 +109,17 @@ export const createFolder = (location: string): string => {
return folderPath
}

export const commitModitication = async (fileNames: string[], repositoryPath: string): Promise<GitRepository> => {
export const commitModitication = async (
fileNames: string[],
repositoryPath: string,
commitMessage = "test commit"
): Promise<GitRepository> => {
fileNames.forEach(fileName => {
appendSomeTextToFile(join(repositoryPath, fileName));
})

const repository = new GitRepository(repositoryPath);
const oid = await repository.commitFiles("test commit", fileNames)
const oid = await repository.commitFiles(commitMessage, fileNames)

console.debug(`[Modified files] Created new commit ${oid}`)

Expand Down
84 changes: 81 additions & 3 deletions test/commits/verification.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { appendSomeTextToFile, cloneMockRepositoryToFolder, commitEmptyFiles, commitFiles, commitModitication, createEmptyFiles, makeUniqueFolderForTest } from "../_utils/utils";
import { appendSomeTextToFile, cloneMockRepositoryToFolder, commitEmptyFiles, commitFiles, commitModitication, createEmptyFiles, makeUniqueFolderForTest, mergeBranchToCurrent } from "../_utils/utils";
import { VerificationStatus, verifyUnpushedCommits } from "../../src/Commands/runVerifyUnpushedCommitsCommand";
import { Utils } from "../../src/Scope/Utils";
import { FileTagsDatabase } from "../../src/Scope/FileTagsDatabase";
import { GitRepository } from "../../src/Git/GitRepository";
import { ConfigFile } from "../../src/Scope/ConfigFile";
import { FileTagsDatabase } from "../../src/Scope/FileTagsDatabase";
import { RelevancyManager } from "../../src/Relevancy/RelevancyManager";
import { Utils } from "../../src/Scope/Utils";
import { join } from "path";

const fs = require('fs');
Expand Down Expand Up @@ -148,4 +150,80 @@ describe("Commit verification by scope tags script", () => {

expect(verificationStatus).toBe(VerificationStatus.NOT_VERIFIED);
});

it("When commit is a merge commit, the commit is marked as merge commit", async () => {
const FOLDER_PATH = makeUniqueFolderForTest();
const REPO_PATH = cloneMockRepositoryToFolder(FOLDER_PATH);

const branchToMergeName = "branch-with-some-new-files";

mergeBranchToCurrent(REPO_PATH, branchToMergeName);

const repository = new GitRepository(REPO_PATH);
const config = new ConfigFile(REPO_PATH);
const database = new FileTagsDatabase(REPO_PATH);
const relevancy = new RelevancyManager();

config.load();
database.load();

const [mergeCommit, realCommit] = await repository.getUnpushedCommits();

expect(mergeCommit).toBeDefined();
expect(realCommit).toBeDefined();

const mergeCommitInfo = await repository.verifyCommit(mergeCommit, config, database, relevancy, true);
const realCommitInfo = await repository.verifyCommit(realCommit, config, database, relevancy, true);

expect(mergeCommitInfo.isMergeCommit).toBe(true);

expect(realCommitInfo.isMergeCommit).toBe(false);
expect(realCommitInfo.isSkipped).toBe(false);

const verificationStatus = await verifyUnpushedCommits([], REPO_PATH, true);

expect(verificationStatus).toBe(VerificationStatus.NOT_VERIFIED);
});

// Not pretty fix for squashed commits, at least in BitBucket
it("When commit summary has 'Merge', the commit is marked as merge commit", async () => {
const FOLDER_PATH = makeUniqueFolderForTest();
const REPO_PATH = cloneMockRepositoryToFolder(FOLDER_PATH);

const testFile = "src/some-file-in-squashed-merge.js";

const database = new FileTagsDatabase(REPO_PATH).load();
const config = new ConfigFile(REPO_PATH).load();

const repository = new GitRepository(REPO_PATH);
const relevancy = new RelevancyManager();

// Some common merge commit messages

const commonMergeCommitMessages = [
"Merge pull request #9 from mhagger/recursive-option",
"Merged in STH-1234-test-branch (pull request #1234)"
];

for (const message of commonMergeCommitMessages) {
await commitModitication(
[testFile],
REPO_PATH,
message
);
}

const unpushedCommits = await repository.getUnpushedCommits();
expect(unpushedCommits.length).toBe(commonMergeCommitMessages.length);

for (const commit of unpushedCommits) {
const commitInfo = await repository.verifyCommit(commit, config, database, relevancy, true);
expect(commitInfo.isMergeCommit).toBe(true);
}
const verificationStatus = await verifyUnpushedCommits([], REPO_PATH, true);

console.debug(`Verification status: ${Utils.getEnumKeyByEnumValue(VerificationStatus, verificationStatus)}`)

expect(verificationStatus).toBe(VerificationStatus.VERIFIED);
});
});

0 comments on commit 5ee3960

Please sign in to comment.