asar-spec.js 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. const { expect } = require('chai');
  2. const ChildProcess = require('child_process');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const temp = require('temp').track();
  6. const util = require('util');
  7. const { emittedOnce } = require('./events-helpers');
  8. const { ifit, ifdescribe } = require('./spec-helpers');
  9. const nativeImage = require('electron').nativeImage;
  10. const features = process._linkedBinding('electron_common_features');
  11. async function expectToThrowErrorWithCode (func, code) {
  12. let error;
  13. try {
  14. await func();
  15. } catch (e) {
  16. error = e;
  17. }
  18. expect(error).is.an('Error');
  19. expect(error).to.have.property('code').which.equals(code);
  20. }
  21. describe('asar package', function () {
  22. const fixtures = path.join(__dirname, 'fixtures');
  23. const asarDir = path.join(fixtures, 'test.asar');
  24. describe('node api', function () {
  25. it('supports paths specified as a Buffer', function () {
  26. const file = Buffer.from(path.join(asarDir, 'a.asar', 'file1'));
  27. expect(fs.existsSync(file)).to.be.true();
  28. });
  29. describe('fs.readFileSync', function () {
  30. it('does not leak fd', function () {
  31. let readCalls = 1;
  32. while (readCalls <= 10000) {
  33. fs.readFileSync(path.join(process.resourcesPath, 'default_app.asar', 'main.js'));
  34. readCalls++;
  35. }
  36. });
  37. it('reads a normal file', function () {
  38. const file1 = path.join(asarDir, 'a.asar', 'file1');
  39. expect(fs.readFileSync(file1).toString().trim()).to.equal('file1');
  40. const file2 = path.join(asarDir, 'a.asar', 'file2');
  41. expect(fs.readFileSync(file2).toString().trim()).to.equal('file2');
  42. const file3 = path.join(asarDir, 'a.asar', 'file3');
  43. expect(fs.readFileSync(file3).toString().trim()).to.equal('file3');
  44. });
  45. it('reads from a empty file', function () {
  46. const file = path.join(asarDir, 'empty.asar', 'file1');
  47. const buffer = fs.readFileSync(file);
  48. expect(buffer).to.be.empty();
  49. expect(buffer.toString()).to.equal('');
  50. });
  51. it('reads a linked file', function () {
  52. const p = path.join(asarDir, 'a.asar', 'link1');
  53. expect(fs.readFileSync(p).toString().trim()).to.equal('file1');
  54. });
  55. it('reads a file from linked directory', function () {
  56. const p1 = path.join(asarDir, 'a.asar', 'link2', 'file1');
  57. expect(fs.readFileSync(p1).toString().trim()).to.equal('file1');
  58. const p2 = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1');
  59. expect(fs.readFileSync(p2).toString().trim()).to.equal('file1');
  60. });
  61. it('throws ENOENT error when can not find file', function () {
  62. const p = path.join(asarDir, 'a.asar', 'not-exist');
  63. expect(() => {
  64. fs.readFileSync(p);
  65. }).to.throw(/ENOENT/);
  66. });
  67. it('passes ENOENT error to callback when can not find file', function () {
  68. const p = path.join(asarDir, 'a.asar', 'not-exist');
  69. let async = false;
  70. fs.readFile(p, function (error) {
  71. expect(async).to.be.true();
  72. expect(error).to.match(/ENOENT/);
  73. });
  74. async = true;
  75. });
  76. it('reads a normal file with unpacked files', function () {
  77. const p = path.join(asarDir, 'unpack.asar', 'a.txt');
  78. expect(fs.readFileSync(p).toString().trim()).to.equal('a');
  79. });
  80. it('reads a file in filesystem', function () {
  81. const p = path.resolve(asarDir, 'file');
  82. expect(fs.readFileSync(p).toString().trim()).to.equal('file');
  83. });
  84. });
  85. describe('fs.readFile', function () {
  86. it('reads a normal file', function (done) {
  87. const p = path.join(asarDir, 'a.asar', 'file1');
  88. fs.readFile(p, function (err, content) {
  89. try {
  90. expect(err).to.be.null();
  91. expect(String(content).trim()).to.equal('file1');
  92. done();
  93. } catch (e) {
  94. done(e);
  95. }
  96. });
  97. });
  98. it('reads from a empty file', function (done) {
  99. const p = path.join(asarDir, 'empty.asar', 'file1');
  100. fs.readFile(p, function (err, content) {
  101. try {
  102. expect(err).to.be.null();
  103. expect(String(content)).to.equal('');
  104. done();
  105. } catch (e) {
  106. done(e);
  107. }
  108. });
  109. });
  110. it('reads from a empty file with encoding', function (done) {
  111. const p = path.join(asarDir, 'empty.asar', 'file1');
  112. fs.readFile(p, 'utf8', function (err, content) {
  113. try {
  114. expect(err).to.be.null();
  115. expect(content).to.equal('');
  116. done();
  117. } catch (e) {
  118. done(e);
  119. }
  120. });
  121. });
  122. it('reads a linked file', function (done) {
  123. const p = path.join(asarDir, 'a.asar', 'link1');
  124. fs.readFile(p, function (err, content) {
  125. try {
  126. expect(err).to.be.null();
  127. expect(String(content).trim()).to.equal('file1');
  128. done();
  129. } catch (e) {
  130. done(e);
  131. }
  132. });
  133. });
  134. it('reads a file from linked directory', function (done) {
  135. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1');
  136. fs.readFile(p, function (err, content) {
  137. try {
  138. expect(err).to.be.null();
  139. expect(String(content).trim()).to.equal('file1');
  140. done();
  141. } catch (e) {
  142. done(e);
  143. }
  144. });
  145. });
  146. it('throws ENOENT error when can not find file', function (done) {
  147. const p = path.join(asarDir, 'a.asar', 'not-exist');
  148. fs.readFile(p, function (err) {
  149. try {
  150. expect(err.code).to.equal('ENOENT');
  151. done();
  152. } catch (e) {
  153. done(e);
  154. }
  155. });
  156. });
  157. });
  158. describe('fs.promises.readFile', function () {
  159. it('reads a normal file', async function () {
  160. const p = path.join(asarDir, 'a.asar', 'file1');
  161. const content = await fs.promises.readFile(p);
  162. expect(String(content).trim()).to.equal('file1');
  163. });
  164. it('reads from a empty file', async function () {
  165. const p = path.join(asarDir, 'empty.asar', 'file1');
  166. const content = await fs.promises.readFile(p);
  167. expect(String(content)).to.equal('');
  168. });
  169. it('reads from a empty file with encoding', async function () {
  170. const p = path.join(asarDir, 'empty.asar', 'file1');
  171. const content = await fs.promises.readFile(p, 'utf8');
  172. expect(content).to.equal('');
  173. });
  174. it('reads a linked file', async function () {
  175. const p = path.join(asarDir, 'a.asar', 'link1');
  176. const content = await fs.promises.readFile(p);
  177. expect(String(content).trim()).to.equal('file1');
  178. });
  179. it('reads a file from linked directory', async function () {
  180. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1');
  181. const content = await fs.promises.readFile(p);
  182. expect(String(content).trim()).to.equal('file1');
  183. });
  184. it('throws ENOENT error when can not find file', async function () {
  185. const p = path.join(asarDir, 'a.asar', 'not-exist');
  186. await expectToThrowErrorWithCode(() => fs.promises.readFile(p), 'ENOENT');
  187. });
  188. });
  189. describe('fs.copyFile', function () {
  190. it('copies a normal file', function (done) {
  191. const p = path.join(asarDir, 'a.asar', 'file1');
  192. const dest = temp.path();
  193. fs.copyFile(p, dest, function (err) {
  194. try {
  195. expect(err).to.be.null();
  196. expect(fs.readFileSync(p).equals(fs.readFileSync(dest))).to.be.true();
  197. done();
  198. } catch (e) {
  199. done(e);
  200. }
  201. });
  202. });
  203. it('copies a unpacked file', function (done) {
  204. const p = path.join(asarDir, 'unpack.asar', 'a.txt');
  205. const dest = temp.path();
  206. fs.copyFile(p, dest, function (err) {
  207. try {
  208. expect(err).to.be.null();
  209. expect(fs.readFileSync(p).equals(fs.readFileSync(dest))).to.be.true();
  210. done();
  211. } catch (e) {
  212. done(e);
  213. }
  214. });
  215. });
  216. });
  217. describe('fs.promises.copyFile', function () {
  218. it('copies a normal file', async function () {
  219. const p = path.join(asarDir, 'a.asar', 'file1');
  220. const dest = temp.path();
  221. await fs.promises.copyFile(p, dest);
  222. expect(fs.readFileSync(p).equals(fs.readFileSync(dest))).to.be.true();
  223. });
  224. it('copies a unpacked file', async function () {
  225. const p = path.join(asarDir, 'unpack.asar', 'a.txt');
  226. const dest = temp.path();
  227. await fs.promises.copyFile(p, dest);
  228. expect(fs.readFileSync(p).equals(fs.readFileSync(dest))).to.be.true();
  229. });
  230. });
  231. describe('fs.copyFileSync', function () {
  232. it('copies a normal file', function () {
  233. const p = path.join(asarDir, 'a.asar', 'file1');
  234. const dest = temp.path();
  235. fs.copyFileSync(p, dest);
  236. expect(fs.readFileSync(p).equals(fs.readFileSync(dest))).to.be.true();
  237. });
  238. it('copies a unpacked file', function () {
  239. const p = path.join(asarDir, 'unpack.asar', 'a.txt');
  240. const dest = temp.path();
  241. fs.copyFileSync(p, dest);
  242. expect(fs.readFileSync(p).equals(fs.readFileSync(dest))).to.be.true();
  243. });
  244. });
  245. describe('fs.lstatSync', function () {
  246. it('handles path with trailing slash correctly', function () {
  247. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1');
  248. fs.lstatSync(p);
  249. fs.lstatSync(p + '/');
  250. });
  251. it('returns information of root', function () {
  252. const p = path.join(asarDir, 'a.asar');
  253. const stats = fs.lstatSync(p);
  254. expect(stats.isFile()).to.be.false();
  255. expect(stats.isDirectory()).to.be.true();
  256. expect(stats.isSymbolicLink()).to.be.false();
  257. expect(stats.size).to.equal(0);
  258. });
  259. it('returns information of root with stats as bigint', function () {
  260. const p = path.join(asarDir, 'a.asar');
  261. const stats = fs.lstatSync(p, { bigint: false });
  262. expect(stats.isFile()).to.be.false();
  263. expect(stats.isDirectory()).to.be.true();
  264. expect(stats.isSymbolicLink()).to.be.false();
  265. expect(stats.size).to.equal(0);
  266. });
  267. it('returns information of a normal file', function () {
  268. const ref2 = ['file1', 'file2', 'file3', path.join('dir1', 'file1'), path.join('link2', 'file1')];
  269. for (let j = 0, len = ref2.length; j < len; j++) {
  270. const file = ref2[j];
  271. const p = path.join(asarDir, 'a.asar', file);
  272. const stats = fs.lstatSync(p);
  273. expect(stats.isFile()).to.be.true();
  274. expect(stats.isDirectory()).to.be.false();
  275. expect(stats.isSymbolicLink()).to.be.false();
  276. expect(stats.size).to.equal(6);
  277. }
  278. });
  279. it('returns information of a normal directory', function () {
  280. const ref2 = ['dir1', 'dir2', 'dir3'];
  281. for (let j = 0, len = ref2.length; j < len; j++) {
  282. const file = ref2[j];
  283. const p = path.join(asarDir, 'a.asar', file);
  284. const stats = fs.lstatSync(p);
  285. expect(stats.isFile()).to.be.false();
  286. expect(stats.isDirectory()).to.be.true();
  287. expect(stats.isSymbolicLink()).to.be.false();
  288. expect(stats.size).to.equal(0);
  289. }
  290. });
  291. it('returns information of a linked file', function () {
  292. const ref2 = ['link1', path.join('dir1', 'link1'), path.join('link2', 'link2')];
  293. for (let j = 0, len = ref2.length; j < len; j++) {
  294. const file = ref2[j];
  295. const p = path.join(asarDir, 'a.asar', file);
  296. const stats = fs.lstatSync(p);
  297. expect(stats.isFile()).to.be.false();
  298. expect(stats.isDirectory()).to.be.false();
  299. expect(stats.isSymbolicLink()).to.be.true();
  300. expect(stats.size).to.equal(0);
  301. }
  302. });
  303. it('returns information of a linked directory', function () {
  304. const ref2 = ['link2', path.join('dir1', 'link2'), path.join('link2', 'link2')];
  305. for (let j = 0, len = ref2.length; j < len; j++) {
  306. const file = ref2[j];
  307. const p = path.join(asarDir, 'a.asar', file);
  308. const stats = fs.lstatSync(p);
  309. expect(stats.isFile()).to.be.false();
  310. expect(stats.isDirectory()).to.be.false();
  311. expect(stats.isSymbolicLink()).to.be.true();
  312. expect(stats.size).to.equal(0);
  313. }
  314. });
  315. it('throws ENOENT error when can not find file', function () {
  316. const ref2 = ['file4', 'file5', path.join('dir1', 'file4')];
  317. for (let j = 0, len = ref2.length; j < len; j++) {
  318. const file = ref2[j];
  319. const p = path.join(asarDir, 'a.asar', file);
  320. expect(() => {
  321. fs.lstatSync(p);
  322. }).to.throw(/ENOENT/);
  323. }
  324. });
  325. });
  326. describe('fs.lstat', function () {
  327. it('handles path with trailing slash correctly', function (done) {
  328. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1');
  329. fs.lstat(p + '/', done);
  330. });
  331. it('returns information of root', function (done) {
  332. const p = path.join(asarDir, 'a.asar');
  333. fs.lstat(p, function (err, stats) {
  334. try {
  335. expect(err).to.be.null();
  336. expect(stats.isFile()).to.be.false();
  337. expect(stats.isDirectory()).to.be.true();
  338. expect(stats.isSymbolicLink()).to.be.false();
  339. expect(stats.size).to.equal(0);
  340. done();
  341. } catch (e) {
  342. done(e);
  343. }
  344. });
  345. });
  346. it('returns information of root with stats as bigint', function (done) {
  347. const p = path.join(asarDir, 'a.asar');
  348. fs.lstat(p, { bigint: false }, function (err, stats) {
  349. try {
  350. expect(err).to.be.null();
  351. expect(stats.isFile()).to.be.false();
  352. expect(stats.isDirectory()).to.be.true();
  353. expect(stats.isSymbolicLink()).to.be.false();
  354. expect(stats.size).to.equal(0);
  355. done();
  356. } catch (e) {
  357. done(e);
  358. }
  359. });
  360. });
  361. it('returns information of a normal file', function (done) {
  362. const p = path.join(asarDir, 'a.asar', 'link2', 'file1');
  363. fs.lstat(p, function (err, stats) {
  364. try {
  365. expect(err).to.be.null();
  366. expect(stats.isFile()).to.be.true();
  367. expect(stats.isDirectory()).to.be.false();
  368. expect(stats.isSymbolicLink()).to.be.false();
  369. expect(stats.size).to.equal(6);
  370. done();
  371. } catch (e) {
  372. done(e);
  373. }
  374. });
  375. });
  376. it('returns information of a normal directory', function (done) {
  377. const p = path.join(asarDir, 'a.asar', 'dir1');
  378. fs.lstat(p, function (err, stats) {
  379. try {
  380. expect(err).to.be.null();
  381. expect(stats.isFile()).to.be.false();
  382. expect(stats.isDirectory()).to.be.true();
  383. expect(stats.isSymbolicLink()).to.be.false();
  384. expect(stats.size).to.equal(0);
  385. done();
  386. } catch (e) {
  387. done(e);
  388. }
  389. });
  390. });
  391. it('returns information of a linked file', function (done) {
  392. const p = path.join(asarDir, 'a.asar', 'link2', 'link1');
  393. fs.lstat(p, function (err, stats) {
  394. try {
  395. expect(err).to.be.null();
  396. expect(stats.isFile()).to.be.false();
  397. expect(stats.isDirectory()).to.be.false();
  398. expect(stats.isSymbolicLink()).to.be.true();
  399. expect(stats.size).to.equal(0);
  400. done();
  401. } catch (e) {
  402. done(e);
  403. }
  404. });
  405. });
  406. it('returns information of a linked directory', function (done) {
  407. const p = path.join(asarDir, 'a.asar', 'link2', 'link2');
  408. fs.lstat(p, function (err, stats) {
  409. try {
  410. expect(err).to.be.null();
  411. expect(stats.isFile()).to.be.false();
  412. expect(stats.isDirectory()).to.be.false();
  413. expect(stats.isSymbolicLink()).to.be.true();
  414. expect(stats.size).to.equal(0);
  415. done();
  416. } catch (e) {
  417. done(e);
  418. }
  419. });
  420. });
  421. it('throws ENOENT error when can not find file', function (done) {
  422. const p = path.join(asarDir, 'a.asar', 'file4');
  423. fs.lstat(p, function (err) {
  424. try {
  425. expect(err.code).to.equal('ENOENT');
  426. done();
  427. } catch (e) {
  428. done(e);
  429. }
  430. });
  431. });
  432. });
  433. describe('fs.promises.lstat', function () {
  434. it('handles path with trailing slash correctly', async function () {
  435. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1');
  436. await fs.promises.lstat(p + '/');
  437. });
  438. it('returns information of root', async function () {
  439. const p = path.join(asarDir, 'a.asar');
  440. const stats = await fs.promises.lstat(p);
  441. expect(stats.isFile()).to.be.false();
  442. expect(stats.isDirectory()).to.be.true();
  443. expect(stats.isSymbolicLink()).to.be.false();
  444. expect(stats.size).to.equal(0);
  445. });
  446. it('returns information of root with stats as bigint', async function () {
  447. const p = path.join(asarDir, 'a.asar');
  448. const stats = await fs.promises.lstat(p, { bigint: false });
  449. expect(stats.isFile()).to.be.false();
  450. expect(stats.isDirectory()).to.be.true();
  451. expect(stats.isSymbolicLink()).to.be.false();
  452. expect(stats.size).to.equal(0);
  453. });
  454. it('returns information of a normal file', async function () {
  455. const p = path.join(asarDir, 'a.asar', 'link2', 'file1');
  456. const stats = await fs.promises.lstat(p);
  457. expect(stats.isFile()).to.be.true();
  458. expect(stats.isDirectory()).to.be.false();
  459. expect(stats.isSymbolicLink()).to.be.false();
  460. expect(stats.size).to.equal(6);
  461. });
  462. it('returns information of a normal directory', async function () {
  463. const p = path.join(asarDir, 'a.asar', 'dir1');
  464. const stats = await fs.promises.lstat(p);
  465. expect(stats.isFile()).to.be.false();
  466. expect(stats.isDirectory()).to.be.true();
  467. expect(stats.isSymbolicLink()).to.be.false();
  468. expect(stats.size).to.equal(0);
  469. });
  470. it('returns information of a linked file', async function () {
  471. const p = path.join(asarDir, 'a.asar', 'link2', 'link1');
  472. const stats = await fs.promises.lstat(p);
  473. expect(stats.isFile()).to.be.false();
  474. expect(stats.isDirectory()).to.be.false();
  475. expect(stats.isSymbolicLink()).to.be.true();
  476. expect(stats.size).to.equal(0);
  477. });
  478. it('returns information of a linked directory', async function () {
  479. const p = path.join(asarDir, 'a.asar', 'link2', 'link2');
  480. const stats = await fs.promises.lstat(p);
  481. expect(stats.isFile()).to.be.false();
  482. expect(stats.isDirectory()).to.be.false();
  483. expect(stats.isSymbolicLink()).to.be.true();
  484. expect(stats.size).to.equal(0);
  485. });
  486. it('throws ENOENT error when can not find file', async function () {
  487. const p = path.join(asarDir, 'a.asar', 'file4');
  488. await expectToThrowErrorWithCode(() => fs.promises.lstat(p), 'ENOENT');
  489. });
  490. });
  491. describe('fs.realpathSync', () => {
  492. it('returns real path root', () => {
  493. const parent = fs.realpathSync(asarDir);
  494. const p = 'a.asar';
  495. const r = fs.realpathSync(path.join(parent, p));
  496. expect(r).to.equal(path.join(parent, p));
  497. });
  498. it('returns real path of a normal file', () => {
  499. const parent = fs.realpathSync(asarDir);
  500. const p = path.join('a.asar', 'file1');
  501. const r = fs.realpathSync(path.join(parent, p));
  502. expect(r).to.equal(path.join(parent, p));
  503. });
  504. it('returns real path of a normal directory', () => {
  505. const parent = fs.realpathSync(asarDir);
  506. const p = path.join('a.asar', 'dir1');
  507. const r = fs.realpathSync(path.join(parent, p));
  508. expect(r).to.equal(path.join(parent, p));
  509. });
  510. it('returns real path of a linked file', () => {
  511. const parent = fs.realpathSync(asarDir);
  512. const p = path.join('a.asar', 'link2', 'link1');
  513. const r = fs.realpathSync(path.join(parent, p));
  514. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'));
  515. });
  516. it('returns real path of a linked directory', () => {
  517. const parent = fs.realpathSync(asarDir);
  518. const p = path.join('a.asar', 'link2', 'link2');
  519. const r = fs.realpathSync(path.join(parent, p));
  520. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'));
  521. });
  522. it('returns real path of an unpacked file', () => {
  523. const parent = fs.realpathSync(asarDir);
  524. const p = path.join('unpack.asar', 'a.txt');
  525. const r = fs.realpathSync(path.join(parent, p));
  526. expect(r).to.equal(path.join(parent, p));
  527. });
  528. it('throws ENOENT error when can not find file', () => {
  529. const parent = fs.realpathSync(asarDir);
  530. const p = path.join('a.asar', 'not-exist');
  531. expect(() => {
  532. fs.realpathSync(path.join(parent, p));
  533. }).to.throw(/ENOENT/);
  534. });
  535. });
  536. describe('fs.realpathSync.native', () => {
  537. it('returns real path root', () => {
  538. const parent = fs.realpathSync.native(asarDir);
  539. const p = 'a.asar';
  540. const r = fs.realpathSync.native(path.join(parent, p));
  541. expect(r).to.equal(path.join(parent, p));
  542. });
  543. it('returns real path of a normal file', () => {
  544. const parent = fs.realpathSync.native(asarDir);
  545. const p = path.join('a.asar', 'file1');
  546. const r = fs.realpathSync.native(path.join(parent, p));
  547. expect(r).to.equal(path.join(parent, p));
  548. });
  549. it('returns real path of a normal directory', () => {
  550. const parent = fs.realpathSync.native(asarDir);
  551. const p = path.join('a.asar', 'dir1');
  552. const r = fs.realpathSync.native(path.join(parent, p));
  553. expect(r).to.equal(path.join(parent, p));
  554. });
  555. it('returns real path of a linked file', () => {
  556. const parent = fs.realpathSync.native(asarDir);
  557. const p = path.join('a.asar', 'link2', 'link1');
  558. const r = fs.realpathSync.native(path.join(parent, p));
  559. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'));
  560. });
  561. it('returns real path of a linked directory', () => {
  562. const parent = fs.realpathSync.native(asarDir);
  563. const p = path.join('a.asar', 'link2', 'link2');
  564. const r = fs.realpathSync.native(path.join(parent, p));
  565. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'));
  566. });
  567. it('returns real path of an unpacked file', () => {
  568. const parent = fs.realpathSync.native(asarDir);
  569. const p = path.join('unpack.asar', 'a.txt');
  570. const r = fs.realpathSync.native(path.join(parent, p));
  571. expect(r).to.equal(path.join(parent, p));
  572. });
  573. it('throws ENOENT error when can not find file', () => {
  574. const parent = fs.realpathSync.native(asarDir);
  575. const p = path.join('a.asar', 'not-exist');
  576. expect(() => {
  577. fs.realpathSync.native(path.join(parent, p));
  578. }).to.throw(/ENOENT/);
  579. });
  580. });
  581. describe('fs.realpath', () => {
  582. it('returns real path root', done => {
  583. const parent = fs.realpathSync(asarDir);
  584. const p = 'a.asar';
  585. fs.realpath(path.join(parent, p), (err, r) => {
  586. try {
  587. expect(err).to.be.null();
  588. expect(r).to.equal(path.join(parent, p));
  589. done();
  590. } catch (e) {
  591. done(e);
  592. }
  593. });
  594. });
  595. it('returns real path of a normal file', done => {
  596. const parent = fs.realpathSync(asarDir);
  597. const p = path.join('a.asar', 'file1');
  598. fs.realpath(path.join(parent, p), (err, r) => {
  599. try {
  600. expect(err).to.be.null();
  601. expect(r).to.equal(path.join(parent, p));
  602. done();
  603. } catch (e) {
  604. done(e);
  605. }
  606. });
  607. });
  608. it('returns real path of a normal directory', done => {
  609. const parent = fs.realpathSync(asarDir);
  610. const p = path.join('a.asar', 'dir1');
  611. fs.realpath(path.join(parent, p), (err, r) => {
  612. try {
  613. expect(err).to.be.null();
  614. expect(r).to.equal(path.join(parent, p));
  615. done();
  616. } catch (e) {
  617. done(e);
  618. }
  619. });
  620. });
  621. it('returns real path of a linked file', done => {
  622. const parent = fs.realpathSync(asarDir);
  623. const p = path.join('a.asar', 'link2', 'link1');
  624. fs.realpath(path.join(parent, p), (err, r) => {
  625. try {
  626. expect(err).to.be.null();
  627. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'));
  628. done();
  629. } catch (e) {
  630. done(e);
  631. }
  632. });
  633. });
  634. it('returns real path of a linked directory', done => {
  635. const parent = fs.realpathSync(asarDir);
  636. const p = path.join('a.asar', 'link2', 'link2');
  637. fs.realpath(path.join(parent, p), (err, r) => {
  638. try {
  639. expect(err).to.be.null();
  640. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'));
  641. done();
  642. } catch (e) {
  643. done(e);
  644. }
  645. });
  646. });
  647. it('returns real path of an unpacked file', done => {
  648. const parent = fs.realpathSync(asarDir);
  649. const p = path.join('unpack.asar', 'a.txt');
  650. fs.realpath(path.join(parent, p), (err, r) => {
  651. try {
  652. expect(err).to.be.null();
  653. expect(r).to.equal(path.join(parent, p));
  654. done();
  655. } catch (e) {
  656. done(e);
  657. }
  658. });
  659. });
  660. it('throws ENOENT error when can not find file', done => {
  661. const parent = fs.realpathSync(asarDir);
  662. const p = path.join('a.asar', 'not-exist');
  663. fs.realpath(path.join(parent, p), err => {
  664. try {
  665. expect(err.code).to.equal('ENOENT');
  666. done();
  667. } catch (e) {
  668. done(e);
  669. }
  670. });
  671. });
  672. });
  673. describe('fs.promises.realpath', () => {
  674. it('returns real path root', async () => {
  675. const parent = fs.realpathSync(asarDir);
  676. const p = 'a.asar';
  677. const r = await fs.promises.realpath(path.join(parent, p));
  678. expect(r).to.equal(path.join(parent, p));
  679. });
  680. it('returns real path of a normal file', async () => {
  681. const parent = fs.realpathSync(asarDir);
  682. const p = path.join('a.asar', 'file1');
  683. const r = await fs.promises.realpath(path.join(parent, p));
  684. expect(r).to.equal(path.join(parent, p));
  685. });
  686. it('returns real path of a normal directory', async () => {
  687. const parent = fs.realpathSync(asarDir);
  688. const p = path.join('a.asar', 'dir1');
  689. const r = await fs.promises.realpath(path.join(parent, p));
  690. expect(r).to.equal(path.join(parent, p));
  691. });
  692. it('returns real path of a linked file', async () => {
  693. const parent = fs.realpathSync(asarDir);
  694. const p = path.join('a.asar', 'link2', 'link1');
  695. const r = await fs.promises.realpath(path.join(parent, p));
  696. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'));
  697. });
  698. it('returns real path of a linked directory', async () => {
  699. const parent = fs.realpathSync(asarDir);
  700. const p = path.join('a.asar', 'link2', 'link2');
  701. const r = await fs.promises.realpath(path.join(parent, p));
  702. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'));
  703. });
  704. it('returns real path of an unpacked file', async () => {
  705. const parent = fs.realpathSync(asarDir);
  706. const p = path.join('unpack.asar', 'a.txt');
  707. const r = await fs.promises.realpath(path.join(parent, p));
  708. expect(r).to.equal(path.join(parent, p));
  709. });
  710. it('throws ENOENT error when can not find file', async () => {
  711. const parent = fs.realpathSync(asarDir);
  712. const p = path.join('a.asar', 'not-exist');
  713. await expectToThrowErrorWithCode(() => fs.promises.realpath(path.join(parent, p)), 'ENOENT');
  714. });
  715. });
  716. describe('fs.realpath.native', () => {
  717. it('returns real path root', done => {
  718. const parent = fs.realpathSync.native(asarDir);
  719. const p = 'a.asar';
  720. fs.realpath.native(path.join(parent, p), (err, r) => {
  721. try {
  722. expect(err).to.be.null();
  723. expect(r).to.equal(path.join(parent, p));
  724. done();
  725. } catch (e) {
  726. done(e);
  727. }
  728. });
  729. });
  730. it('returns real path of a normal file', done => {
  731. const parent = fs.realpathSync.native(asarDir);
  732. const p = path.join('a.asar', 'file1');
  733. fs.realpath.native(path.join(parent, p), (err, r) => {
  734. try {
  735. expect(err).to.be.null();
  736. expect(r).to.equal(path.join(parent, p));
  737. done();
  738. } catch (e) {
  739. done(e);
  740. }
  741. });
  742. });
  743. it('returns real path of a normal directory', done => {
  744. const parent = fs.realpathSync.native(asarDir);
  745. const p = path.join('a.asar', 'dir1');
  746. fs.realpath.native(path.join(parent, p), (err, r) => {
  747. try {
  748. expect(err).to.be.null();
  749. expect(r).to.equal(path.join(parent, p));
  750. done();
  751. } catch (e) {
  752. done(e);
  753. }
  754. });
  755. });
  756. it('returns real path of a linked file', done => {
  757. const parent = fs.realpathSync.native(asarDir);
  758. const p = path.join('a.asar', 'link2', 'link1');
  759. fs.realpath.native(path.join(parent, p), (err, r) => {
  760. try {
  761. expect(err).to.be.null();
  762. expect(r).to.equal(path.join(parent, 'a.asar', 'file1'));
  763. done();
  764. } catch (e) {
  765. done(e);
  766. }
  767. });
  768. });
  769. it('returns real path of a linked directory', done => {
  770. const parent = fs.realpathSync.native(asarDir);
  771. const p = path.join('a.asar', 'link2', 'link2');
  772. fs.realpath.native(path.join(parent, p), (err, r) => {
  773. try {
  774. expect(err).to.be.null();
  775. expect(r).to.equal(path.join(parent, 'a.asar', 'dir1'));
  776. done();
  777. } catch (e) {
  778. done(e);
  779. }
  780. });
  781. });
  782. it('returns real path of an unpacked file', done => {
  783. const parent = fs.realpathSync.native(asarDir);
  784. const p = path.join('unpack.asar', 'a.txt');
  785. fs.realpath.native(path.join(parent, p), (err, r) => {
  786. try {
  787. expect(err).to.be.null();
  788. expect(r).to.equal(path.join(parent, p));
  789. done();
  790. } catch (e) {
  791. done(e);
  792. }
  793. });
  794. });
  795. it('throws ENOENT error when can not find file', done => {
  796. const parent = fs.realpathSync.native(asarDir);
  797. const p = path.join('a.asar', 'not-exist');
  798. fs.realpath.native(path.join(parent, p), err => {
  799. try {
  800. expect(err.code).to.equal('ENOENT');
  801. done();
  802. } catch (e) {
  803. done(e);
  804. }
  805. });
  806. });
  807. });
  808. describe('fs.readdirSync', function () {
  809. it('reads dirs from root', function () {
  810. const p = path.join(asarDir, 'a.asar');
  811. const dirs = fs.readdirSync(p);
  812. expect(dirs).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
  813. });
  814. it('reads dirs from a normal dir', function () {
  815. const p = path.join(asarDir, 'a.asar', 'dir1');
  816. const dirs = fs.readdirSync(p);
  817. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  818. });
  819. it('supports withFileTypes', function () {
  820. const p = path.join(asarDir, 'a.asar');
  821. const dirs = fs.readdirSync(p, { withFileTypes: true });
  822. for (const dir of dirs) {
  823. expect(dir instanceof fs.Dirent).to.be.true();
  824. }
  825. const names = dirs.map(a => a.name);
  826. expect(names).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
  827. });
  828. it('supports withFileTypes for a deep directory', function () {
  829. const p = path.join(asarDir, 'a.asar', 'dir3');
  830. const dirs = fs.readdirSync(p, { withFileTypes: true });
  831. for (const dir of dirs) {
  832. expect(dir instanceof fs.Dirent).to.be.true();
  833. }
  834. const names = dirs.map(a => a.name);
  835. expect(names).to.deep.equal(['file1', 'file2', 'file3']);
  836. });
  837. it('reads dirs from a linked dir', function () {
  838. const p = path.join(asarDir, 'a.asar', 'link2', 'link2');
  839. const dirs = fs.readdirSync(p);
  840. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  841. });
  842. it('throws ENOENT error when can not find file', function () {
  843. const p = path.join(asarDir, 'a.asar', 'not-exist');
  844. expect(() => {
  845. fs.readdirSync(p);
  846. }).to.throw(/ENOENT/);
  847. });
  848. });
  849. describe('fs.readdir', function () {
  850. it('reads dirs from root', function (done) {
  851. const p = path.join(asarDir, 'a.asar');
  852. fs.readdir(p, function (err, dirs) {
  853. try {
  854. expect(err).to.be.null();
  855. expect(dirs).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
  856. done();
  857. } catch (e) {
  858. done(e);
  859. }
  860. });
  861. });
  862. it('supports withFileTypes', function (done) {
  863. const p = path.join(asarDir, 'a.asar');
  864. fs.readdir(p, { withFileTypes: true }, (err, dirs) => {
  865. try {
  866. expect(err).to.be.null();
  867. for (const dir of dirs) {
  868. expect(dir instanceof fs.Dirent).to.be.true();
  869. }
  870. const names = dirs.map(a => a.name);
  871. expect(names).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
  872. done();
  873. } catch (e) {
  874. done(e);
  875. }
  876. });
  877. });
  878. it('reads dirs from a normal dir', function (done) {
  879. const p = path.join(asarDir, 'a.asar', 'dir1');
  880. fs.readdir(p, function (err, dirs) {
  881. try {
  882. expect(err).to.be.null();
  883. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  884. done();
  885. } catch (e) {
  886. done(e);
  887. }
  888. });
  889. });
  890. it('reads dirs from a linked dir', function (done) {
  891. const p = path.join(asarDir, 'a.asar', 'link2', 'link2');
  892. fs.readdir(p, function (err, dirs) {
  893. try {
  894. expect(err).to.be.null();
  895. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  896. done();
  897. } catch (e) {
  898. done(e);
  899. }
  900. });
  901. });
  902. it('throws ENOENT error when can not find file', function (done) {
  903. const p = path.join(asarDir, 'a.asar', 'not-exist');
  904. fs.readdir(p, function (err) {
  905. try {
  906. expect(err.code).to.equal('ENOENT');
  907. done();
  908. } catch (e) {
  909. done(e);
  910. }
  911. });
  912. });
  913. });
  914. describe('fs.promises.readdir', function () {
  915. it('reads dirs from root', async function () {
  916. const p = path.join(asarDir, 'a.asar');
  917. const dirs = await fs.promises.readdir(p);
  918. expect(dirs).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
  919. });
  920. it('supports withFileTypes', async function () {
  921. const p = path.join(asarDir, 'a.asar');
  922. const dirs = await fs.promises.readdir(p, { withFileTypes: true });
  923. for (const dir of dirs) {
  924. expect(dir instanceof fs.Dirent).to.be.true();
  925. }
  926. const names = dirs.map(a => a.name);
  927. expect(names).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
  928. });
  929. it('reads dirs from a normal dir', async function () {
  930. const p = path.join(asarDir, 'a.asar', 'dir1');
  931. const dirs = await fs.promises.readdir(p);
  932. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  933. });
  934. it('reads dirs from a linked dir', async function () {
  935. const p = path.join(asarDir, 'a.asar', 'link2', 'link2');
  936. const dirs = await fs.promises.readdir(p);
  937. expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
  938. });
  939. it('throws ENOENT error when can not find file', async function () {
  940. const p = path.join(asarDir, 'a.asar', 'not-exist');
  941. await expectToThrowErrorWithCode(() => fs.promises.readdir(p), 'ENOENT');
  942. });
  943. });
  944. describe('fs.openSync', function () {
  945. it('opens a normal/linked/under-linked-directory file', function () {
  946. const ref2 = ['file1', 'link1', path.join('link2', 'file1')];
  947. for (let j = 0, len = ref2.length; j < len; j++) {
  948. const file = ref2[j];
  949. const p = path.join(asarDir, 'a.asar', file);
  950. const fd = fs.openSync(p, 'r');
  951. const buffer = Buffer.alloc(6);
  952. fs.readSync(fd, buffer, 0, 6, 0);
  953. expect(String(buffer).trim()).to.equal('file1');
  954. fs.closeSync(fd);
  955. }
  956. });
  957. it('throws ENOENT error when can not find file', function () {
  958. const p = path.join(asarDir, 'a.asar', 'not-exist');
  959. expect(() => {
  960. fs.openSync(p);
  961. }).to.throw(/ENOENT/);
  962. });
  963. });
  964. describe('fs.open', function () {
  965. it('opens a normal file', function (done) {
  966. const p = path.join(asarDir, 'a.asar', 'file1');
  967. fs.open(p, 'r', function (err, fd) {
  968. expect(err).to.be.null();
  969. const buffer = Buffer.alloc(6);
  970. fs.read(fd, buffer, 0, 6, 0, function (err) {
  971. expect(err).to.be.null();
  972. expect(String(buffer).trim()).to.equal('file1');
  973. fs.close(fd, done);
  974. });
  975. });
  976. });
  977. it('throws ENOENT error when can not find file', function (done) {
  978. const p = path.join(asarDir, 'a.asar', 'not-exist');
  979. fs.open(p, 'r', function (err) {
  980. try {
  981. expect(err.code).to.equal('ENOENT');
  982. done();
  983. } catch (e) {
  984. done(e);
  985. }
  986. });
  987. });
  988. });
  989. describe('fs.promises.open', function () {
  990. it('opens a normal file', async function () {
  991. const p = path.join(asarDir, 'a.asar', 'file1');
  992. const fh = await fs.promises.open(p, 'r');
  993. const buffer = Buffer.alloc(6);
  994. await fh.read(buffer, 0, 6, 0);
  995. expect(String(buffer).trim()).to.equal('file1');
  996. await fh.close();
  997. });
  998. it('throws ENOENT error when can not find file', async function () {
  999. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1000. await expectToThrowErrorWithCode(() => fs.promises.open(p, 'r'), 'ENOENT');
  1001. });
  1002. });
  1003. describe('fs.mkdir', function () {
  1004. it('throws error when calling inside asar archive', function (done) {
  1005. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1006. fs.mkdir(p, function (err) {
  1007. try {
  1008. expect(err.code).to.equal('ENOTDIR');
  1009. done();
  1010. } catch (e) {
  1011. done(e);
  1012. }
  1013. });
  1014. });
  1015. });
  1016. describe('fs.promises.mkdir', function () {
  1017. it('throws error when calling inside asar archive', async function () {
  1018. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1019. await expectToThrowErrorWithCode(() => fs.promises.mkdir(p), 'ENOTDIR');
  1020. });
  1021. });
  1022. describe('fs.mkdirSync', function () {
  1023. it('throws error when calling inside asar archive', function () {
  1024. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1025. expect(() => {
  1026. fs.mkdirSync(p);
  1027. }).to.throw(/ENOTDIR/);
  1028. });
  1029. });
  1030. describe('fs.exists', function () {
  1031. it('handles an existing file', function (done) {
  1032. const p = path.join(asarDir, 'a.asar', 'file1');
  1033. // eslint-disable-next-line
  1034. fs.exists(p, function (exists) {
  1035. try {
  1036. expect(exists).to.be.true();
  1037. done();
  1038. } catch (e) {
  1039. done(e);
  1040. }
  1041. });
  1042. });
  1043. it('handles a non-existent file', function (done) {
  1044. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1045. // eslint-disable-next-line
  1046. fs.exists(p, function (exists) {
  1047. try {
  1048. expect(exists).to.be.false();
  1049. done();
  1050. } catch (e) {
  1051. done(e);
  1052. }
  1053. });
  1054. });
  1055. it('promisified version handles an existing file', (done) => {
  1056. const p = path.join(asarDir, 'a.asar', 'file1');
  1057. // eslint-disable-next-line
  1058. util.promisify(fs.exists)(p).then(exists => {
  1059. try {
  1060. expect(exists).to.be.true();
  1061. done();
  1062. } catch (e) {
  1063. done(e);
  1064. }
  1065. });
  1066. });
  1067. it('promisified version handles a non-existent file', function (done) {
  1068. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1069. // eslint-disable-next-line
  1070. util.promisify(fs.exists)(p).then(exists => {
  1071. try {
  1072. expect(exists).to.be.false();
  1073. done();
  1074. } catch (e) {
  1075. done(e);
  1076. }
  1077. });
  1078. });
  1079. });
  1080. describe('fs.existsSync', function () {
  1081. it('handles an existing file', function () {
  1082. const p = path.join(asarDir, 'a.asar', 'file1');
  1083. expect(fs.existsSync(p)).to.be.true();
  1084. });
  1085. it('handles a non-existent file', function () {
  1086. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1087. expect(fs.existsSync(p)).to.be.false();
  1088. });
  1089. });
  1090. describe('fs.access', function () {
  1091. it('accesses a normal file', function (done) {
  1092. const p = path.join(asarDir, 'a.asar', 'file1');
  1093. fs.access(p, function (err) {
  1094. try {
  1095. expect(err).to.be.undefined();
  1096. done();
  1097. } catch (e) {
  1098. done(e);
  1099. }
  1100. });
  1101. });
  1102. it('throws an error when called with write mode', function (done) {
  1103. const p = path.join(asarDir, 'a.asar', 'file1');
  1104. fs.access(p, fs.constants.R_OK | fs.constants.W_OK, function (err) {
  1105. try {
  1106. expect(err.code).to.equal('EACCES');
  1107. done();
  1108. } catch (e) {
  1109. done(e);
  1110. }
  1111. });
  1112. });
  1113. it('throws an error when called on non-existent file', function (done) {
  1114. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1115. fs.access(p, function (err) {
  1116. try {
  1117. expect(err.code).to.equal('ENOENT');
  1118. done();
  1119. } catch (e) {
  1120. done(e);
  1121. }
  1122. });
  1123. });
  1124. it('allows write mode for unpacked files', function (done) {
  1125. const p = path.join(asarDir, 'unpack.asar', 'a.txt');
  1126. fs.access(p, fs.constants.R_OK | fs.constants.W_OK, function (err) {
  1127. try {
  1128. expect(err).to.be.null();
  1129. done();
  1130. } catch (e) {
  1131. done(e);
  1132. }
  1133. });
  1134. });
  1135. });
  1136. describe('fs.promises.access', function () {
  1137. it('accesses a normal file', async function () {
  1138. const p = path.join(asarDir, 'a.asar', 'file1');
  1139. await fs.promises.access(p);
  1140. });
  1141. it('throws an error when called with write mode', async function () {
  1142. const p = path.join(asarDir, 'a.asar', 'file1');
  1143. await expectToThrowErrorWithCode(() => fs.promises.access(p, fs.constants.R_OK | fs.constants.W_OK), 'EACCES');
  1144. });
  1145. it('throws an error when called on non-existent file', async function () {
  1146. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1147. await expectToThrowErrorWithCode(() => fs.promises.access(p), 'ENOENT');
  1148. });
  1149. it('allows write mode for unpacked files', async function () {
  1150. const p = path.join(asarDir, 'unpack.asar', 'a.txt');
  1151. await fs.promises.access(p, fs.constants.R_OK | fs.constants.W_OK);
  1152. });
  1153. });
  1154. describe('fs.accessSync', function () {
  1155. it('accesses a normal file', function () {
  1156. const p = path.join(asarDir, 'a.asar', 'file1');
  1157. expect(() => {
  1158. fs.accessSync(p);
  1159. }).to.not.throw();
  1160. });
  1161. it('throws an error when called with write mode', function () {
  1162. const p = path.join(asarDir, 'a.asar', 'file1');
  1163. expect(() => {
  1164. fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK);
  1165. }).to.throw(/EACCES/);
  1166. });
  1167. it('throws an error when called on non-existent file', function () {
  1168. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1169. expect(() => {
  1170. fs.accessSync(p);
  1171. }).to.throw(/ENOENT/);
  1172. });
  1173. it('allows write mode for unpacked files', function () {
  1174. const p = path.join(asarDir, 'unpack.asar', 'a.txt');
  1175. expect(() => {
  1176. fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK);
  1177. }).to.not.throw();
  1178. });
  1179. });
  1180. describe('child_process.fork', function () {
  1181. before(function () {
  1182. if (!features.isRunAsNodeEnabled()) {
  1183. this.skip();
  1184. }
  1185. });
  1186. it('opens a normal js file', function (done) {
  1187. const child = ChildProcess.fork(path.join(asarDir, 'a.asar', 'ping.js'));
  1188. child.on('message', function (msg) {
  1189. try {
  1190. expect(msg).to.equal('message');
  1191. done();
  1192. } catch (e) {
  1193. done(e);
  1194. }
  1195. });
  1196. child.send('message');
  1197. });
  1198. it('supports asar in the forked js', function (done) {
  1199. const file = path.join(asarDir, 'a.asar', 'file1');
  1200. const child = ChildProcess.fork(path.join(fixtures, 'module', 'asar.js'));
  1201. child.on('message', function (content) {
  1202. try {
  1203. expect(content).to.equal(fs.readFileSync(file).toString());
  1204. done();
  1205. } catch (e) {
  1206. done(e);
  1207. }
  1208. });
  1209. child.send(file);
  1210. });
  1211. });
  1212. describe('child_process.exec', function () {
  1213. const echo = path.join(asarDir, 'echo.asar', 'echo');
  1214. it('should not try to extract the command if there is a reference to a file inside an .asar', function (done) {
  1215. ChildProcess.exec('echo ' + echo + ' foo bar', function (error, stdout) {
  1216. try {
  1217. expect(error).to.be.null();
  1218. expect(stdout.toString().replace(/\r/g, '')).to.equal(echo + ' foo bar\n');
  1219. done();
  1220. } catch (e) {
  1221. done(e);
  1222. }
  1223. });
  1224. });
  1225. it('can be promisified', () => {
  1226. return util.promisify(ChildProcess.exec)('echo ' + echo + ' foo bar').then(({ stdout }) => {
  1227. expect(stdout.toString().replace(/\r/g, '')).to.equal(echo + ' foo bar\n');
  1228. });
  1229. });
  1230. });
  1231. describe('child_process.execSync', function () {
  1232. const echo = path.join(asarDir, 'echo.asar', 'echo');
  1233. it('should not try to extract the command if there is a reference to a file inside an .asar', function (done) {
  1234. try {
  1235. const stdout = ChildProcess.execSync('echo ' + echo + ' foo bar');
  1236. expect(stdout.toString().replace(/\r/g, '')).to.equal(echo + ' foo bar\n');
  1237. done();
  1238. } catch (e) {
  1239. done(e);
  1240. }
  1241. });
  1242. });
  1243. ifdescribe(process.platform === 'darwin' && process.arch !== 'arm64')('child_process.execFile', function () {
  1244. const execFile = ChildProcess.execFile;
  1245. const execFileSync = ChildProcess.execFileSync;
  1246. const echo = path.join(asarDir, 'echo.asar', 'echo');
  1247. it('executes binaries', function (done) {
  1248. execFile(echo, ['test'], function (error, stdout) {
  1249. try {
  1250. expect(error).to.be.null();
  1251. expect(stdout).to.equal('test\n');
  1252. done();
  1253. } catch (e) {
  1254. done(e);
  1255. }
  1256. });
  1257. });
  1258. it('executes binaries without callback', function (done) {
  1259. const process = execFile(echo, ['test']);
  1260. process.on('close', function (code) {
  1261. try {
  1262. expect(code).to.equal(0);
  1263. done();
  1264. } catch (e) {
  1265. done(e);
  1266. }
  1267. });
  1268. process.on('error', function () {
  1269. done('error');
  1270. });
  1271. });
  1272. it('execFileSync executes binaries', function () {
  1273. const output = execFileSync(echo, ['test']);
  1274. expect(String(output)).to.equal('test\n');
  1275. });
  1276. it('can be promisified', () => {
  1277. return util.promisify(ChildProcess.execFile)(echo, ['test']).then(({ stdout }) => {
  1278. expect(stdout).to.equal('test\n');
  1279. });
  1280. });
  1281. });
  1282. describe('internalModuleReadJSON', function () {
  1283. const { internalModuleReadJSON } = process.binding('fs');
  1284. it('reads a normal file', function () {
  1285. const file1 = path.join(asarDir, 'a.asar', 'file1');
  1286. const [s1, c1] = internalModuleReadJSON(file1);
  1287. expect([s1.toString().trim(), c1]).to.eql(['file1', true]);
  1288. const file2 = path.join(asarDir, 'a.asar', 'file2');
  1289. const [s2, c2] = internalModuleReadJSON(file2);
  1290. expect([s2.toString().trim(), c2]).to.eql(['file2', true]);
  1291. const file3 = path.join(asarDir, 'a.asar', 'file3');
  1292. const [s3, c3] = internalModuleReadJSON(file3);
  1293. expect([s3.toString().trim(), c3]).to.eql(['file3', true]);
  1294. });
  1295. it('reads a normal file with unpacked files', function () {
  1296. const p = path.join(asarDir, 'unpack.asar', 'a.txt');
  1297. const [s, c] = internalModuleReadJSON(p);
  1298. expect([s.toString().trim(), c]).to.eql(['a', true]);
  1299. });
  1300. });
  1301. describe('util.promisify', function () {
  1302. it('can promisify all fs functions', function () {
  1303. const originalFs = require('original-fs');
  1304. const { hasOwnProperty } = Object.prototype;
  1305. for (const [propertyName, originalValue] of Object.entries(originalFs)) {
  1306. // Some properties exist but have a value of `undefined` on some platforms.
  1307. // E.g. `fs.lchmod`, which in only available on MacOS, see
  1308. // https://nodejs.org/docs/latest-v10.x/api/fs.html#fs_fs_lchmod_path_mode_callback
  1309. // Also check for `null`s, `hasOwnProperty()` can't handle them.
  1310. if (typeof originalValue === 'undefined' || originalValue === null) continue;
  1311. if (hasOwnProperty.call(originalValue, util.promisify.custom)) {
  1312. expect(fs).to.have.own.property(propertyName)
  1313. .that.has.own.property(util.promisify.custom);
  1314. }
  1315. }
  1316. });
  1317. });
  1318. describe('process.noAsar', function () {
  1319. const errorName = process.platform === 'win32' ? 'ENOENT' : 'ENOTDIR';
  1320. beforeEach(function () {
  1321. process.noAsar = true;
  1322. });
  1323. afterEach(function () {
  1324. process.noAsar = false;
  1325. });
  1326. it('disables asar support in sync API', function () {
  1327. const file = path.join(asarDir, 'a.asar', 'file1');
  1328. const dir = path.join(asarDir, 'a.asar', 'dir1');
  1329. expect(() => {
  1330. fs.readFileSync(file);
  1331. }).to.throw(new RegExp(errorName));
  1332. expect(() => {
  1333. fs.lstatSync(file);
  1334. }).to.throw(new RegExp(errorName));
  1335. expect(() => {
  1336. fs.realpathSync(file);
  1337. }).to.throw(new RegExp(errorName));
  1338. expect(() => {
  1339. fs.readdirSync(dir);
  1340. }).to.throw(new RegExp(errorName));
  1341. });
  1342. it('disables asar support in async API', function (done) {
  1343. const file = path.join(asarDir, 'a.asar', 'file1');
  1344. const dir = path.join(asarDir, 'a.asar', 'dir1');
  1345. fs.readFile(file, function (error) {
  1346. expect(error.code).to.equal(errorName);
  1347. fs.lstat(file, function (error) {
  1348. expect(error.code).to.equal(errorName);
  1349. fs.realpath(file, function (error) {
  1350. expect(error.code).to.equal(errorName);
  1351. fs.readdir(dir, function (error) {
  1352. expect(error.code).to.equal(errorName);
  1353. done();
  1354. });
  1355. });
  1356. });
  1357. });
  1358. });
  1359. it('disables asar support in promises API', async function () {
  1360. const file = path.join(asarDir, 'a.asar', 'file1');
  1361. const dir = path.join(asarDir, 'a.asar', 'dir1');
  1362. await expect(fs.promises.readFile(file)).to.be.eventually.rejectedWith(Error, new RegExp(errorName));
  1363. await expect(fs.promises.lstat(file)).to.be.eventually.rejectedWith(Error, new RegExp(errorName));
  1364. await expect(fs.promises.realpath(file)).to.be.eventually.rejectedWith(Error, new RegExp(errorName));
  1365. await expect(fs.promises.readdir(dir)).to.be.eventually.rejectedWith(Error, new RegExp(errorName));
  1366. });
  1367. it('treats *.asar as normal file', function () {
  1368. const originalFs = require('original-fs');
  1369. const asar = path.join(asarDir, 'a.asar');
  1370. const content1 = fs.readFileSync(asar);
  1371. const content2 = originalFs.readFileSync(asar);
  1372. expect(content1.compare(content2)).to.equal(0);
  1373. expect(() => {
  1374. fs.readdirSync(asar);
  1375. }).to.throw(/ENOTDIR/);
  1376. });
  1377. it('is reset to its original value when execSync throws an error', function () {
  1378. process.noAsar = false;
  1379. expect(() => {
  1380. ChildProcess.execSync(path.join(__dirname, 'does-not-exist.txt'));
  1381. }).to.throw();
  1382. expect(process.noAsar).to.be.false();
  1383. });
  1384. });
  1385. describe('process.env.ELECTRON_NO_ASAR', function () {
  1386. before(function () {
  1387. if (!features.isRunAsNodeEnabled()) {
  1388. this.skip();
  1389. }
  1390. });
  1391. it('disables asar support in forked processes', function (done) {
  1392. const forked = ChildProcess.fork(path.join(__dirname, 'fixtures', 'module', 'no-asar.js'), [], {
  1393. env: {
  1394. ELECTRON_NO_ASAR: true
  1395. }
  1396. });
  1397. forked.on('message', function (stats) {
  1398. try {
  1399. expect(stats.isFile).to.be.true();
  1400. expect(stats.size).to.equal(778);
  1401. done();
  1402. } catch (e) {
  1403. done(e);
  1404. }
  1405. });
  1406. });
  1407. it('disables asar support in spawned processes', function (done) {
  1408. const spawned = ChildProcess.spawn(process.execPath, [path.join(__dirname, 'fixtures', 'module', 'no-asar.js')], {
  1409. env: {
  1410. ELECTRON_NO_ASAR: true,
  1411. ELECTRON_RUN_AS_NODE: true
  1412. }
  1413. });
  1414. let output = '';
  1415. spawned.stdout.on('data', function (data) {
  1416. output += data;
  1417. });
  1418. spawned.stdout.on('close', function () {
  1419. try {
  1420. const stats = JSON.parse(output);
  1421. expect(stats.isFile).to.be.true();
  1422. expect(stats.size).to.equal(778);
  1423. done();
  1424. } catch (e) {
  1425. done(e);
  1426. }
  1427. });
  1428. });
  1429. });
  1430. });
  1431. describe('asar protocol', function () {
  1432. it('can request a file in package', function (done) {
  1433. const p = path.resolve(asarDir, 'a.asar', 'file1');
  1434. $.get('file://' + p, function (data) {
  1435. try {
  1436. expect(data.trim()).to.equal('file1');
  1437. done();
  1438. } catch (e) {
  1439. done(e);
  1440. }
  1441. });
  1442. });
  1443. it('can request a file in package with unpacked files', function (done) {
  1444. const p = path.resolve(asarDir, 'unpack.asar', 'a.txt');
  1445. $.get('file://' + p, function (data) {
  1446. try {
  1447. expect(data.trim()).to.equal('a');
  1448. done();
  1449. } catch (e) {
  1450. done(e);
  1451. }
  1452. });
  1453. });
  1454. it('can request a linked file in package', function (done) {
  1455. const p = path.resolve(asarDir, 'a.asar', 'link2', 'link1');
  1456. $.get('file://' + p, function (data) {
  1457. try {
  1458. expect(data.trim()).to.equal('file1');
  1459. done();
  1460. } catch (e) {
  1461. done(e);
  1462. }
  1463. });
  1464. });
  1465. it('can request a file in filesystem', function (done) {
  1466. const p = path.resolve(asarDir, 'file');
  1467. $.get('file://' + p, function (data) {
  1468. try {
  1469. expect(data.trim()).to.equal('file');
  1470. done();
  1471. } catch (e) {
  1472. done(e);
  1473. }
  1474. });
  1475. });
  1476. it('gets 404 when file is not found', function (done) {
  1477. const p = path.resolve(asarDir, 'a.asar', 'no-exist');
  1478. $.ajax({
  1479. url: 'file://' + p,
  1480. error: function (err) {
  1481. try {
  1482. expect(err.status).to.equal(404);
  1483. done();
  1484. } catch (e) {
  1485. done(e);
  1486. }
  1487. }
  1488. });
  1489. });
  1490. });
  1491. describe('original-fs module', function () {
  1492. const originalFs = require('original-fs');
  1493. it('treats .asar as file', function () {
  1494. const file = path.join(asarDir, 'a.asar');
  1495. const stats = originalFs.statSync(file);
  1496. expect(stats.isFile()).to.be.true();
  1497. });
  1498. ifit(features.isRunAsNodeEnabled())('is available in forked scripts', async function () {
  1499. const child = ChildProcess.fork(path.join(fixtures, 'module', 'original-fs.js'));
  1500. const message = emittedOnce(child, 'message');
  1501. child.send('message');
  1502. const [msg] = await message;
  1503. expect(msg).to.equal('object');
  1504. });
  1505. it('can be used with streams', () => {
  1506. originalFs.createReadStream(path.join(asarDir, 'a.asar'));
  1507. });
  1508. it('can recursively delete a directory with an asar file in it', () => {
  1509. const deleteDir = path.join(asarDir, 'deleteme');
  1510. fs.mkdirSync(deleteDir);
  1511. originalFs.rmdirSync(deleteDir, { recursive: true });
  1512. expect(fs.existsSync(deleteDir)).to.be.false();
  1513. });
  1514. it('has the same APIs as fs', function () {
  1515. expect(Object.keys(require('fs'))).to.deep.equal(Object.keys(require('original-fs')));
  1516. expect(Object.keys(require('fs').promises)).to.deep.equal(Object.keys(require('original-fs').promises));
  1517. });
  1518. });
  1519. describe('graceful-fs module', function () {
  1520. const gfs = require('graceful-fs');
  1521. it('recognize asar archvies', function () {
  1522. const p = path.join(asarDir, 'a.asar', 'link1');
  1523. expect(gfs.readFileSync(p).toString().trim()).to.equal('file1');
  1524. });
  1525. it('does not touch global fs object', function () {
  1526. expect(fs.readdir).to.not.equal(gfs.readdir);
  1527. });
  1528. });
  1529. describe('mkdirp module', function () {
  1530. const mkdirp = require('mkdirp');
  1531. it('throws error when calling inside asar archive', function () {
  1532. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1533. expect(() => {
  1534. mkdirp.sync(p);
  1535. }).to.throw(/ENOTDIR/);
  1536. });
  1537. });
  1538. describe('native-image', function () {
  1539. it('reads image from asar archive', function () {
  1540. const p = path.join(asarDir, 'logo.asar', 'logo.png');
  1541. const logo = nativeImage.createFromPath(p);
  1542. expect(logo.getSize()).to.deep.equal({
  1543. width: 55,
  1544. height: 55
  1545. });
  1546. });
  1547. it('reads image from asar archive with unpacked files', function () {
  1548. const p = path.join(asarDir, 'unpack.asar', 'atom.png');
  1549. const logo = nativeImage.createFromPath(p);
  1550. expect(logo.getSize()).to.deep.equal({
  1551. width: 1024,
  1552. height: 1024
  1553. });
  1554. });
  1555. });
  1556. });