Skip to content

Commit

Permalink
Refactor test remove (closes jprichardson#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
JPeer264 committed Feb 25, 2017
1 parent ebd2266 commit 7bddeb5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 71 deletions.
26 changes: 14 additions & 12 deletions lib/remove/__tests__/remove-dir.test.js
@@ -1,24 +1,26 @@
var assert = require('assert')
var fs = require('fs')
var path = require('path')
var os = require('os')
var fse = require(process.cwd())
'use strict'

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const path = require('path')
const assert = require('assert')

/* global beforeEach, describe, it */

describe('remove / async / dir', function () {
var TEST_DIR
describe('remove / async / dir', () => {
let TEST_DIR

beforeEach(function (done) {
beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'remove-async-dir')
fse.emptyDir(TEST_DIR, done)
})

describe('> when dir does not exist', function () {
it('should not throw an error', function (done) {
var someDir = path.join(TEST_DIR, 'some-dir/')
describe('> when dir does not exist', () => {
it('should not throw an error', done => {
const someDir = path.join(TEST_DIR, 'some-dir/')
assert.equal(fs.existsSync(someDir), false)
fse.remove(someDir, function (err) {
fse.remove(someDir, err => {
assert.ifError(err)
done()
})
Expand Down
24 changes: 13 additions & 11 deletions lib/remove/__tests__/remove-sync-dir.test.js
@@ -1,28 +1,30 @@
var assert = require('assert')
var fs = require('fs')
var os = require('os')
var path = require('path')
var fse = require(process.cwd())
'use strict'

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const path = require('path')
const assert = require('assert')

/* global beforeEach, describe, it */

describe('remove/sync', function () {
var TEST_DIR
describe('remove/sync', () => {
let TEST_DIR

beforeEach(function (done) {
beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'remove-sync')
fse.emptyDir(TEST_DIR, done)
})

describe('+ removeSync()', function () {
it('should delete directories and files synchronously', function () {
describe('+ removeSync()', () => {
it('should delete directories and files synchronously', () => {
assert(fs.existsSync(TEST_DIR))
fs.writeFileSync(path.join(TEST_DIR, 'somefile'), 'somedata')
fse.removeSync(TEST_DIR)
assert(!fs.existsSync(TEST_DIR))
})

it('should delete an empty directory synchronously', function () {
it('should delete an empty directory synchronously', () => {
assert(fs.existsSync(TEST_DIR))
fse.removeSync(TEST_DIR)
assert(!fs.existsSync(TEST_DIR))
Expand Down
24 changes: 13 additions & 11 deletions lib/remove/__tests__/remove-sync-file.test.js
@@ -1,22 +1,24 @@
var assert = require('assert')
var fs = require('fs')
var os = require('os')
var path = require('path')
var fse = require(process.cwd())
'use strict'

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const path = require('path')
const assert = require('assert')

/* global beforeEach, describe, it */

describe('remove/sync', function () {
var TEST_DIR
describe('remove/sync', () => {
let TEST_DIR

beforeEach(function (done) {
beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'remove-sync')
fse.emptyDir(TEST_DIR, done)
})

describe('+ removeSync()', function () {
it('should delete a file synchronously', function () {
var file = path.join(TEST_DIR, 'file')
describe('+ removeSync()', () => {
it('should delete a file synchronously', () => {
const file = path.join(TEST_DIR, 'file')
fs.writeFileSync(file, 'hello')
assert(fs.existsSync(file))
fse.removeSync(file)
Expand Down
72 changes: 35 additions & 37 deletions lib/remove/__tests__/remove.test.js
@@ -1,77 +1,77 @@
var assert = require('assert')
var fs = require('fs')
var os = require('os')
var path = require('path')
var sr = require('secure-random')
var fse = require(process.cwd())
'use strict'

const assert = require('assert')
const fs = require('fs')
const os = require('os')
const path = require('path')
const sr = require('secure-random')
const fse = require(process.cwd())

/* global afterEach, beforeEach, describe, it */

var TEST_DIR
let TEST_DIR

function buildFixtureDir () {
var buf = sr.randomBuffer(5)
var baseDir = path.join(TEST_DIR, 'TEST_fs-extra_remove-' + Date.now())
const buf = sr.randomBuffer(5)
const baseDir = path.join(TEST_DIR, `TEST_fs-extra_remove-${Date.now()}`)

fs.mkdirSync(baseDir)
fs.writeFileSync(path.join(baseDir, Math.random() + ''), buf)
fs.writeFileSync(path.join(baseDir, Math.random() + ''), buf)

var subDir = path.join(TEST_DIR, Math.random() + '')
const subDir = path.join(TEST_DIR, Math.random() + '')
fs.mkdirSync(subDir)
fs.writeFileSync(path.join(subDir, Math.random() + ''))
return baseDir
}

describe('remove', function () {
beforeEach(function (done) {
describe('remove', () => {
beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'remove')
fse.emptyDir(TEST_DIR, done)
})

afterEach(function (done) {
fse.remove(TEST_DIR, done)
})
afterEach(done => fse.remove(TEST_DIR, done))

describe('+ remove()', function () {
it('should delete an empty directory', function (done) {
describe('+ remove()', () => {
it('should delete an empty directory', done => {
assert(fs.existsSync(TEST_DIR))
fse.remove(TEST_DIR, function (err) {
fse.remove(TEST_DIR, err => {
assert.ifError(err)
assert(!fs.existsSync(TEST_DIR))
done()
})
})

it('should delete a directory full of directories and files', function (done) {
it('should delete a directory full of directories and files', done => {
buildFixtureDir()
assert(fs.existsSync(TEST_DIR))
fse.remove(TEST_DIR, function (err) {
fse.remove(TEST_DIR, err => {
assert.ifError(err)
assert(!fs.existsSync(TEST_DIR))
done()
})
})

it('should delete a file', function (done) {
var file = path.join(TEST_DIR, 'file')
it('should delete a file', done => {
const file = path.join(TEST_DIR, 'file')
fs.writeFileSync(file, 'hello')

assert(fs.existsSync(file))
fse.remove(file, function (err) {
fse.remove(file, err => {
assert.ifError(err)
assert(!fs.existsSync(file))
done()
})
})

it('should delete without a callback', function (done) {
var file = path.join(TEST_DIR, 'file')
it('should delete without a callback', done => {
const file = path.join(TEST_DIR, 'file')
fs.writeFileSync(file, 'hello')

assert(fs.existsSync(file))
var existsChecker = setInterval(function () {
fs.exists(file, function (itDoes) {
const existsChecker = setInterval(() => {
fs.exists(file, (itDoes) => {
if (!itDoes) {
clearInterval(existsChecker)
done()
Expand All @@ -82,38 +82,36 @@ describe('remove', function () {
})

it('shouldn’t delete glob matches', function (done) {
var file = path.join(TEST_DIR, 'file?')
const file = path.join(TEST_DIR, 'file?')
try {
fs.writeFileSync(file, 'hello')
} catch (ex) {
if (ex.code === 'ENOENT') {
return this.skip('Windows does not support filenames with ‘?’ or ‘*’ in them.')
}
if (ex.code === 'ENOENT') return this.skip('Windows does not support filenames with ‘?’ or ‘*’ in them.')
throw ex
}

var wrongFile = path.join(TEST_DIR, 'file1')
const wrongFile = path.join(TEST_DIR, 'file1')
fs.writeFileSync(wrongFile, 'yo')

assert(fs.existsSync(file))
assert(fs.existsSync(wrongFile))
fse.remove(file, function (err) {
fse.remove(file, err => {
assert.ifError(err)
assert(!fs.existsSync(file))
assert(fs.existsSync(wrongFile))
done()
})
})

it('shouldn’t delete glob matches when file doesn’t exist', function (done) {
var nonexistentFile = path.join(TEST_DIR, 'file?')
it('shouldn’t delete glob matches when file doesn’t exist', done => {
const nonexistentFile = path.join(TEST_DIR, 'file?')

var wrongFile = path.join(TEST_DIR, 'file1')
const wrongFile = path.join(TEST_DIR, 'file1')
fs.writeFileSync(wrongFile, 'yo')

assert(!fs.existsSync(nonexistentFile))
assert(fs.existsSync(wrongFile))
fse.remove(nonexistentFile, function (err) {
fse.remove(nonexistentFile, err => {
assert.ifError(err)
assert(!fs.existsSync(nonexistentFile))
assert(fs.existsSync(wrongFile))
Expand Down

0 comments on commit 7bddeb5

Please sign in to comment.