]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/_utils/index.js
import 7.12.1 upstream release
[pve-eslint.git] / eslint / tests / _utils / index.js
1 /**
2 * @fileoverview Utilities used in tests
3 */
4
5 "use strict";
6
7 //-----------------------------------------------------------------------------
8 // Requirements
9 //-----------------------------------------------------------------------------
10
11 const {
12 defineInMemoryFs,
13 defineConfigArrayFactoryWithInMemoryFileSystem,
14 defineCascadingConfigArrayFactoryWithInMemoryFileSystem,
15 defineFileEnumeratorWithInMemoryFileSystem,
16 defineCLIEngineWithInMemoryFileSystem,
17 defineESLintWithInMemoryFileSystem
18 } = require("./in-memory-fs");
19
20 const { createTeardown, addFile } = require("fs-teardown");
21
22 //-----------------------------------------------------------------------------
23 // Helpers
24 //-----------------------------------------------------------------------------
25
26 /**
27 * Prevents leading spaces in a multiline template literal from appearing in the resulting string
28 * @param {string[]} strings The strings in the template literal
29 * @param {any[]} values The interpolation values in the template literal.
30 * @returns {string} The template literal, with spaces removed from all lines
31 */
32 function unIndent(strings, ...values) {
33 const text = strings
34 .map((s, i) => (i === 0 ? s : values[i - 1] + s))
35 .join("");
36 const lines = text.replace(/^\n/u, "").replace(/\n\s*$/u, "").split("\n");
37 const lineIndents = lines.filter(line => line.trim()).map(line => line.match(/ */u)[0].length);
38 const minLineIndent = Math.min(...lineIndents);
39
40 return lines.map(line => line.slice(minLineIndent)).join("\n");
41 }
42
43 /**
44 * Creates a new filesystem volume at the given location with the given files.
45 * @param {Object} desc A description of the filesystem volume to create.
46 * @param {string} desc.cwd The current working directory ESLint is using.
47 * @param {Object} desc.files A map of filename to file contents to create.
48 * @returns {Teardown} An object with prepare(), cleanup(), and getPath()
49 * methods.
50 */
51 function createCustomTeardown({ cwd, files }) {
52 const { prepare, cleanup, getPath } = createTeardown(
53 cwd,
54 ...Object.keys(files).map(filename => addFile(filename, files[filename]))
55 );
56
57 return { prepare, cleanup, getPath };
58 }
59
60 //-----------------------------------------------------------------------------
61 // Exports
62 //-----------------------------------------------------------------------------
63
64 module.exports = {
65 unIndent,
66 defineInMemoryFs,
67 defineConfigArrayFactoryWithInMemoryFileSystem,
68 defineCascadingConfigArrayFactoryWithInMemoryFileSystem,
69 defineFileEnumeratorWithInMemoryFileSystem,
70 defineCLIEngineWithInMemoryFileSystem,
71 defineESLintWithInMemoryFileSystem,
72 createCustomTeardown
73 };