@git-temporal/git-log-scraper
Version:
Scrapes commit data from the git cli for a file or directory in a repository and returns an array of Javascript objects representing commits with files and lines added and deleted.
128 lines (127 loc) • 6.17 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = __importStar(require("path"));
const _ = __importStar(require("lodash"));
const commons_1 = require("@git-temporal/commons");
const gitLogScraper_1 = require("./gitLogScraper");
const PATH_TO_TEST = 'packages/git-temporal-react';
const FILE_TO_TEST = 'package.json';
const PATH_COMMITS_TO_TEST = 50;
const FILE_COMMITS_TO_TEST = 30;
const gitRoot = commons_1.findGitRoot('.');
describe('git-log-scraper', () => {
describe('getCommitHistory()', () => {
describe('for directory', () => {
it('should return commit history for our packages directory as relative to root dir', () => {
const history = gitLogScraper_1.getCommitHistory(`./${PATH_TO_TEST}`);
testHistoryIntegrity(history, false, PATH_COMMITS_TO_TEST, PATH_TO_TEST);
});
it('should return commit history for our packages directory as absolute dir', () => {
const history = gitLogScraper_1.getCommitHistory(path.resolve(gitRoot, PATH_TO_TEST));
testHistoryIntegrity(history, false, PATH_COMMITS_TO_TEST, PATH_TO_TEST);
});
});
describe('for file', () => {
it('should return commit history for our packages directory as relative to root dir', () => {
const history = gitLogScraper_1.getCommitHistory(`./${FILE_TO_TEST}`);
testHistoryIntegrity(history, true, FILE_COMMITS_TO_TEST, FILE_TO_TEST);
});
it('should return commit history for our packages directory as absolute dir', () => {
const history = gitLogScraper_1.getCommitHistory(path.resolve(gitRoot, FILE_TO_TEST));
testHistoryIntegrity(history, true, FILE_COMMITS_TO_TEST, FILE_TO_TEST);
});
});
describe('with skip and maxCount options', () => {
it('should return correct numbers of things', () => {
const fullHistory = gitLogScraper_1.getCommitHistory(PATH_TO_TEST);
expect(fullHistory.commits.length).toBeGreaterThan(PATH_COMMITS_TO_TEST);
const first10 = gitLogScraper_1.getCommitHistory(PATH_TO_TEST, { maxCount: 10 });
const remainingCommits = gitLogScraper_1.getCommitHistory(PATH_TO_TEST, { skip: 10 });
expect(first10.commits.length).toEqual(10);
expect(first10.commits.length + remainingCommits.commits.length).toEqual(fullHistory.commits.length);
});
});
});
describe('getCommitRange()', () => {
describe('for directory', () => {
it('should have known first commit and minimum numbers with relative path', () => {
const filePath = `./${PATH_TO_TEST}`;
const commitRange = gitLogScraper_1.getCommitRange(filePath);
testRangeIntegrity(commitRange, PATH_COMMITS_TO_TEST, filePath);
});
it('should have known first commit and minimum numbers with absolute path', () => {
const filePath = path.resolve(gitRoot, PATH_TO_TEST);
const commitRange = gitLogScraper_1.getCommitRange(filePath);
testRangeIntegrity(commitRange, PATH_COMMITS_TO_TEST, filePath);
});
});
describe('for file', () => {
it('should have known first commit and minimum numbers with relative path', () => {
const filePath = `./${FILE_TO_TEST}`;
const commitRange = gitLogScraper_1.getCommitRange(filePath);
testRangeIntegrity(commitRange, FILE_COMMITS_TO_TEST, filePath);
});
it('should have known first commit and minimum numbers with absolute path', () => {
const filePath = path.resolve(gitRoot, FILE_TO_TEST);
const commitRange = gitLogScraper_1.getCommitRange(filePath);
testRangeIntegrity(commitRange, FILE_COMMITS_TO_TEST, filePath);
});
});
});
});
function testHistoryIntegrity(history, isFile, numberOfCommits, path) {
expect(history.isFile).toEqual(isFile);
expectAtLeastNCommits(history, numberOfCommits);
expectAllCommitFilesFromPath(history, path);
expectSumLinesAddedAndDeletedToMatchFiles(history);
expectFirstNToMatchSnapshot(history, numberOfCommits);
}
function testRangeIntegrity(range, numberOfCommits, path) {
expect(range.gitRoot).toEqual(gitRoot);
expect(range.count).toBeGreaterThan(numberOfCommits);
expect(range.path).toEqual(path);
expect(range.existsLocally).toEqual(true);
expect(range.firstCommit).toMatchSnapshot();
}
function expectAtLeastNCommits(history, n) {
expect(history.commits.length).toBeGreaterThan(n);
}
function expectAllCommitFilesFromPath(history, path) {
const foundIrregularities = [];
for (const commit of history.commits) {
for (const file of commit.files) {
if (!file.name.startsWith(path)) {
foundIrregularities.push(file);
}
}
}
expect(foundIrregularities).toEqual([]);
}
function expectSumLinesAddedAndDeletedToMatchFiles(history) {
const foundIrregularities = [];
for (const commit of history.commits) {
let sumAdded = 0;
let sumDeleted = 0;
for (const file of commit.files) {
sumAdded += file.linesAdded;
sumDeleted += file.linesDeleted;
}
if (sumAdded !== commit.linesAdded || sumDeleted !== commit.linesDeleted) {
foundIrregularities.push(commit);
}
}
expect(foundIrregularities).toEqual([]);
}
function expectFirstNToMatchSnapshot(history, n) {
const first190 = history.commits
.slice(n * -1)
.map(obj => _.omit(obj, ['relativeDate', 'index']));
expect(first190).toMatchSnapshot();
}