Skip to content
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

Fix tox with C-extension #310

Closed
wants to merge 3 commits into from

Conversation

mcara
Copy link
Member

@mcara mcara commented Apr 23, 2021

For background info, see #295 and related issues/PRs.

This PR avoids building the package in its own directory when running tox. This is done by changing import mode in pytest (see setup.cfg) and switching to absolute imports in test modules (which is a good thing anyway).

The only test that is failing is doc test when trying to import synphot due to relative import of _astropy_init - you will see this in failing tests.

CC: @pllim

@pllim
Copy link
Contributor

pllim commented Apr 23, 2021

With or without import-mode gave different errors. 😿

tox.ini Outdated
@@ -53,9 +52,6 @@ extras =
alldeps: all

commands =
# FIXME: Not sure why need this for tox to see the C-ext
python setup.py build_ext --inplace
# End of FIXME
pip freeze
!cov: pytest --pyargs synphot {toxinidir}/docs {posargs}
Copy link
Contributor

Choose a reason for hiding this comment

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

@jdavies-st , I do have --pyargs here. Am I using it wrong?

Copy link
Collaborator

Choose a reason for hiding this comment

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

With changedir commented out above, because you are running pytest --pyargs from within a directory that has synphot as a subdir, every import synphot will import it from the local checkout, not the installed location, following normal python import rules that always imports the local dir thing before checking python path.

@pllim pllim marked this pull request as draft April 26, 2021 13:19
@pllim
Copy link
Contributor

pllim commented Apr 26, 2021

I converted this to draft PR for now. FYI.

@codecov
Copy link

codecov bot commented Apr 26, 2021

Codecov Report

Merging #310 (2cce31f) into master (c6623f4) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #310   +/-   ##
=======================================
  Coverage   94.85%   94.85%           
=======================================
  Files          14       14           
  Lines        1983     1983           
=======================================
  Hits         1881     1881           
  Misses        102      102           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c6623f4...2cce31f. Read the comment docs.

@@ -4,8 +4,9 @@ testpaths = "synphot" "docs"
norecursedirs = build docs/_build synphot/src
astropy_header = true
doctest_plus = enabled
doctest_rst = true
Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, why this addition, is addopts = --doctest-rst not enough?

tox.ini Outdated
# Run the tests in a temporary directory to make sure that we don't import
# package from the source tree
#changedir = .tmp/{envname}
changedir = {homedir}
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it have to be homedir? Did changedir = .tmp/{envname} not work?

Copy link
Member Author

Choose a reason for hiding this comment

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

Does it matter?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, homedir might be pre-populated with stuff. .tmp/{envname} is clean.

Copy link
Member Author

Choose a reason for hiding this comment

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

The point is that tests do run in the install directory unlike what it was before - the source directory. Whether they run in

home/.tox/py39.../lib/python3.9/site-packages/synphot

or

chdir .tmp/py39env
../../home/.tox/py39env/lib/python3.9/site-packages/synphot

does it make a difference?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather you change it to .tmp/{envname} anyway, if either one works. That is what astropy is using and easier for me to copy paste from upstream in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

tox.ini Outdated Show resolved Hide resolved
tox.ini Outdated
# FIXME: Seems like using -c option with multiline Pytest is broken: see
# https://github.com/tox-dev/tox-travis/issues/146
# at the very least on CI - it runs fine locally on MacOSX.
cov: pytest --rootdir={toxinidir} -c {toxinidir}/setup.cfg --pyargs synphot --cov={envsitepackagesdir}/synphot --cov-config={toxinidir}/setup.cfg {posargs} --cov-report xml:{toxinidir}/coverage.xml
Copy link
Contributor

@pllim pllim Apr 26, 2021

Choose a reason for hiding this comment

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

Can you explain why the changes here (besides tox-dev/tox-travis#146)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not really. It seems like the directory in which pytest is going to run depends on a combination of factors such as rootdir, location of the config for pytest, etc. I was just lucky enough to guess the right incantation.

@mcara mcara force-pushed the fix-cextensions-tox branch from be8832e to 208bf4b Compare April 26, 2021 21:07
@mcara mcara marked this pull request as ready for review April 26, 2021 21:15
@mcara mcara requested a review from pllim April 26, 2021 21:18
@mcara mcara force-pushed the fix-cextensions-tox branch 2 times, most recently from db483f0 to 5bcdad0 Compare April 28, 2021 07:38
@mcara
Copy link
Member Author

mcara commented Apr 28, 2021

@pllim, in

addopts = --doctest-rst --doctest-ignore-import-errors
why do you use this option --doctest-ignore-import-errors?

@pllim
Copy link
Contributor

pllim commented Apr 28, 2021

It was from scientific-python/pytest-doctestplus#108 but I don't remember now if it was added as a precaution or I needed it. You can try remove it and see.

@mcara mcara force-pushed the fix-cextensions-tox branch from d735c8c to 16107c6 Compare April 28, 2021 16:38
Copy link
Contributor

@pllim pllim left a comment

Choose a reason for hiding this comment

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

I feel like this might have added some stuff that is unnecessary. I'll play with this a bit and report back. Thanks for investigating!

@mcara mcara force-pushed the fix-cextensions-tox branch from 395b7dc to 2cce31f Compare May 4, 2021 22:59
Copy link
Contributor

@pllim pllim left a comment

Choose a reason for hiding this comment

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

So... I look at the logs here and noticed that it is not actually running the doctest in the docs folder.

And... Purely by accident, I found out that the presence of conftest.py in the root directory messes up tox. So, simply by removing it (#313), tox picks up the installed version again and is also running the doctest. Though I hate to lose the really informative pytest header, I think removing conftest.py at the root dir is the more correct way to go. What do you think, @mcara and @jdavies-st ?

Also see spacetelescope/jdaviz#583 .

@pllim pllim changed the title Change to absolute imports in tests and fix tox with C-extension Fix tox with C-extension May 7, 2021
@pllim
Copy link
Contributor

pllim commented May 7, 2021

Actually... Maybe you hit the gold mine with --import-mode=append here. Works nicely over at #314. Not sure about the other stuff you added.

@mcara
Copy link
Member Author

mcara commented May 7, 2021

Not sure about the other stuff you added.

Like what? -c ? I do not think I added anything else.

@pllim
Copy link
Contributor

pllim commented May 8, 2021

Well, look at the diff.

@mcara
Copy link
Member Author

mcara commented May 8, 2021

What specifically about diff worries you? I just removed your workaround that you have marked with # FIXME: Not sure why need this for tox to see the C-ext and the code below that was building the package in place and as the result the test was run in the source directory. I thought you wanted to be able to run tests in the install directory. If that is not what you want - feel free to close this PR.

@mcara
Copy link
Member Author

mcara commented May 8, 2021

That is, this PR allows running unit tests on installed package instead of the source directory with the package build in place (as it is currently done). All tests seem to be passing and running from the installed directory.

However, I see that #314 is achieving the same with other added benefits like picking up rst tests in the source directory. I think that one has better results and for this reason I am closing this PR in favor of #314

@mcara mcara closed this May 8, 2021
@pllim
Copy link
Contributor

pllim commented May 10, 2021

@mcara , this PR really helped shine the light on why things weren't working. So even though this wasn't merged, thank you very much for your hard work on this! I'll be sure to give you co-author credit for #314.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants