Ejemplo n.º 1
0
def test_create_sdist():
    "Test the successful creation of a source distribution."
    with nimporter.cd('tests/proj1'):
        subprocess.Popen(f'{PYTHON} setup.py sdist'.split()).wait()
        dist = Path('dist')
        egg = Path('project1.egg-info')
        try:
            assert dist.exists()
            assert egg.exists()
            targets = list(dist.glob('project1*'))
            assert len(targets) == 1
            assert targets[0].exists()

            # Make sure the appropriate compiler is being used
            for extension in Path('nim-extensions').iterdir():
                (nim_build_data_file,) = extension.glob('*json')
                nim_build_data = json.loads(nim_build_data_file.read_text())
                expected = nimporter.NimCompiler.get_compatible_compiler()
                installed_ccs = nimporter.NimCompiler.get_installed_compilers()
                if not expected:
                    warnings.warn(
                        f'No compatible C compiler installed: {installed_ccs}'
                    )
                else:
                    cc_path = installed_ccs[expected]
                    actual = nim_build_data['linkcmd'].split()[0].strip()
                    if not actual.startswith(cc_path.stem):
                        warnings.warn(
                            f'Nim used a different C compiler than what Python '
                            f'expects. Python uses {cc_path.stem} and Nim used '
                            f'{actual}'
                        )
        finally:
            shutil.rmtree(str(dist.absolute()))
            shutil.rmtree(str(egg.absolute()))
Ejemplo n.º 2
0
def test_install_bdist():
    "Make sure that the wheel can be installed by Pip"
    with nimporter.cd('tests/proj1'):
        subprocess.Popen(f'{PYTHON} setup.py bdist_wheel'.split()).wait()
        dist = Path('dist')
        build = Path('build')
        egg = Path('project1.egg-info')
        try:
            assert dist.exists()
            assert build.exists()
            assert egg.exists()
            targets = list(Path('dist').glob('project1*.whl'))
            assert len(targets) == 1
            wheel = targets[0]
            assert wheel.exists()

            subprocess.Popen(f'{PIP} install {wheel}'.split()).wait()
        finally:
            shutil.rmtree(str(dist.absolute()))
            shutil.rmtree(str(build.absolute()))
            shutil.rmtree(str(egg.absolute()))

    import proj1
    assert proj1
    assert proj1.performance
    assert proj1.lib1
    assert proj1.foo
    assert proj1.bar
    assert proj1.baz
    assert proj1.baz() == 1

    subprocess.Popen(f'{PIP} uninstall project1 -y'.split()).wait()
Ejemplo n.º 3
0
def test_install_sdist():
    "Make sure that the project can be installed by Pip"
    with nimporter.cd('tests/proj1'):
        subprocess.Popen(f'{PYTHON} setup.py sdist'.split()).wait()
        dist = Path('dist')
        egg = Path('project1.egg-info')
        try:
            assert dist.exists()
            assert egg.exists()
            targets = list(dist.glob('project1*'))
            assert len(targets) == 1
            assert targets[0].exists()

            subprocess.Popen(f'{PIP} install .'.split()).wait()
        finally:
            shutil.rmtree(str(dist.absolute()))
            shutil.rmtree(str(egg.absolute()))

    import proj1
    assert proj1
    assert proj1.performance
    assert proj1.lib1
    assert proj1.foo
    assert proj1.bar
    assert proj1.baz
    assert proj1.baz() == 1

    subprocess.Popen(f'{PIP} uninstall project1 -y'.split()).wait()
Ejemplo n.º 4
0
def test_get_import_prefix():
    "Make sure that the right namespace is returned for a given module path."
    with nimporter.cd('tests') as tmpdir:
        module_path1 = Path('pkg1/mod1.nim')
        module_path2 = Path('pkg1/pkg2/mod2.nim')
        gold1 = 'pkg1', 'mod1.nim'
        gold2 = 'pkg1', 'pkg2', 'mod2.nim'
        assert NimCompiler.get_import_prefix(module_path1, Path()) == gold1
        assert NimCompiler.get_import_prefix(module_path2, Path()) == gold2
Ejemplo n.º 5
0
def test_build_library():
    "Test that a given Nim module can produce a Python extension library."
    with nimporter.cd('tests'):
        module = Path('lib1')
        output = NimCompiler.build_artifact(module)
        artifact = NimCompiler.compile_nim_code(module, output, library=True)

        assert artifact.exists()
        assert artifact.parent == output.parent
        assert artifact.suffix == output.suffix
Ejemplo n.º 6
0
def test_install_bdist():
    "Make sure that the wheel can be installed by Pip"
    with nimporter.cd('tests/proj1'):
        subprocess.Popen(f'{PYTHON} setup.py bdist_wheel'.split()).wait()
        dist = Path('dist')
        build = Path('build')
        egg = Path('project1.egg-info')
        try:
            assert dist.exists()
            assert build.exists()
            assert egg.exists()
            targets = list(Path('dist').glob('project1*.whl'))
            assert len(targets) == 1
            wheel = targets[0]
            assert wheel.exists()
            subprocess.Popen(f'{PIP} install {wheel}'.split()).wait()

        finally:
            shutil.rmtree(str(dist.absolute()))
            shutil.rmtree(str(build.absolute()))
            shutil.rmtree(str(egg.absolute()))

    # Make sure that `tests/proj1` is not imported as a SimpleNamespace and that
    # the installed library in `site-packages` is used.
    with nimporter.cd('../..'):
        try:
            import proj1
            assert proj1
            import proj1.performance
            assert proj1.performance
            import proj1.lib1
            assert proj1.lib1
            assert proj1.foo
            assert proj1.bar
            assert proj1.baz
            assert proj1.baz() == 1
        except Exception as e:
            warnings.warn(str(e))

        # Cannot delete a DLL in use by another process on Windows
        if sys.platform != 'win32':
            subprocess.Popen(f'{PIP} uninstall project1 -y'.split()).wait()
Ejemplo n.º 7
0
def test_build_module():
    "Test that a given Nim module can produce a Python extension module."
    with nimporter.cd('tests'):
        module = Path('mod_a.nim')
        output = NimCompiler.build_artifact(module)

        f = io.StringIO()
        with redirect_stdout(f):
            artifact = NimCompiler.compile_nim_code(module, output, library=False)

        assert artifact.exists()
        assert artifact.parent == output.parent
        assert 'Warning:' in f.getvalue()
Ejemplo n.º 8
0
def test_create_sdist():
    "Test the successful creation of a source distribution."
    with nimporter.cd('tests/proj1'):
        subprocess.Popen(f'{PYTHON} setup.py sdist'.split()).wait()
        dist = Path('dist')
        egg = Path('project1.egg-info')
        try:
            assert dist.exists()
            assert egg.exists()
            targets = list(dist.glob('project1*'))
            assert len(targets) == 1
            assert targets[0].exists()
        finally:
            shutil.rmtree(str(dist.absolute()))
            shutil.rmtree(str(egg.absolute()))
Ejemplo n.º 9
0
def test_create_bdist():
    "Test the successful create of a wheel."
    with nimporter.cd('tests/proj1'):
        subprocess.Popen(f'{PYTHON} setup.py bdist_wheel'.split()).wait()
        dist = Path('dist')
        build = Path('build')
        egg = Path('project1.egg-info')
        try:
            assert dist.exists()
            assert build.exists()
            assert egg.exists()
            targets = list(Path('dist').glob('project1*.whl'))
            assert len(targets) == 1
            assert targets[0].exists()
        finally:
            shutil.rmtree(str(dist.absolute()))
            shutil.rmtree(str(build.absolute()))
            shutil.rmtree(str(egg.absolute()))
Ejemplo n.º 10
0
def test_temp_change_directory():
    "Test to see if the nimporter.cd function actually works."
    cwd = Path('.').absolute()
    temp_dir = Path('nim-temp').absolute()
    try:
        temp_dir.mkdir()

        with nimporter.cd(temp_dir) as tmp:
            # Yields correct path name
            assert tmp == temp_dir

            # Actually CDs
            assert Path().resolve() == temp_dir
            assert Path().resolve() == tmp

        # CDs back
        assert Path().resolve() == cwd

    finally:
        temp_dir.rmdir()