Newer
Older
CVSS_3.0_GUI / node_modules / nwjs-builder-phoenix / node_modules / dir-compare / tests / runTests.js
root on 7 May 2019 36 KB Initial commit
  1. "use strict";
  2. var colors = require('colors');
  3. var pathUtils = require('path');
  4. var shelljs = require('shelljs');
  5. var util = require('util');
  6. var fs = require('fs');
  7. var temp = require('temp');
  8. var defaultPrint = require('../print');
  9. var Promise = require('bluebird');
  10. var Streams = require('memory-streams');
  11. var compareSync = require('../index').compareSync;
  12. var compareAsync = require('../index').compare;
  13. var untar = require('./untar');
  14.  
  15. var count = 0, failed = 0, successful = 0;
  16. var syncCount = 0, syncFailed = 0, syncSuccessful = 0;
  17. var asyncCount = 0, asyncFailed = 0, asyncSuccessful = 0;
  18. var cmdLineCount = 0, cmdLineFailed = 0, cmdLineSuccessful = 0;
  19.  
  20. //Automatically track and cleanup files at exit
  21. temp.track();
  22.  
  23.  
  24. function passed (value, type) {
  25. count++;
  26. if (value) {
  27. successful++;
  28. } else {
  29. failed++;
  30. }
  31.  
  32. if(type==='sync'){
  33. syncCount++;
  34. if (value) {
  35. syncSuccessful++;
  36. } else {
  37. syncFailed++;
  38. }
  39. }
  40.  
  41. if(type==='async'){
  42. asyncCount++;
  43. if (value) {
  44. asyncSuccessful++;
  45. } else {
  46. asyncFailed++;
  47. }
  48. }
  49.  
  50. if(type==='cmdLine'){
  51. cmdLineCount++;
  52. if (value) {
  53. cmdLineSuccessful++;
  54. } else {
  55. cmdLineFailed++;
  56. }
  57. }
  58.  
  59. return value ? 'Passed'.green : '!!!!FAILED!!!!'.yellow;
  60. }
  61.  
  62. /**
  63. * Parameters:
  64. * * name - Test name. This represents also the name of the file holding expected result unless overriden by 'expected' param.
  65. * * description - describes what test does
  66. * * expected - Expected result.
  67. * * withRelativePath - Left/right dirs will be relative to current process.
  68. * * options - Options sent to library test. Should match 'commandLineOptions.
  69. * * commandLineOptions - Options sent to command line test. Should match 'options'.
  70. * * exitCode - Command line expected exit code.
  71. * * displayOptions - Display parameters for print method.
  72. * * print - Prints test result. If missing 'defaultPrint()' is used.
  73. * * onlyLibrary - Test is run only on API methods.
  74. * * onlyCommandLine - Test is run only on command line.
  75. * * skipStatisticsCheck - Do not call checkStatistics() after each library test.
  76. */
  77. var tests = [
  78. {
  79. name: 'test001_1', path1: 'd1', path2: 'd2',
  80. options: {compareSize: true,},
  81. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  82. commandLineOptions: '-aw',
  83. exitCode: 1,
  84. },
  85. {
  86. name: 'test001_2', path1: 'd1', path2: 'd2',
  87. options: {compareSize: true,},
  88. displayOptions: {showAll: true, wholeReport: true, csv: true, nocolors: true},
  89. commandLineOptions: '-aw --csv',
  90. exitCode: 1,
  91. },
  92. {
  93. name: 'test001_3', path1: 'd3', path2: 'd4',
  94. options: {compareSize: true,},
  95. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  96. commandLineOptions: '-aw',
  97. exitCode: 1,
  98. },
  99. {
  100. name: 'test001_4', path1: 'd4', path2: 'd4',
  101. options: {compareSize: true,},
  102. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  103. commandLineOptions: '-aw',
  104. exitCode: 0,
  105. },
  106. {
  107. name: 'test001_5', path1: 'd8', path2: 'd9',
  108. options: {compareSize: true,},
  109. displayOptions: {showAll: true, nocolors: true},
  110. commandLineOptions: '-a',
  111. exitCode: 1,
  112. },
  113. {
  114. name: 'test001_6', path1: 'd8', path2: 'd9',
  115. options: {compareSize: true,},
  116. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  117. commandLineOptions: '-aw',
  118. exitCode: 1,
  119. },
  120. {
  121. name: 'test001_8', path1: 'd1', path2: 'd2',
  122. options: {compareSize: true,},
  123. displayOptions: {nocolors: true},
  124. commandLineOptions: '',
  125. exitCode: 1,
  126. },
  127.  
  128. ////////////////////////////////////////////////////
  129. // Filters //
  130. ////////////////////////////////////////////////////
  131. {
  132. name: 'test002_0', path1: 'd6', path2: 'd7',
  133. options: {compareSize: true, includeFilter: '*.e1'},
  134. displayOptions: {showAll: true, nocolors: true},
  135. commandLineOptions: '-a -f "*.e1"',
  136. exitCode: 1,
  137. },
  138. {
  139. name: 'test002_1', path1: 'd1', path2: 'd10',
  140. options: {compareSize: true, excludeFilter: '.x'},
  141. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  142. commandLineOptions: '-aw -x .x',
  143. exitCode: 1,
  144. },
  145. {
  146. name: 'test002_2', path1: 'd6', path2: 'd7',
  147. options: {compareSize: true, includeFilter: '*.e1'},
  148. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  149. commandLineOptions: '-aw -f "*.e1"',
  150. exitCode: 1,
  151. },
  152. {
  153. name: 'test002_3', path1: 'd1', path2: 'd2',
  154. options: {compareSize: true, excludeFilter: '*.txt'},
  155. displayOptions: {showAll: true, nocolors: true},
  156. commandLineOptions: '-a -x "*.txt"',
  157. exitCode: 1,
  158. },
  159. {
  160. name: 'test002_4', path1: 'd1', path2: 'd2',
  161. options: {compareSize: true, excludeFilter: '*.txt'},
  162. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  163. commandLineOptions: '-aw -x "*.txt"',
  164. exitCode: 1,
  165. },
  166. {
  167. name: 'test002_5', path1: 'd6', path2: 'd7',
  168. options: {compareSize: true, excludeFilter: '*.e1,*.e2'},
  169. displayOptions: {showAll: true, nocolors: true},
  170. commandLineOptions: '-a -x "*.e1,*.e2"',
  171. exitCode: 1,
  172. },
  173. {
  174. name: 'test002_6', path1: 'd6', path2: 'd7',
  175. options: {compareSize: true, excludeFilter: '*.e1,*.e2'},
  176. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  177. commandLineOptions: '-aw -x "*.e1,*.e2"',
  178. exitCode: 1,
  179. },
  180. // TODO: test both --exclude and --filter in the same run
  181.  
  182. ////////////////////////////////////////////////////
  183. // Compare by content //
  184. ////////////////////////////////////////////////////
  185. // TODO: add test with compareSize: false, compareContent: true
  186. {
  187. name: 'test003_0', path1: 'd11', path2: 'd12',
  188. options: {compareSize: true, compareContent: true},
  189. displayOptions: {showAll: true, nocolors: true},
  190. commandLineOptions: '-ac',
  191. exitCode: 1,
  192. },
  193. {
  194. name: 'test003_1', path1: 'd1', path2: 'd2',
  195. options: {compareSize: true, compareContent: true},
  196. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  197. commandLineOptions: '-awc',
  198. exitCode: 1,
  199. },
  200. ////////////////////////////////////////////////////
  201. // Command line //
  202. ////////////////////////////////////////////////////
  203. {
  204. name: 'test004_0', path1: 'd11', path2: 'd11',
  205. onlyCommandLine: true,
  206. commandLineOptions: '',
  207. exitCode: 0,
  208. },
  209. {
  210. name: 'test004_1', path1: 'd11', path2: 'd12',
  211. onlyCommandLine: true,
  212. commandLineOptions: '-c',
  213. exitCode: 1,
  214. },
  215. {
  216. name: 'test004_2', path1: 'd11', path2: 'd11',
  217. onlyCommandLine: true,
  218. commandLineOptions: '--WRONGCMD ',
  219. exitCode: 2,
  220. },
  221. {
  222. name: 'test004_3', path1: 'd11', path2: '',
  223. onlyCommandLine: true,
  224. commandLineOptions: '',
  225. exitCode: 2,
  226. },
  227. {
  228. name: 'test004_4', path1: 'd11', path2: 'miss',
  229. onlyCommandLine: true,
  230. commandLineOptions: '',
  231. exitCode: 2,
  232. },
  233. {
  234. name: 'test004_5', path1: 'd11', path2: 'd1',
  235. onlyCommandLine: true,
  236. commandLineOptions: '-ABCD',
  237. exitCode: 2,
  238. },
  239. {
  240. name: 'test004_6', path1: 'd11', path2: 'd1',
  241. onlyCommandLine: true,
  242. commandLineOptions: '-ABCD --csv',
  243. exitCode: 2,
  244. },
  245. {
  246. name: 'test004_7', path1: 'd11', path2: 'd1',
  247. onlyCommandLine: true,
  248. commandLineOptions: '--csv -ABCD --csv',
  249. exitCode: 2,
  250. },
  251. {
  252. name: 'test004_8', path1: 'd11', path2: 'd1',
  253. onlyCommandLine: true,
  254. commandLineOptions: '--csv -ABCD',
  255. exitCode: 2,
  256. },
  257. {
  258. name: 'test004_9', path1: 'd11', path2: 'd1',
  259. onlyCommandLine: true,
  260. commandLineOptions: '--ABC --async -x --async',
  261. exitCode: 2,
  262. },
  263.  
  264. ////////////////////////////////////////////////////
  265. // Symlinks //
  266. ////////////////////////////////////////////////////
  267. {
  268. name: 'test005_0', path1: 'd13', path2: 'd14',
  269. options: {compareSize: true, skipSymlinks: true},
  270. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  271. commandLineOptions: '-awL',
  272. exitCode: 1,
  273. },
  274. {
  275. name: 'test005_1', path1: 'd17', path2: 'd17',
  276. options: {compareSize: true, ignoreCase: true},
  277. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  278. commandLineOptions: '-aw',
  279. exitCode: 0,
  280. },
  281. {
  282. name: 'test005_1_1', path1: 'd17', path2: 'd17', withRelativePath: true,
  283. options: {compareSize: true, ignoreCase: true},
  284. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  285. commandLineOptions: '-aw',
  286. exitCode: 0,
  287. },
  288. {
  289. name: 'test005_2', path1: 'd17', path2: 'd17',
  290. options: {compareSize: true, ignoreCase: true, skipSymlinks: true},
  291. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  292. commandLineOptions: '-awL',
  293. exitCode: 0,
  294. },
  295. {
  296. name: 'test005_3', path1: 'd17', path2: 'd18',
  297. options: {compareSize: true, ignoreCase: true},
  298. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  299. commandLineOptions: '-aw',
  300. exitCode: 1,
  301. },
  302. {
  303. name: 'test005_4', path1: 'd22', path2: 'd22',
  304. options: {compareSize: true, ignoreCase: true},
  305. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  306. commandLineOptions: '-aw',
  307. exitCode: 0,
  308. },
  309. {
  310. name: 'test005_5', path1: 'd19', path2: 'd19',
  311. options: {compareSize: true, ignoreCase: true},
  312. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  313. commandLineOptions: '-aw',
  314. exitCode: 0,
  315. },
  316. {
  317. name: 'test005_5_1', path1: 'd19', path2: 'd19', withRelativePath: true,
  318. options: {compareSize: true, ignoreCase: true},
  319. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  320. commandLineOptions: '-aw',
  321. exitCode: 0,
  322. },
  323. {
  324. name: 'test005_6', path1: 'd19', path2: 'd19',
  325. options: {compareSize: true, ignoreCase: true, skipSymlinks: true},
  326. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  327. commandLineOptions: '-awL',
  328. exitCode: 0,
  329. },
  330. {
  331. name: 'test005_7', path1: 'd20', path2: 'd20',
  332. options: {compareSize: true, ignoreCase: true},
  333. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  334. commandLineOptions: '-aw',
  335. exitCode: 0,
  336. },
  337. {
  338. name: 'test005_8', path1: 'd21', path2: 'd21',
  339. options: {compareSize: true, ignoreCase: true},
  340. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  341. commandLineOptions: '-aw',
  342. exitCode: 0,
  343. },
  344. {
  345. name: 'test005_9', path1: 'd20', path2: 'd21',
  346. options: {compareSize: true, ignoreCase: true},
  347. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  348. commandLineOptions: '-aw',
  349. exitCode: 1,
  350. },
  351. {
  352. name: 'test005_10', path1: 'd21', path2: 'd20',
  353. options: {compareSize: true, ignoreCase: true},
  354. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  355. commandLineOptions: '-aw',
  356. exitCode: 1,
  357. },
  358. {
  359. name: 'test005_11', path1: 'd20', path2: 'd22',
  360. options: {compareSize: true, ignoreCase: true},
  361. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  362. commandLineOptions: '-aw',
  363. exitCode: 1,
  364. },
  365. {
  366. name: 'test005_12', path1: 'd22', path2: 'd20',
  367. options: {compareSize: true, ignoreCase: true},
  368. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  369. commandLineOptions: '-aw',
  370. exitCode: 1,
  371. },
  372. {
  373. name: 'test005_13', path1: 'd23', path2: 'd23',
  374. description: 'be able to compare symlinks to files',
  375. options: {compareSize: true, ignoreCase: true},
  376. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  377. commandLineOptions: '-aw',
  378. exitCode: 0,
  379. },
  380. {
  381. name: 'test005_14', path1: 'd24', path2: 'd24',
  382. options: {compareSize: true, ignoreCase: true},
  383. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  384. commandLineOptions: '-aw',
  385. exitCode: 0,
  386. },
  387. {
  388. name: 'test005_15', path1: 'd25', path2: 'd25',
  389. description: 'do not fail when missing symlinks are encountered',
  390. options: {compareSize: true, ignoreCase: true, skipSymlinks: true},
  391. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  392. commandLineOptions: '-aw --skip-symlinks',
  393. exitCode: 0,
  394. },
  395. {
  396. name: 'test005_16', path1: 'd26', path2: 'd27',
  397. description: 'detect symbolic link loops; loops span between left/right directories',
  398. options: {compareSize: true, ignoreCase: true},
  399. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  400. commandLineOptions: '-aw',
  401. exitCode: 1,
  402. },
  403. {
  404. name: 'test005_17', path1: 'd28', path2: 'd28',
  405. description: 'detect symbolic link loops; loop back to root directory',
  406. options: {compareSize: true, ignoreCase: true},
  407. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  408. commandLineOptions: '-aw',
  409. exitCode: 0,
  410. },
  411. {
  412. name: 'test005_18', path1: 'd29', path2: 'd30',
  413. description: 'compare two symlinks',
  414. options: {compareSize: true, ignoreCase: true},
  415. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  416. commandLineOptions: '-aw',
  417. exitCode: 0,
  418. },
  419.  
  420. ////////////////////////////////////////////////////
  421. // Skip subdirs //
  422. ////////////////////////////////////////////////////
  423. {
  424. name: 'test006_0', path1: 'd1', path2: 'd2',
  425. options: {compareSize: true, skipSubdirs: true},
  426. displayOptions: {showAll: true, nocolors: true},
  427. commandLineOptions: '-aS',
  428. exitCode: 1,
  429. },
  430. {
  431. name: 'test006_1', path1: 'd1', path2: 'd2',
  432. options: {compareSize: true, skipSubdirs: true},
  433. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  434. commandLineOptions: '-awS',
  435. exitCode: 1,
  436. },
  437. ////////////////////////////////////////////////////
  438. // Ignore case //
  439. ////////////////////////////////////////////////////
  440. {
  441. name: 'test007_0', path1: 'd15', path2: 'd16',
  442. options: {compareSize: true, ignoreCase: true},
  443. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  444. commandLineOptions: '-awi',
  445. exitCode: 0,
  446. },
  447. {
  448. name: 'test007_1', path1: 'd15', path2: 'd16',
  449. options: {compareSize: true, ignoreCase: false},
  450. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  451. commandLineOptions: '-aw',
  452. exitCode: 1,
  453. },
  454. ////////////////////////////////////////////////////
  455. // Options handling //
  456. ////////////////////////////////////////////////////
  457. {
  458. name: 'test008_1', path1: 'd1', path2: 'd2',
  459. expected: 'total: 17, equal: 3, distinct: 0, only left: 7, only right: 7',
  460. options: {},
  461. displayOptions: {wholeReport: true, nocolors: true, noDiffIndicator: true},
  462. onlyLibrary: true,
  463. },
  464. {
  465. name: 'test008_2', path1: 'd1', path2: 'd2',
  466. expected: 'total: 17, equal: 3, distinct: 0, only left: 7, only right: 7',
  467. options: undefined,
  468. displayOptions: {wholeReport: true, nocolors: true, noDiffIndicator: true},
  469. onlyLibrary: true,
  470. },
  471. {
  472. name: 'test008_3', path1: 'd1', path2: 'd2',
  473. expected: 'total: 17, equal: 3, distinct: 0, only left: 7, only right: 7',
  474. options: null,
  475. displayOptions: {wholeReport: true, nocolors: true, noDiffIndicator: true},
  476. onlyLibrary: true,
  477. },
  478. ////////////////////////////////////////////////////
  479. // Result Builder Callback //
  480. ////////////////////////////////////////////////////
  481. {
  482. name: 'test009_1', path1: 'd1', path2: 'd2',
  483. expected: 'test: 17',
  484. options: {resultBuilder: function (entry1, entry2, state, level, relativePath, options, statistics, diffSet){
  485. if(!statistics.test){
  486. statistics.test = 0;
  487. }
  488. statistics.test++;
  489. }},
  490. displayOptions: {},
  491. onlyLibrary: true,
  492. skipStatisticsCheck: true,
  493. print: function(res, writer, program){writer.write('test: '+res.test);}
  494. },
  495. {
  496. name: 'test009_2', path1: 'd1', path2: 'd2',
  497. expected: 'diffset: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]',
  498. options: {resultBuilder: function (entry1, entry2, state, level, relativePath, options, statistics, diffSet){
  499. if(!statistics.test){
  500. statistics.test = 0;
  501. }
  502. statistics.test++;
  503. diffSet.push(statistics.test);
  504. }},
  505. displayOptions: {},
  506. onlyLibrary: true,
  507. skipStatisticsCheck: true,
  508. print: function(res, writer, program){writer.write(' diffset: '+JSON.stringify(res.diffSet.sort(function(a, b){return a-b;}), null, 0));}
  509. },
  510. ////////////////////////////////////////////////////
  511. // Compare date //
  512. ////////////////////////////////////////////////////
  513. {
  514. name: 'test010_0', path1: 'd31', path2: 'd32',
  515. options: {compareSize: true, compareDate: false},
  516. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  517. commandLineOptions: '-aw',
  518. exitCode: 0,
  519. },
  520. {
  521. name: 'test010_1', path1: 'd31', path2: 'd32',
  522. options: {compareSize: true, compareDate: true},
  523. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  524. commandLineOptions: '-awD',
  525. exitCode: 1,
  526. },
  527. {
  528. name: 'test010_2', path1: 'd31', path2: 'd32',
  529. options: {compareSize: true, compareDate: false, compareContent: true},
  530. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  531. commandLineOptions: '-awc',
  532. exitCode: 1,
  533. },
  534. {
  535. name: 'test010_3', path1: 'd31', path2: 'd32',
  536. options: {compareSize: true, compareDate: true, compareContent: true},
  537. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  538. commandLineOptions: '-awcD',
  539. exitCode: 1,
  540. },
  541. {
  542. name: 'test010_4', path1: 'd33/1', path2: 'd33/2',
  543. description: 'should correctly use tolerance in date comparison',
  544. options: {compareSize: true, compareDate: true, dateTolerance: 5000},
  545. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  546. commandLineOptions: '-awD --date-tolerance 5000',
  547. exitCode: 1,
  548. },
  549. {
  550. name: 'test010_5', path1: 'd33/1', path2: 'd33/2',
  551. description: 'should correctly use tolerance in date comparison',
  552. options: {compareSize: true, compareDate: true, dateTolerance: 9000},
  553. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  554. commandLineOptions: '-awD --date-tolerance 9000',
  555. exitCode: 0,
  556. },
  557. {
  558. name: 'test010_6', path1: 'd33/1', path2: 'd33/2',
  559. description: 'should default to 1000 ms for date tolerance',
  560. options: {compareSize: true, compareDate: true},
  561. displayOptions: {showAll: true, wholeReport: true, nocolors: true},
  562. commandLineOptions: '-awD',
  563. exitCode: 1,
  564. },
  565. ];
  566.  
  567. //Matches date (ie 2014-11-18T21:32:39.000Z)
  568. function normalize (str) {
  569. str = normalizeDate(str);
  570. str = normalizeLineEnding(str);
  571. return str;
  572. }
  573. var normalizeDateRegexp = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/gm;
  574. function normalizeDate (str) {
  575. // replace date
  576. return str.replace(normalizeDateRegexp, 'x');
  577. }
  578. var normalizeLineEndingRegexp = /\r\n/g;
  579. function normalizeLineEnding (str) {
  580. return str.replace(normalizeLineEndingRegexp, '\n');
  581. }
  582.  
  583. var checkStatistics = function(statistics, test){
  584. if(test.skipStatisticsCheck){
  585. return true;
  586. }
  587. if (statistics.differences != statistics.left + statistics.right + statistics.distinct) {
  588. return false;
  589. }
  590. if (statistics.differencesFiles != statistics.leftFiles + statistics.rightFiles + statistics.distinctFiles) {
  591. return false;
  592. }
  593. if (statistics.differencesDirs != statistics.leftDirs + statistics.rightDirs + statistics.distinctDirs) {
  594. return false;
  595. }
  596. if (statistics.total != statistics.equal + statistics.differences) {
  597. return false;
  598. }
  599. if (statistics.totalFiles != statistics.equalFiles + statistics.differencesFiles) {
  600. return false;
  601. }
  602. if (statistics.totalDirs != statistics.equalDirs + + statistics.differencesDirs) {
  603. return false;
  604. }
  605.  
  606. if (statistics.total != statistics.totalDirs + + statistics.totalFiles) {
  607. return false;
  608. }
  609. if (statistics.equal != statistics.equalDirs + + statistics.equalFiles) {
  610. return false;
  611. }
  612. if (statistics.left != statistics.leftDirs + + statistics.leftFiles) {
  613. return false;
  614. }
  615. if (statistics.right != statistics.rightDirs + + statistics.rightFiles) {
  616. return false;
  617. }
  618. if (statistics.distinct != statistics.distinctDirs + + statistics.distinctFiles) {
  619. return false;
  620. }
  621. return true;
  622. }
  623.  
  624. var getExpected = function(test){
  625. if(test.expected){
  626. return test.expected.trim();
  627. } else{
  628. return normalize(fs.readFileSync(__dirname + '/expected/' + test.name + '.txt', 'utf8')).trim();
  629. }
  630. }
  631.  
  632. var testSync = function(test, testDirPath, saveReport){
  633. process.chdir(testDirPath);
  634. var path1, path2;
  635. if(test.withRelativePath){
  636. path1 = test.path1;
  637. path2 = test.path2;
  638. } else{
  639. path1 = test.path1?testDirPath + '/' + test.path1:'';
  640. path2 = test.path2?testDirPath + '/' + test.path2:'';
  641. }
  642. return new Promise(function(resolve, reject) {
  643. resolve(compareSync(path1, path2, test.options));
  644. }).then(
  645. function(result){
  646. // PRINT DETAILS
  647. var writer = new Streams.WritableStream();
  648. var print = test.print?test.print:defaultPrint;
  649. print(result, writer, test.displayOptions);
  650. var output = normalize(writer.toString()).trim();
  651. var expected = getExpected(test);
  652. if (test.name == 'test010_5x') {
  653. console.log(output);
  654. console.log(expected);
  655. // expected.forEach(function(exp){console.log(exp)});
  656. console.log(output === expected);
  657. }
  658. var statisticsCheck = checkStatistics(result, test);
  659. var res = expected===output && statisticsCheck;
  660. report(test.name, 'sync', output, null, res, saveReport);
  661. console.log(test.name + ' sync: ' + passed(res, 'sync'));
  662. }, function(error){
  663. report(test.name, 'sync', error instanceof Error? error.stack: error, null, false, saveReport);
  664. console.log(test.name + ' sync: ' + passed(false, 'sync') + '. Error: ' + printError(error));
  665. });
  666. }
  667.  
  668. var testAsync = function(test, testDirPath, saveReport){
  669. process.chdir(testDirPath);
  670. var path1, path2;
  671. if(test.withRelativePath){
  672. path1 = test.path1;
  673. path2 = test.path2;
  674. } else{
  675. path1 = test.path1?testDirPath + '/' + test.path1:'';
  676. path2 = test.path2?testDirPath + '/' + test.path2:'';
  677. }
  678. return compareAsync(path1, path2, test.options).then(
  679. function(result){
  680. // PRINT DETAILS
  681. var writer = new Streams.WritableStream();
  682. var print = test.print?test.print:defaultPrint;
  683. print(result, writer, test.displayOptions);
  684. var output = normalize(writer.toString()).trim();
  685. var expected = getExpected(test);
  686.  
  687. if (test.name == 'test005_14x') {
  688. console.log(output);
  689. console.log(expected);
  690. // expected.forEach(function(exp){console.log(exp)});
  691. }
  692. var statisticsCheck = checkStatistics(result, test);
  693. var res = expected===output && statisticsCheck;
  694. report(test.name, 'async', output, null, res, saveReport);
  695. console.log(test.name + ' async: ' + passed(res, 'async'));
  696. }, function(error){
  697. report(test.name, 'async', error instanceof Error? error.stack: error, null, false, saveReport);
  698. console.log(test.name + ' async: ' + passed(false, 'async') + '. Error: ' + printError(error));
  699. });
  700. }
  701.  
  702. function testCommandLineInternal(test, testDirPath, async, saveReport) {
  703. return new Promise(function(resolve, reject) {
  704. var dircompareJs = pathUtils.normalize(__dirname + '/../dircompare.js');
  705. process.chdir(testDirPath);
  706. var path1, path2;
  707. if(test.withRelativePath){
  708. path1 = test.path1;
  709. path2 = test.path2;
  710. } else{
  711. path1 = test.path1?testDirPath + '/' + test.path1:'';
  712. path2 = test.path2?testDirPath + '/' + test.path2:'';
  713. }
  714. var command = util.format("node %s %s %s %s %s",
  715. dircompareJs, test.commandLineOptions, async ? '--async' : '', path1, path2);
  716. var shelljsResult = shelljs.exec(command, {
  717. silent : true
  718. });
  719. var output = normalize(shelljsResult.output).trim();
  720. var exitCode = shelljsResult.code;
  721.  
  722. var expectedExitCode = test.exitCode;
  723. var res;
  724. if(expectedExitCode===2){
  725. // output not relevant for error codes
  726. res = (exitCode === expectedExitCode);
  727. } else{
  728. var expectedOutput = getExpected(test);
  729. res = expectedOutput===output && (exitCode === expectedExitCode);
  730. }
  731. if (test.name == 'test010_5x') {
  732. console.log(output);
  733. console.log(expectedOutput);
  734. }
  735. var testDescription = 'command line ' + (async?'async':'sync');
  736. report(test.name, testDescription, output, exitCode, res, saveReport);
  737. console.log(test.name + ' ' + testDescription + ': ' + passed(res, 'cmdLine'));
  738. resolve();
  739. })
  740. }
  741.  
  742. var testCommandLine = function(test, testDirPath, saveReport){
  743. return Promise.all([
  744. testCommandLineInternal(test, testDirPath, false, saveReport),
  745. testCommandLineInternal(test, testDirPath, true, saveReport)
  746. ]);
  747. }
  748.  
  749. function printError(error){
  750. return error instanceof Error ? error.stack : error;
  751. }
  752.  
  753. function initReport(saveReport){
  754. if(saveReport){
  755. if(fs.existsSync(REPORT_FILE)){
  756. fs.unlinkSync(REPORT_FILE);
  757. }
  758. var os = require('os');
  759. var pjson = require('../package.json');
  760. fs.appendFileSync(REPORT_FILE, util.format('Date: %s, Node version: %s. OS platform: %s, OS release: %s, dir-compare version: %s\n',
  761. new Date(), process.version, os.platform(), os.release(), pjson.version));
  762. }
  763. }
  764.  
  765. var REPORT_FILE = __dirname + "/report.txt";
  766. function report(testName, testDescription, output, exitCode, result, saveReport){
  767. if(saveReport && !result){
  768. fs.appendFileSync(REPORT_FILE, util.format(
  769. "\n%s %s failed - result: %s, exitCode: %s, output: %s\n", testName, testDescription, result,
  770. exitCode?exitCode:'n/a', output?output:'n/a'));
  771. }
  772.  
  773. }
  774.  
  775. function endReport(saveReport){
  776. if(saveReport){
  777. fs.appendFileSync(REPORT_FILE, 'Tests: ' + count + ', failed: ' + failed + ', succeeded: ' + successful);
  778. }
  779. }
  780.  
  781. var runTests = function () {
  782. var args = process.argv;
  783. var saveReport = true;
  784. initReport(saveReport);
  785.  
  786. temp.mkdir('dircompare-test', function (err, testDirPath) {
  787. if (err) {
  788. throw err;
  789. }
  790.  
  791. function onError (err) {
  792. console.error('Error occurred:', err);
  793. }
  794.  
  795. function onExtracted () {
  796. Promise.resolve(tests).then(function(tests){
  797. // Run sync tests
  798. var syncTestsPromises = [];
  799. tests.filter(function(test){return !test.onlyCommandLine;})
  800. // tests.filter(function(test){return test.name==='test009_2';})
  801. .forEach(function(test){
  802. syncTestsPromises.push(testSync(test, testDirPath, saveReport));
  803. });
  804. return Promise.all(syncTestsPromises);
  805. }).then(function(){
  806. console.log();
  807. console.log('Sync tests: ' + syncCount + ', failed: ' + syncFailed.toString().yellow + ', succeeded: ' + syncSuccessful.toString().green);
  808. console.log();
  809. }).then(function(){
  810. // Run async tests
  811. var asyncTestsPromises = [];
  812. tests.filter(function(test){return !test.onlyCommandLine;})
  813. // tests.filter(function(test){return test.name==='test009_2';})
  814. .forEach(function(test){
  815. asyncTestsPromises.push(testAsync(test, testDirPath, saveReport));
  816. });
  817. return Promise.all(asyncTestsPromises);
  818. }).then(function(){
  819. console.log();
  820. console.log('Async tests: ' + asyncCount + ', failed: ' + asyncFailed.toString().yellow + ', succeeded: ' + asyncSuccessful.toString().green);
  821. console.log();
  822. }).then(function(){
  823. // Run command line tests
  824. var commandLinePromises = [];
  825. tests.filter(function(test){return !test.onlyLibrary;})
  826. // tests.filter(function(test){return test.name=='test002_3';})
  827. .forEach(function(test){
  828. commandLinePromises.push(testCommandLine(test, testDirPath, saveReport));
  829. });
  830. return Promise.all(commandLinePromises);
  831. }).then(function(){
  832. console.log();
  833. console.log('Command line tests: ' + cmdLineCount + ', failed: ' + cmdLineFailed.toString().yellow + ', succeeded: ' + cmdLineSuccessful.toString().green);
  834. }).then(function(){
  835. console.log();
  836. console.log('All tests: ' + count + ', failed: ' + failed.toString().yellow + ', succeeded: ' + successful.toString().green);
  837. endReport(saveReport);
  838. process.chdir(pathUtils.dirname(testDirPath));
  839. });
  840. }
  841.  
  842. untar(__dirname + "/testdir.tar", testDirPath, onExtracted, onError);
  843. });
  844. }
  845.  
  846. runTests();
Buy Me A Coffee