fix: support ASAR in fs.copyFile (#14942)

This commit is contained in:
Cheng Zhao 2018-10-04 01:36:20 +09:00 committed by Shelley Vohr
parent 820d99d37f
commit 4c4d8c5bfa
2 changed files with 45 additions and 0 deletions

View file

@ -748,6 +748,12 @@
}
}
// Strictly implementing the flags of fs.copyFile is hard, just do a simple
// implementation for now. Doing 2 copies won't spend much time more as OS
// has filesystem caching.
overrideAPI(fs, 'copyFile')
overrideAPISync(fs, 'copyFileSync')
overrideAPI(fs, 'open')
overrideAPI(childProcess, 'execFile')
overrideAPISync(process, 'dlopen', 1)

View file

@ -3,6 +3,7 @@ const ChildProcess = require('child_process')
const { expect } = require('chai')
const fs = require('fs')
const path = require('path')
const temp = require('temp').track()
const util = require('util')
const { closeWindow } = require('./window-helpers')
@ -137,6 +138,44 @@ describe('asar package', function () {
})
})
describe('fs.copyFile', function () {
it('copies a normal file', function (done) {
const p = path.join(fixtures, 'asar', 'a.asar', 'file1')
const dest = temp.path()
fs.copyFile(p, dest, function (err) {
assert.strictEqual(err, null)
assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
done()
})
})
it('copies a unpacked file', function (done) {
const p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt')
const dest = temp.path()
fs.copyFile(p, dest, function (err) {
assert.strictEqual(err, null)
assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
done()
})
})
})
describe('fs.copyFileSync', function () {
it('copies a normal file', function () {
const p = path.join(fixtures, 'asar', 'a.asar', 'file1')
const dest = temp.path()
fs.copyFileSync(p, dest)
assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
})
it('copies a unpacked file', function () {
const p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt')
const dest = temp.path()
fs.copyFileSync(p, dest)
assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
})
})
describe('fs.lstatSync', function () {
it('handles path with trailing slash correctly', function () {
const p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2', 'file1')