Skip to content

Commit

Permalink
add test for downloading empty file to fs
Browse files Browse the repository at this point in the history
  • Loading branch information
joehand committed Jun 17, 2016
1 parent d954586 commit 5ecb112
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"devDependencies": {
"concat-stream": "^1.5.1",
"memdb": "^1.3.1",
"mkdirp": "^0.5.1",
"random-access-file": "^1.2.0",
"standard": "^5.4.1",
"tape": "^4.4.0"
Expand Down
84 changes: 82 additions & 2 deletions test/replicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var tape = require('tape')
var memdb = require('memdb')
var path = require('path')
var fs = require('fs')
var os = require('os')
var raf = require('random-access-file')
var hyperdrive = require('../')

Expand Down Expand Up @@ -51,8 +52,7 @@ tape('replicates empty files', function (t) {

var archive = drive.createArchive()

var ws = archive.createFileWriteStream('empty.txt')
ws.end()
archive.createFileWriteStream('empty.txt').end()

archive.finalize(function (err) {
t.error(err, 'no error')
Expand Down Expand Up @@ -111,3 +111,83 @@ tape('replicates empty directories', function (t) {
stream.pipe(streamClone).pipe(stream)
})
})

tape('downloads empty files to fs', function (t) {
var drive = hyperdrive(memdb())
var driveClone = hyperdrive(memdb())
var tmp = path.join(os.tmpdir(), 'hyperdrive-test')

var archive = drive.createArchive()

archive.createFileWriteStream('empty.txt').end()

archive.finalize(function (err) {
t.error(err, 'no error')

var clone = driveClone.createArchive(archive.key, {
file: function (name) {
return raf(path.join(tmp, name))
}
})

clone.get(0, function (err, entry) {
t.error(err, 'no error')
t.same(entry.name, 'empty.txt')
t.same(entry.length, 0, 'empty')

clone.download(entry, function (err) {
var fileList = fs.readdirSync(tmp).join(' ')
t.ok(fileList.indexOf('empty.txt') > -1, 'has empty file')
t.error(err, 'no error')
t.end()
})
})

var stream = archive.replicate()
var streamClone = clone.replicate()

stream.pipe(streamClone).pipe(stream)
})
})

tape('downloads empty directories to fs', function (t) {
var drive = hyperdrive(memdb())
var driveClone = hyperdrive(memdb())
var tmp = path.join(os.tmpdir(), 'hyperdrive-test-2')

var archive = drive.createArchive()

archive.append({
type: 'directory',
name: 'a-dir'
})

archive.finalize(function (err) {
t.error(err, 'no error')

var clone = driveClone.createArchive(archive.key, {
file: function (name) {
return raf(path.join(tmp, name))
}
})

clone.get(0, function (err, entry) {
t.error(err, 'no error')
t.same(entry.type, 'directory')
t.same(entry.name, 'a-dir')
t.same(entry.length, 0, 'empty')

clone.download(0, function (err) {
var fileList = fs.readdirSync(tmp).join(' ')
t.ok(fileList.indexOf('a-dir') > -1, 'has empty dir')
t.error(err, 'no error')
t.end()
})
})

var stream = archive.replicate()
var streamClone = clone.replicate()

stream.pipe(streamClone).pipe(stream)
})
})

0 comments on commit 5ecb112

Please sign in to comment.