ObfuscatedCodeWriter.spec.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. const inputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
  74. const rawInputPath: string = `${tmpDirectoryPath}/input`;
  75. const rawOutputPath: string = `${tmpDirectoryPath}/output`;
  76. const expectedOutputCodePath: string = `${tmpDirectoryPath}/output/${tmpDirectoryPath}/input/test-input.js`;
  77. let outputCodePath: string;
  78. before(() => {
  79. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  80. rawInputPath,
  81. {
  82. output: rawOutputPath
  83. }
  84. );
  85. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  86. });
  87. it('should return output path that contains raw output path and actual file input path', () => {
  88. assert.equal(outputCodePath, expectedOutputCodePath);
  89. });
  90. });
  91. after(() => {
  92. rimraf.sync(tmpDirectoryPath);
  93. });
  94. });
  95. describe('getOutputSourceMapPath', () => {
  96. const rawInputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
  97. const rawOutputPath: string = `${tmpDirectoryPath}/output/test-output.js`;
  98. const outputCodePath: string = `${tmpDirectoryPath}/output/test-output.js`;
  99. const expectedOutputSourceMapPath: string = `${tmpDirectoryPath}/output/test-output.js.map`;
  100. let outputSourceMapPath: string;
  101. before(() => {
  102. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  103. rawInputPath,
  104. {
  105. output: rawOutputPath
  106. }
  107. );
  108. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath);
  109. });
  110. it('should return output path for source map', () => {
  111. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  112. });
  113. });
  114. });