Path cannot contain slash

385 Views Asked by At

I'm trying to make unit testing in C using Python and CFFI module. It's pretty much working, but I can't use it into a sub directory.

While testing, my project looks like :

$ tree tests
tests/
├── sum.c
├── sum.h
├── tests_units.py
...

$ python3 tests_unit.py

...

OK

But when I'm converting it for my project :

$ tree
.
├── Makefile
├── src
│   ├── sum.c
│   └── sum.h
│   └── ...
└── tests
    └── tests_units.py

My make checkrun the following :

check:
    python3 tests/tests_units.py

And I've so to adapt my test file :

import unittest
import cffi
import importlib

def load(filename):
    # load source code
    source = open(filename + '.c').read()
    includes = open(filename + '.h').read()

    # pass source code to CFFI
    ffibuilder = cffi.FFI()
    ffibuilder.cdef(includes)
    ffibuilder.set_source(filename + '_', source)
    ffibuilder.compile()

    # import and return resulting module
    module = importlib.import_module(filename + '_')

    return module.lib


class SumTest(unittest.TestCase):
    def setUp(self):
        self.module = load('src/sum')

    def test_zero(self):
        self.assertEqual(self.module.sum(0), 0)

if __name__ == '__main__':
    unittest.main()

Pay attention to this line :

self.module = load('src/sum')

So my log is

...
Traceback (most recent call last):
File "tests/tests_units.py", line 28, in setUp
  self.module = load('src/sum')
File "tests/tests_units.py", line 17, in load
  ffibuilder.set_source(filename + '_', source)
File "/usr/local/lib/python3.6/site-packages/cffi/api.py", line 625, in set_source
raise ValueError("'module_name' must not contain '/': use a dotted "
ValueError: 'module_name' must not contain '/': use a dotted name to make a 'package.module' location
...

But it's not a module, it's a simple directory.

Can you have a solution ?

Regards.

1

There are 1 best solutions below

1
On

Sub-directories are still considered packages in Python, and as such you still need to use dots. As in src.sum.