Skip to content

Commit

Permalink
handle other types of links
Browse files Browse the repository at this point in the history
  • Loading branch information
joehand committed Dec 10, 2016
1 parent 3b505f6 commit 2de81ba
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/commands/clone.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require('fs')
var stringKey = require('dat-key-as').string
var exit = require('../ui').exitErr
var download = require('../download')

Expand All @@ -24,6 +25,8 @@ function clone (opts) {

// Create the directory if it doesn't exist
// If no dir is specified, we put dat in a dir with name = key
opts.key = opts.key.replace(/\/$/, '') // dat-encoding PR #13
opts.key = stringKey(opts.key) // removes dat://
opts.dir = opts._[1] || opts.dir
if (!opts.dir || opts.dir === process.cwd()) opts.dir = opts.key // Don't allow download to cwd for now
try {
Expand Down
49 changes: 49 additions & 0 deletions tests/clone.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var path = require('path')
var test = require('tape')
var rimraf = require('rimraf')
var mkdirp = require('mkdirp')
var spawn = require('./helpers/spawn.js')
var help = require('./helpers')

Expand Down Expand Up @@ -98,6 +99,54 @@ test('clone - quiet', function (t) {
st.end()
})

test('clone - dat:// link', function (t) {
var key = 'dat://' + shareDat.key.toString('hex') + '/'
var baseDir = path.join(baseTestDir, 'dat_proto_dir')
mkdirp.sync(baseDir)
var downloadDir = path.join(baseDir, shareDat.key.toString('hex'))
var cmd = dat + ' clone ' + key
var st = spawn(t, cmd, {cwd: baseDir})
st.succeeds('exits after finishing download')
st.stdout.match(function (output) {
var downloadFinished = output.indexOf('Download Finished') > -1
if (!downloadFinished) return false

t.ok(output.indexOf(downloadDir) > -1, 'prints dir')
t.ok(help.isDir(downloadDir, 'creates download directory'))

st.kill()
return true
})
st.stderr.empty()
st.end(function () {
rimraf.sync(downloadDir)
})
})

test('clone - dat.land link', function (t) {
var key = 'dat.land/' + shareDat.key.toString('hex') + '/'
var baseDir = path.join(baseTestDir, 'dat_land_dir')
mkdirp.sync(baseDir)
var downloadDir = path.join(baseDir, shareDat.key.toString('hex'))
var cmd = dat + ' clone ' + key
var st = spawn(t, cmd, {cwd: baseDir})
st.succeeds('exits after finishing download')
st.stdout.match(function (output) {
var downloadFinished = output.indexOf('Download Finished') > -1
if (!downloadFinished) return false

t.ok(output.indexOf(downloadDir) > -1, 'prints dir')
t.ok(help.isDir(downloadDir, 'creates download directory'))

st.kill()
return true
})
st.stderr.empty()
st.end(function () {
rimraf.sync(downloadDir)
})
})

test('close sharer', function (t) {
shareDat.close(function () {
rimraf.sync(path.join(shareDat.path, '.dat'))
Expand Down

0 comments on commit 2de81ba

Please sign in to comment.