-
-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved workarounds for win32 #119
Closed
Closed
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
14bb35a
Stat from file to make sure it was renamed
mekwall ecbebd8
Resolve if source and destination is the same size
mekwall 92dd004
Except rename to not return an error
mekwall c2831ea
Unlink source if it's the same as destination
mekwall 5d39057
Do not unlink file
mekwall 2528177
Add fs.renameSync for win32
mekwall 4882f5d
Remove extraneous line
mekwall 6e491f5
Improve stat compare between src and dest
mekwall 4ddb118
Ignore ENOENT for src on retry
mekwall f7f1d95
Wait until dest exist and src does not
mekwall b564fd9
Revamped rename tests
mekwall 3c4e270
Remove sneaky arrow functions
mekwall 625f52b
Updated comments and formatting
mekwall 1b41578
Assume rename was successful if source no longer exists
mekwall 641fa52
Make sure both statFrom and statTo are set
mekwall 516fa6c
Add additional comment about size/ctime comparison
mekwall 78f7a5b
Unlink target it if exists
mekwall a10aa57
Add test for renaming dirs
mekwall File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,72 @@ | ||
process.env.GRACEFUL_FS_PLATFORM = 'win32' | ||
|
||
var fs = require('fs') | ||
fs.rename = function (a, b, cb) { | ||
setTimeout(function () { | ||
var er = new Error('EPERM blerg') | ||
er.code = 'EPERM' | ||
cb(er) | ||
}) | ||
} | ||
|
||
var gfs = require('../') | ||
var t = require('tap') | ||
var a = __dirname + '/a' | ||
var b = __dirname + '/b' | ||
var tmpDir = __dirname + '/tmp' | ||
var testFiles = []; | ||
var id = 0; | ||
|
||
t.test('setup', function (t) { | ||
try { fs.mkdirSync(a) } catch (e) {} | ||
try { fs.mkdirSync(b) } catch (e) {} | ||
function anyFileExists (files) { | ||
var exists = false; | ||
for (var i = 0, len = files.length; i < len; i++) { | ||
if (fs.existsSync(files[i])) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
t.test('setup async', function (t) { | ||
try { fs.mkdirSync(tmpDir) } catch (e) {} | ||
for (var i = 0; i < 500; i++) { | ||
var testFile = tmpDir + '/test-' + id++ | ||
fs.writeFileSync(testFile, id) | ||
testFiles.push(testFile) | ||
} | ||
t.end() | ||
}) | ||
|
||
t.test('rename', { timeout: 100 }, function (t) { | ||
t.plan(1) | ||
t.test('rename async', { timeout: 60000 }, function (t) { | ||
t.plan(testFiles.length * 2) | ||
var dest = tmpDir + '/test' | ||
testFiles.forEach((src) => { | ||
gfs.rename(src, dest, function (er) { | ||
t.error(er, 'Failed to rename file', er) | ||
t.notOk(fs.existsSync(src), 'Source file still exists:' + src) | ||
}) | ||
}) | ||
}) | ||
|
||
gfs.rename(a, b, function (er) { | ||
t.ok(er) | ||
t.test('setup sync', function (t) { | ||
try { fs.mkdirSync(tmpDir) } catch (e) {} | ||
testFiles = [] | ||
for (var i = 0; i < 100; i++) { | ||
var testFile = tmpDir + '/test-' + id++ | ||
fs.writeFileSync(testFile, id) | ||
testFiles.push(testFile) | ||
} | ||
t.end() | ||
}) | ||
|
||
t.test('rename sync', { timeout: 60000 }, function (t) { | ||
t.plan((testFiles.length * 2) + 1) | ||
var done = 0; | ||
var errors = 0; | ||
var dest = tmpDir + '/test' | ||
testFiles.forEach((src) => { | ||
var srcData = fs.readFileSync(src).toString() | ||
t.doesNotThrow(function () { gfs.renameSync(src, dest) }, 'Exception thrown when renaming') | ||
var destData = fs.readFileSync(dest).toString() | ||
t.equal(srcData, destData, 'Data between source and destination differs: ' + srcData + ' !== ' + destData) | ||
}) | ||
t.notOk(anyFileExists(testFiles), 'Some source files still exist') | ||
}) | ||
|
||
t.test('cleanup', function (t) { | ||
try { fs.rmdirSync(a) } catch (e) {} | ||
try { fs.rmdirSync(b) } catch (e) {} | ||
testFiles.forEach(function (file) { | ||
try { fs.removeSync(file) } catch (e) {} | ||
}) | ||
try { fs.removeSync(tmpDir + '/test') } catch (e) {} | ||
try { fs.rmdirSync(tmpDir) } catch (e) {} | ||
t.end() | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly the worst hack ever, but it seems to be the only real way to solve inconsistent behavior on Windows where the destination and source can exist at the same time.