Skip to content

Fix handling of ini configuration in subdirs when specifying :: #2149

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

Merged
merged 1 commit into from
Dec 27, 2016

Conversation

pelme
Copy link
Member

@pelme pelme commented Dec 20, 2016

This PR fixes #2148.

pelme added a commit to pelme/pytest that referenced this pull request Dec 20, 2016
@pelme pelme changed the title Failing test for issue #2148. Fix handling of ini configuration in subdirs when specifying :: Dec 20, 2016
@coveralls
Copy link

Coverage Status

Coverage increased (+0.0008%) to 92.833% when pulling 649834f on pelme:issue2148 into da40bcf on pytest-dev:master.


* Specifying tests with colons like ``test_foo.py::test_bar`` for tests in
subdirectories with ini configuration files now uses the correct ini file.
Thanks `@pelme`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing link for user name and issue link as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've updated the PR!

if not str(arg).startswith('-')
]

return [d for d in possible_dirs if d.exists()]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it harder to parse? Maybe ...

    return [
        py.path.local(str(arg).split('::')[0])
        for arg in args
        if not str(arg).startswith('-') and arg.exists()
    ]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I played around with a couple of variants of this. I was not a big fan of the original nested generator expression in the list comprehension and it became worse with this additional check.

The args may not be py.path.local from the beginning. Also, not stripping the part after :: before calling .exists() leaves us with the original bug. To do everything in a single list comprehension it would then look like

   return [
        py.path.local(str(arg).split('::')[0])
        for arg in args
        if not str(arg).startswith('-') and py.path.local(str(arg).split('::')[0]).exists()
    ]

I was not happy with that since stripping :: and creating the py.path.local object happens twice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you like my generator expression?! ;)
In your case str is still called twice on arg.. but I can see that readability is better with a named var here.
Nitpick: remove the added newline to keep it more compact.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree using a generator expression instead of a list comprehension would improve things a bit, although I'm pretty sure this won't impact performance at all. 😉

Copy link
Member

@nicoddemus nicoddemus Dec 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO what would improve readability would be to use small functions that act as documentation to explain all that's being handled there:

def get_dirs_from_args(args):
    def is_option(x):
        return str(x).startswith('-')

    def get_file_part_from_node_id(x):
        return str(x).split('::')[0]

    possible_dirs = (
        py.path.local(get_file_part_from_node_id(arg))
        for arg in args
        if not is_option(arg)      
    )
    return [d for d in possible_dirs if d.exists()]

Also I just noticed that although the function is named get_dirs_from_args, it actually returns directories and filenames. 😁

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the PR to use a generator expression, small functions and made it always return dirs for consistency. I was also able to get rid of a couple of redundant checks in get_common_ancestor.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.0008%) to 92.833% when pulling b3a52e7 on pelme:issue2148 into da40bcf on pytest-dev:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.0008%) to 92.833% when pulling 8c6a688 on pelme:issue2148 into da40bcf on pytest-dev:master.

Copy link
Contributor

@blueyed blueyed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be squashed, to not break git-bisect, and also otherwise.

if not str(arg).startswith('-')
]

return [d for d in possible_dirs if d.exists()]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you like my generator expression?! ;)
In your case str is still called twice on arg.. but I can see that readability is better with a named var here.
Nitpick: remove the added newline to keep it more compact.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.005%) to 92.837% when pulling 3d0c45b on pelme:issue2148 into 3164062 on pytest-dev:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.005%) to 92.837% when pulling da051bd on pelme:issue2148 into 3164062 on pytest-dev:master.

@decentral1se
Copy link
Contributor

🍻

@coveralls
Copy link

Coverage Status

Coverage increased (+0.005%) to 92.837% when pulling c99cd73 on pelme:issue2148 into 3164062 on pytest-dev:master.

Copy link
Contributor

@blueyed blueyed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

return path
return py.path.local(path.dirname)

# These looks like paths but may not exist
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/looks/look/

@pelme pelme force-pushed the issue2148 branch 2 times, most recently from 8be42a3 to b692c64 Compare December 27, 2016 14:28
…ains ::.

This commit also improves readbility in get_dirs_from_args by using self
documenting local functions.

get_dirs_from_args also now only returns directories that actually exists,
and not files to avoid confusion.

This commit also removes redundant checks in get_common_ancestor that
was already performed in get_dirs_from_args..
@coveralls
Copy link

Coverage Status

Coverage increased (+0.005%) to 92.837% when pulling 0bb8a4a on pelme:issue2148 into 3164062 on pytest-dev:master.

@nicoddemus
Copy link
Member

Many thanks @pelme for the PR and @blueyed and @lwm for helping reviewing it! 🎆

@nicoddemus nicoddemus merged commit 088b742 into pytest-dev:master Dec 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ini configuration are not found in subdirectories when specifying tests with ::
5 participants