1/* 2 * Copyright (c) 2023-2024 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 16const fs = require('fs'); 17const path = require('path'); 18const diff = require('diff'); 19const { execSync } = require('child_process'); 20import { Extension } from '../src/common/type'; 21import { FileUtils } from '../src/utils/FileUtils'; 22 23const testDirectory = path.resolve('./test/local'); 24const NonExecutableFile = ['name_as_export_api_1.ts', 'name_as_import_api_1.ts', 'ohmurl_test.ts', 'ohmurl_test_new.ts', 25 'export_struct_transform_class.ts', 'nosymbolIdentifierTest.ts']; 26 27function runTest(filePath) { 28 try { 29 const command = `node ./node_modules/ts-node/dist/bin.js ${filePath}`; 30 execSync(command); 31 } catch (error) { 32 console.error(`Test case ${filePath} failed:`, error); 33 return false; 34 } 35 return true; 36} 37let successCount = 0; 38let failureCount = 0; 39let contentcomparationSuccessCount = 0; 40let contentcomparationFailureCount = 0; 41const failedFiles = []; 42const contentComparisionFailureFiles = []; 43 44function runTestsInDirectory(directoryPath) { 45 const files = fs.readdirSync(directoryPath); 46 47 for (const file of files) { 48 const filePath = path.join(directoryPath, file); 49 50 if (fs.statSync(filePath).isDirectory()) { 51 runTestsInDirectory(filePath); 52 } else if (filePath.includes('assert-expectation.ts')) { 53 const isSuccess = runTest(filePath); 54 if (isSuccess) { 55 successCount++; 56 } else { 57 failureCount++; 58 failedFiles.push(filePath); 59 } 60 } else if ((filePath.endsWith(Extension.TS) || filePath.endsWith(Extension.JS)) && !(filePath.endsWith(Extension.DETS) || 61 filePath.endsWith(Extension.DTS))) { 62 executeRunTest(file, filePath); 63 compareContent(filePath); 64 } else if (filePath.endsWith(Extension.DETS) || filePath.endsWith(Extension.DTS)) { 65 compareContent(filePath); 66 } 67 } 68} 69 70function executeRunTest(fileName, filePath) { 71 if (!NonExecutableFile.includes(fileName)) { 72 const isSuccess = runTest(filePath); 73 if (isSuccess) { 74 successCount++; 75 } else { 76 failureCount++; 77 failedFiles.push(filePath); 78 } 79 } 80} 81 82function compareContent(filePath) { 83 const sourcePath = filePath.replace('/test/local/', '/test/grammar/'); 84 const sourcePathAndExtension = FileUtils.getFileSuffix(sourcePath); 85 const expectationPath = sourcePathAndExtension.path + '_expected.txt'; 86 const resultPathAndExtension = FileUtils.getFileSuffix(filePath); 87 const resultCachePath = resultPathAndExtension.path + '.ts.cache.json'; 88 const expectationCachePath = sourcePathAndExtension.path + '_expected_cache.txt'; 89 const hasExpectationFile = fs.existsSync(expectationPath); 90 const hasExpectationCache = fs.existsSync(expectationCachePath); 91 const hasResultCache = fs.existsSync(resultCachePath); 92 const compareExpected = function(filePath, actual, expectation) { 93 if (actual.replace(/(\n|\r\n)/g, '') === expectation.replace(/(\n|\r\n)/g, '')) { 94 contentcomparationSuccessCount++; 95 } else { 96 contentcomparationFailureCount++; 97 contentComparisionFailureFiles.push(filePath); 98 const differences = diff.diffLines(actual, expectation); 99 differences.forEach(part => { 100 const color = part.added ? '\x1b[32m' : part.removed ? '\x1b[31m' : '\x1b[0m'; 101 console.log(color + part.value + '\x1b[0m'); 102 }); 103 } 104 }; 105 if (hasExpectationFile || (hasExpectationCache && hasResultCache)) { 106 if (hasExpectationFile) { 107 let actual = fs.readFileSync(filePath).toString(); 108 let expectation = fs.readFileSync(expectationPath).toString(); 109 compareExpected(filePath, actual, expectation); 110 } 111 if (hasExpectationCache) { 112 let actual = fs.readFileSync(resultCachePath).toString(); 113 let expectation = fs.readFileSync(expectationCachePath).toString(); 114 compareExpected(filePath, actual, expectation); 115 } 116 } 117} 118 119runTestsInDirectory(testDirectory); 120 121console.log('--- Grammar Test Results ---'); 122console.log(`Success count: ${successCount}`); 123console.log(`Failure count: ${failureCount}`); 124if (failureCount > 0) { 125 console.log('Execution failed files:'); 126 for (const failedFile of failedFiles) { 127 console.log(failedFile); 128 } 129} 130 131console.log(`Content comparison Success count: ${contentcomparationSuccessCount}`); 132console.log(`Content comparison Failure count: ${contentcomparationFailureCount}`); 133if (contentcomparationFailureCount > 0) { 134 console.log('Content comparision failed files:'); 135 for (const failedFile of contentComparisionFailureFiles) { 136 console.log(failedFile); 137 } 138}