JavaScriptObfuscatorCLI.spec.ts 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  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 * as sinon from 'sinon';
  6. import { resolveSources } from 'source-map-resolve';
  7. import { assert } from 'chai';
  8. import { ISourceMap } from '../../../src/interfaces/source-code/ISourceMap';
  9. import { StdoutWriteMock } from '../../mocks/StdoutWriteMock';
  10. import { JavaScriptObfuscatorCLI } from '../../../src/JavaScriptObfuscatorCLIFacade';
  11. import { parseSourceMapFromObfuscatedCode } from '../../helpers/parseSourceMapFromObfuscatedCode';
  12. describe('JavaScriptObfuscatorCLI', function (): void {
  13. this.timeout(100000);
  14. const expectedError: RegExp = /Given input path must be a valid/;
  15. const fixturesDirName: string = path.join('test', 'fixtures');
  16. const fixtureFileName: string = 'sample.js';
  17. const fixtureFilePath: string = path.join(fixturesDirName, fixtureFileName);
  18. const outputDirName: string = path.join('test', 'tmp');
  19. const outputFileName: string = 'sample-obfuscated.js';
  20. const outputFilePath: string = path.join(outputDirName, outputFileName);
  21. const configDirName: string = path.join('test', 'fixtures');
  22. const configFileName: string = 'config.js';
  23. const configFilePath: string = path.join(configDirName, configFileName);
  24. describe('run', () => {
  25. before(() => {
  26. mkdirp.sync(outputDirName);
  27. });
  28. describe('Variant #1: obfuscation of single file', () => {
  29. describe('--output` option is set', () => {
  30. describe('Variant #1: input file path is before options', () => {
  31. let isFileExist: boolean;
  32. before(() => {
  33. JavaScriptObfuscatorCLI.obfuscate([
  34. 'node',
  35. 'javascript-obfuscator',
  36. fixtureFilePath,
  37. '--output',
  38. outputFilePath,
  39. '--compact',
  40. 'true',
  41. '--self-defending',
  42. '0'
  43. ]);
  44. isFileExist = fs.existsSync(outputFilePath);
  45. });
  46. it('should create file with obfuscated code in `--output` directory', () => {
  47. assert.equal(isFileExist, true);
  48. });
  49. after(() => {
  50. fs.unlinkSync(outputFilePath);
  51. });
  52. });
  53. describe('Variant #2: input file path is after options', () => {
  54. let isFileExist: boolean;
  55. before(() => {
  56. JavaScriptObfuscatorCLI.obfuscate([
  57. 'node',
  58. 'javascript-obfuscator',
  59. '--output',
  60. outputFilePath,
  61. '--compact',
  62. 'true',
  63. '--self-defending',
  64. '0',
  65. fixtureFilePath
  66. ]);
  67. isFileExist = fs.existsSync(outputFilePath);
  68. });
  69. it('should create file with obfuscated code in `--output` directory', () => {
  70. assert.equal(isFileExist, true);
  71. });
  72. after(() => {
  73. fs.unlinkSync(outputFilePath);
  74. });
  75. });
  76. });
  77. describe('`--output` option isn\'t set', () => {
  78. describe('Variant #1: default behaviour', () => {
  79. let outputFixturesFilePath: string,
  80. isFileExist: boolean;
  81. before(() => {
  82. outputFixturesFilePath = path.join(fixturesDirName, outputFileName);
  83. JavaScriptObfuscatorCLI.obfuscate([
  84. 'node',
  85. 'javascript-obfuscator',
  86. fixtureFilePath
  87. ]);
  88. isFileExist = fs.existsSync(outputFixturesFilePath);
  89. });
  90. it(`should create file \`${outputFileName}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
  91. assert.equal(isFileExist, true);
  92. });
  93. after(() => {
  94. fs.unlinkSync(outputFixturesFilePath);
  95. });
  96. });
  97. describe('Variant #2: invalid input file path', () => {
  98. let testFunc: () => void;
  99. before(() => {
  100. testFunc = () => JavaScriptObfuscatorCLI.obfuscate([
  101. 'node',
  102. 'javascript-obfuscator',
  103. path.join('wrong', 'file', 'path')
  104. ]);
  105. });
  106. it(`should throw an error`, () => {
  107. assert.throws(testFunc, expectedError);
  108. });
  109. });
  110. describe('Variant #3: input file extension isn\'t `.js`', () => {
  111. const expectedError: RegExp = /Given input path must be a valid/;
  112. const outputFileName: string = 'sample-obfuscated.ts';
  113. const outputFilePath: string = path.join(outputDirName, outputFileName);
  114. let testFunc: () => void;
  115. before(() => {
  116. fs.writeFileSync(outputFilePath, 'data');
  117. testFunc = () => JavaScriptObfuscatorCLI.obfuscate([
  118. 'node',
  119. 'javascript-obfuscator',
  120. outputFilePath
  121. ]);
  122. });
  123. it(`should throw an error`, () => {
  124. assert.throws(testFunc, expectedError);
  125. });
  126. after(() => {
  127. fs.unlinkSync(outputFilePath);
  128. });
  129. });
  130. });
  131. describe('--exclude option', () => {
  132. describe('Variant #1: --exclude option is pointed on different file', () => {
  133. let isFileExist: boolean;
  134. before(() => {
  135. JavaScriptObfuscatorCLI.obfuscate([
  136. 'node',
  137. 'javascript-obfuscator',
  138. fixtureFilePath,
  139. '--output',
  140. outputFilePath,
  141. '--exclude',
  142. path.join('**', 'foo.js')
  143. ]);
  144. isFileExist = fs.existsSync(outputFilePath);
  145. });
  146. it('should create file with obfuscated code in `--output` directory', () => {
  147. assert.equal(isFileExist, true);
  148. });
  149. });
  150. describe('Variant #2: --exclude option is pointed on input file', () => {
  151. let testFunc: () => void;
  152. before(() => {
  153. testFunc = () => JavaScriptObfuscatorCLI.obfuscate([
  154. 'node',
  155. 'javascript-obfuscator',
  156. fixtureFilePath,
  157. '--output',
  158. outputFilePath,
  159. '--exclude',
  160. path.join('**', 'sample.js')
  161. ]);
  162. });
  163. it('should throw an error', () => {
  164. assert.throws(testFunc, expectedError);
  165. });
  166. after(() => {
  167. fs.unlinkSync(outputFilePath);
  168. });
  169. });
  170. });
  171. });
  172. describe('Variant #2: obfuscation of directory', () => {
  173. describe(`Variant #1: default behaviour`, () => {
  174. const directoryPath: string = path.join(fixturesDirName, 'directory-obfuscation');
  175. const outputFileName1: string = 'foo-obfuscated.js';
  176. const outputFileName2: string = 'bar-obfuscated.js';
  177. const outputFileName3: string = 'baz-obfuscated.js';
  178. const readFileEncoding = 'utf8';
  179. const regExp1: RegExp = /^var a1_0x(\w){4,6} *= *0x1;$/;
  180. const regExp2: RegExp = /^var a0_0x(\w){4,6} *= *0x2;$/;
  181. let outputFixturesFilePath1: string,
  182. outputFixturesFilePath2: string,
  183. outputFixturesFilePath3: string,
  184. isFileExist1: boolean,
  185. isFileExist2: boolean,
  186. isFileExist3: boolean,
  187. fileContent1: string,
  188. fileContent2: string;
  189. before(() => {
  190. outputFixturesFilePath1 = path.join(directoryPath, outputFileName1);
  191. outputFixturesFilePath2 = path.join(directoryPath, outputFileName2);
  192. outputFixturesFilePath3 = path.join(directoryPath, outputFileName3);
  193. JavaScriptObfuscatorCLI.obfuscate([
  194. 'node',
  195. 'javascript-obfuscator',
  196. directoryPath,
  197. '--rename-globals',
  198. 'true'
  199. ]);
  200. isFileExist1 = fs.existsSync(outputFixturesFilePath1);
  201. isFileExist2 = fs.existsSync(outputFixturesFilePath2);
  202. isFileExist3 = fs.existsSync(outputFixturesFilePath3);
  203. fileContent1 = fs.readFileSync(outputFixturesFilePath1, readFileEncoding);
  204. fileContent2 = fs.readFileSync(outputFixturesFilePath2, readFileEncoding);
  205. });
  206. it(`should create file \`${outputFileName1}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
  207. assert.equal(isFileExist1, true);
  208. });
  209. it(`should create file \`${outputFileName2}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
  210. assert.equal(isFileExist2, true);
  211. });
  212. it(`shouldn't create file \`${outputFileName3}\` in \`${fixturesDirName}\` directory`, () => {
  213. assert.equal(isFileExist3, false);
  214. });
  215. it(`match #1: should create file with obfuscated code with prefixed identifier`, () => {
  216. assert.match(fileContent1, regExp1);
  217. });
  218. it(`match #2: should create file with obfuscated code with prefixed identifier`, () => {
  219. assert.match(fileContent2, regExp2);
  220. });
  221. after(() => {
  222. rimraf.sync(outputFixturesFilePath1);
  223. rimraf.sync(outputFixturesFilePath2);
  224. });
  225. });
  226. describe('Variant #2: obfuscation of directory with `identifiersPrefix` option value', () => {
  227. const directoryPath: string = path.join(fixturesDirName, 'directory-obfuscation');
  228. const identifiersPrefix: string = 'foo';
  229. const outputFileName1: string = 'foo-obfuscated.js';
  230. const outputFileName2: string = 'bar-obfuscated.js';
  231. const readFileEncoding = 'utf8';
  232. const regExp1: RegExp = /^var foo1_0x(\w){4,6} *= *0x1;$/;
  233. const regExp2: RegExp = /^var foo0_0x(\w){4,6} *= *0x2;$/;
  234. let outputFixturesFilePath1: string,
  235. outputFixturesFilePath2: string,
  236. isFileExist1: boolean,
  237. isFileExist2: boolean,
  238. fileContent1: string,
  239. fileContent2: string;
  240. before(() => {
  241. outputFixturesFilePath1 = path.join(directoryPath, outputFileName1);
  242. outputFixturesFilePath2 = path.join(directoryPath, outputFileName2);
  243. JavaScriptObfuscatorCLI.obfuscate([
  244. 'node',
  245. 'javascript-obfuscator',
  246. directoryPath,
  247. '--identifiers-prefix',
  248. identifiersPrefix,
  249. '--rename-globals',
  250. 'true'
  251. ]);
  252. isFileExist1 = fs.existsSync(outputFixturesFilePath1);
  253. isFileExist2 = fs.existsSync(outputFixturesFilePath2);
  254. fileContent1 = fs.readFileSync(outputFixturesFilePath1, readFileEncoding);
  255. fileContent2 = fs.readFileSync(outputFixturesFilePath2, readFileEncoding);
  256. });
  257. it(`should create file \`${outputFileName1}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
  258. assert.equal(isFileExist1, true);
  259. });
  260. it(`should create file \`${outputFileName2}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
  261. assert.equal(isFileExist2, true);
  262. });
  263. it(`match #1: should create file with obfuscated code with prefixed identifier`, () => {
  264. assert.match(fileContent1, regExp1);
  265. });
  266. it(`match #2: should create file with obfuscated code with prefixed identifier`, () => {
  267. assert.match(fileContent2, regExp2);
  268. });
  269. after(() => {
  270. rimraf.sync(outputFixturesFilePath1);
  271. rimraf.sync(outputFixturesFilePath2);
  272. });
  273. });
  274. describe('Variant #3: obfuscation of directory with `output` option', () => {
  275. const directoryPath: string = path.join(fixturesDirName, 'directory-obfuscation');
  276. const outputDirectoryName: string = 'obfuscated';
  277. const outputDirectoryPath: string = path.join(directoryPath, outputDirectoryName);
  278. const outputFileName1: string = 'foo.js';
  279. const outputFileName2: string = 'bar.js';
  280. const outputFileName3: string = 'baz.js';
  281. let outputFixturesFilePath1: string,
  282. outputFixturesFilePath2: string,
  283. outputFixturesFilePath3: string,
  284. isFileExist1: boolean,
  285. isFileExist2: boolean,
  286. isFileExist3: boolean;
  287. before(() => {
  288. outputFixturesFilePath1 = path.join(outputDirectoryPath, outputFileName1);
  289. outputFixturesFilePath2 = path.join(outputDirectoryPath, outputFileName2);
  290. outputFixturesFilePath3 = path.join(outputDirectoryPath, outputFileName3);
  291. JavaScriptObfuscatorCLI.obfuscate([
  292. 'node',
  293. 'javascript-obfuscator',
  294. directoryPath,
  295. '--output',
  296. outputDirectoryPath
  297. ]);
  298. isFileExist1 = fs.existsSync(outputFixturesFilePath1);
  299. isFileExist2 = fs.existsSync(outputFixturesFilePath2);
  300. isFileExist3 = fs.existsSync(outputFixturesFilePath3);
  301. });
  302. it(
  303. `should create file \`${outputFileName1}\` with obfuscated code in ` +
  304. `\`${path.join(fixturesDirName, outputDirectoryName)}\` directory`,
  305. () => {
  306. assert.equal(isFileExist1, true);
  307. }
  308. );
  309. it(
  310. `should create file \`${outputFileName2}\` with obfuscated code in ` +
  311. `\`${path.join(fixturesDirName, outputDirectoryName)}\` directory`,
  312. () => {
  313. assert.equal(isFileExist2, true);
  314. }
  315. );
  316. it(
  317. `shouldn't create file \`${outputFileName3}\` in ` +
  318. `\`${path.join(fixturesDirName, outputDirectoryName)}\` directory`,
  319. () => {
  320. assert.equal(isFileExist3, false);
  321. }
  322. );
  323. after(() => {
  324. rimraf.sync(outputDirectoryPath);
  325. });
  326. });
  327. describe('Variant #4: --exclude option', () => {
  328. describe('Variant #1: --exclude option is pointed on different file', () => {
  329. const directoryPath: string = path.join(fixturesDirName, 'directory-obfuscation');
  330. const outputFileName1: string = 'foo-obfuscated.js';
  331. const outputFileName2: string = 'bar-obfuscated.js';
  332. const outputFileName3: string = 'baz-obfuscated.js';
  333. const readFileEncoding = 'utf8';
  334. const regExp1: RegExp = /^var a1_0x(\w){4,6} *= *0x1;$/;
  335. const regExp2: RegExp = /^var a0_0x(\w){4,6} *= *0x2;$/;
  336. let outputFixturesFilePath1: string,
  337. outputFixturesFilePath2: string,
  338. outputFixturesFilePath3: string,
  339. isFileExist1: boolean,
  340. isFileExist2: boolean,
  341. isFileExist3: boolean,
  342. fileContent1: string,
  343. fileContent2: string;
  344. before(() => {
  345. outputFixturesFilePath1 = path.join(directoryPath, outputFileName1);
  346. outputFixturesFilePath2 = path.join(directoryPath, outputFileName2);
  347. outputFixturesFilePath3 = path.join(directoryPath, outputFileName3);
  348. JavaScriptObfuscatorCLI.obfuscate([
  349. 'node',
  350. 'javascript-obfuscator',
  351. directoryPath,
  352. '--exclude',
  353. path.join('**', 'bark.js'),
  354. '--rename-globals',
  355. 'true'
  356. ]);
  357. isFileExist1 = fs.existsSync(outputFixturesFilePath1);
  358. isFileExist2 = fs.existsSync(outputFixturesFilePath2);
  359. isFileExist3 = fs.existsSync(outputFixturesFilePath3);
  360. fileContent1 = fs.readFileSync(outputFixturesFilePath1, readFileEncoding);
  361. fileContent2 = fs.readFileSync(outputFixturesFilePath2, readFileEncoding);
  362. });
  363. it(`should create file \`${outputFileName1}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
  364. assert.equal(isFileExist1, true);
  365. });
  366. it(`should create file \`${outputFileName2}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
  367. assert.equal(isFileExist2, true);
  368. });
  369. it(`shouldn't create file \`${outputFileName3}\` in \`${fixturesDirName}\` directory`, () => {
  370. assert.equal(isFileExist3, false);
  371. });
  372. it(`match #1: should create file with obfuscated code with prefixed identifier`, () => {
  373. assert.match(fileContent1, regExp1);
  374. });
  375. it(`match #2: should create file with obfuscated code with prefixed identifier`, () => {
  376. assert.match(fileContent2, regExp2);
  377. });
  378. after(() => {
  379. rimraf.sync(outputFixturesFilePath1);
  380. rimraf.sync(outputFixturesFilePath2);
  381. });
  382. });
  383. describe('Variant #2: --exclude option is pointed on file under obfuscating directory', () => {
  384. const directoryPath: string = path.join(fixturesDirName, 'directory-obfuscation');
  385. const outputFileName1: string = 'foo-obfuscated.js';
  386. const outputFileName2: string = 'bar-obfuscated.js';
  387. const outputFileName3: string = 'baz-obfuscated.js';
  388. const readFileEncoding = 'utf8';
  389. const regExp1: RegExp = /^var a0_0x(\w){4,6} *= *0x2;$/;
  390. let outputFixturesFilePath1: string,
  391. outputFixturesFilePath2: string,
  392. outputFixturesFilePath3: string,
  393. isFileExist1: boolean,
  394. isFileExist2: boolean,
  395. isFileExist3: boolean,
  396. fileContent1: string;
  397. before(() => {
  398. outputFixturesFilePath1 = path.join(directoryPath, outputFileName1);
  399. outputFixturesFilePath2 = path.join(directoryPath, outputFileName2);
  400. outputFixturesFilePath3 = path.join(directoryPath, outputFileName3);
  401. JavaScriptObfuscatorCLI.obfuscate([
  402. 'node',
  403. 'javascript-obfuscator',
  404. directoryPath,
  405. '--exclude',
  406. path.join('**', 'foo.js'),
  407. '--rename-globals',
  408. 'true'
  409. ]);
  410. isFileExist1 = fs.existsSync(outputFixturesFilePath1);
  411. isFileExist2 = fs.existsSync(outputFixturesFilePath2);
  412. isFileExist3 = fs.existsSync(outputFixturesFilePath3);
  413. fileContent1 = fs.readFileSync(outputFixturesFilePath2, readFileEncoding);
  414. });
  415. it(`shouldn't create file \`${outputFileName1}\` in \`${fixturesDirName}\` directory`, () => {
  416. assert.equal(isFileExist1, false);
  417. });
  418. it(`should create file \`${outputFileName2}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
  419. assert.equal(isFileExist2, true);
  420. });
  421. it(`shouldn't create file \`${outputFileName3}\` in \`${fixturesDirName}\` directory`, () => {
  422. assert.equal(isFileExist3, false);
  423. });
  424. it(`match #1: should create file with obfuscated code with prefixed identifier`, () => {
  425. assert.match(fileContent1, regExp1);
  426. });
  427. after(() => {
  428. rimraf.sync(outputFixturesFilePath1);
  429. rimraf.sync(outputFixturesFilePath2);
  430. });
  431. });
  432. });
  433. });
  434. describe('`--sourceMap` option is set', () => {
  435. const outputSourceMapPath: string = `${outputFilePath}.map`;
  436. describe('Variant #1: `--sourceMapMode` option value is `separate`', () => {
  437. describe('Variant #1: default behaviour', () => {
  438. let isFileExist: boolean,
  439. resolvedSources: string,
  440. sourceCodeContent: string,
  441. sourceMapObject: ISourceMap;
  442. before((done) => {
  443. JavaScriptObfuscatorCLI.obfuscate([
  444. 'node',
  445. 'javascript-obfuscator',
  446. fixtureFilePath,
  447. '--output',
  448. outputFilePath,
  449. '--compact',
  450. 'true',
  451. '--self-defending',
  452. '0',
  453. '--source-map',
  454. 'true'
  455. ]);
  456. try {
  457. sourceCodeContent = fs.readFileSync(fixtureFilePath, { encoding: 'utf8' });
  458. const sourceMapContent: string = fs.readFileSync(outputSourceMapPath, { encoding: 'utf8' });
  459. isFileExist = true;
  460. sourceMapObject = JSON.parse(sourceMapContent);
  461. resolveSources(sourceMapObject, fixtureFilePath, fs.readFile, (error, result) => {
  462. resolvedSources = typeof result.sourcesContent[0] === 'string'
  463. ? result.sourcesContent[0]
  464. : '';
  465. done();
  466. });
  467. } catch (e) {
  468. isFileExist = false;
  469. }
  470. });
  471. it('should create file with source map in the same directory as output file', () => {
  472. assert.equal(isFileExist, true);
  473. });
  474. it('source map from created file should contains property `version`', () => {
  475. assert.property(sourceMapObject, 'version');
  476. });
  477. it('source map from created file should contains property `names`', () => {
  478. assert.property(sourceMapObject, 'names');
  479. });
  480. it('should resolve correct sources from source map', () => {
  481. assert.equal(resolvedSources, sourceCodeContent);
  482. });
  483. after(() => {
  484. rimraf.sync(outputFilePath);
  485. rimraf.sync(outputSourceMapPath);
  486. });
  487. });
  488. describe('Variant #2: `sourceMapBaseUrl` option is set', () => {
  489. let isFileExist: boolean,
  490. resolvedSources: string,
  491. sourceCodeContent: string,
  492. sourceMapObject: ISourceMap;
  493. before((done) => {
  494. JavaScriptObfuscatorCLI.obfuscate([
  495. 'node',
  496. 'javascript-obfuscator',
  497. fixtureFilePath,
  498. '--output',
  499. outputFilePath,
  500. '--compact',
  501. 'true',
  502. '--self-defending',
  503. '0',
  504. '--source-map',
  505. 'true',
  506. '--source-map-base-url',
  507. 'http://localhost:9000/'
  508. ]);
  509. try {
  510. sourceCodeContent = fs.readFileSync(fixtureFilePath, { encoding: 'utf8' });
  511. const sourceMapContent: string = fs.readFileSync(outputSourceMapPath, { encoding: 'utf8' });
  512. isFileExist = true;
  513. sourceMapObject = JSON.parse(sourceMapContent);
  514. resolveSources(sourceMapObject, fixtureFilePath, fs.readFile, (error, result) => {
  515. resolvedSources = typeof result.sourcesContent[0] === 'string'
  516. ? result.sourcesContent[0]
  517. : '';
  518. done();
  519. });
  520. } catch (e) {
  521. isFileExist = false;
  522. }
  523. });
  524. it('should create file with source map in the same directory as output file', () => {
  525. assert.equal(isFileExist, true);
  526. });
  527. it('source map from created file should contains property `version`', () => {
  528. assert.property(sourceMapObject, 'version');
  529. });
  530. it('source map from created file should contains property `names`', () => {
  531. assert.property(sourceMapObject, 'names');
  532. });
  533. it('should resolve correct sources from source map', () => {
  534. assert.equal(resolvedSources, sourceCodeContent);
  535. });
  536. after(() => {
  537. fs.unlinkSync(outputFilePath);
  538. fs.unlinkSync(outputSourceMapPath);
  539. });
  540. });
  541. describe('Variant #3: `--sourceMapFileName` option is set', () => {
  542. const sourceMapFileName: string = 'test';
  543. const sourceMapFilePath: string = `${sourceMapFileName}.js.map`;
  544. const outputSourceMapFilePath: string = path.join(outputDirName, sourceMapFilePath);
  545. let isFileExist: boolean,
  546. resolvedSources: string,
  547. sourceCodeContent: string,
  548. sourceMapObject: ISourceMap;
  549. before((done) => {
  550. JavaScriptObfuscatorCLI.obfuscate([
  551. 'node',
  552. 'javascript-obfuscator',
  553. fixtureFilePath,
  554. '--output',
  555. outputFilePath,
  556. '--compact',
  557. 'true',
  558. '--self-defending',
  559. '0',
  560. '--source-map',
  561. 'true',
  562. '--source-map-file-name',
  563. sourceMapFileName
  564. ]);
  565. try {
  566. sourceCodeContent = fs.readFileSync(fixtureFilePath, { encoding: 'utf8' });
  567. const sourceMapContent: string = fs.readFileSync(outputSourceMapFilePath, { encoding: 'utf8' });
  568. isFileExist = true;
  569. sourceMapObject = JSON.parse(sourceMapContent);
  570. resolveSources(sourceMapObject, fixtureFilePath, fs.readFile, (error, result) => {
  571. resolvedSources = typeof result.sourcesContent[0] === 'string'
  572. ? result.sourcesContent[0]
  573. : '';
  574. done();
  575. });
  576. } catch (e) {
  577. isFileExist = false;
  578. }
  579. });
  580. it('should create source map file with given name in the same directory as output file', () => {
  581. assert.equal(isFileExist, true);
  582. });
  583. it('source map from created file should contains property `version`', () => {
  584. assert.property(sourceMapObject, 'version');
  585. });
  586. it('source map from created file should contains property `names`', () => {
  587. assert.property(sourceMapObject, 'names');
  588. });
  589. it('should resolve correct sources from source map', () => {
  590. assert.equal(resolvedSources, sourceCodeContent);
  591. });
  592. after(() => {
  593. fs.unlinkSync(outputFilePath);
  594. fs.unlinkSync(outputSourceMapFilePath);
  595. });
  596. });
  597. describe('Variant #4: `sourceMapSourcesMode` option is set', () => {
  598. describe('Variant #1: `sourcesContent` value', () => {
  599. const expectedSourceMapSourceName: string = 'sourceMap';
  600. let isFileExist: boolean,
  601. resolvedSources: string,
  602. sourceCodeContent: string,
  603. sourceMapObject: ISourceMap;
  604. before((done) => {
  605. JavaScriptObfuscatorCLI.obfuscate([
  606. 'node',
  607. 'javascript-obfuscator',
  608. fixtureFilePath,
  609. '--output',
  610. outputFilePath,
  611. '--compact',
  612. 'true',
  613. '--self-defending',
  614. '0',
  615. '--source-map',
  616. 'true',
  617. '--source-map-sources-mode',
  618. 'sources-content'
  619. ]);
  620. try {
  621. sourceCodeContent = fs.readFileSync(fixtureFilePath, { encoding: 'utf8' });
  622. const sourceMapContent: string = fs.readFileSync(outputSourceMapPath, { encoding: 'utf8' });
  623. isFileExist = true;
  624. sourceMapObject = JSON.parse(sourceMapContent);
  625. resolveSources(sourceMapObject, fixtureFilePath, fs.readFile, (error, result) => {
  626. resolvedSources = typeof result.sourcesContent[0] === 'string'
  627. ? result.sourcesContent[0]
  628. : '';
  629. done();
  630. });
  631. } catch (e) {
  632. isFileExist = false;
  633. }
  634. });
  635. it('should create file with source map in the same directory as output file', () => {
  636. assert.equal(isFileExist, true);
  637. });
  638. it('source map from created file should contains property `sources`', () => {
  639. assert.property(sourceMapObject, 'sources');
  640. });
  641. it('source map from created file should contains property `sourcesContent`', () => {
  642. assert.property(sourceMapObject, 'sourcesContent');
  643. });
  644. it('source map source should has correct sources', () => {
  645. assert.equal(sourceMapObject.sources[0], expectedSourceMapSourceName);
  646. });
  647. it('source map source should has correct sources content', () => {
  648. assert.equal(sourceMapObject.sourcesContent[0], sourceCodeContent);
  649. });
  650. it('should resolve correct sources from source map', () => {
  651. assert.equal(resolvedSources, sourceCodeContent);
  652. });
  653. after(() => {
  654. rimraf.sync(outputFilePath);
  655. rimraf.sync(outputSourceMapPath);
  656. });
  657. });
  658. describe('Variant #2: `sources` value', () => {
  659. const expectedSourceMapSourceName: string = path.basename(fixtureFileName);
  660. let isFileExist: boolean,
  661. resolvedSources: string,
  662. sourceCodeContent: string,
  663. sourceMapObject: ISourceMap;
  664. before((done) => {
  665. JavaScriptObfuscatorCLI.obfuscate([
  666. 'node',
  667. 'javascript-obfuscator',
  668. fixtureFilePath,
  669. '--output',
  670. outputFilePath,
  671. '--compact',
  672. 'true',
  673. '--self-defending',
  674. '0',
  675. '--source-map',
  676. 'true',
  677. '--source-map-sources-mode',
  678. 'sources'
  679. ]);
  680. try {
  681. sourceCodeContent = fs.readFileSync(fixtureFilePath, { encoding: 'utf8' });
  682. const sourceMapContent: string = fs.readFileSync(outputSourceMapPath, { encoding: 'utf8' });
  683. isFileExist = true;
  684. sourceMapObject = JSON.parse(sourceMapContent);
  685. resolveSources(sourceMapObject, fixtureFilePath, fs.readFile, (error, result) => {
  686. resolvedSources = typeof result.sourcesContent[0] === 'string'
  687. ? result.sourcesContent[0]
  688. : '';
  689. done();
  690. });
  691. } catch (e) {
  692. isFileExist = false;
  693. }
  694. });
  695. it('should create file with source map in the same directory as output file', () => {
  696. assert.equal(isFileExist, true);
  697. });
  698. it('source map from created file should contains property `sources`', () => {
  699. assert.property(sourceMapObject, 'sources');
  700. });
  701. it('source map from created file should not contains property `sourcesContent`', () => {
  702. assert.notProperty(sourceMapObject, 'sourcesContent');
  703. });
  704. it('source map source should has correct sources', () => {
  705. assert.equal(sourceMapObject.sources[0], expectedSourceMapSourceName);
  706. });
  707. it('should resolve correct sources from source map', () => {
  708. assert.equal(resolvedSources, sourceCodeContent);
  709. });
  710. after(() => {
  711. rimraf.sync(outputFilePath);
  712. rimraf.sync(outputSourceMapPath);
  713. });
  714. });
  715. });
  716. });
  717. describe('Variant #2: `--sourceMapMode` option is `inline`', () => {
  718. describe('Variant #1: default behaviour', () => {
  719. let isFileExist: boolean,
  720. resolvedSources: string,
  721. sourceCodeContent: string,
  722. sourceMapObject: ISourceMap;
  723. before((done) => {
  724. JavaScriptObfuscatorCLI.obfuscate([
  725. 'node',
  726. 'javascript-obfuscator',
  727. fixtureFilePath,
  728. '--output',
  729. outputFilePath,
  730. '--compact',
  731. 'true',
  732. '--self-defending',
  733. '0',
  734. '--source-map',
  735. 'true',
  736. '--source-map-mode',
  737. 'inline'
  738. ]);
  739. isFileExist = fs.existsSync(outputSourceMapPath);
  740. sourceCodeContent = fs.readFileSync(fixtureFilePath, { encoding: 'utf8' });
  741. const obfuscatedCodeContent = fs.readFileSync(outputFilePath, { encoding: 'utf8' });
  742. sourceMapObject = parseSourceMapFromObfuscatedCode(obfuscatedCodeContent);
  743. resolveSources(sourceMapObject, fixtureFilePath, fs.readFile, (error, result) => {
  744. resolvedSources = typeof result.sourcesContent[0] === 'string'
  745. ? result.sourcesContent[0]
  746. : '';
  747. done();
  748. });
  749. });
  750. it('shouldn\'t create file with source map', () => {
  751. assert.equal(isFileExist, false);
  752. });
  753. it('should resolve correct sources from source map', () => {
  754. assert.equal(resolvedSources, sourceCodeContent);
  755. });
  756. after(() => {
  757. fs.unlinkSync(outputFilePath);
  758. });
  759. });
  760. describe('Variant #2: `sourceMapSourcesMode` option is set', () => {
  761. describe('Variant #1: `sourcesContent` value', () => {
  762. const expectedSourceMapSourceName: string = 'sourceMap';
  763. let isFileExist: boolean,
  764. resolvedSources: string,
  765. sourceCodeContent: string,
  766. sourceMapObject: ISourceMap;
  767. before((done) => {
  768. JavaScriptObfuscatorCLI.obfuscate([
  769. 'node',
  770. 'javascript-obfuscator',
  771. fixtureFilePath,
  772. '--output',
  773. outputFilePath,
  774. '--compact',
  775. 'true',
  776. '--self-defending',
  777. '0',
  778. '--source-map',
  779. 'true',
  780. '--source-map-mode',
  781. 'inline',
  782. '--source-map-sources-mode',
  783. 'sources-content'
  784. ]);
  785. isFileExist = fs.existsSync(outputSourceMapPath);
  786. sourceCodeContent = fs.readFileSync(fixtureFilePath, { encoding: 'utf8' });
  787. const obfuscatedCodeContent = fs.readFileSync(outputFilePath, { encoding: 'utf8' });
  788. sourceMapObject = parseSourceMapFromObfuscatedCode(obfuscatedCodeContent);
  789. resolveSources(sourceMapObject, fixtureFilePath, fs.readFile, (error, result) => {
  790. resolvedSources = typeof result.sourcesContent[0] === 'string'
  791. ? result.sourcesContent[0]
  792. : '';
  793. done();
  794. });
  795. });
  796. it('shouldn\'t create file with source map', () => {
  797. assert.equal(isFileExist, false);
  798. });
  799. it('source map from created file should contains property `sources`', () => {
  800. assert.property(sourceMapObject, 'sources');
  801. });
  802. it('source map from created file should contains property `sourcesContent`', () => {
  803. assert.property(sourceMapObject, 'sourcesContent');
  804. });
  805. it('source map source should has correct sources', () => {
  806. assert.equal(sourceMapObject.sources[0], expectedSourceMapSourceName);
  807. });
  808. it('source map source should has correct sources content', () => {
  809. assert.equal(sourceMapObject.sourcesContent[0], sourceCodeContent);
  810. });
  811. it('should resolve correct sources from source map', () => {
  812. assert.equal(resolvedSources, sourceCodeContent);
  813. });
  814. after(() => {
  815. fs.unlinkSync(outputFilePath);
  816. });
  817. });
  818. describe('Variant #2: `sources` value', () => {
  819. const expectedSourceMapSourceName: string = path.basename(fixtureFileName);
  820. let isFileExist: boolean,
  821. resolvedSources: string,
  822. sourceCodeContent: string,
  823. sourceMapObject: ISourceMap;
  824. before((done) => {
  825. JavaScriptObfuscatorCLI.obfuscate([
  826. 'node',
  827. 'javascript-obfuscator',
  828. fixtureFilePath,
  829. '--output',
  830. outputFilePath,
  831. '--compact',
  832. 'true',
  833. '--self-defending',
  834. '0',
  835. '--source-map',
  836. 'true',
  837. '--source-map-mode',
  838. 'inline',
  839. '--source-map-sources-mode',
  840. 'sources'
  841. ]);
  842. isFileExist = fs.existsSync(outputSourceMapPath);
  843. sourceCodeContent = fs.readFileSync(fixtureFilePath, { encoding: 'utf8' });
  844. const obfuscatedCodeContent = fs.readFileSync(outputFilePath, { encoding: 'utf8' });
  845. sourceMapObject = parseSourceMapFromObfuscatedCode(obfuscatedCodeContent);
  846. resolveSources(sourceMapObject, fixtureFilePath, fs.readFile, (error, result) => {
  847. resolvedSources = typeof result.sourcesContent[0] === 'string'
  848. ? result.sourcesContent[0]
  849. : '';
  850. done();
  851. });
  852. });
  853. it('shouldn\'t create file with source map', () => {
  854. assert.equal(isFileExist, false);
  855. });
  856. it('source map from created file should contains property `sources`', () => {
  857. assert.property(sourceMapObject, 'sources');
  858. });
  859. it('source map from created file should contains property `sourcesContent`', () => {
  860. assert.notProperty(sourceMapObject, 'sourcesContent');
  861. });
  862. it('source map source should has correct sources', () => {
  863. assert.equal(sourceMapObject.sources[0], expectedSourceMapSourceName);
  864. });
  865. it('should resolve correct sources from source map', () => {
  866. assert.equal(resolvedSources, sourceCodeContent);
  867. });
  868. after(() => {
  869. fs.unlinkSync(outputFilePath);
  870. });
  871. });
  872. });
  873. });
  874. });
  875. describe('help output', () => {
  876. let callback: sinon.SinonSpy<any, void>,
  877. stdoutWriteMock: StdoutWriteMock,
  878. stubExit: sinon.SinonStub;
  879. beforeEach(() => {
  880. stubExit = sinon.stub(process, 'exit');
  881. callback = sinon.spy(console, 'log');
  882. stdoutWriteMock = new StdoutWriteMock(process.stdout.write);
  883. });
  884. describe('`--help` option is set without any additional parameters', () => {
  885. let isConsoleLogCalled: boolean;
  886. beforeEach(() => {
  887. stdoutWriteMock.mute();
  888. JavaScriptObfuscatorCLI.obfuscate([
  889. 'node',
  890. 'javascript-obfuscator',
  891. '--help'
  892. ]);
  893. stdoutWriteMock.restore();
  894. isConsoleLogCalled = callback.called;
  895. });
  896. it('should print `console.log` help', () => {
  897. assert.equal(isConsoleLogCalled, true);
  898. });
  899. });
  900. describe('`--help` option is set before file path', () => {
  901. let isConsoleLogCalled: boolean;
  902. beforeEach(() => {
  903. stdoutWriteMock.mute();
  904. JavaScriptObfuscatorCLI.obfuscate([
  905. 'node',
  906. 'javascript-obfuscator',
  907. '--help',
  908. fixtureFilePath
  909. ]);
  910. stdoutWriteMock.restore();
  911. isConsoleLogCalled = callback.called;
  912. });
  913. it('should print `console.log` help', () => {
  914. assert.equal(isConsoleLogCalled, true);
  915. });
  916. });
  917. describe('`--help` option is set after file path', () => {
  918. let isConsoleLogCalled: boolean;
  919. beforeEach(() => {
  920. stdoutWriteMock.mute();
  921. JavaScriptObfuscatorCLI.obfuscate([
  922. 'node',
  923. 'javascript-obfuscator',
  924. fixtureFilePath,
  925. '--help'
  926. ]);
  927. stdoutWriteMock.restore();
  928. isConsoleLogCalled = callback.called;
  929. });
  930. it('should print `console.log` help', () => {
  931. assert.equal(isConsoleLogCalled, true);
  932. });
  933. });
  934. describe('no arguments passed', () => {
  935. let isConsoleLogCalled: boolean;
  936. beforeEach(() => {
  937. stdoutWriteMock.mute();
  938. JavaScriptObfuscatorCLI.obfuscate([
  939. 'node',
  940. 'javascript-obfuscator'
  941. ]);
  942. stdoutWriteMock.restore();
  943. isConsoleLogCalled = callback.called;
  944. });
  945. it('should print `console.log` help', () => {
  946. assert.equal(isConsoleLogCalled, true);
  947. });
  948. });
  949. afterEach(() => {
  950. stubExit.restore();
  951. callback.restore();
  952. });
  953. });
  954. describe('`--config` option is set', () => {
  955. describe('Base options', () => {
  956. const outputSourceMapPath: string = `${outputFilePath}.map`;
  957. let isFileExist: boolean,
  958. sourceMapObject: any;
  959. before(() => {
  960. JavaScriptObfuscatorCLI.obfuscate([
  961. 'node',
  962. 'javascript-obfuscator',
  963. fixtureFilePath,
  964. '--output',
  965. outputFilePath,
  966. '--config',
  967. configFilePath
  968. ]);
  969. try {
  970. const content: string = fs.readFileSync(outputSourceMapPath, {encoding: 'utf8'});
  971. isFileExist = true;
  972. sourceMapObject = JSON.parse(content);
  973. } catch (e) {
  974. isFileExist = false;
  975. }
  976. });
  977. it('should create file with source map in the same directory as output file', () => {
  978. assert.equal(isFileExist, true);
  979. });
  980. it('source map from created file should contains property `version`', () => {
  981. assert.property(sourceMapObject, 'version');
  982. });
  983. it('source map from created file should contains property `sources`', () => {
  984. assert.property(sourceMapObject, 'sources');
  985. });
  986. it('source map from created file should contains property `names`', () => {
  987. assert.property(sourceMapObject, 'names');
  988. });
  989. after(() => {
  990. fs.unlinkSync(outputFilePath);
  991. fs.unlinkSync(outputSourceMapPath);
  992. });
  993. });
  994. describe('`--exclude` option', () => {
  995. const directoryPath: string = path.join(fixturesDirName, 'directory-obfuscation');
  996. const outputFileName1: string = 'foo-obfuscated.js';
  997. const outputFileName2: string = 'bar-obfuscated.js';
  998. let outputFixturesFilePath1: string,
  999. outputFixturesFilePath2: string,
  1000. isFileExist1: boolean,
  1001. isFileExist2: boolean;
  1002. before(() => {
  1003. outputFixturesFilePath1 = path.join(directoryPath, outputFileName1);
  1004. outputFixturesFilePath2 = path.join(directoryPath, outputFileName2);
  1005. JavaScriptObfuscatorCLI.obfuscate([
  1006. 'node',
  1007. 'javascript-obfuscator',
  1008. directoryPath,
  1009. '--config',
  1010. configFilePath
  1011. ]);
  1012. isFileExist1 = fs.existsSync(outputFixturesFilePath1);
  1013. isFileExist2 = fs.existsSync(outputFixturesFilePath2);
  1014. });
  1015. it(`shouldn't create file \`${outputFileName1}\` in \`${fixturesDirName}\` directory`, () => {
  1016. assert.equal(isFileExist1, false);
  1017. });
  1018. it(`should create file \`${outputFileName2}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
  1019. assert.equal(isFileExist2, true);
  1020. });
  1021. after(() => {
  1022. rimraf.sync(outputFixturesFilePath1);
  1023. rimraf.sync(outputFixturesFilePath2);
  1024. });
  1025. });
  1026. });
  1027. describe('`--config` option is set but overridden by CLI option', () => {
  1028. const outputSourceMapPath: string = `${outputFilePath}.map`;
  1029. let isFileExist: boolean;
  1030. before(() => {
  1031. JavaScriptObfuscatorCLI.obfuscate([
  1032. 'node',
  1033. 'javascript-obfuscator',
  1034. fixtureFilePath,
  1035. '--output',
  1036. outputFilePath,
  1037. '--config',
  1038. configFilePath,
  1039. '--source-map',
  1040. 'false',
  1041. ]);
  1042. try {
  1043. fs.readFileSync(outputSourceMapPath, {encoding: 'utf8'});
  1044. isFileExist = true;
  1045. } catch (e) {
  1046. isFileExist = false;
  1047. }
  1048. });
  1049. it('should create file without source map in the same directory as output file', () => {
  1050. assert.equal(isFileExist, false);
  1051. });
  1052. after(() => {
  1053. fs.unlinkSync(outputFilePath);
  1054. });
  1055. });
  1056. describe('Logging', () => {
  1057. describe('Obfuscating file message', () => {
  1058. const directoryPath: string = path.join(fixturesDirName, 'directory-obfuscation');
  1059. const inputFileName1: string = 'foo.js';
  1060. const inputFileName2: string = 'bar.js';
  1061. const inputFilePath1: string = path.join(directoryPath, inputFileName1);
  1062. const inputFilePath2: string = path.join(directoryPath, inputFileName2);
  1063. const outputFileName1: string = 'foo-obfuscated.js';
  1064. const outputFileName2: string = 'bar-obfuscated.js';
  1065. const outputFilePath1: string = path.join(directoryPath, outputFileName1);
  1066. const outputFilePath2: string = path.join(directoryPath, outputFileName2);
  1067. const expectedLoggingMessage1: string = `[javascript-obfuscator-cli] Obfuscating file: ${inputFilePath1}...`;
  1068. const expectedLoggingMessage2: string = `[javascript-obfuscator-cli] Obfuscating file: ${inputFilePath2}...`;
  1069. let consoleLogSpy: sinon.SinonSpy<any, void>,
  1070. loggingMessageResult1: string,
  1071. loggingMessageResult2: string;
  1072. before(() => {
  1073. consoleLogSpy = sinon.spy(console, 'log');
  1074. JavaScriptObfuscatorCLI.obfuscate([
  1075. 'node',
  1076. 'javascript-obfuscator',
  1077. directoryPath,
  1078. '--rename-globals',
  1079. 'true'
  1080. ]);
  1081. loggingMessageResult1 = consoleLogSpy.getCall(1).args[0];
  1082. loggingMessageResult2 = consoleLogSpy.getCall(0).args[0];
  1083. });
  1084. it('Variant #1: should log file name to the console', () => {
  1085. assert.include(loggingMessageResult1, expectedLoggingMessage1);
  1086. });
  1087. it('Variant #2: should log file name to the console', () => {
  1088. assert.include(loggingMessageResult2, expectedLoggingMessage2);
  1089. });
  1090. after(() => {
  1091. rimraf.sync(outputFilePath1);
  1092. rimraf.sync(outputFilePath2);
  1093. consoleLogSpy.restore();
  1094. });
  1095. });
  1096. describe('Error message', () => {
  1097. const directoryPath: string = path.join(fixturesDirName, 'directory-obfuscation-error');
  1098. const inputFileName: string = 'foo.js';
  1099. const inputFilePath: string = path.join(directoryPath, inputFileName);
  1100. const expectedLoggingMessage1: string = `[javascript-obfuscator-cli] Error in file: ${inputFilePath}...`;
  1101. let consoleLogSpy: sinon.SinonSpy<any, void>,
  1102. loggingMessageResult: string
  1103. before(() => {
  1104. consoleLogSpy = sinon.spy(console, 'log');
  1105. try {
  1106. JavaScriptObfuscatorCLI.obfuscate([
  1107. 'node',
  1108. 'javascript-obfuscator',
  1109. directoryPath,
  1110. '--rename-globals',
  1111. 'true'
  1112. ]);
  1113. } catch {}
  1114. loggingMessageResult = consoleLogSpy.getCall(1).args[0];
  1115. });
  1116. it('Should log file name to the console', () => {
  1117. assert.include(loggingMessageResult, expectedLoggingMessage1);
  1118. });
  1119. after(() => {
  1120. consoleLogSpy.restore();
  1121. });
  1122. });
  1123. });
  1124. after(() => {
  1125. rimraf.sync(outputDirName);
  1126. });
  1127. });
  1128. });