ObfuscatedCodeWriter.spec.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { assert } from 'chai';
  2. import * as mkdirp from 'mkdirp';
  3. import * as rimraf from 'rimraf';
  4. import * as fs from "fs";
  5. import { ObfuscatedCodeWriter } from '../../../../src/cli/utils/ObfuscatedCodeWriter';
  6. describe('ObfuscatedCodeWriter', () => {
  7. const tmpDirectoryPath: string = 'test/tmp';
  8. describe('getOutputCodePath', () => {
  9. before(() => {
  10. mkdirp.sync(`${tmpDirectoryPath}/input/`);
  11. fs.writeFileSync(
  12. `${tmpDirectoryPath}/input/test-input.js`,
  13. 'var foo = 1;'
  14. );
  15. });
  16. describe('Variant #1: raw input path is a file path, raw output path is a file path', () => {
  17. const inputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
  18. const rawInputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
  19. const rawOutputPath: string = `${tmpDirectoryPath}/output/test-output.js`;
  20. const expectedOutputCodePath: string = `${tmpDirectoryPath}/output/test-output.js`;
  21. let outputCodePath: string;
  22. before(() => {
  23. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  24. rawInputPath,
  25. {
  26. output: rawOutputPath
  27. }
  28. );
  29. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  30. });
  31. it('should return output path that equals to passed output file path', () => {
  32. assert.equal(outputCodePath, expectedOutputCodePath);
  33. });
  34. });
  35. describe('Variant #2: raw input path is a file path, raw output path is a directory path', () => {
  36. const inputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
  37. const rawInputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
  38. const rawOutputPath: string = `${tmpDirectoryPath}/output`;
  39. const expectedOutputCodePath: string = `${tmpDirectoryPath}/output/test-input.js`;
  40. let outputCodePath: string;
  41. before(() => {
  42. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  43. rawInputPath,
  44. {
  45. output: rawOutputPath
  46. }
  47. );
  48. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  49. });
  50. it('should return output path that equals to passed output directory with file name from actual file path', () => {
  51. assert.equal(outputCodePath, expectedOutputCodePath);
  52. });
  53. });
  54. describe('Variant #3: raw input path is a directory path, raw output path is a file path', () => {
  55. const inputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
  56. const rawInputPath: string = `${tmpDirectoryPath}/input`;
  57. const rawOutputPath: string = `${tmpDirectoryPath}/output/test-output.js`;
  58. let testFunc: () => string;
  59. before(() => {
  60. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  61. rawInputPath,
  62. {
  63. output: rawOutputPath
  64. }
  65. );
  66. testFunc = () => obfuscatedCodeWriter.getOutputCodePath(inputPath);
  67. });
  68. it('should throw an error if output path is a file path', () => {
  69. assert.throws(testFunc, Error);
  70. });
  71. });
  72. describe('Variant #4: raw input path is a directory path, raw output path is a directory path', () => {
  73. describe('Variant #1: base directory name', () => {
  74. const inputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
  75. const rawInputPath: string = `${tmpDirectoryPath}/input`;
  76. const rawOutputPath: string = `${tmpDirectoryPath}/output`;
  77. const expectedOutputCodePath: string = `${tmpDirectoryPath}/output/${tmpDirectoryPath}/input/test-input.js`;
  78. let outputCodePath: string;
  79. before(() => {
  80. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  81. rawInputPath,
  82. {
  83. output: rawOutputPath
  84. }
  85. );
  86. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  87. });
  88. it('should return output path that contains raw output path and actual file input path', () => {
  89. assert.equal(outputCodePath, expectedOutputCodePath);
  90. });
  91. });
  92. describe('Variant #2: directory name with dot', () => {
  93. const inputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
  94. const rawInputPath: string = `${tmpDirectoryPath}/input`;
  95. const rawOutputPath: string = `${tmpDirectoryPath}/output/foo.bar`;
  96. const expectedOutputCodePath: string = `${tmpDirectoryPath}/output/foo.bar/${tmpDirectoryPath}/input/test-input.js`;
  97. let outputCodePath: string;
  98. before(() => {
  99. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  100. rawInputPath,
  101. {
  102. output: rawOutputPath
  103. }
  104. );
  105. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  106. });
  107. it('should return output path that contains raw output path and actual file input path', () => {
  108. assert.equal(outputCodePath, expectedOutputCodePath);
  109. });
  110. });
  111. });
  112. after(() => {
  113. rimraf.sync(tmpDirectoryPath);
  114. });
  115. });
  116. describe('getOutputSourceMapPath', () => {
  117. const rawInputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
  118. const rawOutputPath: string = `${tmpDirectoryPath}/output/test-output.js`;
  119. const outputCodePath: string = `${tmpDirectoryPath}/output/test-output.js`;
  120. const expectedOutputSourceMapPath: string = `${tmpDirectoryPath}/output/test-output.js.map`;
  121. let outputSourceMapPath: string;
  122. before(() => {
  123. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  124. rawInputPath,
  125. {
  126. output: rawOutputPath
  127. }
  128. );
  129. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath);
  130. });
  131. it('should return output path for source map', () => {
  132. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  133. });
  134. });
  135. });