13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ciimport {describe, it} from 'mocha';
173af6ab5fSopenharmony_ciimport {FileUtils} from '../../../src/utils/FileUtils';
183af6ab5fSopenharmony_ciimport {assert} from 'chai';
193af6ab5fSopenharmony_ci
203af6ab5fSopenharmony_ciconst renameFileNameModule = require('../../../src/transformers/rename/RenameFileNameTransformer');
213af6ab5fSopenharmony_ci
223af6ab5fSopenharmony_cidescribe('Tester Cases for <FileUtils>.', function () {
233af6ab5fSopenharmony_ci  /** test for readFile */
243af6ab5fSopenharmony_ci  it('Tester: <file not found> case for FileUtils#readFile', function () {
253af6ab5fSopenharmony_ci    let path = '/user/local/tester';
263af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.readFile(path), undefined);
273af6ab5fSopenharmony_ci  });
283af6ab5fSopenharmony_ci
293af6ab5fSopenharmony_ci  it('Tester: <read file content.> case for FileUtils#readFile', function () {
303af6ab5fSopenharmony_ci    let path = 'test/ut/utils/demo.txt';
313af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.readFile(path), 'hello world!');
323af6ab5fSopenharmony_ci  });
333af6ab5fSopenharmony_ci
343af6ab5fSopenharmony_ci  /** test for readFileAsJson */
353af6ab5fSopenharmony_ci  it('Tester: <read file as json.> case for FileUtils#readFileAsJson', function () {
363af6ab5fSopenharmony_ci    let path = 'test/ut/utils/demo.json';
373af6ab5fSopenharmony_ci    let obj = FileUtils.readFileAsJson(path);
383af6ab5fSopenharmony_ci    assert.strictEqual(obj?.mCompact, true);
393af6ab5fSopenharmony_ci  });
403af6ab5fSopenharmony_ci
413af6ab5fSopenharmony_ci  it('Tester: <file not found.> case for FileUtils#readFileAsJson', function () {
423af6ab5fSopenharmony_ci    let path = 'test/utils/demo_not_found.json';
433af6ab5fSopenharmony_ci    let obj = FileUtils.readFileAsJson(path);
443af6ab5fSopenharmony_ci    assert.strictEqual(obj, undefined);
453af6ab5fSopenharmony_ci  });
463af6ab5fSopenharmony_ci
473af6ab5fSopenharmony_ci  it('Tester: <error json format.> case for FileUtils#readFileAsJson', function () {
483af6ab5fSopenharmony_ci    let path = 'test/utils/error_json.txt';
493af6ab5fSopenharmony_ci    let obj = FileUtils.readFileAsJson(path);
503af6ab5fSopenharmony_ci    assert.strictEqual(obj, undefined);
513af6ab5fSopenharmony_ci  });
523af6ab5fSopenharmony_ci
533af6ab5fSopenharmony_ci  /** test for getFileName */
543af6ab5fSopenharmony_ci  it('Tester: <get file name with undefined input.> case for FileUtils#getFileName', function () {
553af6ab5fSopenharmony_ci    let path = null;
563af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getFileName(path), undefined);
573af6ab5fSopenharmony_ci
583af6ab5fSopenharmony_ci    path = undefined;
593af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getFileName(path), undefined);
603af6ab5fSopenharmony_ci  });
613af6ab5fSopenharmony_ci
623af6ab5fSopenharmony_ci  it('Tester: <get relative file fullname.> case for FileUtils#getFileName', function () {
633af6ab5fSopenharmony_ci    let path = 'resources/configs/user_profile.json';
643af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getFileName(path), 'user_profile.json');
653af6ab5fSopenharmony_ci  });
663af6ab5fSopenharmony_ci
673af6ab5fSopenharmony_ci  it('Tester: <get windows file fullname.> case for FileUtils#getFileName', function () {
683af6ab5fSopenharmony_ci    let path = 'D:\\HuaweiApp\\ohsdk\\ets\\3.2.7.5\\user_profile.json';
693af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getFileName(path), 'user_profile.json');
703af6ab5fSopenharmony_ci  });
713af6ab5fSopenharmony_ci
723af6ab5fSopenharmony_ci  it('Tester: <get single file fullname.> case for FileUtils#getFileName', function () {
733af6ab5fSopenharmony_ci    let path = 'user_profile.json';
743af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getFileName(path), 'user_profile.json');
753af6ab5fSopenharmony_ci  });
763af6ab5fSopenharmony_ci
773af6ab5fSopenharmony_ci  /** test for getFileExtension */
783af6ab5fSopenharmony_ci  it('Tester: <get file extension with undefined input.> case for FileUtils#getFileExtension', function () {
793af6ab5fSopenharmony_ci    let path = null;
803af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getFileExtension(path), undefined);
813af6ab5fSopenharmony_ci
823af6ab5fSopenharmony_ci    path = undefined;
833af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getFileExtension(path), undefined);
843af6ab5fSopenharmony_ci  });
853af6ab5fSopenharmony_ci
863af6ab5fSopenharmony_ci  it('Tester: <get file extension with input not contain point.> case for FileUtils#getFileExtension', function () {
873af6ab5fSopenharmony_ci    let path = 'resources/configs/user_profile';
883af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getFileExtension(path), undefined);
893af6ab5fSopenharmony_ci  });
903af6ab5fSopenharmony_ci
913af6ab5fSopenharmony_ci  it('Tester: <get file extension with dir contain point.> case for FileUtils#getFileExtension', function () {
923af6ab5fSopenharmony_ci    let path = 'resources/configs.dir/user_profile.conf';
933af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getFileExtension(path), 'conf');
943af6ab5fSopenharmony_ci  });
953af6ab5fSopenharmony_ci
963af6ab5fSopenharmony_ci  it('Tester: <get file extension.> case for FileUtils#getFileExtension', function () {
973af6ab5fSopenharmony_ci    let path = 'resources/configs/user_profile.json';
983af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getFileExtension(path), 'json');
993af6ab5fSopenharmony_ci  });
1003af6ab5fSopenharmony_ci
1013af6ab5fSopenharmony_ci  it('Tester: <get file extension with point end.> case for FileUtils#getFileExtension', function () {
1023af6ab5fSopenharmony_ci    let path = 'resources/configs/user_profile.';
1033af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getFileExtension(path), '');
1043af6ab5fSopenharmony_ci  });
1053af6ab5fSopenharmony_ci
1063af6ab5fSopenharmony_ci  /** test for writeFile */
1073af6ab5fSopenharmony_ci  it('Tester: <write file test.> case for FileUtils#writeFile', function () {
1083af6ab5fSopenharmony_ci    let path = 'test/ut/utils/write_demo.txt';
1093af6ab5fSopenharmony_ci    let content = 'hello';
1103af6ab5fSopenharmony_ci    FileUtils.writeFile(path, content);
1113af6ab5fSopenharmony_ci
1123af6ab5fSopenharmony_ci    const fileContent = FileUtils.readFile(path);
1133af6ab5fSopenharmony_ci    assert.strictEqual(fileContent, content);
1143af6ab5fSopenharmony_ci  });
1153af6ab5fSopenharmony_ci
1163af6ab5fSopenharmony_ci  /** test for getPrefix */
1173af6ab5fSopenharmony_ci  it('Tester: <get prefix test.> case for FileUtils#getPrefix', function () {
1183af6ab5fSopenharmony_ci    let path = 'test/utils/write_demo.txt';
1193af6ab5fSopenharmony_ci    let prefix = 'test/utils/';
1203af6ab5fSopenharmony_ci
1213af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getPrefix(path), prefix);
1223af6ab5fSopenharmony_ci  });
1233af6ab5fSopenharmony_ci
1243af6ab5fSopenharmony_ci  it('Tester: <get windows prefix test.> case for FileUtils#getPrefix', function () {
1253af6ab5fSopenharmony_ci    let path = 'D:\\HuaweiApp\\ohsdk\\ets\\3.2.7.5\\us';
1263af6ab5fSopenharmony_ci    let prefix = 'D:\\HuaweiApp\\ohsdk\\ets\\3.2.7.5\\';
1273af6ab5fSopenharmony_ci
1283af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getPrefix(path), prefix);
1293af6ab5fSopenharmony_ci  });
1303af6ab5fSopenharmony_ci
1313af6ab5fSopenharmony_ci  it('Tester: <get no prefix test.> case for FileUtils#getPrefix', function () {
1323af6ab5fSopenharmony_ci    let path = 'D:';
1333af6ab5fSopenharmony_ci    let prefix = undefined;
1343af6ab5fSopenharmony_ci
1353af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getPrefix(path), prefix);
1363af6ab5fSopenharmony_ci  });
1373af6ab5fSopenharmony_ci
1383af6ab5fSopenharmony_ci  /** test for getPathWithoutPrefix */
1393af6ab5fSopenharmony_ci  it('Tester: <get path without prefix no prefix test.> case for FileUtils#getPathWithoutPrefix', function () {
1403af6ab5fSopenharmony_ci    let path = 'D:';
1413af6ab5fSopenharmony_ci    let prefix = 'D:\\HuaweiApp';
1423af6ab5fSopenharmony_ci
1433af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getPathWithoutPrefix(path, prefix), path);
1443af6ab5fSopenharmony_ci  });
1453af6ab5fSopenharmony_ci
1463af6ab5fSopenharmony_ci  it('Tester: <get path without prefix contain prefix test.> case for FileUtils#getPathWithoutPrefix', function () {
1473af6ab5fSopenharmony_ci    let path = 'D:\\HuaweiApp\\ohsdk\\ets\\3.2.7.5';
1483af6ab5fSopenharmony_ci    let prefix = 'D:\\HuaweiApp';
1493af6ab5fSopenharmony_ci
1503af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getPathWithoutPrefix(path, prefix), '\\ohsdk\\ets\\3.2.7.5');
1513af6ab5fSopenharmony_ci  });
1523af6ab5fSopenharmony_ci
1533af6ab5fSopenharmony_ci  it('Tester: <get path without prefix path and prefix equal test.> case for FileUtils#getPathWithoutPrefix', function () {
1543af6ab5fSopenharmony_ci    let path = 'D:\\HuaweiApp\\ohsdk\\ets\\3.2.7.5';
1553af6ab5fSopenharmony_ci    let prefix = 'D:\\HuaweiApp\\ohsdk\\ets\\3.2.7.5';
1563af6ab5fSopenharmony_ci
1573af6ab5fSopenharmony_ci    assert.strictEqual(FileUtils.getPathWithoutPrefix(path, prefix), '');
1583af6ab5fSopenharmony_ci  });
1593af6ab5fSopenharmony_ci
1603af6ab5fSopenharmony_ci  it('Tester: <determine whether oh_modules or not.> case for renameFileNameModule#isInOhModules', function () {
1613af6ab5fSopenharmony_ci    let projectInfo = {
1623af6ab5fSopenharmony_ci      projectRootPath: '/test/Obfuscation/arkguard',
1633af6ab5fSopenharmony_ci      packageDir: 'oh_modules'
1643af6ab5fSopenharmony_ci    };
1653af6ab5fSopenharmony_ci    let originalPath = '/test/Obfuscation/rkguard/entry/src/main/ets/pages/Index.ets';
1663af6ab5fSopenharmony_ci
1673af6ab5fSopenharmony_ci    assert.strictEqual(renameFileNameModule.isInOhModules(projectInfo, originalPath), false);
1683af6ab5fSopenharmony_ci  });
1693af6ab5fSopenharmony_ci
1703af6ab5fSopenharmony_ci  it('Tester: <determine whether oh_modules or not.> case for renameFileNameModule#isInOhModules', function () {
1713af6ab5fSopenharmony_ci    let projectInfo = {
1723af6ab5fSopenharmony_ci      projectRootPath: '/test/Obfuscation/arkguard',
1733af6ab5fSopenharmony_ci      packageDir: 'oh_modules'
1743af6ab5fSopenharmony_ci    };
1753af6ab5fSopenharmony_ci    let originalPath = '/test/Obfuscation/arkguard/oh_modules/.ohpm/json5@2.2.3/oh_modules/json5/dist/index.mjs';
1763af6ab5fSopenharmony_ci
1773af6ab5fSopenharmony_ci    assert.strictEqual(renameFileNameModule.isInOhModules(projectInfo, originalPath), true);
1783af6ab5fSopenharmony_ci  });
1793af6ab5fSopenharmony_ci
1803af6ab5fSopenharmony_ci  it('Tester: test API collectPathReservedString', function () {
1813af6ab5fSopenharmony_ci    let filePath1 = 'D:\\workplace\\Obfuscation\\TestForFilename\\entry';
1823af6ab5fSopenharmony_ci    let reservedNames = [];
1833af6ab5fSopenharmony_ci    FileUtils.collectPathReservedString(filePath1, reservedNames);
1843af6ab5fSopenharmony_ci    let filePath2 = '/OpenHarmony/arkcompiler/ets_frontend/arkguard/test/grammar/test.ts';
1853af6ab5fSopenharmony_ci    FileUtils.collectPathReservedString(filePath2, reservedNames);
1863af6ab5fSopenharmony_ci    let filePath3 = '../../test.ts.ts';
1873af6ab5fSopenharmony_ci    FileUtils.collectPathReservedString(filePath3, reservedNames);
1883af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[0], 'D:');
1893af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[1], 'workplace');
1903af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[2], 'Obfuscation');
1913af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[3], 'TestForFilename');
1923af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[4], 'entry');
1933af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[5], '');
1943af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[6], 'OpenHarmony');
1953af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[7], 'arkcompiler');
1963af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[8], 'ets_frontend');
1973af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[9], 'arkguard');
1983af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[10], 'test');
1993af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[11], 'grammar');
2003af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[12], 'test.ts');
2013af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[13], '..');
2023af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[14], '..');
2033af6ab5fSopenharmony_ci    assert.strictEqual(reservedNames[15], 'test.ts.ts');
2043af6ab5fSopenharmony_ci  });
2053af6ab5fSopenharmony_ci});
206