-
Notifications
You must be signed in to change notification settings - Fork 667
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
Use absolute file paths in ITPParser #3108
Changes from 12 commits
7f3af66
eb6b8aa
45fa573
26806fc
ba1e31d
9c9ed50
f6228a4
62307b6
0708e65
a69ecff
c7b2fb0
16cad3d
dda1efa
3f12b43
13273cb
4d7fe44
54f90d1
abc9bb3
34222e5
3a1b2b5
b14e1e5
84d0fd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -265,10 +265,9 @@ def find_path(self, path): | |
current_file = self.current_file | ||
|
||
try: | ||
path = path.name | ||
path = os.path.abspath(path.name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This try/except is to check whether By the way, your test should check for both, just in case :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @lilyminium, that's a good observation! However, if the user isn't providing a string and triggering this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @aditya-kamath,
If a string is provided and the AttributeError is triggered, does your fix still work? The IOError test is probably not necessary. However, your test should check that your fix applies to both a relative
This was a typo, sorry. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, the answer to if it will work when there's an Attribute error is no! :( |
||
except AttributeError: | ||
pass | ||
|
||
current_dir = os.path.dirname(current_file) | ||
dir_path = os.path.join(current_dir, path) | ||
if os.path.exists(dir_path): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -335,3 +335,14 @@ def test_missing_endif(self): | |
with pytest.raises(IOError): | ||
with self.parser(ITP_no_endif) as p: | ||
top = p.parse(include_dir=GMX_DIR) | ||
|
||
aditya-kamath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class Testrelativepath: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow Python convention and use camel casing here if you create a class. However, since you only have one tests, I would suggest that you don't need a class for this test. Instead, you could structure |
||
|
||
def test_relpath(self, tmp_path): | ||
content="[ atoms ]\n" +\ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be neater here to use |
||
" 1 H 1 SOL HW1 1 0.41 1.00800" | ||
d = tmp_path / "sub" | ||
d.mkdir() | ||
p = d / "test.itp" | ||
p.write_text(content) | ||
u = mda.Universe(tmp_path / "sub/test.itp", format='ITP') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would still be an absolute path. Instead, I would suggest using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @lilyminium, I've been looking at pytest tools and os.paths. I see that there's a command called relative_to in the pathlib module which computes a relative path to a specified directory. My approach would have been to make a tmpdir and set that as 'observer' directory and use that command (relative_to) to find the path that it sees. As it turns out, the relative_to command only works when computing paths from parent directory reference, not sibling directory. So, I can't compute something like '../dir'. The second thing is even if I manually create a relative string which tmpdir would use to read my temporary itp file, I am not sure how to ask pytest to run from my tmpdir while performing this test. I might have missed something here, especially because I am new to pytest, but I am happy to hear any advice you have here! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @aditya-kamath, If you go into your temporary directory, write a file called "test.itp", make a new subdirectory, and change into that directory, you should be able to test that file with the path "../test.itp". You might like the tmpdir.as_cwd context manager for that. Edit: see this StackOverflow question for examples and notes https://stackoverflow.com/questions/55658866/how-to-get-the-temporary-path-using-pytest-tmpdir-as-cwd There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great advice! I figured the tests out. Just a note that I've done the reverse of what you might expect. Instead of testing a relative path and then converting it to a relative string. I've first tested a relative string and then followed it up by converting it to a path. Hope that's alright! |
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.