feat(parse): only accepts a string

This commit is contained in:
Julien Fontanet 2016-06-08 14:36:10 +02:00
parent fb17de7988
commit 45a94fe73d
3 changed files with 42 additions and 56 deletions

View File

@ -32,6 +32,7 @@
"babel-preset-es2015": "^6.3.13",
"babel-preset-stage-0": "^6.3.13",
"clarify": "^1.0.5",
"deep-freeze": "0.0.1",
"dependency-check": "^2.5.1",
"ghooks": "^1.2.1",
"mocha": "^2.3.4",

View File

@ -5,19 +5,21 @@ import trimStart from 'lodash/trimStart'
const sanitizePath = (...paths) => filter(map(paths, s => s && filter(map(s.split('/'), trim)).join('/'))).join('/')
export const parse = remote => {
const [type, rest] = remote.url.split('://')
export const parse = string => {
const object = { }
const [type, rest] = string.split('://')
if (type === 'file') {
remote.type = 'local'
remote.path = `/${trimStart(rest, '/')}` // the leading slash has been forgotten on client side first implementation
object.type = 'local'
object.path = `/${trimStart(rest, '/')}` // the leading slash has been forgotten on client side first implementation
} else if (type === 'nfs') {
remote.type = 'nfs'
object.type = 'nfs'
const [host, share] = rest.split(':')
remote.path = `/tmp/xo-server/mounts/${remote.id}`
remote.host = host
remote.share = share
object.path = `/tmp/xo-server/mounts/${object.id}`
object.host = host
object.share = share
} else if (type === 'smb') {
remote.type = 'smb'
object.type = 'smb'
const lastAtSign = rest.lastIndexOf('@')
const smb = rest.slice(lastAtSign + 1)
const auth = rest.slice(0, lastAtSign)
@ -26,23 +28,23 @@ export const parse = remote => {
const password = auth.slice(firstColon + 1)
const [domain, sh] = smb.split('\\\\')
const [host, path] = sh.split('\0')
remote.host = host
remote.path = path
remote.domain = domain
remote.username = username
remote.password = password
object.host = host
object.path = path
object.domain = domain
object.username = username
object.password = password
}
return remote
return object
}
export const format = ({type, host, path, username, password, domain}) => {
type === 'local' && (type = 'file')
let url = `${type}://`
let string = `${type}://`
if (type === 'nfs') {
url += `${host}:`
string += `${host}:`
}
if (type === 'smb') {
url += `${username}:${password}@${domain}\\\\${host}`
string += `${username}:${password}@${domain}\\\\${host}`
}
path = sanitizePath(path)
if (type === 'smb') {
@ -51,6 +53,6 @@ export const format = ({type, host, path, username, password, domain}) => {
} else {
type === 'file' && (path = `/${path}`)
}
url += path
return url
string += path
return string
}

View File

@ -1,5 +1,6 @@
/* eslint-env mocha */
import deepFreeze from 'deep-freeze'
import expect from 'must'
// ===================================================================
@ -9,56 +10,38 @@ import { parse, format } from './'
// ===================================================================
// Data used for both parse and format (i.e. correctly formatted).
const data = {
const data = deepFreeze({
file: {
in: {
url: 'file:///var/lib/xoa/backup',
name: 'fileRemote'
},
out: {
url: 'file:///var/lib/xoa/backup',
string: 'file:///var/lib/xoa/backup',
object: {
type: 'local',
path: '/var/lib/xoa/backup',
name: 'fileRemote'
},
url: 'file:///var/lib/xoa/backup'
path: '/var/lib/xoa/backup'
}
},
SMB: {
in: {
url: 'smb://Administrator:pas:sw@ord@toto\\\\192.168.100.225\\smb\0',
name: 'smbRemote'
},
out: {
url: 'smb://Administrator:pas:sw@ord@toto\\\\192.168.100.225\\smb\0',
string: 'smb://Administrator:pas:sw@ord@toto\\\\192.168.100.225\\smb\0',
object: {
type: 'smb',
host: '192.168.100.225\\smb',
path: '',
domain: 'toto',
username: 'Administrator',
password: 'pas:sw@ord',
name: 'smbRemote'
},
url: 'smb://Administrator:pas:sw@ord@toto\\\\192.168.100.225\\smb\0'
password: 'pas:sw@ord'
}
}
}
})
const parseData = {
const parseData = deepFreeze({
...data,
'file with missing leading slash (#7)': {
in: {
url: 'file://var/lib/xoa/backup',
name: 'fileRemote'
},
out: {
url: 'file://var/lib/xoa/backup',
string: 'file://var/lib/xoa/backup',
object: {
type: 'local',
path: '/var/lib/xoa/backup',
name: 'fileRemote'
},
url: 'file:///var/lib/xoa/backup'
path: '/var/lib/xoa/backup'
}
}
}
})
const formatData = data
@ -68,7 +51,7 @@ describe('format', () => {
for (const name in formatData) {
const datum = formatData[name]
it(name, () => {
expect(format({...datum.out})).to.equal(datum.url)
expect(format(datum.object)).to.equal(datum.string)
})
}
})
@ -77,7 +60,7 @@ describe('parse', () => {
for (const name in parseData) {
const datum = parseData[name]
it(name, () => {
expect(parse({...datum.in})).to.eql(datum.out)
expect(parse(datum.string)).to.eql(datum.object)
})
}
})