asar-spec.js 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  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 } = 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. describe('child_process.execFile', function () {
  1244. const execFile = ChildProcess.execFile;
  1245. const execFileSync = ChildProcess.execFileSync;
  1246. const echo = path.join(asarDir, 'echo.asar', 'echo');
  1247. before(function () {
  1248. if (process.platform !== 'darwin') {
  1249. this.skip();
  1250. }
  1251. });
  1252. it('executes binaries', function (done) {
  1253. execFile(echo, ['test'], function (error, stdout) {
  1254. try {
  1255. expect(error).to.be.null();
  1256. expect(stdout).to.equal('test\n');
  1257. done();
  1258. } catch (e) {
  1259. done(e);
  1260. }
  1261. });
  1262. });
  1263. it('executes binaries without callback', function (done) {
  1264. const process = execFile(echo, ['test']);
  1265. process.on('close', function (code) {
  1266. try {
  1267. expect(code).to.equal(0);
  1268. done();
  1269. } catch (e) {
  1270. done(e);
  1271. }
  1272. });
  1273. process.on('error', function () {
  1274. done('error');
  1275. });
  1276. });
  1277. it('execFileSync executes binaries', function () {
  1278. const output = execFileSync(echo, ['test']);
  1279. expect(String(output)).to.equal('test\n');
  1280. });
  1281. it('can be promisified', () => {
  1282. return util.promisify(ChildProcess.execFile)(echo, ['test']).then(({ stdout }) => {
  1283. expect(stdout).to.equal('test\n');
  1284. });
  1285. });
  1286. });
  1287. describe('internalModuleReadJSON', function () {
  1288. const { internalModuleReadJSON } = process.binding('fs');
  1289. it('reads a normal file', function () {
  1290. const file1 = path.join(asarDir, 'a.asar', 'file1');
  1291. const [s1, c1] = internalModuleReadJSON(file1);
  1292. expect([s1.toString().trim(), c1]).to.eql(['file1', true]);
  1293. const file2 = path.join(asarDir, 'a.asar', 'file2');
  1294. const [s2, c2] = internalModuleReadJSON(file2);
  1295. expect([s2.toString().trim(), c2]).to.eql(['file2', true]);
  1296. const file3 = path.join(asarDir, 'a.asar', 'file3');
  1297. const [s3, c3] = internalModuleReadJSON(file3);
  1298. expect([s3.toString().trim(), c3]).to.eql(['file3', true]);
  1299. });
  1300. it('reads a normal file with unpacked files', function () {
  1301. const p = path.join(asarDir, 'unpack.asar', 'a.txt');
  1302. const [s, c] = internalModuleReadJSON(p);
  1303. expect([s.toString().trim(), c]).to.eql(['a', true]);
  1304. });
  1305. });
  1306. describe('util.promisify', function () {
  1307. it('can promisify all fs functions', function () {
  1308. const originalFs = require('original-fs');
  1309. const { hasOwnProperty } = Object.prototype;
  1310. for (const [propertyName, originalValue] of Object.entries(originalFs)) {
  1311. // Some properties exist but have a value of `undefined` on some platforms.
  1312. // E.g. `fs.lchmod`, which in only available on MacOS, see
  1313. // https://nodejs.org/docs/latest-v10.x/api/fs.html#fs_fs_lchmod_path_mode_callback
  1314. // Also check for `null`s, `hasOwnProperty()` can't handle them.
  1315. if (typeof originalValue === 'undefined' || originalValue === null) continue;
  1316. if (hasOwnProperty.call(originalValue, util.promisify.custom)) {
  1317. expect(fs).to.have.own.property(propertyName)
  1318. .that.has.own.property(util.promisify.custom);
  1319. }
  1320. }
  1321. });
  1322. });
  1323. describe('process.noAsar', function () {
  1324. const errorName = process.platform === 'win32' ? 'ENOENT' : 'ENOTDIR';
  1325. beforeEach(function () {
  1326. process.noAsar = true;
  1327. });
  1328. afterEach(function () {
  1329. process.noAsar = false;
  1330. });
  1331. it('disables asar support in sync API', function () {
  1332. const file = path.join(asarDir, 'a.asar', 'file1');
  1333. const dir = path.join(asarDir, 'a.asar', 'dir1');
  1334. expect(() => {
  1335. fs.readFileSync(file);
  1336. }).to.throw(new RegExp(errorName));
  1337. expect(() => {
  1338. fs.lstatSync(file);
  1339. }).to.throw(new RegExp(errorName));
  1340. expect(() => {
  1341. fs.realpathSync(file);
  1342. }).to.throw(new RegExp(errorName));
  1343. expect(() => {
  1344. fs.readdirSync(dir);
  1345. }).to.throw(new RegExp(errorName));
  1346. });
  1347. it('disables asar support in async API', function (done) {
  1348. const file = path.join(asarDir, 'a.asar', 'file1');
  1349. const dir = path.join(asarDir, 'a.asar', 'dir1');
  1350. fs.readFile(file, function (error) {
  1351. expect(error.code).to.equal(errorName);
  1352. fs.lstat(file, function (error) {
  1353. expect(error.code).to.equal(errorName);
  1354. fs.realpath(file, function (error) {
  1355. expect(error.code).to.equal(errorName);
  1356. fs.readdir(dir, function (error) {
  1357. expect(error.code).to.equal(errorName);
  1358. done();
  1359. });
  1360. });
  1361. });
  1362. });
  1363. });
  1364. it('disables asar support in promises API', async function () {
  1365. const file = path.join(asarDir, 'a.asar', 'file1');
  1366. const dir = path.join(asarDir, 'a.asar', 'dir1');
  1367. await expect(fs.promises.readFile(file)).to.be.eventually.rejectedWith(Error, new RegExp(errorName));
  1368. await expect(fs.promises.lstat(file)).to.be.eventually.rejectedWith(Error, new RegExp(errorName));
  1369. await expect(fs.promises.realpath(file)).to.be.eventually.rejectedWith(Error, new RegExp(errorName));
  1370. await expect(fs.promises.readdir(dir)).to.be.eventually.rejectedWith(Error, new RegExp(errorName));
  1371. });
  1372. it('treats *.asar as normal file', function () {
  1373. const originalFs = require('original-fs');
  1374. const asar = path.join(asarDir, 'a.asar');
  1375. const content1 = fs.readFileSync(asar);
  1376. const content2 = originalFs.readFileSync(asar);
  1377. expect(content1.compare(content2)).to.equal(0);
  1378. expect(() => {
  1379. fs.readdirSync(asar);
  1380. }).to.throw(/ENOTDIR/);
  1381. });
  1382. it('is reset to its original value when execSync throws an error', function () {
  1383. process.noAsar = false;
  1384. expect(() => {
  1385. ChildProcess.execSync(path.join(__dirname, 'does-not-exist.txt'));
  1386. }).to.throw();
  1387. expect(process.noAsar).to.be.false();
  1388. });
  1389. });
  1390. describe('process.env.ELECTRON_NO_ASAR', function () {
  1391. before(function () {
  1392. if (!features.isRunAsNodeEnabled()) {
  1393. this.skip();
  1394. }
  1395. });
  1396. it('disables asar support in forked processes', function (done) {
  1397. const forked = ChildProcess.fork(path.join(__dirname, 'fixtures', 'module', 'no-asar.js'), [], {
  1398. env: {
  1399. ELECTRON_NO_ASAR: true
  1400. }
  1401. });
  1402. forked.on('message', function (stats) {
  1403. try {
  1404. expect(stats.isFile).to.be.true();
  1405. expect(stats.size).to.equal(778);
  1406. done();
  1407. } catch (e) {
  1408. done(e);
  1409. }
  1410. });
  1411. });
  1412. it('disables asar support in spawned processes', function (done) {
  1413. const spawned = ChildProcess.spawn(process.execPath, [path.join(__dirname, 'fixtures', 'module', 'no-asar.js')], {
  1414. env: {
  1415. ELECTRON_NO_ASAR: true,
  1416. ELECTRON_RUN_AS_NODE: true
  1417. }
  1418. });
  1419. let output = '';
  1420. spawned.stdout.on('data', function (data) {
  1421. output += data;
  1422. });
  1423. spawned.stdout.on('close', function () {
  1424. try {
  1425. const stats = JSON.parse(output);
  1426. expect(stats.isFile).to.be.true();
  1427. expect(stats.size).to.equal(778);
  1428. done();
  1429. } catch (e) {
  1430. done(e);
  1431. }
  1432. });
  1433. });
  1434. });
  1435. });
  1436. describe('asar protocol', function () {
  1437. it('can request a file in package', function (done) {
  1438. const p = path.resolve(asarDir, 'a.asar', 'file1');
  1439. $.get('file://' + p, function (data) {
  1440. try {
  1441. expect(data.trim()).to.equal('file1');
  1442. done();
  1443. } catch (e) {
  1444. done(e);
  1445. }
  1446. });
  1447. });
  1448. it('can request a file in package with unpacked files', function (done) {
  1449. const p = path.resolve(asarDir, 'unpack.asar', 'a.txt');
  1450. $.get('file://' + p, function (data) {
  1451. try {
  1452. expect(data.trim()).to.equal('a');
  1453. done();
  1454. } catch (e) {
  1455. done(e);
  1456. }
  1457. });
  1458. });
  1459. it('can request a linked file in package', function (done) {
  1460. const p = path.resolve(asarDir, 'a.asar', 'link2', 'link1');
  1461. $.get('file://' + p, function (data) {
  1462. try {
  1463. expect(data.trim()).to.equal('file1');
  1464. done();
  1465. } catch (e) {
  1466. done(e);
  1467. }
  1468. });
  1469. });
  1470. it('can request a file in filesystem', function (done) {
  1471. const p = path.resolve(asarDir, 'file');
  1472. $.get('file://' + p, function (data) {
  1473. try {
  1474. expect(data.trim()).to.equal('file');
  1475. done();
  1476. } catch (e) {
  1477. done(e);
  1478. }
  1479. });
  1480. });
  1481. it('gets 404 when file is not found', function (done) {
  1482. const p = path.resolve(asarDir, 'a.asar', 'no-exist');
  1483. $.ajax({
  1484. url: 'file://' + p,
  1485. error: function (err) {
  1486. try {
  1487. expect(err.status).to.equal(404);
  1488. done();
  1489. } catch (e) {
  1490. done(e);
  1491. }
  1492. }
  1493. });
  1494. });
  1495. });
  1496. describe('original-fs module', function () {
  1497. const originalFs = require('original-fs');
  1498. it('treats .asar as file', function () {
  1499. const file = path.join(asarDir, 'a.asar');
  1500. const stats = originalFs.statSync(file);
  1501. expect(stats.isFile()).to.be.true();
  1502. });
  1503. ifit(features.isRunAsNodeEnabled())('is available in forked scripts', async function () {
  1504. const child = ChildProcess.fork(path.join(fixtures, 'module', 'original-fs.js'));
  1505. const message = emittedOnce(child, 'message');
  1506. child.send('message');
  1507. const [msg] = await message;
  1508. expect(msg).to.equal('object');
  1509. });
  1510. it('can be used with streams', () => {
  1511. originalFs.createReadStream(path.join(asarDir, 'a.asar'));
  1512. });
  1513. it('can recursively delete a directory with an asar file in it', () => {
  1514. const deleteDir = path.join(asarDir, 'deleteme');
  1515. originalFs.rmdirSync(deleteDir, { recursive: true });
  1516. expect(fs.existsSync(deleteDir)).to.be.false();
  1517. });
  1518. it('has the same APIs as fs', function () {
  1519. expect(Object.keys(require('fs'))).to.deep.equal(Object.keys(require('original-fs')));
  1520. expect(Object.keys(require('fs').promises)).to.deep.equal(Object.keys(require('original-fs').promises));
  1521. });
  1522. });
  1523. describe('graceful-fs module', function () {
  1524. const gfs = require('graceful-fs');
  1525. it('recognize asar archvies', function () {
  1526. const p = path.join(asarDir, 'a.asar', 'link1');
  1527. expect(gfs.readFileSync(p).toString().trim()).to.equal('file1');
  1528. });
  1529. it('does not touch global fs object', function () {
  1530. expect(fs.readdir).to.not.equal(gfs.readdir);
  1531. });
  1532. });
  1533. describe('mkdirp module', function () {
  1534. const mkdirp = require('mkdirp');
  1535. it('throws error when calling inside asar archive', function () {
  1536. const p = path.join(asarDir, 'a.asar', 'not-exist');
  1537. expect(() => {
  1538. mkdirp.sync(p);
  1539. }).to.throw(/ENOTDIR/);
  1540. });
  1541. });
  1542. describe('native-image', function () {
  1543. it('reads image from asar archive', function () {
  1544. const p = path.join(asarDir, 'logo.asar', 'logo.png');
  1545. const logo = nativeImage.createFromPath(p);
  1546. expect(logo.getSize()).to.deep.equal({
  1547. width: 55,
  1548. height: 55
  1549. });
  1550. });
  1551. it('reads image from asar archive with unpacked files', function () {
  1552. const p = path.join(asarDir, 'unpack.asar', 'atom.png');
  1553. const logo = nativeImage.createFromPath(p);
  1554. expect(logo.getSize()).to.deep.equal({
  1555. width: 1024,
  1556. height: 1024
  1557. });
  1558. });
  1559. });
  1560. });