Skip to content

Commit

Permalink
added test for a specific case
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Duda committed May 22, 2024
1 parent c1ec1e7 commit 76713f0
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/Commands/runVerifyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ export function runVerifyCommand(args: Array<string>, root: string) {
const relevancyManager = new RelevancyManager();

repository.getCommitByHash(commitHash).then(async (commit: Commit) => {
console.log(`Loading relevancy map...'`);
const relevancyMap = relevancyManager.loadRelevancyMapFromCommits([commit]);

console.log(`Checking commit '${commit.summary()}'`);

const commitInfo = await repository.verifyCommit(commit, config, fileTagsDatabase, relevancyManager);
const commitInfo = await repository.verifyCommit(commit, config, fileTagsDatabase, relevancyManager, relevancyMap);

console.log(commitInfo);
});
Expand Down
7 changes: 5 additions & 2 deletions src/Commands/runVerifyUnpushedCommitsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ export async function verifyUnpushedCommits(args: Array<string>, root: string, u

const unpushedCommits: Commit[] = await repository.getUnpushedCommits();

console.log(`[Scope tags]: Loading relevancy map...'`);
const relevancyMap = relevancyManager.loadRelevancyMapFromCommits(unpushedCommits);

if (!unpushedCommits.length) {
console.log("No commits found that can be verified");
return VerificationStatus.VERIFIED;
}

for (const commit of unpushedCommits) {
console.log(`[Scope tags] Checking: '${commit.message().trim()}'`)
const verificationInfo = await repository.verifyCommit(commit, config, fileTagsDatabase, relevancyManager, useGitNatively);
const verificationInfo = await repository.verifyCommit(commit, config, fileTagsDatabase, relevancyManager, relevancyMap, useGitNatively);

if (verificationInfo.isSkipped) {
console.log(`[Scope Tags] Skipped check for '${commit.summary()}'`);
Expand Down Expand Up @@ -79,7 +82,7 @@ export async function verifyUnpushedCommits(args: Array<string>, root: string, u
const filesWhichShouldHaveRelevancyAdded: string[] = [];

info.filesToBeRelevancyTagged.forEach(fileData => {
const fileHasRelevancy = info.relevancy.some(relevancy => relevancy.path === fileData.newPath);
const fileHasRelevancy = relevancyMap.get(fileData.newPath);

if (!fileHasRelevancy) {
filesWhichShouldHaveRelevancyAdded.push(fileData.newPath);
Expand Down
23 changes: 21 additions & 2 deletions src/Git/GitRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FileTagsDatabase, FileStatusInDatabase } from "../Scope/FileTagsDatabas
import { RelevancyManager } from "../Relevancy/RelevancyManager";
import { ConfigFile } from "../Scope/ConfigFile";
import { execSync } from "child_process";
import { RelevancyMap } from "../Relevancy/Relevancy";

export class GitRepository {

Expand Down Expand Up @@ -298,7 +299,14 @@ export class GitRepository {
await index.write();
}

public async verifyCommit(commit: Commit, config: ConfigFile, database: FileTagsDatabase, relevancyManager: RelevancyManager, useGitNatively = false): Promise<VerificationInfo> {
public async verifyCommit(
commit: Commit,
config: ConfigFile,
database: FileTagsDatabase,
relevancyManager: RelevancyManager,
relevancyMap?: RelevancyMap,
useGitNatively = false
): Promise<VerificationInfo> {
const commitInfo: VerificationInfo = {
isVerified: false,
filesToTag: [],
Expand Down Expand Up @@ -342,13 +350,24 @@ export class GitRepository {
commitInfo.filesToBeRelevancyTagged = fileDataArray.filter(fileData => {
const fileStatus = statusMap.get(fileData);
return fileStatus !== FileStatusInDatabase.IGNORED;
}).filter(fileData => {
if (!relevancyMap) {
return true;
}

const mapHasRelevancy = [...relevancyMap.values()].some(relevancyInfo => relevancyInfo.some(info => info.path === fileData.oldPath || info.path === fileData.newPath))

if (mapHasRelevancy) {
return false;
}

return true;
});

if (!commitInfo.filesToTag.length) {
commitInfo.isVerified = true;
}

// Check relevancy
commitInfo.hasRelevancy = relevancyManager.doesCommitMessageHaveRelevancyData(commit.message());

if (commitInfo.hasRelevancy) {
Expand Down
4 changes: 1 addition & 3 deletions src/Relevancy/Relevancy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { FilePath } from "../Git/Types";

export enum Relevancy {
LOW = "LOW", // Does not search for references
HIGH = "HIGH", // Searches for references
Expand All @@ -14,7 +12,7 @@ export type CommitMessageRelevancyInfo = {
commit: string
};

export type RelevancyMap = Map<FilePath, Array<CommitMessageRelevancyInfo>>;
export type RelevancyMap = Map<string, Array<CommitMessageRelevancyInfo>>; // Maps commit SHA to relevancies

// Descriptions

Expand Down
2 changes: 1 addition & 1 deletion test/commits/fileData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("Tesing if file data is retrieved correctly", () => {

const fileDataArray: FileData[] = repo.getFileDataUsingNativeGitCommand(commit);

expect(fileDataArray.length === 1).toBeDefined();
expect(fileDataArray.length).toBe(1);

const ourFileData = fileDataArray[0];

Expand Down
50 changes: 47 additions & 3 deletions test/commits/verification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { FileTagsDatabase } from "../../src/Scope/FileTagsDatabase";
import { RelevancyManager } from "../../src/Relevancy/RelevancyManager";
import { Utils } from "../../src/Scope/Utils";
import { join } from "path";
import { FileData } from "../../src/Git/Types";

const fs = require('fs');

Expand Down Expand Up @@ -172,8 +173,8 @@ describe("Commit verification by scope tags script", () => {
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);
const mergeCommitInfo = await repository.verifyCommit(mergeCommit, config, database, relevancy, undefined, true);
const realCommitInfo = await repository.verifyCommit(realCommit, config, database, relevancy, undefined, true);

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

Expand Down Expand Up @@ -217,7 +218,7 @@ describe("Commit verification by scope tags script", () => {
expect(unpushedCommits.length).toBe(commonMergeCommitMessages.length);

for (const commit of unpushedCommits) {
const commitInfo = await repository.verifyCommit(commit, config, database, relevancy, true);
const commitInfo = await repository.verifyCommit(commit, config, database, relevancy, undefined, true);
expect(commitInfo.isMergeCommit).toBe(true);
}
const verificationStatus = await verifyUnpushedCommits([], REPO_PATH, true);
Expand All @@ -226,4 +227,47 @@ describe("Commit verification by scope tags script", () => {

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

it("When multiple commits modifies the same file, but only the last one includes relevancy, all commits are marked as verified", async () => {
const FOLDER_PATH = makeUniqueFolderForTest();
const REPO_PATH = cloneMockRepositoryToFolder(FOLDER_PATH);

const filesToModify = [
"src/tagged-file.js",
"src/tagged-file-with-multiple-modules.js",
"src/file-ignored-by-database.js"
];

const relevancy = new RelevancyManager();

await commitModitication(filesToModify, REPO_PATH);

const repository = await commitModitication(filesToModify, REPO_PATH, `test commit
__relevancy__[{"path":"src/tagged-file.js","relevancy":"LOW","commit":"__current__"},{"path":"src/tagged-file-with-multiple-modules.js","relevancy":"HIGH","commit":"__current__"}]__relevancy__
`);

const unpushedCommits = await repository.getUnpushedCommits();

expect(unpushedCommits.length).toBe(2);

const commitWithRelevancy = unpushedCommits[0];
const commitWithoutRelevancy = unpushedCommits[1];

expect(relevancy.doesCommitMessageHaveRelevancyData(commitWithRelevancy.message())).toBe(true);
expect(relevancy.doesCommitMessageHaveRelevancyData(commitWithoutRelevancy.message())).toBe(false);

const commitWithRelevancyfileDataArray: FileData[] = repository.getFileDataUsingNativeGitCommand(commitWithRelevancy);
const commitWithoutRelevancyfileDataArray: FileData[] = repository.getFileDataUsingNativeGitCommand(commitWithoutRelevancy);

expect(commitWithRelevancyfileDataArray.length).toBe(3);
expect(commitWithoutRelevancyfileDataArray.length).toBe(3);

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

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

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

});

0 comments on commit 76713f0

Please sign in to comment.