Hi, is there a way to bundle a set of mock files w...
# general
r
Hi, is there a way to bundle a set of mock files with my tests ? I've looked to define them as
files
and adding them as a dependency to the tests but they are still not found or not in the correct path, thx.
h
Yes, files is how you do it. What does your files BUILD entry look like?
r
so my test directory looks like this:
Copy code
tests
 ┣ mocks
 ┃ ┣ mock1.json
 ┃ ┣ mock2.json
 ┃ ┗ mock3.json
 ┣ snapshots
 ┃ ┣ __init__.py
 ┃ ┗ snap_test1.py
 ┣ BUILD
 ┣ __init__.py
 ┣ test1.py
 ┗ test2.py
and my BUILD file contains the following:
Copy code
python_tests(
  sources=globs('test_*.py'),
  dependencies=[
    ':mock_files',
    'packages-python/package1/data'
  ],
  tags = {'unittest'},
)

files(
  name='mock_files',
  sources=globs('mocks/*')
)
and finally the error I get
FileNotFoundError: [Errno 2] No such file or directory: '/Users/ouailbendidi/XXX/.pants.d/pyprep/sources/f5a7b57cc06b12bc690ae1336634144b46ef5b19/data/tests/mocks/mock1.json'
in my tests, the mock file path is defined as
Copy code
mock_path = os.path.join(
        os.path.dirname(__file__), "mocks/mock1.json"
    )
h
Ah so you want the file name in your tests to be relative to the build root, so
tests/mocks/
r
yep
h
Also try changing the
globs
to
rglobs
(recursive) for the files entry
And you can get rid of the sources argument for
python_tests
, that’s the default sources for
python_tests
already :)
r
Haha I learned to not trust default values lol, stayed as a habit
h
Fair, although we use the default all the time internally and it’s not going away soon. We wanted to make writing BUILD files a little less verbose. Also our deprecation policy means we’d never change that overnight https://www.pantsbuild.org/deprecation_policy.html
r
ah cool then
so when you said :
Ah so you want the file name in your tests to be relative to the build root
you mean that files are mounted, but it's just the path that is incorrect ?
so I've done some digging, this is how the
files
are bootsraped:
Copy code
750f0318a7b1d89c5eab58b4890e97d2c9664b9b
 ┣ .bootstrap
 ┃ ┃ ┗ ........
 ┣ package1
 ┃ ┣ data
 ┃ ┃ ┣ tests
 ┃ ┃ ┃ ┣ __init__.py
 ┃ ┃ ┃ ┣ test1.py
 ┃ ┃ ┃ ┗ test2.py
 ┃ ┃ ┣ __init__.py
 ┃ ┃ ┣ script1.py
 ┃ ┃ ┗ script2.py
 ┃ ┗ __init__.py
 ┣ packages-python
 ┃ ┣ package1
 ┃ ┃ ┗ data
 ┃ ┃ ┃ ┗ tests
 ┃ ┃ ┃ ┃ ┗ mocks
 ┃ ┃ ┃ ┃ ┃ ┣ mock1.json
 ┃ ┃ ┃ ┃ ┃ ┣ mock2.json
 ┃ ┃ ┃ ┃ ┃ ┗ mock3.json
 ┃ ┗ third_party
 ┃ ┃ ┗ requirements.txt
 ┣ PEX-INFO
 ┣ __main__.py
 ┗ pytestdebug.log
which is logical too, but still kind of un-intuitive
h
Sorry, I was afk. You don’t want to mark the sources as
mocks/*
because we directly feed the
sources
of a
python_tests
into the arguments for Pytest. (We don’t rely on Pytest auto-discovery because it would run over a bunch of unwanted things) Instead, I believe the solution should look like this:
Copy code
python_tests(
  dependencies=[
    ':mock_files',
  ],
  tags = {'unittest'},
)

files(
  name='mock_files',
  sources=rglobs('mocks/*')
)
In your test, you would say
open('tests/mocks/mock1.json')
r
actually if I do it like that I need to open
open('packages-python/package1/data/tests/mocks/mock1.json')
wich makes sense too because my
source_roots
is
package-python
h
ah, yes, that is what it should be. Does that work?
r
yep works fine, thx
h
Awesome
r
thought I'm still not completely keen on removing the relativity between the tests and mock files
but still better than it not working at all haha
I assume
snapshottesting
is not supported too then? (https://github.com/syrusakbary/snapshottest)
h
If you want to be able to have the resources be relative, you can try using
resources()
instead of
files()
. Here’s an example of how Pants does this to read in the
VERSION
file: https://github.com/pantsbuild/pants/blob/279595cd5f0350726a1da3920e5d9bb392bce926/src/python/pants/BUILD#L54 and https://github.com/pantsbuild/pants/blob/279595cd5f0350726a1da3920e5d9bb392bce926/src/python/pants/version.py#L17
r
didn't work, I'll investigate, I'm probably doing something wrong on my end
I think it's because of my source_roots, for python my
source_roots
is defined as :
Copy code
source_roots: {
    'packages-python': ('python',)
  }
so my
package1
is bootstrapped directly as
package1/data/tests
in the test env, while the ressource are bootstrapped as
packages-python/package1/data.tests/mock
fixed by defining:
Copy code
[source]
source_roots: {
    'packages-python': ('python',)
  }
test_roots: {
    'packages-python': ('python',)
  }
in my
pants.ini
, thanks @hundreds-father-404 🙂
h
Ah yes, good catch! Yay!