idlize
65 строк · 2.0 Кб
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*/
15
16import * as path from "path"
17import * as fs from "fs"
18import { assert } from "chai"
19
20let logText: string = ""
21
22export function log(msg: string) {
23logText = logText.concat(msg, "\n")
24}
25
26export function getLog(): string {
27return logText
28}
29
30export function filterDirname(text: string): string {
31// tsc AST on windows uses C:/foo/bar representation, but
32// __dirname on windows returns C:\foo\bar.
33const platformDirname = __dirname.replaceAll(path.sep, "/")
34return text.replaceAll(platformDirname, "DIRNAME")
35}
36
37export function getLogFiltered(): string {
38return filterDirname(logText)
39}
40
41export function resetLog() {
42logText = ""
43}
44
45export function cleanDumpDirectory() {
46fs.rmSync(`${__dirname}/dump}`, {recursive: true, force: true})
47}
48
49export function loadDump(name: string, kind: string, module: string): string|undefined {
50const dirname = __dirname
51const dumpFile = `${dirname}/${kind}/${name}_${module}.test.ts_dump`
52const dump = fs.readFileSync(dumpFile, 'utf8')
53return (dump && dump.includes('\r'))
54? dump.replace(/[\r]/g, '')
55: dump
56}
57
58export function checkDump(name: string, module: string) {
59test(`Transformed ${name} dump matches the golden dump`, () => {
60const test = loadDump(name, "dump", module)
61const testFiltered = test ? filterDirname(test) : undefined
62const golden = loadDump(name, "golden", module)
63assert.equal(golden, testFiltered)
64})
65}
66