-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#41 table relative paths #48
#41 table relative paths #48
Conversation
}) | ||
|
||
it('URL resource', async () => { | ||
const descriptor = 'https://dev.keitaro.info/dpkjs/datapackage.json' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is temporary, we will change it to github once this branch is merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good 👍 I've added some comments.
path: resourcePath, | ||
}, basePath) | ||
|
||
assert(resource.source === path.normalize(`${basePath}/${resourcePath}`)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you change to resource.source === PATH_STRING
- we need to be sure it returns exactly the same result we expect. If there is an error in "path.normalize(${basePath}/${resourcePath}
)" than we just replicate it in tests. But we need to test correctness of this code also.
path: resourcePath, | ||
}, baseURL) | ||
|
||
assert(resource.source === url.resolve(baseURL, resourcePath)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above I suppose better will be write resource.source === EXPECTED_URL
.
return url.resolve(this._basePath, source) | ||
} | ||
|
||
return path.normalize(`${this._basePath}/${source}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose we better use something like path join instead of string interpolation to make it cross-platform.
@@ -71,12 +72,19 @@ export default class Resource { | |||
* @returns {String|Array|Object} | |||
*/ | |||
get source() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function logic seems a little bit complicated - may be it's possible to make it more straightforward using comments and groups? We also should analyze it for vulnerabilities (accessing e.g. /etc/passwd
using relative paths) so this code should be pretty clear to understand.
For example:
// Inline data
if (this.type === 'inline') return source;
// Local data
...
// Remote data
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As for relative paths - it should be addresed on the spec level first. It already has a restriction of usage of dot-paths, but it is not enough (forbids usage only in the start of that path)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Fak3
One of the spec WG meetings has decided to ban any traverses outside of datapackage.json
basedir for local paths (no ^/
and ..
). So we consider it as a rule just waiting official finalization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dumyan it('resource source/type correctly works with base path', async () => {
const paths = [
{base: 'folder1', path: 'data/data.csv', source: 'folder1/data/data.csv', type: 'local'},
// ... like 10 more)
]
paths.forEach({base, path, source, type} => {
const resource = new Resource({path}, base)
assert(resource.source === source)
assert(resource.type === type)
})
})
it('resource source/type correctly works without base path', async () => { The same technique could be used for other tests to ensure |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dumyan
I've reviewed latest changes.
self._basePath = basePath || self._getBasePath(descriptor) | ||
|
||
// Check if basePath is valid and throw error if needed | ||
const basePathErrors = Utils.checkPath(basePath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to always raise on bad base path. valid/errors/raiseInvalid
is only for descriptor validation.
@@ -73,7 +73,7 @@ export default class Profiles { | |||
|
|||
const validation = tv4.validateMultiple(data, schema) | |||
if (validation.valid) { | |||
return true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return True
is correct. See validate
- frictionlessdata/software-legacy#2
@@ -46,15 +45,14 @@ export default class Resource { | |||
* | |||
* @returns {String} | |||
*/ | |||
get type() { | |||
get typeOfResourcePath() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow frictionlessdata/software-legacy#2 (Resource.type
for now)
|
||
return this.descriptor[this._sourceKey] | ||
} else if (this._sourceKey === 'path' && this._basePath === '') { | ||
// Local path, no basePath provided |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why this.__sourceKey === 'path'
means that a local path:
get _sourceKey() {
const inlineData = this.descriptor.data
if (inlineData) {
return 'data'
}
return 'path'
}
Based on this code remote path also satisfies this condition.
|
||
// we need to check the validity of the paths here because one can use | ||
// only the Resource class to read file contents with `table` | ||
if (this._validPaths) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See basePath
comment in constructor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error is always thrown here if the paths are invalid.
82daa70
to
611f2e8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems everything work correct 👍
Resolves #41 by adding
basePath
for remote datapackages and after that usingurl.resolve
inResources
class to point to the relative resource.