mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-09 14:22:27 +02:00
test: add tests for gitUtils (#6949)
* test: add tests for gitUtils * debug test * try fix
This commit is contained in:
parent
3c7b39b739
commit
5ee7e8c48e
3 changed files with 194 additions and 19 deletions
|
@ -12,7 +12,22 @@ export class GitNotFoundError extends Error {}
|
|||
|
||||
export class FileNotTrackedError extends Error {}
|
||||
|
||||
export const getFileCommitDate = (
|
||||
export function getFileCommitDate(
|
||||
file: string,
|
||||
args: {age?: 'oldest' | 'newest'; includeAuthor?: false},
|
||||
): {
|
||||
date: Date;
|
||||
timestamp: number;
|
||||
};
|
||||
export function getFileCommitDate(
|
||||
file: string,
|
||||
args: {age?: 'oldest' | 'newest'; includeAuthor: true},
|
||||
): {
|
||||
date: Date;
|
||||
timestamp: number;
|
||||
author: string;
|
||||
};
|
||||
export function getFileCommitDate(
|
||||
file: string,
|
||||
{
|
||||
age = 'oldest',
|
||||
|
@ -25,7 +40,7 @@ export const getFileCommitDate = (
|
|||
date: Date;
|
||||
timestamp: number;
|
||||
author?: string;
|
||||
} => {
|
||||
} {
|
||||
if (!shell.which('git')) {
|
||||
throw new GitNotFoundError(
|
||||
`Failed to retrieve git history for "${file}" because git is not installed.`,
|
||||
|
@ -38,9 +53,6 @@ export const getFileCommitDate = (
|
|||
);
|
||||
}
|
||||
|
||||
const fileBasename = path.basename(file);
|
||||
const fileDirname = path.dirname(file);
|
||||
|
||||
let formatArg = '--format=%ct';
|
||||
if (includeAuthor) {
|
||||
formatArg += ',%an';
|
||||
|
@ -54,10 +66,10 @@ export const getFileCommitDate = (
|
|||
}
|
||||
|
||||
const result = shell.exec(
|
||||
`git log ${extraArgs} ${formatArg} -- "${fileBasename}"`,
|
||||
`git log ${extraArgs} ${formatArg} -- "${path.basename(file)}"`,
|
||||
{
|
||||
// cwd is important, see: https://github.com/facebook/docusaurus/pull/5048
|
||||
cwd: fileDirname,
|
||||
cwd: path.dirname(file),
|
||||
silent: true,
|
||||
},
|
||||
);
|
||||
|
@ -81,22 +93,17 @@ export const getFileCommitDate = (
|
|||
|
||||
const match = output.match(regex);
|
||||
|
||||
if (
|
||||
!match ||
|
||||
!match.groups ||
|
||||
!match.groups.timestamp ||
|
||||
(includeAuthor && !match.groups.author)
|
||||
) {
|
||||
if (!match) {
|
||||
throw new Error(
|
||||
`Failed to retrieve the git history for file "${file}" with unexpected output: ${output}`,
|
||||
);
|
||||
}
|
||||
|
||||
const timestamp = Number(match.groups.timestamp);
|
||||
const timestamp = Number(match.groups!.timestamp);
|
||||
const date = new Date(timestamp * 1000);
|
||||
|
||||
if (includeAuthor) {
|
||||
return {date, timestamp, author: match.groups.author};
|
||||
return {date, timestamp, author: match.groups!.author!};
|
||||
}
|
||||
return {date, timestamp};
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue