-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add fs.relative_to() function. Closes #2184 #7908
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fa820fa
Add fs.relative_to. Closes #2184
zeehio cdee574
Improve windows support of relative_to
zeehio e823fc9
Use os.path.relpath() to find the relative path
zeehio abd96b3
Relax absolute path requirements
zeehio 0faabfb
Fixes related to the rebase
zeehio e3a88e9
Update documentation with respect to absolute paths
zeehio 11956a2
Rename within to if_within, add allow_absolute.
zeehio b161af5
Fix type annotations
zeehio b48a220
Clarify doc
zeehio 8655f6c
Fix argument name in doc
zeehio 499602b
Documentation consistency with if_within
zeehio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
## Add fs.relative_to function to get a path relative to another path | ||
|
||
Finding what is the path to a directory relative to another directory is now possible. | ||
For instance to find the path to `/foo/lib` as if we were in `/foo/bin` we now can use: | ||
|
||
```meson | ||
fs = import('fs') | ||
fs.relative_to('/foo/lib', '/foo/bin')` # '../lib' | ||
``` | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
project('relpath', []) | ||
|
||
fs = import('fs') | ||
|
||
if host_machine.system() == 'windows' | ||
assert(fs.relative_to('c:\\proj1\\foo', 'c:\\proj1\\bar') == '..\\foo', 'Path with windows common prefix is broken') | ||
#expect_error(fs.relative_to('c:\\proj1\\foo', 'd:\\proj1\\bar') == 'c:\\proj1\\foo', 'Error was expected because relative path does not exist') | ||
#expect_error(fs.relative_to('c:\\proj1\\foo', 'c:\\proj2\\bar', if_within: 'c:\\proj2'), 'Error expected, path1 is outside `if_within`) | ||
assert(fs.relative_to('c:\\proj1\\foo', 'd:\\proj1\\bar', allow_absolute: true) == 'c:\\proj1\\foo', 'Path falls back if allow absolute is true') | ||
assert(fs.relative_to('c:\\proj1\\foo', 'c:\\proj2\\bar', if_within: 'c:\\proj2', allow_absolute: true) == 'c:\\proj1\\foo', 'Path falls back to path1 if allow_absolute is True') | ||
else | ||
assert(fs.relative_to('/prefix/lib/foo', '/prefix') == 'lib/foo', 'Path inside source is broken') | ||
assert(fs.relative_to('/prefix/lib', '/prefix/bin') == '../lib', 'Path with common prefix with source is broken') | ||
assert(fs.relative_to('/usr/lib/foo', '/usr/bin') == '../lib/foo', 'Path with common prefix with source is broken') | ||
assert(fs.relative_to('/usr/lib/foo', '/usr/bin', if_within: '/usr') == '../lib/foo', 'Path with common prefix using `if_within` is broken') | ||
#expect_error(fs.relative_to('/project1/lib/foo', '/usr/bin', if_within: '/usr'), 'Expected error because /project1 is not in /usr') | ||
assert(fs.relative_to('/project1/lib/foo', '/usr/bin', if_within: '/usr', allow_absolute: true) == '/project1/lib/foo', 'Expected path1 because allow_absolute is true') | ||
endif | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is that an error? Doc above says " if
path1
is found inpath3
, otherwise it returnspath1
unchanged", so shouldn't this returnc:\\proj1\\foo
?Uh oh!
There was an error while loading. Please reload this page.
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.
The example was right, the docs were wrong.
If
path1
is not found onpath3
, one can do two things: either we returnpath1
or we error.path1
as was documented may lead to confusion if bothpath1
andpath2
happen to be relative paths. In that scenario we could not always know if the returned path is thepath1
we gave as input (because it wasn't contained inpath3
) or if the outcome is the result of computing it relative topath2
(becausepath1
was contained inpath3
). Therefore the only safe behaviour is to returnpath1
if it is absolute, or raise an error. But for consistency, absolute paths can only be returned ifallow_absolute : true
, so that argument would need to be set as well to avoid the error.I've pushed a commit to clarify the documentation as well