def setup_class(cls): import array cls.array = array.array import struct cls.struct = struct cls.tempfile = str(pytest.ensuretemp('array').join('tmpfile')) cls.maxint = sys.maxint
def pytest_sessionstart(session): """ pytest_sessionstart hook This runs *before* import and collection of tests. This is *THE* place to do mocking of things that are global, such as `appdirs`. Do teardown in `pytest_sessionfinish()` """ print("Pre-Session Setup..") # Figuring out how to use the pytest tmpdir fixture externally was kinda awful. Vars.tmpdir_home = pytest.ensuretemp('fake_home') Vars.tmpdir_data = Vars.tmpdir_home.mkdir('appdirs_datadir') Vars.tmpdir_cache = Vars.tmpdir_home.mkdir('appdirs_cachedir') user_data_dir = lambda *x: str(Vars.tmpdir_data / x[0] if x else Vars.tmpdir_data) user_cache_dir = lambda *x: str(Vars.tmpdir_cache / x[0] if x else Vars.tmpdir_cache) # Mockers that need to be loaded before any of our code Vars.extrasession_mockers.extend([ mock.patch('appdirs.user_data_dir', user_data_dir), mock.patch('appdirs.user_cache_dir', user_cache_dir), ]) for mocker in Vars.extrasession_mockers: mocker.start()
def setup_class(cls): from rpython.rlib import rgc from pypy.interpreter import gateway AppTestCpythonExtensionBase.setup_class.im_func(cls) tmpname = str(pytest.ensuretemp('out', dir=0)) cls.w_tmpname = cls.space.wrap(tmpname) if not cls.runappdirect: cls.total_mem = 0 def add_memory_pressure(estimate, object=None): assert estimate >= 0 cls.total_mem += estimate cls.orig_add_memory_pressure = [rgc.add_memory_pressure] rgc.add_memory_pressure = add_memory_pressure def _reset_memory_pressure(space): cls.total_mem = 0 cls.w_reset_memory_pressure = cls.space.wrap( gateway.interp2app(_reset_memory_pressure)) def _cur_memory_pressure(space): return space.newint(cls.total_mem) cls.w_cur_memory_pressure = cls.space.wrap( gateway.interp2app(_cur_memory_pressure)) else: def _skip_test(*ignored): skip("not for -A testing") cls.w_reset_memory_pressure = _skip_test
def test_env_vars_from_file_bom(self): tmpdir = pytest.ensuretemp('env_file') self.addCleanup(tmpdir.remove) with codecs.open('{}/bom.env'.format(str(tmpdir)), 'w', encoding='utf-8') as f: f.write('\ufeffPARK_BOM=박봄\n') assert env_vars_from_file(str(tmpdir.join('bom.env'))) == { 'PARK_BOM': '박봄' }
def setup_class(cls): cls.w_array = cls.space.appexec([], """(): import array return array.array """) cls.w_tempfile = cls.space.wrap( str(pytest.ensuretemp('array').join('tmpfile'))) cls.w_maxint = cls.space.wrap(sys.maxint)
def test_env_vars_from_file_whitespace(self): tmpdir = pytest.ensuretemp('env_file') self.addCleanup(tmpdir.remove) with codecs.open('{}/whitespace.env'.format(str(tmpdir)), 'w', encoding='utf-8') as f: f.write('WHITESPACE =yes\n') with pytest.raises(ConfigurationError) as exc: env_vars_from_file(str(tmpdir.join('whitespace.env'))) assert 'environment variable' in exc.exconly()
def view(self): from dotviewer import graphclient import pytest r = ["digraph G {"] self._dot(r) r.append("}") p = pytest.ensuretemp("pyparser").join("temp.dot") p.write("\n".join(r)) graphclient.display_dot_file(str(p))
def test_export_dump(data): inputfile = str(data.join('RGB.byte.tif')) outputfile = str(data.join('export.mbtiles')) dumpdir = pytest.ensuretemp('dump') runner = CliRunner() result = runner.invoke( mbtiles, [inputfile, outputfile, '--image-dump', str(dumpdir)]) assert result.exit_code == 0 assert len(os.listdir(str(dumpdir))) == 6
def compile_so_file(): udir = pytest.ensuretemp('_ctypes_test') cfile = py.path.local(__file__).dirpath().join("_ctypes_test.c") if sys.platform == 'win32': libraries = ['oleaut32'] else: libraries = [] return c_compile([cfile], str(udir / '_ctypes_test'), libraries=libraries)
def test_export_dump(tmpdir, data): inputfile = str(data.join('RGB.byte.tif')) outputfile = str(tmpdir.join('export.mbtiles')) dumpdir = pytest.ensuretemp('dump') runner = CliRunner() result = runner.invoke( main_group, ['mbtiles', inputfile, outputfile, '--image-dump', str(dumpdir)]) assert result.exit_code == 0 assert len(os.listdir(str(dumpdir))) == 6
def test_env_vars_from_file_quoted(self): tmpdir = pytest.ensuretemp('env_file') self.addCleanup(tmpdir.remove) with codecs.open('{}/quoted.env'.format(str(tmpdir)), 'w', encoding='utf-8') as f: f.write('DOUBLE_QUOTES="testing \"double\" quotes"\n') f.write("SINGLE_QUOTES='testing \'single\' quotes'\n") assert env_vars_from_file(str(tmpdir.join('quoted.env'))) == { 'DOUBLE_QUOTES': 'testing "double" quotes', 'SINGLE_QUOTES': "testing 'single' quotes", }
def test_export_tile_size(tmpdir, data, tile_size): inputfile = str(data.join('RGB.byte.tif')) outputfile = str(tmpdir.join('export.mbtiles')) dumpdir = pytest.ensuretemp('dump') runner = CliRunner() result = runner.invoke(main_group, [ 'mbtiles', inputfile, outputfile, '--image-dump', str(dumpdir), '--tile-size', tile_size ]) dump_files = os.listdir(str(dumpdir)) assert result.exit_code == 0 with rasterio.open(os.path.join(str(dumpdir), dump_files[0]), "r") as src: assert src.shape == (tile_size, tile_size)
def compile_so_file(): from rpython.translator.platform import platform from rpython.translator.tool.cbuild import ExternalCompilationInfo udir = pytest.ensuretemp('_ctypes_test') cfile = py.path.local(__file__).dirpath().join("_ctypes_test.c") if sys.platform == 'win32': libraries = ['oleaut32'] else: libraries = [] eci = ExternalCompilationInfo(libraries=libraries) return platform.compile([cfile], eci, str(udir.join('_ctypes_test')), standalone=False)
def test_export_tile_size(tmpdir, data, tile_size): inputfile = str(data.join('RGB.byte.tif')) outputfile = str(tmpdir.join('export.mbtiles')) dumpdir = pytest.ensuretemp('dump') runner = CliRunner() result = runner.invoke( main_group, ['mbtiles', inputfile, outputfile, '--image-dump', str(dumpdir), '--tile-size', tile_size]) dump_files = os.listdir(str(dumpdir)) assert result.exit_code == 0 warnings.simplefilter('ignore') with rasterio.open(os.path.join(str(dumpdir), dump_files[0]), "r") as src: assert src.shape == (tile_size, tile_size) warnings.resetwarnings()
def compile_so_file(): from pypy.translator.platform import platform from pypy.translator.tool.cbuild import ExternalCompilationInfo udir = pytest.ensuretemp('_ctypes_test') cfile = py.path.local(__file__).dirpath().join("_ctypes_test.c") if sys.platform == 'win32': libraries = ['oleaut32'] else: libraries = [] eci = ExternalCompilationInfo(libraries=libraries) return platform.compile([cfile], eci, str(udir.join('_ctypes_test')), standalone=False)
def setup_class(cls): cls.tmpdir = pytest.ensuretemp('test_apipkg') sys.path = [str(cls.tmpdir)] + sys.path pkgdir = cls.tmpdir.ensure('realtest', dir=1) tfile = pkgdir.join('__init__.py') tfile.write( textwrap.dedent(""" import apipkg apipkg.initpkg(__name__, { 'x': { 'module': { '__doc__': '_xyz.testmodule:__doc__', 'mytest0': '_xyz.testmodule:mytest0', 'mytest1': '_xyz.testmodule:mytest1', 'MyTest': '_xyz.testmodule:MyTest', } } } ) """)) ipkgdir = cls.tmpdir.ensure("_xyz", dir=1) tfile = ipkgdir.join('testmodule.py') ipkgdir.ensure("__init__.py") tfile.write( textwrap.dedent(""" 'test module' from _xyz.othermodule import MyTest #__all__ = ['mytest0', 'mytest1', 'MyTest'] def mytest0(): pass def mytest1(): pass """)) ipkgdir.join("othermodule.py").write("class MyTest: pass")
def test_ensuretemp(recwarn): d1 = pytest.ensuretemp("hello") d2 = pytest.ensuretemp("hello") assert d1 == d2 assert d1.check(dir=1)
def setup_class(cls): cls.w_temppath = cls.space.wrap( str(pytest.ensuretemp("fileimpl").join("foo.txt"))) cls.w_file = getfile(cls.space)
def setup_class(cls): """set up temporary file to use as backing file""" cls.temp_file = str(pytest.ensuretemp('tempfiles').join('table.dat'))
def tmppath(self): if self._tmppath is None: self._tmppath = Path(str(pytest.ensuretemp('mgtest'))) return self._tmppath
def setup_class(cls): cls.w_temppath = cls.space.wrap( str(pytest.ensuretemp("fileimpl").join("foo.txt"))) cls.w_file = getfile(cls.space) cls.w_regex_search = cls.space.wrap(interp2app(regex_search))
def dockerdb(request): """Connection to a dockerized Postgresql instance filled with data.""" client = docker.Client(base_url='unix://var/run/docker.sock') curdir = os.path.dirname(os.path.abspath(request.module.__file__)) tmpdir = pytest.ensuretemp('docker') tmpdir.chdir() DBTIMEOUT = getattr(request.cls, 'DBTIMEOUT', 60) DBNAME = getattr(request.cls, 'DBNAME', 'testdb') signature = '' files = getattr(request.cls, 'DATA', []) for f in files: src = os.path.join(curdir, 'data', f) shutil.copy(src, tmpdir.strpath) with open(src) as f: content = f.read() signature = md5.md5(content + signature).hexdigest() sql = getattr(request.cls, 'SQL', []) for s in sql: signature = md5.md5(s + signature).hexdigest() dockerfile = jinja2.Template(''' FROM ubuntu:15.04 MAINTAINER Guido Amoruso <*****@*****.**> RUN apt-get update && apt-get install -y postgresql-9.4 USER postgres {% for file in FILES %} COPY {{ file }} /tmp/ {% endfor %} RUN echo {{ SIGNATURE }} RUN /etc/init.d/postgresql start \ && createdb {{ DBNAME }} \ {% for file in FILES %} && psql -d {{ DBNAME }} < /tmp/{{ file }} {% endfor %} \ {% if SQL %} && psql -d {{ DBNAME }} -c "{% for s in SQL %} {{ s }}; {% endfor %}" {% endif %} RUN /etc/init.d/postgresql stop && echo done RUN echo "host all all 0.0.0.0/0 trust" >> /etc/postgresql/9.4/main/pg_hba.conf RUN echo "listen_addresses='*'" >> /etc/postgresql/9.4/main/postgresql.conf EXPOSE 5432 VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"] CMD ["/usr/lib/postgresql/9.4/bin/postgres", "-D", "/var/lib/postgresql/9.4/main", "-c", "config_file=/etc/postgresql/9.4/main/postgresql.conf"] ''') dockerfile = dockerfile.render(FILES=files, SQL=sql, DBNAME=DBNAME, SIGNATURE=signature) dfile = tmpdir.join('Dockerfile') dfile.write(dockerfile) print "Rebuilding docker image..." img = client.build(path=tmpdir.strpath, rm=True, tag="test-xmlapi") pprint.pprint([json.loads(i) for i in img]) container = client.create_container( image='test-xmlapi', name='test-xmlapi', ports=[5432], host_config=docker.utils.create_host_config(port_bindings={ 5432: None, }), ) response = client.start(container=container.get('Id')) container = [c for c in client.containers() if c['Id'] == container['Id']][0] public_port = container['Ports'][0]['PublicPort'] t = time.time() while True: if time.time() - t > DBTIMEOUT: yield None break try: conn = psycopg2.connect(database=DBNAME, user='******', port=public_port, host='127.0.0.1') except psycopg2.OperationalError: print "Waiting for database startup..." time.sleep(2) continue except Exception, ex: print ex yield None break else: yield conn break
def setup_class(cls): if not cls.runappdirect: pytest.skip("likely to deadlock when interpreted by py.py") cls.w_temppath = cls.space.wrap( str(pytest.ensuretemp("fileimpl").join("concurrency.txt"))) cls.w_file = getfile(cls.space)
def setup_class(cls): from mmap_backed_array import mmaparray, _mmap cls.mmaparray = mmaparray cls._mmap = _mmap cls.tempfile = str(pytest.ensuretemp('mmaparray').join('tmpfile'))
def test_ensuretemp(recwarn): #pytest.deprecated_call(pytest.ensuretemp, 'hello') d1 = pytest.ensuretemp('hello') d2 = pytest.ensuretemp('hello') assert d1 == d2 assert d1.check(dir=1)
def gpath(): """ Returns a temp filename for graph storage """ return pytest.ensuretemp("graphs").join("graph.png").strpath
def setup_class(cls): cls.w_temppath = cls.space.wrap( str(pytest.ensuretemp("signal").join("foo.txt"))) cls.w_appdirect = cls.space.wrap(cls.runappdirect)