idlize
55 строк · 2.1 Кб
1/*
2* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15import * as path from "path";
16import * as fs from "fs";
17import { assert } from "chai";
18let logText: string = "";
19export function log(msg: string) {
20logText = logText.concat(msg, "\n");
21}
22export function getLog(): string {
23return logText;
24}
25export function filterDirname(text: string): string {
26// tsc AST on windows uses C:/foo/bar representation, but
27// __dirname on windows returns C:\foo\bar.
28const platformDirname = __dirname.replaceAll(path.sep, "/");
29return text.replaceAll(platformDirname, "DIRNAME");
30}
31export function getLogFiltered(): string {
32return filterDirname(logText);
33}
34export function resetLog() {
35logText = "";
36}
37export function cleanDumpDirectory() {
38fs.rmSync(`${__dirname}/dump}`, { recursive: true, force: true });
39}
40export function loadDump(name: string, kind: string, module: string): string | undefined {
41const dirname = __dirname;
42const dumpFile = `${dirname}/${kind}/${name}_${module}.test.ts_dump`;
43const dump = fs.readFileSync(dumpFile, 'utf8');
44return (dump && dump.includes('\r'))
45? dump.replace(/[\r]/g, '')
46: dump;
47}
48export function checkDump(name: string, module: string) {
49test(`Transformed ${name} dump matches the golden dump`, () => {
50const test = loadDump(name, "dump", module);
51const testFiltered = test ? filterDirname(test) : undefined;
52const golden = loadDump(name, "golden", module);
53assert.equal(golden, testFiltered);
54});
55}
56
57