https://pantsbuild.org/ logo
#general
Title
# general
c

creamy-airplane-38079

01/25/2022, 6:03 PM
is it normal for the
pantsv2
test
goal to run on
__init__.py
files in the test target ? If so, has anyone encountered situations where it will fail , even though they are empty files ? Tried running with debug logs but nothing meaningful is shown other than the exit code being 5
h

high-yak-85899

01/25/2022, 6:04 PM
I don't see anything in
python_tests
target that suggests that. Here's the default for sources and you can see init files shouldn't be showing up.
Do you need the init files around if they're empty?
Not sure it'd be the recommended way, but you could pants_ignore them
Also, exit code 5 is what you'd expect. There's nothing in the module so no tests are run. Pants treats all non-zero exit codes as a process failure and raises indiscriminately. Ref
I had raised some similar odd test behavior here
c

creamy-airplane-38079

01/25/2022, 6:11 PM
the
__init__.py
are there to help with the import of the source code. The codebase comes from an older state where it was stored as
src
and
tests
style and without
_init_.py
, the tests will have a hard time importing the code frm
src
h

high-yak-85899

01/25/2022, 6:12 PM
Maybe you don't need the inits anymore now that pants helps locate things?
c

creamy-airplane-38079

01/25/2022, 6:13 PM
I have seen example repos structure the code so that test and source files are in the same directory ? Is that the recommended approach ?
w

witty-crayon-22786

01/25/2022, 6:13 PM
which Pants version is this? anything after
2.x
should skip those files and not run them
h

high-yak-85899

01/25/2022, 6:13 PM
My interpretation is that pants does not intend to have any recommended structure and aims to be flexible to user needs.
c

creamy-airplane-38079

01/25/2022, 6:13 PM
2.9.0
w

witty-crayon-22786

01/25/2022, 6:14 PM
what is the
BUILD
content in the relevant directory?
c

creamy-airplane-38079

01/25/2022, 6:15 PM
BUILD
Copy code
dtl_version = {}
with open("products/datalogue/dtl-python-sdk/datalogue/version.py") as fp:
    exec(fp.read(), dtl_version)

python_sources(
    name="datalogue",
    sources=["datalogue/**/*.py"],
    dependencies=['3rdparty/datalogue/python:requests',
                  '3rdparty/datalogue/python:python-dateutil',
                  '3rdparty/datalogue/python:validators',
                  '3rdparty/datalogue/python:pytest',
                  '3rdparty/datalogue/python:numpy',
                  '3rdparty/datalogue/python:pyyaml',
                  '3rdparty/datalogue/python:pyarrow',
                  '3rdparty/datalogue/python:pandas',
                  '3rdparty/datalogue/python:pbkdf2'
                  ]
)

python_distribution(
    provides=setup_py(
        name="datalogue",
        version=dtl_version["__version__"],
        author="Nicolas Joseph",
        author_email="<mailto:nic@datalogue.io|nic@datalogue.io>",
        license="""
        Copyright 2021 Datalogue, Inc.

        This Datalogue SDK is licensed solely pursuant to the terms of the Master Software License executed between you as Licensee and Datalogue, Inc.

        All rights reserved.
        """,
        description="SDK to interact with the datalogue platform",
        long_description="",
        long_description_content_type="text/markdown",
        url="<https://github.com/datalogue/platform>",
        classifiers=[
            "Programming Language :: Python :: 3",
            'Programming Language :: Python :: 3.6',
            'Programming Language :: Python :: 3.7',
            'Programming Language :: Python :: 3.8',
            'Programming Language :: Python :: 3.9',
            "Operating System :: OS Independent"
        ],
        python_requires=">=3.6",
        setup_requires=['pytest-runner'],
        tests_require=['pytest>=3.6.3', 'pytest-cov>=2.6.0']
    )
)

python_sources(
    name="tests-utils",
    sources=["tests/util/*",
             "tests/kit/*"]
)

python_tests(
    name='unit-tests',
    sources=["tests/unit/**"],
    dependencies=[":datalogue",
                  ":tests-utils",
                  ":test_resources"
                  ],
)

python_tests(
    name='integration-tests',
    sources=["tests/integration/**", "tests/unit/**"],
    dependencies=[":datalogue",
                  ":tests-utils",
                  ":test_resources"
                  ],
)

resources(
    name='test_resources',
    sources=['products/datalogue/dtl-python-sdk/tests/resources/**/*.json',
             'products/datalogue/dtl-python-sdk/tests/resources/**/*.csv',
             'products/datalogue/dtl-python-sdk/tests/resources/**/*.yaml'
             ],
)
and command is
Copy code
./pantsv2 test products/datalogue/dtl-python-sdk:unit-tests -ldebug
w

witty-crayon-22786

01/25/2022, 6:17 PM
@creamy-airplane-38079: so, by default,
python_tests
does not glob
__init__.py
files, but by globbing
tests/integration/**", "tests/unit/**"
, you are including them here
2
it defaults to
"test_*.py", "*_test.py", "tests.py"
c

creamy-airplane-38079

01/25/2022, 6:18 PM
so I remove the
sources
field
it should run on defaults ?
or just set that to the directory ?
w

witty-crayon-22786

01/25/2022, 6:18 PM
@creamy-airplane-38079: the default is not recursive… targets default to owning files in their directory. so you will still need to set it with this layout
h

high-yak-85899

01/25/2022, 6:19 PM
That glob pattern is only that directory. You'd need
tests/unit/**, tests/unit/**/!__init__.py
(I think)
w

witty-crayon-22786

01/25/2022, 6:19 PM
so in this case, you’d need something like:
Copy code
"tests/integration/**/test_*.py", "tests/unit/**/test_*.py", ...
…etc, for the relevant patterns
c

creamy-airplane-38079

01/25/2022, 6:19 PM
gotcha
thanks
w

witty-crayon-22786

01/25/2022, 6:20 PM
it’s a larger conversation to have, but in general: rather than recursive globbing like this, we recommend 111, where targets live in BUILD files in the same directory as their files
ah, yea: or what @high-yak-85899 said: you can do a full glob, and then exclude the
__init__.py
files.
c

creamy-airplane-38079

01/25/2022, 6:21 PM
got it , thanks for the feedback
w

witty-crayon-22786

01/25/2022, 6:22 PM
re: 111: if you were to remove that BUILD file entirely, and then run
./pants tailor $dir
for that directory, it would generate a very different layout, which might be a bit more maintainable
👀 1
but adjusting the globs should also work.
c

creamy-airplane-38079

01/25/2022, 7:07 PM
any docs or paragraphs that describe the pros of the 111 approach ? i see the docs mentioning to keep the build files with the code they reference to but can't remember the benefits
nevermind I found the doc I was looking for
👍 1