Skip to content

Commit

Permalink
Tests for getTempDir (#79-3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Aug 8, 2024
1 parent 321e52d commit 743eaa0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as path from 'node:path';

export const CONFIG_KEY = 'vscode-deephaven';
export const CONFIG_CORE_SERVERS = 'core-servers';

Expand All @@ -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');
54 changes: 54 additions & 0 deletions src/util/downloadUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
);
});
12 changes: 10 additions & 2 deletions src/util/downloadUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 743eaa0

Please sign in to comment.