1
>
/*---------------------------------------------------------------------------------------------
snapshot.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import { Lazy } from '../../common/lazy.js';
7
>
import { FileAccess } from '../../common/network.js';
8
>
import { URI } from '../../common/uri.js';
9
>
10
>
declare const __readFileInTests: (path: string) => Promise<string>;
11
>
declare const __writeFileInTests: (path: string, contents: string) => Promise<void>;
12
>
declare const __readDirInTests: (path: string) => Promise<string[]>;
13
>
declare const __unlinkInTests: (path: string) => Promise<void>;
14
>
declare const __mkdirPInTests: (path: string) => Promise<void>;
15
>
16
>
// setup on import so assertSnapshot has the current context without explicit passing
17
>
let context: Lazy<SnapshotContext> | undefined;
18
>
const sanitizeName = (name: string) => name.replace(/[^a-z0-9_-]/gi, '_');
19
>
const normalizeCrlf = (str: string) => str.replace(/\r\n/g, '\n');
20
>
21
>
export interface ISnapshotOptions {
22
>
/** Name for snapshot file, rather than an incremented number */
23
>
name?: string;
24
>
/** Extension name of the snapshot file, defaults to `.snap` */
25
>
extension?: string;
26
>
}
27
>
28
>
/**
29
>
* This is exported only for tests against the snapshotting itself! Use
30
>
* {@link assertSnapshot} as a consumer!
31
>
*/
32
>
export class SnapshotContext {
33
>
private nextIndex = 0;
34
>
protected snapshotsDir: URI;
35
>
private readonly namePrefix: string;
36
>
private readonly usedNames = new Set();
37
>
38
>
constructor(private readonly test: Mocha.Test | undefined) {
39
if (!test) {
40
throw new Error('assertSnapshot can only be used in a test');