Skip to content

Commit

Permalink
refactor: replace rimraf with fs-extra
Browse files Browse the repository at this point in the history
  • Loading branch information
malept committed Mar 25, 2020
1 parent eba4001 commit d79bc4e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
},
"devDependencies": {
"ava": "^3.5.1",
"fs-extra": "^9.0.0",
"nyc": "^15.0.0",
"rimraf": "^3.0.2",
"standard": "^14.3.3"
},
"directories": {
Expand Down
44 changes: 21 additions & 23 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const extract = require('../')
const { existsSync, promises: fs } = require('fs')
const fs = require('fs-extra')
const os = require('os')
const path = require('path')
const { promisify } = require('util')
const rimraf = promisify(require('rimraf'))
const test = require('ava')

const catsZip = path.join(__dirname, 'cats.zip')
Expand All @@ -26,13 +24,13 @@ async function tempExtract (t, suffix, zipPath) {
return dirPath
}

function exists (t, pathToCheck, message) {
const exists = existsSync(pathToCheck)
async function pathExists (t, pathToCheck, message) {
const exists = await fs.pathExists(pathToCheck)
t.true(exists, message)
}

function doesntExist (t, pathToCheck, message) {
const exists = existsSync(pathToCheck)
async function pathDoesntExist (t, pathToCheck, message) {
const exists = await fs.pathExists(pathToCheck)
t.false(exists, message)
}

Expand All @@ -44,15 +42,15 @@ async function assertPermissions (t, pathToCheck, expectedMode) {

test('files', async t => {
const dirPath = await tempExtract(t, 'files', catsZip)
exists(t, path.join(dirPath, 'cats', 'gJqEYBs.jpg'), 'file created')
await pathExists(t, path.join(dirPath, 'cats', 'gJqEYBs.jpg'), 'file created')
})

test('symlinks', async t => {
const dirPath = await tempExtract(t, 'symlinks', catsZip)
const symlink = path.join(dirPath, 'cats', 'orange_symlink')

exists(t, path.join(dirPath, 'cats'), 'directory created')
exists(t, symlink, 'symlink created')
await pathExists(t, path.join(dirPath, 'cats'), 'directory created')
await pathExists(t, symlink, `symlink created: ${symlink}`)

const stats = await fs.lstat(symlink)
t.truthy(stats.isSymbolicLink(), 'symlink is valid')
Expand All @@ -65,20 +63,20 @@ test('directories', async t => {
const dirWithContent = path.join(dirPath, 'cats', 'orange')
const dirWithoutContent = path.join(dirPath, 'cats', 'empty')

exists(t, dirWithContent, 'directory created')
await pathExists(t, dirWithContent, 'directory created')

const filesWithContent = await fs.readdir(dirWithContent)
t.not(filesWithContent.length, 0, 'directory has files')

exists(t, dirWithoutContent, 'empty directory created')
await pathExists(t, dirWithoutContent, 'empty directory created')

const filesWithoutContent = await fs.readdir(dirWithoutContent)
t.is(filesWithoutContent.length, 0, 'empty directory has no files')
})

test('verify github zip extraction worked', async t => {
const dirPath = await tempExtract(t, 'verify-extraction', githubZip)
exists(t, path.join(dirPath, 'extract-zip-master', 'test'), 'folder created')
await pathExists(t, path.join(dirPath, 'extract-zip-master', 'test'), 'folder created')
if (process.platform !== 'win32') {
await assertPermissions(t, path.join(dirPath, 'extract-zip-master', 'test'), 0o755)
}
Expand All @@ -100,18 +98,18 @@ test('opts.onEntry', async t => {
})

test('relative target directory', async t => {
await rimraf(relativeTarget)
await fs.remove(relativeTarget)
await t.throwsAsync(extract(catsZip, { dir: relativeTarget }), {
message: 'Target directory is expected to be absolute'
})
doesntExist(t, path.join(__dirname, relativeTarget), 'folder not created')
await rimraf(relativeTarget)
await pathDoesntExist(t, path.join(__dirname, relativeTarget), 'folder not created')
await fs.remove(relativeTarget)
})

if (process.platform !== 'win32') {
test('symlink destination disallowed', async t => {
const dirPath = await mkdtemp(t, 'symlink-destination-disallowed')
doesntExist(t, path.join(dirPath, 'file.txt'), "file doesn't exist at symlink target")
await pathDoesntExist(t, path.join(dirPath, 'file.txt'), "file doesn't exist at symlink target")

await t.throwsAsync(extract(symlinkDestZip, { dir: dirPath }), {
message: /Out of bound path ".*?" found while processing file symlink-dest\/aaa\/file.txt/
Expand All @@ -124,11 +122,11 @@ if (process.platform !== 'win32') {

const symlinkDestDir = path.join(dirPath, 'symlink-dest')

exists(t, symlinkDestDir, 'target folder created')
exists(t, path.join(symlinkDestDir, 'aaa'), 'symlink created')
exists(t, path.join(symlinkDestDir, 'ccc'), 'parent folder created')
doesntExist(t, path.join(symlinkDestDir, 'ccc/file.txt'), 'file not created in original folder')
doesntExist(t, path.join(dirPath, 'file.txt'), 'file not created in symlink target')
await pathExists(t, symlinkDestDir, 'target folder created')
await pathExists(t, path.join(symlinkDestDir, 'aaa'), 'symlink created')
await pathExists(t, path.join(symlinkDestDir, 'ccc'), 'parent folder created')
await pathDoesntExist(t, path.join(symlinkDestDir, 'ccc/file.txt'), 'file not created in original folder')
await pathDoesntExist(t, path.join(dirPath, 'file.txt'), 'file not created in symlink target')
})

test('defaultDirMode', async t => {
Expand All @@ -154,7 +152,7 @@ if (process.platform !== 'win32') {

test('files in subdirs where the subdir does not have its own entry is extracted', async t => {
const dirPath = await tempExtract(t, 'subdir-file', subdirZip)
exists(t, path.join(dirPath, 'foo', 'bar'), 'file created')
await pathExists(t, path.join(dirPath, 'foo', 'bar'), 'file created')
})

test('extract broken zip', async t => {
Expand Down

0 comments on commit d79bc4e

Please sign in to comment.