Skip to content

Commit

Permalink
Cleanup lib/util/ to remove unused code (#757)
Browse files Browse the repository at this point in the history
Move function only used in tests to test files to avoid bundling it
  • Loading branch information
RyanZim committed Feb 7, 2020
1 parent 3120c5c commit 9bfd380
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 92 deletions.
42 changes: 15 additions & 27 deletions lib/util/__tests__/utimes.test.js
Expand Up @@ -11,6 +11,20 @@ let utimes

/* global beforeEach, describe, it */

// HFS, ext{2,3}, FAT do not
function hasMillisResSync () {
let tmpfile = path.join('millis-test-sync' + Date.now().toString() + Math.random().toString().slice(2))
tmpfile = path.join(os.tmpdir(), tmpfile)

// 550 millis past UNIX epoch
const d = new Date(1435410243862)
fs.writeFileSync(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141')
const fd = fs.openSync(tmpfile, 'r+')
fs.futimesSync(fd, d, d)
fs.closeSync(fd)
return fs.statSync(tmpfile).mtime > 1435410243000
}

describe('utimes', () => {
let TEST_DIR

Expand All @@ -22,32 +36,6 @@ describe('utimes', () => {
utimes = proxyquire('../utimes', { 'graceful-fs': gracefulFsStub })
})

describe('hasMillisResSync()', () => {
it('should return a boolean indicating whether it has support', () => {
const res = utimes.hasMillisResSync()
assert.strictEqual(typeof res, 'boolean')

// HFS => false
if (process.platform === 'darwin') assert.strictEqual(res, false)

// does anyone use FAT anymore?
// if (process.platform === 'win32') assert.strictEqual(res, true)
// fails on appveyor... could appveyor be using FAT?

// this would fail if ext2/ext3
if (process.platform === 'linux') assert.strictEqual(res, true)
})
})

describe('timeRemoveMills()', () => {
it('should remove millisecond precision from a timestamp', () => {
const ts = 1334990868773
const ets = 1334990868000
assert.strictEqual(utimes.timeRemoveMillis(ts), ets)
assert.strictEqual(utimes.timeRemoveMillis(new Date(ts)).getTime(), ets)
})
})

describe('utimesMillis()', () => {
// see discussion https://github.com/jprichardson/node-fs-extra/pull/141
it('should set the utimes w/ millisecond precision', done => {
Expand All @@ -66,7 +54,7 @@ describe('utimes', () => {
utimes.utimesMillis(tmpFile, awhileAgo, awhileAgo, err => {
assert.ifError(err)
stats = fs.statSync(tmpFile)
if (utimes.hasMillisResSync()) {
if (hasMillisResSync()) {
assert.deepStrictEqual(stats.mtime, awhileAgo)
assert.deepStrictEqual(stats.atime, awhileAgo)
} else {
Expand Down
12 changes: 0 additions & 12 deletions lib/util/buffer.js

This file was deleted.

53 changes: 0 additions & 53 deletions lib/util/utimes.js
@@ -1,56 +1,6 @@
'use strict'

const fs = require('graceful-fs')
const os = require('os')
const path = require('path')

// HFS, ext{2,3}, FAT do not
function hasMillisResSync () {
let tmpfile = path.join('millis-test-sync' + Date.now().toString() + Math.random().toString().slice(2))
tmpfile = path.join(os.tmpdir(), tmpfile)

// 550 millis past UNIX epoch
const d = new Date(1435410243862)
fs.writeFileSync(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141')
const fd = fs.openSync(tmpfile, 'r+')
fs.futimesSync(fd, d, d)
fs.closeSync(fd)
return fs.statSync(tmpfile).mtime > 1435410243000
}

function hasMillisRes (callback) {
let tmpfile = path.join('millis-test' + Date.now().toString() + Math.random().toString().slice(2))
tmpfile = path.join(os.tmpdir(), tmpfile)

// 550 millis past UNIX epoch
const d = new Date(1435410243862)
fs.writeFile(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141', err => {
if (err) return callback(err)
fs.open(tmpfile, 'r+', (err, fd) => {
if (err) return callback(err)
fs.futimes(fd, d, d, err => {
if (err) return callback(err)
fs.close(fd, err => {
if (err) return callback(err)
fs.stat(tmpfile, (err, stats) => {
if (err) return callback(err)
callback(null, stats.mtime > 1435410243000)
})
})
})
})
})
}

function timeRemoveMillis (timestamp) {
if (typeof timestamp === 'number') {
return Math.floor(timestamp / 1000) * 1000
} else if (timestamp instanceof Date) {
return new Date(Math.floor(timestamp.getTime() / 1000) * 1000)
} else {
throw new Error('fs-extra: timeRemoveMillis() unknown parameter type')
}
}

function utimesMillis (path, atime, mtime, callback) {
// if (!HAS_MILLIS_RES) return fs.utimes(path, atime, mtime, callback)
Expand All @@ -71,9 +21,6 @@ function utimesMillisSync (path, atime, mtime) {
}

module.exports = {
hasMillisRes,
hasMillisResSync,
timeRemoveMillis,
utimesMillis,
utimesMillisSync
}

0 comments on commit 9bfd380

Please sign in to comment.