asar-spec.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. const assert = require('assert')
  2. const ChildProcess = require('child_process')
  3. const { expect } = require('chai')
  4. const fs = require('fs')
  5. const path = require('path')
  6. const temp = require('temp').track()
  7. const util = require('util')
  8. const { closeWindow } = require('./window-helpers')
  9. const nativeImage = require('electron').nativeImage
  10. const remote = require('electron').remote
  11. const ipcMain = remote.require('electron').ipcMain
  12. const BrowserWindow = remote.require('electron').BrowserWindow
  13. const features = process.atomBinding('features')
  14. describe('asar package', function () {
  15. const fixtures = path.join(__dirname, 'fixtures')
  16. describe('node api', function () {
  17. it('supports paths specified as a Buffer', function () {
  18. const file = Buffer.from(path.join(fixtures, 'asar', 'a.asar', 'file1'))
  19. assert.strictEqual(fs.existsSync(file), true)
  20. })
  21. describe('fs.readFileSync', function () {
  22. it('does not leak fd', function () {
  23. let readCalls = 1
  24. while (readCalls <= 10000) {
  25. fs.readFileSync(path.join(process.resourcesPath, 'electron.asar', 'renderer', 'api', 'ipc-renderer.js'))
  26. readCalls++
  27. }
  28. })
  29. it('reads a normal file', function () {
  30. const file1 = path.join(fixtures, 'asar', 'a.asar', 'file1')
  31. assert.strictEqual(fs.readFileSync(file1).toString().trim(), 'file1')
  32. const file2 = path.join(fixtures, 'asar', 'a.asar', 'file2')
  33. assert.strictEqual(fs.readFileSync(file2).toString().trim(), 'file2')
  34. const file3 = path.join(fixtures, 'asar', 'a.asar', 'file3')
  35. assert.strictEqual(fs.readFileSync(file3).toString().trim(), 'file3')
  36. })
  37. it('reads from a empty file', function () {
  38. const file = path.join(fixtures, 'asar', 'empty.asar', 'file1')
  39. const buffer = fs.readFileSync(file)
  40. assert.strictEqual(buffer.length, 0)
  41. assert.strictEqual(buffer.toString(), '')
  42. })
  43. it('reads a linked file', function () {
  44. const p = path.join(fixtures, 'asar', 'a.asar', 'link1')
  45. assert.strictEqual(fs.readFileSync(p).toString().trim(), 'file1')
  46. })
  47. it('reads a file from linked directory', function () {
  48. const p1 = path.join(fixtures, 'asar', 'a.asar', 'link2', 'file1')
  49. assert.strictEqual(fs.readFileSync(p1).toString().trim(), 'file1')
  50. const p2 = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2', 'file1')
  51. assert.strictEqual(fs.readFileSync(p2).toString().trim(), 'file1')
  52. })
  53. it('throws ENOENT error when can not find file', function () {
  54. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  55. const throws = function () {
  56. fs.readFileSync(p)
  57. }
  58. assert.throws(throws, /ENOENT/)
  59. })
  60. it('passes ENOENT error to callback when can not find file', function () {
  61. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  62. let async = false
  63. fs.readFile(p, function (e) {
  64. assert(async)
  65. assert(/ENOENT/.test(e))
  66. })
  67. async = true
  68. })
  69. it('reads a normal file with unpacked files', function () {
  70. const p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt')
  71. assert.strictEqual(fs.readFileSync(p).toString().trim(), 'a')
  72. })
  73. })
  74. describe('fs.readFile', function () {
  75. it('reads a normal file', function (done) {
  76. const p = path.join(fixtures, 'asar', 'a.asar', 'file1')
  77. fs.readFile(p, function (err, content) {
  78. assert.strictEqual(err, null)
  79. assert.strictEqual(String(content).trim(), 'file1')
  80. done()
  81. })
  82. })
  83. it('reads from a empty file', function (done) {
  84. const p = path.join(fixtures, 'asar', 'empty.asar', 'file1')
  85. fs.readFile(p, function (err, content) {
  86. assert.strictEqual(err, null)
  87. assert.strictEqual(String(content), '')
  88. done()
  89. })
  90. })
  91. it('reads from a empty file with encoding', function (done) {
  92. const p = path.join(fixtures, 'asar', 'empty.asar', 'file1')
  93. fs.readFile(p, 'utf8', function (err, content) {
  94. assert.strictEqual(err, null)
  95. assert.strictEqual(content, '')
  96. done()
  97. })
  98. })
  99. it('reads a linked file', function (done) {
  100. const p = path.join(fixtures, 'asar', 'a.asar', 'link1')
  101. fs.readFile(p, function (err, content) {
  102. assert.strictEqual(err, null)
  103. assert.strictEqual(String(content).trim(), 'file1')
  104. done()
  105. })
  106. })
  107. it('reads a file from linked directory', function (done) {
  108. const p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2', 'file1')
  109. fs.readFile(p, function (err, content) {
  110. assert.strictEqual(err, null)
  111. assert.strictEqual(String(content).trim(), 'file1')
  112. done()
  113. })
  114. })
  115. it('throws ENOENT error when can not find file', function (done) {
  116. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  117. fs.readFile(p, function (err) {
  118. assert.strictEqual(err.code, 'ENOENT')
  119. done()
  120. })
  121. })
  122. })
  123. describe('fs.copyFile', function () {
  124. it('copies a normal file', function (done) {
  125. const p = path.join(fixtures, 'asar', 'a.asar', 'file1')
  126. const dest = temp.path()
  127. fs.copyFile(p, dest, function (err) {
  128. assert.strictEqual(err, null)
  129. assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
  130. done()
  131. })
  132. })
  133. it('copies a unpacked file', function (done) {
  134. const p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt')
  135. const dest = temp.path()
  136. fs.copyFile(p, dest, function (err) {
  137. assert.strictEqual(err, null)
  138. assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
  139. done()
  140. })
  141. })
  142. })
  143. describe('fs.copyFileSync', function () {
  144. it('copies a normal file', function () {
  145. const p = path.join(fixtures, 'asar', 'a.asar', 'file1')
  146. const dest = temp.path()
  147. fs.copyFileSync(p, dest)
  148. assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
  149. })
  150. it('copies a unpacked file', function () {
  151. const p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt')
  152. const dest = temp.path()
  153. fs.copyFileSync(p, dest)
  154. assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
  155. })
  156. })
  157. describe('fs.lstatSync', function () {
  158. it('handles path with trailing slash correctly', function () {
  159. const p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2', 'file1')
  160. fs.lstatSync(p)
  161. fs.lstatSync(p + '/')
  162. })
  163. it('returns information of root', function () {
  164. const p = path.join(fixtures, 'asar', 'a.asar')
  165. const stats = fs.lstatSync(p)
  166. assert.strictEqual(stats.isFile(), false)
  167. assert.strictEqual(stats.isDirectory(), true)
  168. assert.strictEqual(stats.isSymbolicLink(), false)
  169. assert.strictEqual(stats.size, 0)
  170. })
  171. it('returns information of root with stats as bigint', function () {
  172. const p = path.join(fixtures, 'asar', 'a.asar')
  173. const stats = fs.lstatSync(p, { bigint: false })
  174. assert.strictEqual(stats.isFile(), false)
  175. assert.strictEqual(stats.isDirectory(), true)
  176. assert.strictEqual(stats.isSymbolicLink(), false)
  177. assert.strictEqual(stats.size, 0)
  178. })
  179. it('returns information of a normal file', function () {
  180. const ref2 = ['file1', 'file2', 'file3', path.join('dir1', 'file1'), path.join('link2', 'file1')]
  181. for (let j = 0, len = ref2.length; j < len; j++) {
  182. const file = ref2[j]
  183. const p = path.join(fixtures, 'asar', 'a.asar', file)
  184. const stats = fs.lstatSync(p)
  185. assert.strictEqual(stats.isFile(), true)
  186. assert.strictEqual(stats.isDirectory(), false)
  187. assert.strictEqual(stats.isSymbolicLink(), false)
  188. assert.strictEqual(stats.size, 6)
  189. }
  190. })
  191. it('returns information of a normal directory', function () {
  192. const ref2 = ['dir1', 'dir2', 'dir3']
  193. for (let j = 0, len = ref2.length; j < len; j++) {
  194. const file = ref2[j]
  195. const p = path.join(fixtures, 'asar', 'a.asar', file)
  196. const stats = fs.lstatSync(p)
  197. assert.strictEqual(stats.isFile(), false)
  198. assert.strictEqual(stats.isDirectory(), true)
  199. assert.strictEqual(stats.isSymbolicLink(), false)
  200. assert.strictEqual(stats.size, 0)
  201. }
  202. })
  203. it('returns information of a linked file', function () {
  204. const ref2 = ['link1', path.join('dir1', 'link1'), path.join('link2', 'link2')]
  205. for (let j = 0, len = ref2.length; j < len; j++) {
  206. const file = ref2[j]
  207. const p = path.join(fixtures, 'asar', 'a.asar', file)
  208. const stats = fs.lstatSync(p)
  209. assert.strictEqual(stats.isFile(), false)
  210. assert.strictEqual(stats.isDirectory(), false)
  211. assert.strictEqual(stats.isSymbolicLink(), true)
  212. assert.strictEqual(stats.size, 0)
  213. }
  214. })
  215. it('returns information of a linked directory', function () {
  216. const ref2 = ['link2', path.join('dir1', 'link2'), path.join('link2', 'link2')]
  217. for (let j = 0, len = ref2.length; j < len; j++) {
  218. const file = ref2[j]
  219. const p = path.join(fixtures, 'asar', 'a.asar', file)
  220. const stats = fs.lstatSync(p)
  221. assert.strictEqual(stats.isFile(), false)
  222. assert.strictEqual(stats.isDirectory(), false)
  223. assert.strictEqual(stats.isSymbolicLink(), true)
  224. assert.strictEqual(stats.size, 0)
  225. }
  226. })
  227. it('throws ENOENT error when can not find file', function () {
  228. const ref2 = ['file4', 'file5', path.join('dir1', 'file4')]
  229. for (let j = 0, len = ref2.length; j < len; j++) {
  230. const file = ref2[j]
  231. const p = path.join(fixtures, 'asar', 'a.asar', file)
  232. const throws = function () {
  233. fs.lstatSync(p)
  234. }
  235. assert.throws(throws, /ENOENT/)
  236. }
  237. })
  238. })
  239. describe('fs.lstat', function () {
  240. it('handles path with trailing slash correctly', function (done) {
  241. const p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2', 'file1')
  242. fs.lstat(p + '/', done)
  243. })
  244. it('returns information of root', function (done) {
  245. const p = path.join(fixtures, 'asar', 'a.asar')
  246. fs.lstat(p, function (err, stats) {
  247. assert.strictEqual(err, null)
  248. assert.strictEqual(stats.isFile(), false)
  249. assert.strictEqual(stats.isDirectory(), true)
  250. assert.strictEqual(stats.isSymbolicLink(), false)
  251. assert.strictEqual(stats.size, 0)
  252. done()
  253. })
  254. })
  255. it('returns information of root with stats as bigint', function (done) {
  256. const p = path.join(fixtures, 'asar', 'a.asar')
  257. fs.lstat(p, { bigint: false }, function (err, stats) {
  258. assert.strictEqual(err, null)
  259. assert.strictEqual(stats.isFile(), false)
  260. assert.strictEqual(stats.isDirectory(), true)
  261. assert.strictEqual(stats.isSymbolicLink(), false)
  262. assert.strictEqual(stats.size, 0)
  263. done()
  264. })
  265. })
  266. it('returns information of a normal file', function (done) {
  267. const p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'file1')
  268. fs.lstat(p, function (err, stats) {
  269. assert.strictEqual(err, null)
  270. assert.strictEqual(stats.isFile(), true)
  271. assert.strictEqual(stats.isDirectory(), false)
  272. assert.strictEqual(stats.isSymbolicLink(), false)
  273. assert.strictEqual(stats.size, 6)
  274. done()
  275. })
  276. })
  277. it('returns information of a normal directory', function (done) {
  278. const p = path.join(fixtures, 'asar', 'a.asar', 'dir1')
  279. fs.lstat(p, function (err, stats) {
  280. assert.strictEqual(err, null)
  281. assert.strictEqual(stats.isFile(), false)
  282. assert.strictEqual(stats.isDirectory(), true)
  283. assert.strictEqual(stats.isSymbolicLink(), false)
  284. assert.strictEqual(stats.size, 0)
  285. done()
  286. })
  287. })
  288. it('returns information of a linked file', function (done) {
  289. const p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link1')
  290. fs.lstat(p, function (err, stats) {
  291. assert.strictEqual(err, null)
  292. assert.strictEqual(stats.isFile(), false)
  293. assert.strictEqual(stats.isDirectory(), false)
  294. assert.strictEqual(stats.isSymbolicLink(), true)
  295. assert.strictEqual(stats.size, 0)
  296. done()
  297. })
  298. })
  299. it('returns information of a linked directory', function (done) {
  300. const p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2')
  301. fs.lstat(p, function (err, stats) {
  302. assert.strictEqual(err, null)
  303. assert.strictEqual(stats.isFile(), false)
  304. assert.strictEqual(stats.isDirectory(), false)
  305. assert.strictEqual(stats.isSymbolicLink(), true)
  306. assert.strictEqual(stats.size, 0)
  307. done()
  308. })
  309. })
  310. it('throws ENOENT error when can not find file', function (done) {
  311. const p = path.join(fixtures, 'asar', 'a.asar', 'file4')
  312. fs.lstat(p, function (err) {
  313. assert.strictEqual(err.code, 'ENOENT')
  314. done()
  315. })
  316. })
  317. })
  318. describe('fs.realpathSync', () => {
  319. it('returns real path root', () => {
  320. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  321. const p = 'a.asar'
  322. const r = fs.realpathSync(path.join(parent, p))
  323. assert.strictEqual(r, path.join(parent, p))
  324. })
  325. it('returns real path of a normal file', () => {
  326. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  327. const p = path.join('a.asar', 'file1')
  328. const r = fs.realpathSync(path.join(parent, p))
  329. assert.strictEqual(r, path.join(parent, p))
  330. })
  331. it('returns real path of a normal directory', () => {
  332. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  333. const p = path.join('a.asar', 'dir1')
  334. const r = fs.realpathSync(path.join(parent, p))
  335. assert.strictEqual(r, path.join(parent, p))
  336. })
  337. it('returns real path of a linked file', () => {
  338. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  339. const p = path.join('a.asar', 'link2', 'link1')
  340. const r = fs.realpathSync(path.join(parent, p))
  341. assert.strictEqual(r, path.join(parent, 'a.asar', 'file1'))
  342. })
  343. it('returns real path of a linked directory', () => {
  344. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  345. const p = path.join('a.asar', 'link2', 'link2')
  346. const r = fs.realpathSync(path.join(parent, p))
  347. assert.strictEqual(r, path.join(parent, 'a.asar', 'dir1'))
  348. })
  349. it('returns real path of an unpacked file', () => {
  350. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  351. const p = path.join('unpack.asar', 'a.txt')
  352. const r = fs.realpathSync(path.join(parent, p))
  353. assert.strictEqual(r, path.join(parent, p))
  354. })
  355. it('throws ENOENT error when can not find file', () => {
  356. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  357. const p = path.join('a.asar', 'not-exist')
  358. const throws = () => fs.realpathSync(path.join(parent, p))
  359. assert.throws(throws, /ENOENT/)
  360. })
  361. })
  362. describe('fs.realpathSync.native', () => {
  363. it('returns real path root', () => {
  364. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  365. const p = 'a.asar'
  366. const r = fs.realpathSync.native(path.join(parent, p))
  367. assert.strictEqual(r, path.join(parent, p))
  368. })
  369. it('returns real path of a normal file', () => {
  370. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  371. const p = path.join('a.asar', 'file1')
  372. const r = fs.realpathSync.native(path.join(parent, p))
  373. assert.strictEqual(r, path.join(parent, p))
  374. })
  375. it('returns real path of a normal directory', () => {
  376. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  377. const p = path.join('a.asar', 'dir1')
  378. const r = fs.realpathSync.native(path.join(parent, p))
  379. assert.strictEqual(r, path.join(parent, p))
  380. })
  381. it('returns real path of a linked file', () => {
  382. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  383. const p = path.join('a.asar', 'link2', 'link1')
  384. const r = fs.realpathSync.native(path.join(parent, p))
  385. assert.strictEqual(r, path.join(parent, 'a.asar', 'file1'))
  386. })
  387. it('returns real path of a linked directory', () => {
  388. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  389. const p = path.join('a.asar', 'link2', 'link2')
  390. const r = fs.realpathSync.native(path.join(parent, p))
  391. assert.strictEqual(r, path.join(parent, 'a.asar', 'dir1'))
  392. })
  393. it('returns real path of an unpacked file', () => {
  394. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  395. const p = path.join('unpack.asar', 'a.txt')
  396. const r = fs.realpathSync.native(path.join(parent, p))
  397. assert.strictEqual(r, path.join(parent, p))
  398. })
  399. it('throws ENOENT error when can not find file', () => {
  400. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  401. const p = path.join('a.asar', 'not-exist')
  402. const throws = () => fs.realpathSync.native(path.join(parent, p))
  403. assert.throws(throws, /ENOENT/)
  404. })
  405. })
  406. describe('fs.realpath', () => {
  407. it('returns real path root', done => {
  408. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  409. const p = 'a.asar'
  410. fs.realpath(path.join(parent, p), (err, r) => {
  411. assert.strictEqual(err, null)
  412. assert.strictEqual(r, path.join(parent, p))
  413. done()
  414. })
  415. })
  416. it('returns real path of a normal file', done => {
  417. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  418. const p = path.join('a.asar', 'file1')
  419. fs.realpath(path.join(parent, p), (err, r) => {
  420. assert.strictEqual(err, null)
  421. assert.strictEqual(r, path.join(parent, p))
  422. done()
  423. })
  424. })
  425. it('returns real path of a normal directory', done => {
  426. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  427. const p = path.join('a.asar', 'dir1')
  428. fs.realpath(path.join(parent, p), (err, r) => {
  429. assert.strictEqual(err, null)
  430. assert.strictEqual(r, path.join(parent, p))
  431. done()
  432. })
  433. })
  434. it('returns real path of a linked file', done => {
  435. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  436. const p = path.join('a.asar', 'link2', 'link1')
  437. fs.realpath(path.join(parent, p), (err, r) => {
  438. assert.strictEqual(err, null)
  439. assert.strictEqual(r, path.join(parent, 'a.asar', 'file1'))
  440. done()
  441. })
  442. })
  443. it('returns real path of a linked directory', done => {
  444. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  445. const p = path.join('a.asar', 'link2', 'link2')
  446. fs.realpath(path.join(parent, p), (err, r) => {
  447. assert.strictEqual(err, null)
  448. assert.strictEqual(r, path.join(parent, 'a.asar', 'dir1'))
  449. done()
  450. })
  451. })
  452. it('returns real path of an unpacked file', done => {
  453. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  454. const p = path.join('unpack.asar', 'a.txt')
  455. fs.realpath(path.join(parent, p), (err, r) => {
  456. assert.strictEqual(err, null)
  457. assert.strictEqual(r, path.join(parent, p))
  458. done()
  459. })
  460. })
  461. it('throws ENOENT error when can not find file', done => {
  462. const parent = fs.realpathSync(path.join(fixtures, 'asar'))
  463. const p = path.join('a.asar', 'not-exist')
  464. fs.realpath(path.join(parent, p), err => {
  465. assert.strictEqual(err.code, 'ENOENT')
  466. done()
  467. })
  468. })
  469. })
  470. describe('fs.realpath.native', () => {
  471. it('returns real path root', done => {
  472. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  473. const p = 'a.asar'
  474. fs.realpath.native(path.join(parent, p), (err, r) => {
  475. assert.strictEqual(err, null)
  476. assert.strictEqual(r, path.join(parent, p))
  477. done()
  478. })
  479. })
  480. it('returns real path of a normal file', done => {
  481. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  482. const p = path.join('a.asar', 'file1')
  483. fs.realpath.native(path.join(parent, p), (err, r) => {
  484. assert.strictEqual(err, null)
  485. assert.strictEqual(r, path.join(parent, p))
  486. done()
  487. })
  488. })
  489. it('returns real path of a normal directory', done => {
  490. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  491. const p = path.join('a.asar', 'dir1')
  492. fs.realpath.native(path.join(parent, p), (err, r) => {
  493. assert.strictEqual(err, null)
  494. assert.strictEqual(r, path.join(parent, p))
  495. done()
  496. })
  497. })
  498. it('returns real path of a linked file', done => {
  499. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  500. const p = path.join('a.asar', 'link2', 'link1')
  501. fs.realpath.native(path.join(parent, p), (err, r) => {
  502. assert.strictEqual(err, null)
  503. assert.strictEqual(r, path.join(parent, 'a.asar', 'file1'))
  504. done()
  505. })
  506. })
  507. it('returns real path of a linked directory', done => {
  508. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  509. const p = path.join('a.asar', 'link2', 'link2')
  510. fs.realpath.native(path.join(parent, p), (err, r) => {
  511. assert.strictEqual(err, null)
  512. assert.strictEqual(r, path.join(parent, 'a.asar', 'dir1'))
  513. done()
  514. })
  515. })
  516. it('returns real path of an unpacked file', done => {
  517. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  518. const p = path.join('unpack.asar', 'a.txt')
  519. fs.realpath.native(path.join(parent, p), (err, r) => {
  520. assert.strictEqual(err, null)
  521. assert.strictEqual(r, path.join(parent, p))
  522. done()
  523. })
  524. })
  525. it('throws ENOENT error when can not find file', done => {
  526. const parent = fs.realpathSync.native(path.join(fixtures, 'asar'))
  527. const p = path.join('a.asar', 'not-exist')
  528. fs.realpath.native(path.join(parent, p), err => {
  529. assert.strictEqual(err.code, 'ENOENT')
  530. done()
  531. })
  532. })
  533. })
  534. describe('fs.readdirSync', function () {
  535. it('reads dirs from root', function () {
  536. const p = path.join(fixtures, 'asar', 'a.asar')
  537. const dirs = fs.readdirSync(p)
  538. assert.deepStrictEqual(dirs, ['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js'])
  539. })
  540. it('reads dirs from a normal dir', function () {
  541. const p = path.join(fixtures, 'asar', 'a.asar', 'dir1')
  542. const dirs = fs.readdirSync(p)
  543. assert.deepStrictEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2'])
  544. })
  545. it('reads dirs from a linked dir', function () {
  546. const p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2')
  547. const dirs = fs.readdirSync(p)
  548. assert.deepStrictEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2'])
  549. })
  550. it('throws ENOENT error when can not find file', function () {
  551. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  552. const throws = function () {
  553. fs.readdirSync(p)
  554. }
  555. assert.throws(throws, /ENOENT/)
  556. })
  557. })
  558. describe('fs.readdir', function () {
  559. it('reads dirs from root', function (done) {
  560. const p = path.join(fixtures, 'asar', 'a.asar')
  561. fs.readdir(p, function (err, dirs) {
  562. assert.strictEqual(err, null)
  563. assert.deepStrictEqual(dirs, ['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js'])
  564. done()
  565. })
  566. })
  567. it('reads dirs from a normal dir', function (done) {
  568. const p = path.join(fixtures, 'asar', 'a.asar', 'dir1')
  569. fs.readdir(p, function (err, dirs) {
  570. assert.strictEqual(err, null)
  571. assert.deepStrictEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2'])
  572. done()
  573. })
  574. })
  575. it('reads dirs from a linked dir', function (done) {
  576. const p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2')
  577. fs.readdir(p, function (err, dirs) {
  578. assert.strictEqual(err, null)
  579. assert.deepStrictEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2'])
  580. done()
  581. })
  582. })
  583. it('throws ENOENT error when can not find file', function (done) {
  584. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  585. fs.readdir(p, function (err) {
  586. assert.strictEqual(err.code, 'ENOENT')
  587. done()
  588. })
  589. })
  590. })
  591. describe('fs.openSync', function () {
  592. it('opens a normal/linked/under-linked-directory file', function () {
  593. const ref2 = ['file1', 'link1', path.join('link2', 'file1')]
  594. for (let j = 0, len = ref2.length; j < len; j++) {
  595. const file = ref2[j]
  596. const p = path.join(fixtures, 'asar', 'a.asar', file)
  597. const fd = fs.openSync(p, 'r')
  598. const buffer = Buffer.alloc(6)
  599. fs.readSync(fd, buffer, 0, 6, 0)
  600. assert.strictEqual(String(buffer).trim(), 'file1')
  601. fs.closeSync(fd)
  602. }
  603. })
  604. it('throws ENOENT error when can not find file', function () {
  605. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  606. const throws = function () {
  607. fs.openSync(p)
  608. }
  609. assert.throws(throws, /ENOENT/)
  610. })
  611. })
  612. describe('fs.open', function () {
  613. it('opens a normal file', function (done) {
  614. const p = path.join(fixtures, 'asar', 'a.asar', 'file1')
  615. fs.open(p, 'r', function (err, fd) {
  616. assert.strictEqual(err, null)
  617. const buffer = Buffer.alloc(6)
  618. fs.read(fd, buffer, 0, 6, 0, function (err) {
  619. assert.strictEqual(err, null)
  620. assert.strictEqual(String(buffer).trim(), 'file1')
  621. fs.close(fd, done)
  622. })
  623. })
  624. })
  625. it('throws ENOENT error when can not find file', function (done) {
  626. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  627. fs.open(p, 'r', function (err) {
  628. assert.strictEqual(err.code, 'ENOENT')
  629. done()
  630. })
  631. })
  632. })
  633. describe('fs.mkdir', function () {
  634. it('throws error when calling inside asar archive', function (done) {
  635. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  636. fs.mkdir(p, function (err) {
  637. assert.strictEqual(err.code, 'ENOTDIR')
  638. done()
  639. })
  640. })
  641. })
  642. describe('fs.mkdirSync', function () {
  643. it('throws error when calling inside asar archive', function () {
  644. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  645. assert.throws(function () {
  646. fs.mkdirSync(p)
  647. }, new RegExp('ENOTDIR'))
  648. })
  649. })
  650. describe('fs.exists', function () {
  651. it('handles an existing file', function (done) {
  652. const p = path.join(fixtures, 'asar', 'a.asar', 'file1')
  653. // eslint-disable-next-line
  654. fs.exists(p, function (exists) {
  655. assert.strictEqual(exists, true)
  656. done()
  657. })
  658. })
  659. it('handles a non-existent file', function (done) {
  660. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  661. // eslint-disable-next-line
  662. fs.exists(p, function (exists) {
  663. assert.strictEqual(exists, false)
  664. done()
  665. })
  666. })
  667. it('promisified version handles an existing file', (done) => {
  668. const p = path.join(fixtures, 'asar', 'a.asar', 'file1')
  669. // eslint-disable-next-line
  670. util.promisify(fs.exists)(p).then(exists => {
  671. assert.strictEqual(exists, true)
  672. done()
  673. })
  674. })
  675. it('promisified version handles a non-existent file', function (done) {
  676. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  677. // eslint-disable-next-line
  678. util.promisify(fs.exists)(p).then(exists => {
  679. assert.strictEqual(exists, false)
  680. done()
  681. })
  682. })
  683. })
  684. describe('fs.existsSync', function () {
  685. it('handles an existing file', function () {
  686. const p = path.join(fixtures, 'asar', 'a.asar', 'file1')
  687. assert.doesNotThrow(function () {
  688. assert.strictEqual(fs.existsSync(p), true)
  689. })
  690. })
  691. it('handles a non-existent file', function () {
  692. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  693. assert.doesNotThrow(function () {
  694. assert.strictEqual(fs.existsSync(p), false)
  695. })
  696. })
  697. })
  698. describe('fs.access', function () {
  699. it('accesses a normal file', function (done) {
  700. const p = path.join(fixtures, 'asar', 'a.asar', 'file1')
  701. fs.access(p, function (err) {
  702. assert(err == null)
  703. done()
  704. })
  705. })
  706. it('throws an error when called with write mode', function (done) {
  707. const p = path.join(fixtures, 'asar', 'a.asar', 'file1')
  708. fs.access(p, fs.constants.R_OK | fs.constants.W_OK, function (err) {
  709. assert.strictEqual(err.code, 'EACCES')
  710. done()
  711. })
  712. })
  713. it('throws an error when called on non-existent file', function (done) {
  714. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  715. fs.access(p, function (err) {
  716. assert.strictEqual(err.code, 'ENOENT')
  717. done()
  718. })
  719. })
  720. it('allows write mode for unpacked files', function (done) {
  721. const p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt')
  722. fs.access(p, fs.constants.R_OK | fs.constants.W_OK, function (err) {
  723. assert(err == null)
  724. done()
  725. })
  726. })
  727. })
  728. describe('fs.accessSync', function () {
  729. it('accesses a normal file', function () {
  730. const p = path.join(fixtures, 'asar', 'a.asar', 'file1')
  731. assert.doesNotThrow(function () {
  732. fs.accessSync(p)
  733. })
  734. })
  735. it('throws an error when called with write mode', function () {
  736. const p = path.join(fixtures, 'asar', 'a.asar', 'file1')
  737. assert.throws(function () {
  738. fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK)
  739. }, /EACCES/)
  740. })
  741. it('throws an error when called on non-existent file', function () {
  742. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  743. assert.throws(function () {
  744. fs.accessSync(p)
  745. }, /ENOENT/)
  746. })
  747. it('allows write mode for unpacked files', function () {
  748. const p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt')
  749. assert.doesNotThrow(function () {
  750. fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK)
  751. })
  752. })
  753. })
  754. describe('child_process.fork', function () {
  755. before(function () {
  756. if (!features.isRunAsNodeEnabled()) {
  757. this.skip()
  758. }
  759. })
  760. it('opens a normal js file', function (done) {
  761. const child = ChildProcess.fork(path.join(fixtures, 'asar', 'a.asar', 'ping.js'))
  762. child.on('message', function (msg) {
  763. assert.strictEqual(msg, 'message')
  764. done()
  765. })
  766. child.send('message')
  767. })
  768. it('supports asar in the forked js', function (done) {
  769. const file = path.join(fixtures, 'asar', 'a.asar', 'file1')
  770. const child = ChildProcess.fork(path.join(fixtures, 'module', 'asar.js'))
  771. child.on('message', function (content) {
  772. assert.strictEqual(content, fs.readFileSync(file).toString())
  773. done()
  774. })
  775. child.send(file)
  776. })
  777. })
  778. describe('child_process.exec', function () {
  779. const echo = path.join(fixtures, 'asar', 'echo.asar', 'echo')
  780. it('should not try to extract the command if there is a reference to a file inside an .asar', function (done) {
  781. ChildProcess.exec('echo ' + echo + ' foo bar', function (error, stdout) {
  782. assert.strictEqual(error, null)
  783. assert.strictEqual(stdout.toString().replace(/\r/g, ''), echo + ' foo bar\n')
  784. done()
  785. })
  786. })
  787. it('can be promisified', () => {
  788. return util.promisify(ChildProcess.exec)('echo ' + echo + ' foo bar').then(({ stdout }) => {
  789. assert.strictEqual(stdout.toString().replace(/\r/g, ''), echo + ' foo bar\n')
  790. })
  791. })
  792. })
  793. describe('child_process.execSync', function () {
  794. const echo = path.join(fixtures, 'asar', 'echo.asar', 'echo')
  795. it('should not try to extract the command if there is a reference to a file inside an .asar', function (done) {
  796. const stdout = ChildProcess.execSync('echo ' + echo + ' foo bar')
  797. assert.strictEqual(stdout.toString().replace(/\r/g, ''), echo + ' foo bar\n')
  798. done()
  799. })
  800. })
  801. describe('child_process.execFile', function () {
  802. const execFile = ChildProcess.execFile
  803. const execFileSync = ChildProcess.execFileSync
  804. const echo = path.join(fixtures, 'asar', 'echo.asar', 'echo')
  805. before(function () {
  806. if (process.platform !== 'darwin') {
  807. this.skip()
  808. }
  809. })
  810. it('executes binaries', function (done) {
  811. execFile(echo, ['test'], function (error, stdout) {
  812. assert.strictEqual(error, null)
  813. assert.strictEqual(stdout, 'test\n')
  814. done()
  815. })
  816. })
  817. it('execFileSync executes binaries', function () {
  818. const output = execFileSync(echo, ['test'])
  819. assert.strictEqual(String(output), 'test\n')
  820. })
  821. it('can be promisified', () => {
  822. return util.promisify(ChildProcess.execFile)(echo, ['test']).then(({ stdout }) => {
  823. assert.strictEqual(stdout, 'test\n')
  824. })
  825. })
  826. })
  827. describe('internalModuleReadJSON', function () {
  828. const internalModuleReadJSON = process.binding('fs').internalModuleReadJSON
  829. it('read a normal file', function () {
  830. const file1 = path.join(fixtures, 'asar', 'a.asar', 'file1')
  831. assert.strictEqual(internalModuleReadJSON(file1).toString().trim(), 'file1')
  832. const file2 = path.join(fixtures, 'asar', 'a.asar', 'file2')
  833. assert.strictEqual(internalModuleReadJSON(file2).toString().trim(), 'file2')
  834. const file3 = path.join(fixtures, 'asar', 'a.asar', 'file3')
  835. assert.strictEqual(internalModuleReadJSON(file3).toString().trim(), 'file3')
  836. })
  837. it('reads a normal file with unpacked files', function () {
  838. const p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt')
  839. assert.strictEqual(internalModuleReadJSON(p).toString().trim(), 'a')
  840. })
  841. })
  842. describe('util.promisify', function () {
  843. it('can promisify all fs functions', function () {
  844. const originalFs = require('original-fs')
  845. const { hasOwnProperty } = Object.prototype
  846. for (const [propertyName, originalValue] of Object.entries(originalFs)) {
  847. // Some properties exist but have a value of `undefined` on some platforms.
  848. // E.g. `fs.lchmod`, which in only available on MacOS, see
  849. // https://nodejs.org/docs/latest-v10.x/api/fs.html#fs_fs_lchmod_path_mode_callback
  850. // Also check for `null`s, `hasOwnProperty()` can't handle them.
  851. if (typeof originalValue === 'undefined' || originalValue === null) continue
  852. if (hasOwnProperty.call(originalValue, util.promisify.custom)) {
  853. expect(fs).to.have.own.property(propertyName)
  854. .that.has.own.property(util.promisify.custom)
  855. }
  856. }
  857. })
  858. })
  859. describe('process.noAsar', function () {
  860. const errorName = process.platform === 'win32' ? 'ENOENT' : 'ENOTDIR'
  861. beforeEach(function () {
  862. process.noAsar = true
  863. })
  864. afterEach(function () {
  865. process.noAsar = false
  866. })
  867. it('disables asar support in sync API', function () {
  868. const file = path.join(fixtures, 'asar', 'a.asar', 'file1')
  869. const dir = path.join(fixtures, 'asar', 'a.asar', 'dir1')
  870. assert.throws(function () {
  871. fs.readFileSync(file)
  872. }, new RegExp(errorName))
  873. assert.throws(function () {
  874. fs.lstatSync(file)
  875. }, new RegExp(errorName))
  876. assert.throws(function () {
  877. fs.realpathSync(file)
  878. }, new RegExp(errorName))
  879. assert.throws(function () {
  880. fs.readdirSync(dir)
  881. }, new RegExp(errorName))
  882. })
  883. it('disables asar support in async API', function (done) {
  884. const file = path.join(fixtures, 'asar', 'a.asar', 'file1')
  885. const dir = path.join(fixtures, 'asar', 'a.asar', 'dir1')
  886. fs.readFile(file, function (error) {
  887. assert.strictEqual(error.code, errorName)
  888. fs.lstat(file, function (error) {
  889. assert.strictEqual(error.code, errorName)
  890. fs.realpath(file, function (error) {
  891. assert.strictEqual(error.code, errorName)
  892. fs.readdir(dir, function (error) {
  893. assert.strictEqual(error.code, errorName)
  894. done()
  895. })
  896. })
  897. })
  898. })
  899. })
  900. it('treats *.asar as normal file', function () {
  901. const originalFs = require('original-fs')
  902. const asar = path.join(fixtures, 'asar', 'a.asar')
  903. const content1 = fs.readFileSync(asar)
  904. const content2 = originalFs.readFileSync(asar)
  905. assert.strictEqual(content1.compare(content2), 0)
  906. assert.throws(function () {
  907. fs.readdirSync(asar)
  908. }, /ENOTDIR/)
  909. })
  910. it('is reset to its original value when execSync throws an error', function () {
  911. process.noAsar = false
  912. assert.throws(function () {
  913. ChildProcess.execSync(path.join(__dirname, 'does-not-exist.txt'))
  914. })
  915. assert.strictEqual(process.noAsar, false)
  916. })
  917. })
  918. describe('process.env.ELECTRON_NO_ASAR', function () {
  919. before(function () {
  920. if (!features.isRunAsNodeEnabled()) {
  921. this.skip()
  922. }
  923. })
  924. it('disables asar support in forked processes', function (done) {
  925. const forked = ChildProcess.fork(path.join(__dirname, 'fixtures', 'module', 'no-asar.js'), [], {
  926. env: {
  927. ELECTRON_NO_ASAR: true
  928. }
  929. })
  930. forked.on('message', function (stats) {
  931. assert.strictEqual(stats.isFile, true)
  932. assert.strictEqual(stats.size, 778)
  933. done()
  934. })
  935. })
  936. it('disables asar support in spawned processes', function (done) {
  937. const spawned = ChildProcess.spawn(process.execPath, [path.join(__dirname, 'fixtures', 'module', 'no-asar.js')], {
  938. env: {
  939. ELECTRON_NO_ASAR: true,
  940. ELECTRON_RUN_AS_NODE: true
  941. }
  942. })
  943. let output = ''
  944. spawned.stdout.on('data', function (data) {
  945. output += data
  946. })
  947. spawned.stdout.on('close', function () {
  948. const stats = JSON.parse(output)
  949. assert.strictEqual(stats.isFile, true)
  950. assert.strictEqual(stats.size, 778)
  951. done()
  952. })
  953. })
  954. })
  955. })
  956. describe('asar protocol', function () {
  957. let w = null
  958. afterEach(function () {
  959. return closeWindow(w).then(function () { w = null })
  960. })
  961. it('can request a file in package', function (done) {
  962. const p = path.resolve(fixtures, 'asar', 'a.asar', 'file1')
  963. $.get('file://' + p, function (data) {
  964. assert.strictEqual(data.trim(), 'file1')
  965. done()
  966. })
  967. })
  968. it('can request a file in package with unpacked files', function (done) {
  969. const p = path.resolve(fixtures, 'asar', 'unpack.asar', 'a.txt')
  970. $.get('file://' + p, function (data) {
  971. assert.strictEqual(data.trim(), 'a')
  972. done()
  973. })
  974. })
  975. it('can request a linked file in package', function (done) {
  976. const p = path.resolve(fixtures, 'asar', 'a.asar', 'link2', 'link1')
  977. $.get('file://' + p, function (data) {
  978. assert.strictEqual(data.trim(), 'file1')
  979. done()
  980. })
  981. })
  982. it('can request a file in filesystem', function (done) {
  983. const p = path.resolve(fixtures, 'asar', 'file')
  984. $.get('file://' + p, function (data) {
  985. assert.strictEqual(data.trim(), 'file')
  986. done()
  987. })
  988. })
  989. it('gets 404 when file is not found', function (done) {
  990. const p = path.resolve(fixtures, 'asar', 'a.asar', 'no-exist')
  991. $.ajax({
  992. url: 'file://' + p,
  993. error: function (err) {
  994. assert.strictEqual(err.status, 404)
  995. done()
  996. }
  997. })
  998. })
  999. it('sets __dirname correctly', function (done) {
  1000. after(function () {
  1001. ipcMain.removeAllListeners('dirname')
  1002. })
  1003. w = new BrowserWindow({
  1004. show: false,
  1005. width: 400,
  1006. height: 400
  1007. })
  1008. const p = path.resolve(fixtures, 'asar', 'web.asar', 'index.html')
  1009. ipcMain.once('dirname', function (event, dirname) {
  1010. assert.strictEqual(dirname, path.dirname(p))
  1011. done()
  1012. })
  1013. w.loadFile(p)
  1014. })
  1015. it('loads script tag in html', function (done) {
  1016. after(function () {
  1017. ipcMain.removeAllListeners('ping')
  1018. })
  1019. w = new BrowserWindow({
  1020. show: false,
  1021. width: 400,
  1022. height: 400
  1023. })
  1024. const p = path.resolve(fixtures, 'asar', 'script.asar', 'index.html')
  1025. w.loadFile(p)
  1026. ipcMain.once('ping', function (event, message) {
  1027. assert.strictEqual(message, 'pong')
  1028. done()
  1029. })
  1030. })
  1031. it('loads video tag in html', function (done) {
  1032. this.timeout(60000)
  1033. after(function () {
  1034. ipcMain.removeAllListeners('asar-video')
  1035. })
  1036. w = new BrowserWindow({
  1037. show: false,
  1038. width: 400,
  1039. height: 400
  1040. })
  1041. const p = path.resolve(fixtures, 'asar', 'video.asar', 'index.html')
  1042. w.loadFile(p)
  1043. ipcMain.on('asar-video', function (event, message, error) {
  1044. if (message === 'ended') {
  1045. assert(!error)
  1046. done()
  1047. } else if (message === 'error') {
  1048. done(error)
  1049. }
  1050. })
  1051. })
  1052. })
  1053. describe('original-fs module', function () {
  1054. const originalFs = require('original-fs')
  1055. it('treats .asar as file', function () {
  1056. const file = path.join(fixtures, 'asar', 'a.asar')
  1057. const stats = originalFs.statSync(file)
  1058. assert(stats.isFile())
  1059. })
  1060. it('is available in forked scripts', function (done) {
  1061. if (!features.isRunAsNodeEnabled()) {
  1062. this.skip()
  1063. done()
  1064. }
  1065. const child = ChildProcess.fork(path.join(fixtures, 'module', 'original-fs.js'))
  1066. child.on('message', function (msg) {
  1067. assert.strictEqual(msg, 'object')
  1068. done()
  1069. })
  1070. child.send('message')
  1071. })
  1072. })
  1073. describe('graceful-fs module', function () {
  1074. const gfs = require('graceful-fs')
  1075. it('recognize asar archvies', function () {
  1076. const p = path.join(fixtures, 'asar', 'a.asar', 'link1')
  1077. assert.strictEqual(gfs.readFileSync(p).toString().trim(), 'file1')
  1078. })
  1079. it('does not touch global fs object', function () {
  1080. assert.notStrictEqual(fs.readdir, gfs.readdir)
  1081. })
  1082. })
  1083. describe('mkdirp module', function () {
  1084. const mkdirp = require('mkdirp')
  1085. it('throws error when calling inside asar archive', function () {
  1086. const p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
  1087. assert.throws(function () {
  1088. mkdirp.sync(p)
  1089. }, new RegExp('ENOTDIR'))
  1090. })
  1091. })
  1092. describe('native-image', function () {
  1093. it('reads image from asar archive', function () {
  1094. const p = path.join(fixtures, 'asar', 'logo.asar', 'logo.png')
  1095. const logo = nativeImage.createFromPath(p)
  1096. assert.deepStrictEqual(logo.getSize(), {
  1097. width: 55,
  1098. height: 55
  1099. })
  1100. })
  1101. it('reads image from asar archive with unpacked files', function () {
  1102. const p = path.join(fixtures, 'asar', 'unpack.asar', 'atom.png')
  1103. const logo = nativeImage.createFromPath(p)
  1104. assert.deepStrictEqual(logo.getSize(), {
  1105. width: 1024,
  1106. height: 1024
  1107. })
  1108. })
  1109. })
  1110. })