Skip to content

Commit

Permalink
Merge pull request #364 from JPeer264/feature/refactor-es6
Browse files Browse the repository at this point in the history
Refactor tests copy/copy-sync to ES6
  • Loading branch information
jprichardson authored Feb 21, 2017
2 parents 9fc96fd + 1ad32b3 commit 5ff2fc8
Show file tree
Hide file tree
Showing 15 changed files with 539 additions and 535 deletions.
30 changes: 15 additions & 15 deletions lib/__tests__/fs-integration.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
var assert = require('assert')
var path = require('path')
var os = require('os')
var fs = require('fs')
var fse = require('../')
'use strict'

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

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

describe('native fs', function () {
var TEST_DIR
describe('native fs', () => {
let TEST_DIR

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

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

it('should use native fs methods', function () {
var file = path.join(TEST_DIR, 'write.txt')
it('should use native fs methods', () => {
const file = path.join(TEST_DIR, 'write.txt')
fse.writeFileSync(file, 'hello')
var data = fse.readFileSync(file, 'utf8')
const data = fse.readFileSync(file, 'utf8')
assert.equal(data, 'hello')
})

it('should have native fs constants', function () {
it('should have native fs constants', () => {
// Node.js v0.12 / IO.js
if ('F_OK' in fs) {
assert.equal(fse.F_OK, fs.F_OK)
Expand Down
54 changes: 25 additions & 29 deletions lib/copy-sync/__tests__/broken-symlink.test.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,48 @@
var assert = require('assert')
var fs = require('fs')
var path = require('path')
var os = require('os')
var fse = require(process.cwd())
var copySync = require('../copy-sync')
'use strict'

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const path = require('path')
const assert = require('assert')
const copySync = require('../copy-sync')

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

describe('copy-sync / broken symlink', function () {
var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-broken-symlinks')
var src = path.join(TEST_DIR, 'src')
var out = path.join(TEST_DIR, 'out')
describe('copy-sync / broken symlink', () => {
const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-broken-symlinks')
const src = path.join(TEST_DIR, 'src')
const out = path.join(TEST_DIR, 'out')

beforeEach(function (done) {
fse.emptyDir(TEST_DIR, function (err) {
beforeEach(done => {
fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
createFixtures(src, done)
})
})

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

it('should copy broken symlinks by default', function () {
assert.doesNotThrow(function () {
copySync(src, out)
})
afterEach(done => fse.remove(TEST_DIR, done))

it('should copy broken symlinks by default', () => {
assert.doesNotThrow(() => copySync(src, out))
assert.equal(fs.readlinkSync(path.join(out, 'broken-symlink')), path.join(src, 'does-not-exist'))
})

it('should throw an error when dereference=true', function () {
assert.throws(function () {
copySync(src, out, {dereference: true})
}, function (err) {
return err.code === 'ENOENT'
})
it('should throw an error when dereference=true', () => {
assert.throws(() => copySync(src, out, {dereference: true}), err => err.code === 'ENOENT')
})
})

function createFixtures (srcDir, callback) {
fs.mkdir(srcDir, function (err) {
fs.mkdir(srcDir, err => {
let brokenFile
let brokenFileLink

if (err) return callback(err)

try {
var brokenFile = path.join(srcDir, 'does-not-exist')
var brokenFileLink = path.join(srcDir, 'broken-symlink')
brokenFile = path.join(srcDir, 'does-not-exist')
brokenFileLink = path.join(srcDir, 'broken-symlink')
fs.writeFileSync(brokenFile, 'does not matter')
fs.symlinkSync(brokenFile, brokenFileLink, 'file')
} catch (err) {
Expand Down
98 changes: 48 additions & 50 deletions lib/copy-sync/__tests__/copy-sync-dir.test.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
var assert = require('assert')
var crypto = require('crypto')
var os = require('os')
var path = require('path')
var fs = require(process.cwd())
'use strict'

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

/* global beforeEach, describe, it */

describe('+ copySync()', function () {
var TEST_DIR
var SIZE = 16 * 64 * 1024 + 7
var src, dest
describe('+ copySync()', () => {
const SIZE = 16 * 64 * 1024 + 7
let TEST_DIR
let src, dest

beforeEach(function (done) {
beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-dir')
src = path.join(TEST_DIR, 'src')
dest = path.join(TEST_DIR, 'dest')
fs.emptyDir(TEST_DIR, done)
})

describe('> when the source is a directory', function () {
it('should copy the directory synchronously', function () {
var FILES = 2
var i, j
var src = path.join(TEST_DIR, 'src')
var dest = path.join(TEST_DIR, 'dest')
describe('> when the source is a directory', () => {
it('should copy the directory synchronously', () => {
const FILES = 2

src = path.join(TEST_DIR, 'src')
dest = path.join(TEST_DIR, 'dest')

fs.mkdirsSync(src)

for (i = 0; i < FILES; ++i) {
for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE))
}

var subdir = path.join(src, 'subdir')
const subdir = path.join(src, 'subdir')

fs.mkdirsSync(subdir)

for (i = 0; i < FILES; ++i) {
for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(subdir, i.toString()), crypto.randomBytes(SIZE))
}

fs.copySync(src, dest)
assert(fs.existsSync(dest))

for (i = 0; i < FILES; ++i) {
for (let i = 0; i < FILES; ++i) {
assert(fs.existsSync(path.join(dest, i.toString())))
}

var destSub = path.join(dest, 'subdir')
for (j = 0; j < FILES; ++j) {
const destSub = path.join(dest, 'subdir')
for (let j = 0; j < FILES; ++j) {
assert(fs.existsSync(path.join(destSub, j.toString())))
}
})

it('should preserve symbolic links', function () {
it('should preserve symbolic links', () => {
fs.mkdirsSync(src)
fs.symlinkSync('destination', path.join(src, 'symlink'))

fs.copySync(src, dest)

var link = fs.readlinkSync(path.join(dest, 'symlink'))
const link = fs.readlinkSync(path.join(dest, 'symlink'))
assert.strictEqual(link, 'destination')
})

it('should should apply filter recursively', function () {
var FILES = 2
var filter = function (s) {
// Don't match anything that ends with a digit higher than 0:
return /(0|\D)$/i.test(s)
}
it('should should apply filter recursively', () => {
const FILES = 2
// Don't match anything that ends with a digit higher than 0:
const filter = s => /(0|\D)$/i.test(s)

fs.mkdirsSync(src)

for (var i = 0; i < FILES; ++i) {
for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE))
}

var subdir = path.join(src, 'subdir')
const subdir = path.join(src, 'subdir')
fs.mkdirsSync(subdir)

for (i = 0; i < FILES; ++i) {
for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(subdir, i.toString()), crypto.randomBytes(SIZE))
}

Expand All @@ -87,17 +87,17 @@ describe('+ copySync()', function () {
assert(fs.existsSync(dest))
assert(FILES > 1)

for (i = 0; i < FILES; ++i) {
for (let i = 0; i < FILES; ++i) {
if (i === 0) {
assert(fs.existsSync(path.join(dest, i.toString())))
} else {
assert(!fs.existsSync(path.join(dest, i.toString())))
}
}

var destSub = path.join(dest, 'subdir')
const destSub = path.join(dest, 'subdir')

for (var j = 0; j < FILES; ++j) {
for (let j = 0; j < FILES; ++j) {
if (j === 0) {
assert(fs.existsSync(path.join(destSub, j.toString())))
} else {
Expand All @@ -106,15 +106,13 @@ describe('+ copySync()', function () {
}
})

it('should apply the filter to directory names', function () {
var IGNORE = 'ignore'
var filter = function (p) {
return !~p.indexOf(IGNORE)
}
it('should apply the filter to directory names', () => {
const IGNORE = 'ignore'
const filter = p => !~p.indexOf(IGNORE)

fs.mkdirsSync(src)

var ignoreDir = path.join(src, IGNORE)
const ignoreDir = path.join(src, IGNORE)
fs.mkdirsSync(ignoreDir)

fs.writeFileSync(path.join(ignoreDir, 'file'), crypto.randomBytes(SIZE))
Expand All @@ -125,23 +123,23 @@ describe('+ copySync()', function () {
assert(!fs.existsSync(path.join(dest, IGNORE, 'file')), 'file was not ignored')
})

describe('> when the destination dir does not exist', function () {
it('should create the destination directory and copy the file', function () {
var src = path.join(TEST_DIR, 'data/')
describe('> when the destination dir does not exist', () => {
it('should create the destination directory and copy the file', () => {
const src = path.join(TEST_DIR, 'data/')
fs.mkdirSync(src)

var d1 = 'file1'
var d2 = 'file2'
const d1 = 'file1'
const d2 = 'file2'

fs.writeFileSync(path.join(src, 'f1.txt'), d1)
fs.writeFileSync(path.join(src, 'f2.txt'), d2)

var dest = path.join(TEST_DIR, 'this/path/does/not/exist/outputDir')
const dest = path.join(TEST_DIR, 'this/path/does/not/exist/outputDir')

fs.copySync(src, dest)

var o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8')
var o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8')
const o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8')
const o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8')

assert.strictEqual(d1, o1)
assert.strictEqual(d2, o2)
Expand Down
Loading

0 comments on commit 5ff2fc8

Please sign in to comment.