IdentifierNamesCacheFileUtils.spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import * as fs from 'fs';
  2. import * as mkdirp from 'mkdirp';
  3. import * as path from 'path';
  4. import * as rimraf from 'rimraf';
  5. import { assert } from 'chai';
  6. import { TIdentifierNamesCache } from '../../../../src/types/TIdentifierNamesCache';
  7. import { IdentifierNamesCacheFileUtils } from '../../../../src/cli/utils/IdentifierNamesCacheFileUtils';
  8. describe('IdentifierNamesCacheFileUtils', () => {
  9. const expectedFilePathError: RegExp = /Given identifier names cache path must be/;
  10. const expectedFileContentError: RegExp = /Identifier names cache file must contains/;
  11. const expectedIdentifierNamesCache: TIdentifierNamesCache = {
  12. globalIdentifiers: {
  13. foo: '_0x123456'
  14. },
  15. propertyIdentifiers: {
  16. bar: '_0x654321'
  17. }
  18. };
  19. const fileContent: string = JSON.stringify(expectedIdentifierNamesCache);
  20. const tmpDirectoryPath: string = path.join('test', 'tmp');
  21. before(() => {
  22. mkdirp.sync(tmpDirectoryPath);
  23. });
  24. describe('readFile', () => {
  25. describe('Variant #1: input path is a file path', () => {
  26. describe('Variant #1: `identifierNamesCachePath` is a valid cache path', () => {
  27. describe('Variant #1: valid `json` as identifier names cache file content', () => {
  28. const tmpFileName: string = 'cache.json';
  29. const inputPath: string = path.join(tmpDirectoryPath, tmpFileName);
  30. let identifierNamesCache: TIdentifierNamesCache | null;
  31. before(() => {
  32. fs.writeFileSync(inputPath, fileContent);
  33. identifierNamesCache = new IdentifierNamesCacheFileUtils(inputPath).readFile();
  34. });
  35. it('should return valid identifier names cache', () => {
  36. assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
  37. });
  38. after(() => {
  39. fs.unlinkSync(inputPath);
  40. });
  41. });
  42. describe('Variant #2: invalid `json` as identifier names cache file content', () => {
  43. const tmpFileName: string = 'cache.json';
  44. const fileContent: string = '{globalIdentifiers: }';
  45. const inputPath: string = path.join(tmpDirectoryPath, tmpFileName);
  46. let testFunc: () => TIdentifierNamesCache | null;
  47. before(() => {
  48. fs.writeFileSync(inputPath, fileContent);
  49. testFunc = () => new IdentifierNamesCacheFileUtils(inputPath).readFile();
  50. });
  51. it('should throw an error', () => {
  52. assert.throws(testFunc, expectedFileContentError);
  53. });
  54. after(() => {
  55. fs.unlinkSync(inputPath);
  56. });
  57. });
  58. describe('Variant #3: some string as identifier names cache file content', () => {
  59. const tmpFileName: string = 'cache.json';
  60. const fileContent: string = 'cache string';
  61. const inputPath: string = path.join(tmpDirectoryPath, tmpFileName);
  62. let testFunc: () => TIdentifierNamesCache | null;
  63. before(() => {
  64. fs.writeFileSync(inputPath, fileContent);
  65. testFunc = () => new IdentifierNamesCacheFileUtils(inputPath).readFile();
  66. });
  67. it('should throw an error', () => {
  68. assert.throws(testFunc, expectedFileContentError);
  69. });
  70. after(() => {
  71. fs.unlinkSync(inputPath);
  72. });
  73. });
  74. });
  75. describe('Variant #2: `identifierNamesCachePath` is a valid path with invalid extension', () => {
  76. const tmpFileName: string = 'cache.js';
  77. const inputPath: string = path.join(tmpDirectoryPath, tmpFileName);
  78. let testFunc: () => void;
  79. before(() => {
  80. fs.writeFileSync(inputPath, fileContent);
  81. testFunc = () => new IdentifierNamesCacheFileUtils(inputPath).readFile();
  82. });
  83. it('should throw an error if `identifierNamesCachePath` is not a valid path', () => {
  84. assert.throws(testFunc, expectedFilePathError);
  85. });
  86. after(() => {
  87. fs.unlinkSync(inputPath);
  88. });
  89. });
  90. describe('Variant #3: `identifierNamesCachePath` is not a valid cache path', () => {
  91. const tmpFileName: string = 'cache.js';
  92. const inputPath: string = path.join(tmpDirectoryPath, tmpFileName);
  93. let testFunc: () => void;
  94. before(() => {
  95. testFunc = () => new IdentifierNamesCacheFileUtils(inputPath).readFile();
  96. });
  97. it('should throw an error if `identifierNamesCachePath` is not a valid path', () => {
  98. assert.throws(testFunc, expectedFilePathError);
  99. });
  100. });
  101. });
  102. describe('Variant #2: input path is a directory path', () => {
  103. describe('Variant #1: `inputPath` is a valid path', () => {
  104. let testFunc: () => TIdentifierNamesCache;
  105. before(() => {
  106. testFunc = () => new IdentifierNamesCacheFileUtils(tmpDirectoryPath).readFile();
  107. });
  108. it('should throw an error if `identifierNamesCachePath` is a directory path', () => {
  109. assert.throws(testFunc, expectedFilePathError);
  110. });
  111. });
  112. });
  113. });
  114. after(() => {
  115. rimraf.sync(tmpDirectoryPath);
  116. });
  117. });