mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-20 10:15:17 -04:00
Utils: Fix getServerVersionFromPackageJson
The cache is never null, because it defaults to undefined, and therefore this function always returns undefined. Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
caa4b1c927
commit
dff5a635e0
2 changed files with 28 additions and 1 deletions
27
src/utils/serverVersion.spec.ts
Normal file
27
src/utils/serverVersion.spec.ts
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { promises as fs } from 'fs';
|
||||||
|
import { getServerVersionFromPackageJson } from './serverVersion';
|
||||||
|
|
||||||
|
it('getServerVersionFromPackageJson works', async () => {
|
||||||
|
const major = 2;
|
||||||
|
const minor = 0;
|
||||||
|
const patch = 0;
|
||||||
|
const preRelease = 'dev';
|
||||||
|
/* eslint-disable @typescript-eslint/require-await*/
|
||||||
|
jest.spyOn(fs, 'readFile').mockImplementationOnce(async (_) => {
|
||||||
|
return `{
|
||||||
|
"version": "${major}.${minor}.${patch}"
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
});
|
||||||
|
const serverVersion = await getServerVersionFromPackageJson();
|
||||||
|
expect(serverVersion.major).toEqual(major);
|
||||||
|
expect(serverVersion.minor).toEqual(minor);
|
||||||
|
expect(serverVersion.patch).toEqual(patch);
|
||||||
|
expect(serverVersion.preRelease).toEqual(preRelease);
|
||||||
|
});
|
|
@ -11,7 +11,7 @@ import { join as joinPath } from 'path';
|
||||||
let versionCache: ServerVersion;
|
let versionCache: ServerVersion;
|
||||||
|
|
||||||
export async function getServerVersionFromPackageJson(): Promise<ServerVersion> {
|
export async function getServerVersionFromPackageJson(): Promise<ServerVersion> {
|
||||||
if (versionCache === null) {
|
if (versionCache === undefined) {
|
||||||
const rawFileContent: string = await fs.readFile(
|
const rawFileContent: string = await fs.readFile(
|
||||||
joinPath(__dirname, '../../package.json'),
|
joinPath(__dirname, '../../package.json'),
|
||||||
{ encoding: 'utf8' },
|
{ encoding: 'utf8' },
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue