add more gitUtils tests

This commit is contained in:
sebastien 2025-11-06 18:44:23 +01:00
parent 4fa2e093ac
commit 2491491328
3 changed files with 76 additions and 30 deletions

View File

@ -10,7 +10,12 @@ import fs from 'fs-extra';
import path from 'path';
import {createTempRepo} from '@testing-utils/git';
import execa from 'execa';
import {FileNotTrackedError, getFileCommitDate,getGitLastUpdate} from '../gitUtils';
import {
FileNotTrackedError,
getFileCommitDate,
getGitLastUpdate,
getGitCreation,
} from '../gitUtils';
/* eslint-disable no-restricted-properties */
function initializeTempRepo() {
@ -251,3 +256,52 @@ describe('getGitLastUpdate', () => {
await fs.unlink(tempFilePath2);
});
});
describe('test repo commit info', () => {
const repoDir = initializeTempRepo();
it('returns creation info for test.txt', async () => {
const filePath = path.join(repoDir, 'test.txt');
await expect(getGitCreation(filePath)).resolves.toEqual({
author: 'Caroline',
timestamp: new Date('2020-06-19').getTime(),
});
await expect(getGitLastUpdate(filePath)).resolves.toEqual({
author: 'Caroline',
timestamp: new Date('2020-09-13').getTime(),
});
});
it('returns creation info for dest.txt', async () => {
const filePath = path.join(repoDir, 'dest.txt');
await expect(getGitCreation(filePath)).resolves.toEqual({
author: 'Caroline',
timestamp: new Date('2020-09-13').getTime(),
});
await expect(getGitLastUpdate(filePath)).resolves.toEqual({
author: 'Josh-Cena',
timestamp: new Date('2020-11-13').getTime(),
});
});
it('returns creation info for untracked.txt', async () => {
const filePath = path.join(repoDir, 'untracked.txt');
await expect(getGitCreation(filePath)).resolves.toEqual(null);
await expect(getGitLastUpdate(filePath)).resolves.toEqual(null);
});
it('returns creation info for non-existing.txt', async () => {
const filePath = path.join(repoDir, 'non-existing.txt');
await expect(
getGitCreation(filePath),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"An error occurred when trying to get the last update date"`,
);
await expect(
getGitLastUpdate(filePath),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"An error occurred when trying to get the last update date"`,
);
});
});

View File

@ -83,7 +83,7 @@ export async function getFileCommitDate(
},
): Promise<{
/** Relevant commit date. */
date: Date;
date: Date; // TODO duplicate data, not really useful?
/** Timestamp returned from git, converted to **milliseconds**. */
timestamp: number;
}>;
@ -206,36 +206,23 @@ export async function getFileCommitDate(
return {date, timestamp};
}
type GitLastUpdateResult = {
/**
* A timestamp in **milliseconds**
* `undefined`: not read
* `null`: no value to read (usual for untracked files)
*/
timestamp: number | undefined | null;
/**
* The Git author's name
* `undefined`: not read
* `null`: no value to read (usual for untracked files)
*/
author: string | undefined | null;
};
let showedGitRequirementError = false;
let showedFileNotTrackedError = false;
export async function getGitLastUpdate(
type GitCommitInfo = {timestamp: number; author: string};
async function getGitCommitInfo(
filePath: string,
): Promise<GitLastUpdateResult | null> {
age: 'oldest' | 'newest',
): Promise<GitCommitInfo | null> {
if (!filePath) {
return null;
}
// Wrap in try/catch in case the shell commands fail
// (e.g. project doesn't use Git, etc).
try {
const result = await getFileCommitDate(filePath, {
age: 'newest',
age,
includeAuthor: true,
});
return {timestamp: result.timestamp, author: result.author};
@ -262,3 +249,15 @@ export async function getGitLastUpdate(
return null;
}
}
export async function getGitLastUpdate(
filePath: string,
): Promise<GitCommitInfo | null> {
return getGitCommitInfo(filePath, 'newest');
}
export async function getGitCreation(
filePath: string,
): Promise<GitCommitInfo | null> {
return getGitCommitInfo(filePath, 'oldest');
}

View File

@ -9,8 +9,8 @@ import {
FileNotTrackedError,
getFileCommitDate,
GitNotFoundError,
getGitLastUpdate,
} from './gitUtils';
import {getGitLastUpdate} from '../lastUpdateUtils';
import type {VcsConfig} from '@docusaurus/types';
/**
@ -52,13 +52,6 @@ export const VcsGitAdHoc: VcsConfig = {
// TODO non-ideal integration but good enough for now
// This keeps this new VscConfig system retro-compatible with the existing
// historical Docusaurus behavior based on Git
const result = await getGitLastUpdate(filePath);
if (result === null) {
return null;
}
return {
timestamp: result.lastUpdatedAt!,
author: result.lastUpdatedBy!,
};
return getGitLastUpdate(filePath);
},
};