From 743eaa05bd591f88105ac658bb5d6ef647aee467 Mon Sep 17 00:00:00 2001 From: Brian Ingles Date: Thu, 8 Aug 2024 12:15:15 -0500 Subject: [PATCH] Tests for getTempDir (#79-3) --- src/common/constants.ts | 4 +++ src/util/downloadUtils.spec.ts | 54 ++++++++++++++++++++++++++++++++++ src/util/downloadUtils.ts | 12 ++++++-- 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/util/downloadUtils.spec.ts diff --git a/src/common/constants.ts b/src/common/constants.ts index 98a29f2a..00f1819f 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -1,3 +1,5 @@ +import * as path from 'node:path'; + export const CONFIG_KEY = 'vscode-deephaven'; export const CONFIG_CORE_SERVERS = 'core-servers'; @@ -17,3 +19,5 @@ export const DOWNLOAD_LOGS_TEXT = 'Download Logs'; export const SERVER_LANGUAGE_SET = new Set(['python', 'groovy']) as ReadonlySet< 'python' | 'groovy' >; + +export const TMP_DIR_ROOT = path.join(__dirname, '..', 'tmp'); diff --git a/src/util/downloadUtils.spec.ts b/src/util/downloadUtils.spec.ts new file mode 100644 index 00000000..4c204c05 --- /dev/null +++ b/src/util/downloadUtils.spec.ts @@ -0,0 +1,54 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import { getTempDir } from './downloadUtils'; +import { TMP_DIR_ROOT } from '../common'; + +vi.mock('node:fs'); + +beforeEach(() => { + vi.clearAllMocks(); +}); + +describe('getTempDir', () => { + it.each([ + [true, undefined, TMP_DIR_ROOT], + [true, 'subDirectory', path.join(TMP_DIR_ROOT, 'subDirectory')], + [false, undefined, TMP_DIR_ROOT], + [false, 'subDirectory', path.join(TMP_DIR_ROOT, 'subDirectory')], + ])( + 'should create temp directory if it does not already exist: %s, %s', + (dirExists, subDirectory, expectedPath) => { + vi.mocked(fs.existsSync).mockReturnValue(dirExists); + getTempDir(true, subDirectory); + + expect(fs.existsSync).toHaveBeenCalledWith(expectedPath); + + if (dirExists) { + expect(fs.mkdirSync).not.toHaveBeenCalled(); + } else { + expect(fs.mkdirSync).toHaveBeenCalledWith(expectedPath); + } + } + ); + + it.each([ + [true, undefined, TMP_DIR_ROOT], + [true, 'subDirectory', path.join(TMP_DIR_ROOT, 'subDirectory')], + [false, undefined, TMP_DIR_ROOT], + [false, 'subDirectory', path.join(TMP_DIR_ROOT, 'subDirectory')], + ])( + 'should remove directory if recreate is true: %s, %s, %s', + (recreate, subDirectory, expectedPath) => { + getTempDir(recreate, subDirectory); + + if (recreate) { + expect(fs.rmSync).toHaveBeenCalledWith(expectedPath, { + recursive: true, + }); + } else { + expect(fs.rmSync).not.toHaveBeenCalled(); + } + } + ); +}); diff --git a/src/util/downloadUtils.ts b/src/util/downloadUtils.ts index 04c62bf5..8627100f 100644 --- a/src/util/downloadUtils.ts +++ b/src/util/downloadUtils.ts @@ -2,12 +2,20 @@ import * as fs from 'node:fs'; import * as http from 'node:http'; import * as https from 'node:https'; import * as path from 'node:path'; +import { TMP_DIR_ROOT } from '../common'; import { Logger } from './Logger'; const logger = new Logger('downloadUtils'); -export function getTempDir(recreate: boolean, subDirectory?: string) { - let tempDir = path.join(__dirname, '..', 'tmp'); +/** + * Return the path of the temp directory with optional sub directory. If recreate + * is true, the directory will be deleted and recreated. + * @param recreate If true, delete and recreate the directory + * @param subDirectory Optional sub directory to create + * @returns The path of the temp directory + */ +export function getTempDir(recreate: boolean, subDirectory?: string): string { + let tempDir = TMP_DIR_ROOT; if (subDirectory != null) { tempDir = path.join(tempDir, subDirectory); }