| 1 | import fs from "fs"; |
| 2 | import os from "os"; |
| 3 | import path from "path"; |
| 4 | import { localPathOrUriToPath, localPathToUri } from "../util/pathToUri"; |
| 5 | |
| 6 | // Want this outside of the git repository so we can change branches in tests |
| 7 | export const TEST_DIR_PATH = path.join(os.tmpdir(), "testWorkspaceDir"); |
| 8 | export const TEST_DIR = localPathToUri(TEST_DIR_PATH); // URI |
| 9 | |
| 10 | export function setUpTestDir() { |
| 11 | if (fs.existsSync(TEST_DIR_PATH)) { |
| 12 | fs.rmSync(TEST_DIR_PATH, { recursive: true }); |
| 13 | } |
| 14 | fs.mkdirSync(TEST_DIR_PATH); |
| 15 | } |
| 16 | |
| 17 | export function tearDownTestDir() { |
| 18 | if (fs.existsSync(TEST_DIR_PATH)) { |
| 19 | fs.rmSync(TEST_DIR_PATH, { recursive: true }); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | /* |
| 24 | accepts array of items in 3 formats, e.g. |
| 25 | "index/" creates index directory |
| 26 | "index/index.ts" creates an empty index/index.ts |
| 27 | ["index/index.ts", "hello"] creates index/index.ts with contents "hello" |
| 28 | */ |
| 29 | export function addToTestDir(pathsOrUris: (string | [string, string])[]) { |
| 30 | // Allow tests to use URIs or local paths |
| 31 | const paths = pathsOrUris.map((val) => { |
| 32 | if (Array.isArray(val)) { |
| 33 | return [localPathOrUriToPath(val[0]), val[1]]; |
| 34 | } else { |
| 35 | return localPathOrUriToPath(val); |
| 36 | } |
| 37 | }); |
| 38 | |
| 39 | for (const p of paths) { |
| 40 | const filepath = path.join(TEST_DIR_PATH, Array.isArray(p) ? p[0] : p); |
| 41 | fs.mkdirSync(path.dirname(filepath), { recursive: true }); |
| 42 | |
| 43 | if (Array.isArray(p)) { |
| 44 | fs.writeFileSync(filepath, p[1]); |
| 45 | } else if (p.endsWith("/")) { |
| 46 | fs.mkdirSync(filepath, { recursive: true }); |
| 47 | } else { |
| 48 | fs.writeFileSync(filepath, ""); |
| 49 | } |
| 50 | } |
| 51 | } |