fix: file protocol starts with 3 / (#6)

Fixes #7
This commit is contained in:
Fabrice Marsaud 2016-06-06 10:57:27 +02:00 committed by Julien Fontanet
parent cd73c8f82f
commit 808cc5d8d0
3 changed files with 43 additions and 24 deletions

View File

@ -24,9 +24,7 @@
"node": ">=0.12"
},
"dependencies": {
"lodash.filter": "^4.0.1",
"lodash.map": "^4.0.1",
"lodash.trim": "^4.0.1"
"lodash": "^4.13.1"
},
"devDependencies": {
"babel-cli": "^6.3.17",

View File

@ -1,6 +1,7 @@
import filter from 'lodash.filter'
import map from 'lodash.map'
import trim from 'lodash.trim'
import filter from 'lodash/filter'
import map from 'lodash/map'
import trim from 'lodash/trim'
import trimStart from 'lodash/trimStart'
const sanitizePath = (...paths) => filter(map(paths, s => s && filter(map(s.split('/'), trim)).join('/'))).join('/')
@ -8,7 +9,7 @@ export const parse = (remote) => {
const [type, rest] = remote.url.split('://')
if (type === 'file') {
remote.type = 'local'
remote.path = `/${rest}` // FIXME the heading slash has been forgotten on client side first implementation
remote.path = `/${trimStart(rest, '/')}` // the leading slash has been forgotten on client side first implementation
} else if (type === 'nfs') {
remote.type = 'nfs'
const [host, share] = rest.split(':')
@ -31,7 +32,8 @@ export const parse = (remote) => {
}
export const format = ({type, host, path, username, password, domain}) => {
let url = `${type === 'local' ? 'file' : type}://`
type === 'local' && (type = 'file')
let url = `${type}://`
if (type === 'nfs') {
url += `${host}:`
}
@ -43,7 +45,7 @@ export const format = ({type, host, path, username, password, domain}) => {
path = path.split('/')
path = '\0' + path.join('\\') // FIXME saving with the windows fashion \ was a bad idea :,(
} else {
type === 'smb' && (path = `/${path}`) // FIXME file type should have a / too, but it has been forgotten on client side first implementation...
type === 'file' && (path = `/${path}`)
}
url += path
return url

View File

@ -8,28 +8,47 @@ import { parse, format } from './'
// ===================================================================
const data = {
const FILE_FIXED = {
url: 'file:///var/lib/xoa/backup',
type: 'local',
path: '/var/lib/xoa/backup'
}
const SMB = {
url: 'smb://Administrator:password@toto\\\\192.168.100.225\\smb\0',
type: 'smb',
host: '192.168.100.225\\smb',
path: undefined,
domain: 'toto',
username: 'Administrator',
password: 'password'
}
const parseData = {
file: {
url: 'file://var/lib/xoa/backup',
url: 'file://var/lib/xoa/backup', // Remotes formatted before fixing #7 will not break when reparses
type: 'local',
path: '/var/lib/xoa/backup'
},
smb: {
url: 'smb://Administrator:password@toto\\\\192.168.100.225\\smb\0',
type: 'smb',
host: '192.168.100.225\\smb',
path: undefined,
domain: 'toto',
username: 'Administrator',
password: 'password'
}
fileFixed: FILE_FIXED,
smb: SMB
}
const formatData = {
file: {
url: 'file:///var/lib/xoa/backup',
type: 'local',
path: '/var/lib/xoa/backup'
},
fileFixed: FILE_FIXED,
smb: SMB
}
// -------------------------------------------------------------------
describe('format', () => {
for (const name in data) {
const datum = data[name]
for (const name in formatData) {
const datum = formatData[name]
it(name, () => {
expect(format(datum)).to.equal(datum.url)
})
@ -37,8 +56,8 @@ describe('format', () => {
})
describe('parse', () => {
for (const name in data) {
const datum = data[name]
for (const name in parseData) {
const datum = parseData[name]
it(name, () => {
expect(parse(datum)).to.eql(datum)
})