import-linter icon indicating copy to clipboard operation
import-linter copied to clipboard

Target files not only packages.

Open proofit404 opened this issue 6 years ago • 5 comments

Hi, thank you for the useful project!

Is it possible to setup import linter to validate my test suite, not my packages?

The source code layout looks like this:

src/
  _priavte/
    __init__.py
  public/
    __init__.py
tests/
  test_one.py
  test_two.py

I want to test modules only touches public package and not private.

Is it possible to archive with import linter?

Best regards, Artem.

proofit404 avatar Sep 21 '19 20:09 proofit404

Hi Artem,

Thanks for your question! It should be possible, but you might need to add an __init__.pyunder tests to make sure it's a package.

Your .importlinter might look something like this:

[importlinter]
root_package = tests
include_external_packages = True

[importlinter:contract:1]
name = Tests must not import _private
type = forbidden
source_modules =
    tests
forbidden_modules =
    _private

seddonym avatar Sep 22 '19 10:09 seddonym

Thanks for the quick response!

The main reason I'm bringing it here is to avoid the transition of the test suite into a package. I want to follow pytest best practices.

Maybe there is a possibility to support regular expression in place of root package or source modules.

proofit404 avatar Sep 22 '19 10:09 proofit404

The main reason I'm bringing it here is to avoid the transition of the test suite into a package. I want to follow pytest best practices.

Interesting. What is the problem with making it a package? Pytest even seems to suggest it. From the Pytest docs: "If you need to have test modules with the same name, you might add __init__.py files to your tests folder and subfolders, changing them to packages:"

seddonym avatar Sep 22 '19 10:09 seddonym

Hi,

Sorry for the long delay.

Turning test modules into a package is a workaround for some tool not working.

It enables an ability to cross import stuff from different test files. Without that, you're forced to express useful pieces of code with py.test fixtures. And that forces you to think about the setup and teardown process explicitly.

For example, coverage didn't work without tests being a package a long time ago. But they decided to fix that instead of forcing everyone to turn tests into a package.

Have a good day :tada:

Best regards, Artem.

proofit404 avatar Oct 05 '19 11:10 proofit404

I see... Very interesting, I've not thought about tests like that before.

I'll have a think about what the best thing is for import-linter.

seddonym avatar Oct 05 '19 13:10 seddonym

Just doing some spring-cleaning... It's possible this now works, due to Import Linter's support for namespace packages.

I'm going to close the ticket, but happy to reopen if the problem still exists.

seddonym avatar Feb 03 '23 14:02 seddonym