diff --git a/src/vscode_plugin/src/common/file.ts b/src/vscode_plugin/src/common/file.ts index 0bb974e2deaf553792dfed5371f43117df46a1db..f68628ab207bec1b6af3900092133a28ea22417a 100644 --- a/src/vscode_plugin/src/common/file.ts +++ b/src/vscode_plugin/src/common/file.ts @@ -16,43 +16,101 @@ const vscode = require("vscode"); import * as fs from 'fs'; import { getGenerateConf, GEN_TYPE } from './conf'; +import { getCurrentTimeString } from './tool'; +import * as logs from './log'; -export function saveFileSync(filePath: string, fileContent: string) { - let genType = getGenerateConf(); - if (!fs.existsSync(filePath)) { - // TODO: if file exist, replace, append or new file - switch (genType) { - case GEN_TYPE.GEN_APPEND: - break; - case GEN_TYPE.GEN_REPLACE: - break; - case GEN_TYPE.GEN_NEW: - break; - default: - break; +export class Filer { + genType: number = GEN_TYPE.GEN_NEW; + private static instance: Filer; + + constructor() { + this.genType = getGenerateConf(); + } + + static getInstance(): Filer { + if (!Filer.instance) { + Filer.instance = new Filer(); } - fs.writeFileSync(filePath, fileContent); - } else { - fs.writeFileSync(filePath, fileContent); + return Filer.instance; + } + + setGenType(value: number) { + this.genType = value; + } + + getGenType(): number { + return this.genType; + } + + getNewFileName(filePath: string): string { + let pathList = filePath.split('.'); + let prevFile = ''; + for (let i=0;i { + vscode.window.showInformationMessage('Start all tests.'); + + //1, 测试getGenerateConf + test('getGenerateConf_test_1', () => { + let genType = getGenerateConf(); + assert.strictEqual(genType, 1); + }); + + //1, 测试getLogPath + test('getLogPath_test_1', () => { + let logPath = getLogPath(); + assert.strictEqual(logPath, './'); + }); + + //1, 测试getLogName + test('getLogName_test_1', () => { + let logName = getLogName(); + assert.strictEqual(logName, 'dmesg.log'); + }); + +}); diff --git a/src/vscode_plugin/src/test/suite/common/file.test.ts b/src/vscode_plugin/src/test/suite/common/file.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..f8cbd0edac82f9e747c5ddb3e37ee3fecb6908b3 --- /dev/null +++ b/src/vscode_plugin/src/test/suite/common/file.test.ts @@ -0,0 +1,208 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import * as assert from 'assert'; + +// You can import and use all API from the 'vscode' module +// as well as import your extension to test it +import * as vscode from 'vscode'; +import * as fs from 'fs'; +import * as files from '../../../common/file' +import { GEN_TYPE } from '../../../common/conf'; +// import * as myExtension from '../../extension'; + +suite('Common_File_Test_Suite', () => { + vscode.window.showInformationMessage('Start all tests.'); + + //1, 测试一般情况 + test('saveFileSync_new_test_1', () => { + let fPath = './testfile.txt'; + if (fs.existsSync(fPath)) { + fs.unlinkSync(fPath); + } + files.Filer.getInstance().saveFileSync(fPath, 'test file'); + let isExist = fs.existsSync(fPath); + assert.strictEqual(isExist, true); + if (fs.existsSync(fPath)) { + fs.unlinkSync(fPath); + } + }); + + //2, 测试边界情况 + test('saveFileSync_new_test_2', () => { + // 短文件名 + let fPath = './1.txt'; + let filer = files.Filer.getInstance(); + filer.saveFileSync(fPath, 'test file'); + let isExist = fs.existsSync(fPath); + assert.strictEqual(isExist, true); + // 长文件名 + fPath = './1'; + for (let i=0;i<100;i++) { + fPath = './1' + i; + } + fPath = fPath + '.txt'; + filer.saveFileSync(fPath, 'test file'); + isExist = fs.existsSync(fPath); + assert.strictEqual(isExist, true); + // 中文文件名 + fPath = './测试中文.txt'; + filer.saveFileSync(fPath, 'test file'); + isExist = fs.existsSync(fPath); + assert.strictEqual(isExist, true); + }); + + //3, 测试异常情况 + test('saveFileSync_new_test_3', () => { + let resultStr = '' + let fPath = './1.txt'; + let filer = files.Filer.getInstance(); + filer.saveFileSync(fPath, 'test file'); + let isExist = fs.existsSync(fPath); + assert.strictEqual(isExist, true); + let fcontent = fs.readFileSync(fPath); + assert.strictEqual(fcontent.toString(), 'test file'); + + filer.saveFileSync(fPath, 'test file'); + isExist = fs.existsSync(fPath); + assert.strictEqual(isExist, true); + fcontent = fs.readFileSync(fPath); + assert.strictEqual(fcontent.toString(), 'test file'); + }); + + //4, 测试错误情况 + test('saveFileSync_new_test_4', () => { + let fPath = './'; + let filer = files.Filer.getInstance(); + try { + filer.saveFileSync(fPath, 'test file'); + } catch (e) { + let isError = true; + assert.strictEqual(isError, true); + let emsg = JSON.stringify(e); + let isLike = emsg.includes('EISDIR'); + assert.strictEqual(isLike, true); + } + }); + + //1, 测试一般情况 + test('saveFileSync_append_test_1', () => { + files.Filer.getInstance().setGenType(GEN_TYPE.GEN_APPEND); + let fPath = './testfile.txt'; + if (fs.existsSync(fPath)) { + fs.unlinkSync(fPath); + } + + let filer = files.Filer.getInstance(); + filer.saveFileSync(fPath, 'test file'); + let isExist = fs.existsSync(fPath); + assert.strictEqual(isExist, true); + let fcontent = fs.readFileSync(fPath); + assert.strictEqual(fcontent.toString(), 'test file'); + + filer.saveFileSync(fPath, 'test file'); + isExist = fs.existsSync(fPath); + assert.strictEqual(isExist, true); + fcontent = fs.readFileSync(fPath); + assert.strictEqual(fcontent.toString(), 'test file'+'test file'); + if (fs.existsSync(fPath)) { + fs.unlinkSync(fPath); + } + }); + + //2, 测试边界情况 + test('saveFileSync_append_test_2', () => { + // 短文件名 + let fPath = './1.txt'; + if (fs.existsSync(fPath)) { + fs.unlinkSync(fPath); + } + let filer = files.Filer.getInstance(); + filer.saveFileSync(fPath, 'test file'); + let isExist = fs.existsSync(fPath); + assert.strictEqual(isExist, true); + filer.saveFileSync(fPath, fPath); + let fcontent = fs.readFileSync(fPath); + assert.strictEqual(fcontent.toString(), 'test file'+fPath); + if (fs.existsSync(fPath)) { + fs.unlinkSync(fPath); + } + + // 长文件名 + fPath = './1'; + if (fs.existsSync(fPath)) { + fs.unlinkSync(fPath); + } + for (let i=0;i<100;i++) { + fPath = fPath + i; + } + fPath = fPath + '.txt'; + filer.saveFileSync(fPath, 'test file'); + isExist = fs.existsSync(fPath); + assert.strictEqual(isExist, true); + filer.saveFileSync(fPath, fPath); + fcontent = fs.readFileSync(fPath); + assert.strictEqual(fcontent.toString(), 'test file'+fPath); + if (fs.existsSync(fPath)) { + fs.unlinkSync(fPath); + } + + + // 中文文件名 + fPath = './测试中文.txt'; + if (fs.existsSync(fPath)) { + fs.unlinkSync(fPath); + } + filer.saveFileSync(fPath, 'test file'); + isExist = fs.existsSync(fPath); + assert.strictEqual(isExist, true); + filer.saveFileSync(fPath, fPath); + fcontent = fs.readFileSync(fPath); + assert.strictEqual(fcontent.toString(), 'test file'+fPath); + if (fs.existsSync(fPath)) { + fs.unlinkSync(fPath); + } + }); + + //3, 测试异常情况 + test('saveFileSync_append_test_3', () => { + let fPath = './1.txt'; + if (fs.existsSync(fPath)) { + fs.unlinkSync(fPath); + } + + let filer = files.Filer.getInstance(); + filer.saveFileSync(fPath, 'test file'); + let isExist = fs.existsSync(fPath); + assert.strictEqual(isExist, true); + let fcontent = fs.readFileSync(fPath); + assert.strictEqual(fcontent.toString(), 'test file'); + }); + + //4, 测试错误情况 + test('saveFileSync_append_test_4', () => { + let fPath = './'; + let filer = files.Filer.getInstance(); + try { + filer.saveFileSync(fPath, 'test file'); + } catch (e) { + let isError = true; + assert.strictEqual(isError, true); + let emsg = JSON.stringify(e); + let isLike = emsg.includes('EISDIR'); + assert.strictEqual(isLike, true); + } + }); +}); diff --git a/src/vscode_plugin/src/test/suite/common/log.test.ts b/src/vscode_plugin/src/test/suite/common/log.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..436ff2de68ed8563bf6eb74c59d283db4420c932 --- /dev/null +++ b/src/vscode_plugin/src/test/suite/common/log.test.ts @@ -0,0 +1,160 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import * as assert from 'assert'; + +// You can import and use all API from the 'vscode' module +// as well as import your extension to test it +import * as vscode from 'vscode'; +import * as fs from 'fs'; +import * as path from 'path'; +import * as logs from '../../../common/log' +// import * as myExtension from '../../extension'; + +suite('Common_Log_Test_Suite', () => { + vscode.window.showInformationMessage('Start all tests.'); + + //1, 测试debug一般情况 + test('debug_test_1', () => { + let logger = logs.Logger.getInstance(); + logger.debug('test debug'); + let logFilePath = './dmesg.log'; + let isExist = fs.existsSync(logFilePath); + assert.strictEqual(isExist, true); + let fcontent = fs.readFileSync(logFilePath, 'utf-8'); + let isLike = fcontent.includes('test debug'); + assert.strictEqual(isLike, true); + fs.unlinkSync(logFilePath); + }); + + //2, 测试debug一般边界情况 + test('debug_test_2', () => { + let logger = logs.Logger.getInstance(); + logger.debug('test debug'); + let logFilePath = './dmesg.log'; + let isExist = fs.existsSync(logFilePath); + assert.strictEqual(isExist, true); + let fcontent = fs.readFileSync(logFilePath, 'utf-8'); + let isLike = fcontent.includes('test debug'); + assert.strictEqual(isLike, true); + let flen = 1024; + let fcont = 'fcont:'; + for (let i=0;i 1048000 && fsize < 1049000) ? true : false; + assert.strictEqual(fflag, true); + // fs.unlinkSync(logFilePath); + }); + + //1, 测试info一般情况 + test('info_test_1', () => { + let logger = logs.Logger.getInstance(); + logger.info('test info'); + let logFilePath = './dmesg.log'; + let isExist = fs.existsSync(logFilePath); + assert.strictEqual(isExist, true); + let fcontent = fs.readFileSync(logFilePath, 'utf-8'); + let isLike = fcontent.includes('test info'); + assert.strictEqual(isLike, true); + fs.unlinkSync(logFilePath); + }); + + //2, 测试info边界情况 + test('info_test_2', () => { + let logger = logs.Logger.getInstance(); + let logFilePath = './dmesg.log'; + let isExist = fs.existsSync(logFilePath); + let flen = 1024; + let fcont = 'fcont:'; + for (let i=0;i 1 && fsize < 1048000) ? true : false; + assert.strictEqual(fflag, true); + + let dirList = fs.readdirSync('./', { withFileTypes: true }); + let isCurrent = false; + for (const ditem of dirList) { + if (ditem.isFile()) { + if (ditem.name.includes('dmesg')) { + if (ditem.name != 'dmesg.log') { + const now = new Date().toISOString(); + let strlist = now.split('.'); + let strcont = strlist[0].replace(/[:.]/g, '-') + isCurrent = ditem.name.includes(strcont); + if (isCurrent) { + break; + } + } + } + } + } + + assert.strictEqual(isCurrent, true); + fs.unlinkSync(logFilePath); + }); + + //1, 测试error一般情况 + test('error_test_1', () => { + let logger = logs.Logger.getInstance(); + logger.error('test error'); + let logFilePath = './dmesg.log'; + let isExist = fs.existsSync(logFilePath); + assert.strictEqual(isExist, true); + let fcontent = fs.readFileSync(logFilePath, 'utf-8'); + let isLike = fcontent.includes('test error'); + assert.strictEqual(isLike, true); + fs.unlinkSync(logFilePath); + }); + + //1, 测试warn一般情况 + test('warn_test_4', () => { + let logger = logs.Logger.getInstance(); + logger.warn('test warn'); + let logFilePath = './dmesg.log'; + let isExist = fs.existsSync(logFilePath); + assert.strictEqual(isExist, true); + let fcontent = fs.readFileSync(logFilePath, 'utf-8'); + let isLike = fcontent.includes('test warn'); + assert.strictEqual(isLike, true); + fs.unlinkSync(logFilePath); + }); + + test('file_test_last', () => { + let dirList = fs.readdirSync('./', { withFileTypes: true }); + for (const ditem of dirList) { + if (ditem.isFile()) { + const { name, ext } = path.parse(ditem.name); + if (name.includes('dmesg') && ext.includes('.log')) { + + } + } + } + }); +});