Skip to content

Commit

Permalink
fixed error, added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Duda committed Jul 22, 2024
1 parent 513d983 commit 9bcd5b8
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scope-tags",
"version": "0.3.7",
"version": "0.3.8",
"description": "Output human readable test scope report for QA",
"main": "dist/scope.js",
"types": "dist/scope.d.ts",
Expand Down
14 changes: 13 additions & 1 deletion src/Git/GitRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,10 @@ export class GitRepository {
return commitInfo;
}

const fileDataArray = useGitNatively ? this.getFileDataUsingNativeGitCommand(commit) : await this.getFileDataForCommit(commit);
const allfileDataArray = useGitNatively ? this.getFileDataUsingNativeGitCommand(commit) : await this.getFileDataForCommit(commit);

const fileDataArray = allfileDataArray.filter(fileData => fileData.change !== GitDeltaType.DELETED);

const statusMap = database.checkMultipleFileStatusInDatabase(fileDataArray, config);

const allFilesAreIgnored = fileDataArray.every(fileData => {
Expand Down Expand Up @@ -514,4 +517,13 @@ export class GitRepository {

execSync(`cd ${this._root} && git commit --amend -m ${JSON.stringify(normalizedCommitMessage)}`);
}

/**
* Used for tests only, because deleted file can't be commited using nodegit
*/
public commitAllUsingNativeGitCommand(commitMessage: string) {
const normalizedCommitMessage = commitMessage.replace(/(\r\n|\n|\r)/gm, "");

execSync(`cd ${this._root} && git add . && git commit -m ${JSON.stringify(normalizedCommitMessage)}`);
}
}
1 change: 1 addition & 0 deletions src/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/_repo
Submodule _repo updated 1 files
+1 −0 src/index.js
75 changes: 73 additions & 2 deletions test/commits/verification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ import { GitRepository } from "../../src/Git/GitRepository";
import { FileData } from "../../src/Git/Types";
import { RelevancyManager } from "../../src/Relevancy/RelevancyManager";
import { ConfigFile } from "../../src/Scope/ConfigFile";
import { FileTagsDatabase } from "../../src/Scope/FileTagsDatabase";
import { FileTagsDatabase, TagIdentifier } from "../../src/Scope/FileTagsDatabase";
import { TagsDefinitionFile } from "../../src/Scope/TagsDefinitionFile";
import { Utils } from "../../src/Scope/Utils";
import { appendSomeTextToFile, cloneMockRepositoryToFolder, commitEmptyFiles, commitFiles, commitModitication, createEmptyFiles, makeUniqueFolderForTest, mergeBranchToCurrent } from "../utils/utils";
import {
appendSomeTextToFile,
cloneMockRepositoryToFolder,
commitEmptyFiles,
commitFiles,
commitModitication,
createEmptyFiles,
deleteFiles,
makeUniqueFolderForTest,
mergeBranchToCurrent,
} from "../utils/utils";

describe("Commit verification by scope tags script", () => {

Expand Down Expand Up @@ -334,4 +345,64 @@ describe("Commit verification by scope tags script", () => {

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


it("When a tagged file is deleted and some other file is modified in the same commit, after tagging the modified the commit is marked as verified", async () => {
const FOLDER_PATH = makeUniqueFolderForTest();
const REPO_PATH = cloneMockRepositoryToFolder(FOLDER_PATH);

const taggedFile = "src/tagged-file.js"; // to be deleted
const untaggedFile = "src/index.js"; // to be tagged

const database = new FileTagsDatabase(REPO_PATH);
const config = new ConfigFile(REPO_PATH);
const tags = new TagsDefinitionFile(REPO_PATH);

const repository = new GitRepository(REPO_PATH);

deleteFiles([taggedFile], REPO_PATH);
appendSomeTextToFile(join(REPO_PATH, untaggedFile));

repository.commitAllUsingNativeGitCommand("[TESTING] automatic message");

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

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

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

// Update database.json based on commit changes

const fileData: FileData[] = repository.getFileDataUsingNativeGitCommand(unpushedCommits[0]);

let fileDataToTag = database.updateDatabaseBasedOnChanges(fileData).filter(file => !config.isFileExtensionIgnored(file.newPath));

expect(fileDataToTag.length).toBe(1);

// Tag the untagged file and add relevancy

database.addSingleTagToFile({ tag: "Tag", module: "Default module" } as TagIdentifier, fileDataToTag[0].oldPath);
database.save();

// Amend these changes

const simulatedCommitMessage = `updated commit message with relevancy
__relevancy__[{"path":"src/index.js","relevancy":"HIGH","commit":"__current__"}]__relevancy__
`;

await repository.amendMostRecentCommit([database.getPath()], simulatedCommitMessage, true);

// Database should be up to date and commit should be verified

const newUnpushedCommits = await repository.getUnpushedCommits();
expect(newUnpushedCommits.length).toBe(1);

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

// Verified, because 1) deleted file should not be checked 2) modified file is correctly tagged

expect(newVerificationStatus).toBe(VerificationStatus.VERIFIED);
});
});
2 changes: 1 addition & 1 deletion test/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const deleteFiles = (
): void => {
for (const fileName of fileNames) {
const filePath = join(repositoryPath, fileName);
if (existsSync(filePath)) {
if (existsSync(resolve(filePath))) {
rimraf.sync(filePath);
} else {
console.debug(`[Delete files] File ${filePath} does not exist`);
Expand Down

0 comments on commit 9bcd5b8

Please sign in to comment.