From 249149132825559e61b3ef495dfa949f302aaf10 Mon Sep 17 00:00:00 2001 From: sebastien Date: Thu, 6 Nov 2025 18:44:23 +0100 Subject: [PATCH] add more gitUtils tests --- .../src/vcs/__tests__/gitUtils.test.ts | 56 ++++++++++++++++++- packages/docusaurus-utils/src/vcs/gitUtils.ts | 39 +++++++------ .../docusaurus-utils/src/vcs/vcsGitAdHoc.ts | 11 +--- 3 files changed, 76 insertions(+), 30 deletions(-) diff --git a/packages/docusaurus-utils/src/vcs/__tests__/gitUtils.test.ts b/packages/docusaurus-utils/src/vcs/__tests__/gitUtils.test.ts index 6d9b5f77b5..978e903376 100644 --- a/packages/docusaurus-utils/src/vcs/__tests__/gitUtils.test.ts +++ b/packages/docusaurus-utils/src/vcs/__tests__/gitUtils.test.ts @@ -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"`, + ); + }); +}); diff --git a/packages/docusaurus-utils/src/vcs/gitUtils.ts b/packages/docusaurus-utils/src/vcs/gitUtils.ts index 09a21a74d0..9d97021fe4 100644 --- a/packages/docusaurus-utils/src/vcs/gitUtils.ts +++ b/packages/docusaurus-utils/src/vcs/gitUtils.ts @@ -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 { + age: 'oldest' | 'newest', +): Promise { 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 { + return getGitCommitInfo(filePath, 'newest'); +} + +export async function getGitCreation( + filePath: string, +): Promise { + return getGitCommitInfo(filePath, 'oldest'); +} diff --git a/packages/docusaurus-utils/src/vcs/vcsGitAdHoc.ts b/packages/docusaurus-utils/src/vcs/vcsGitAdHoc.ts index b0fb0dafb8..297bc4f79c 100644 --- a/packages/docusaurus-utils/src/vcs/vcsGitAdHoc.ts +++ b/packages/docusaurus-utils/src/vcs/vcsGitAdHoc.ts @@ -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); }, };