def virtualenv_template( request: pytest.FixtureRequest, tmpdir_factory: pytest.TempdirFactory, pip_src: Path, setuptools_install: Path, coverage_install: Path, ) -> Iterator[VirtualEnvironment]: venv_type: VirtualEnvironmentType if request.config.getoption("--use-venv"): venv_type = "venv" else: venv_type = "virtualenv" # Create the virtual environment tmpdir = Path(str(tmpdir_factory.mktemp("virtualenv"))) venv = VirtualEnvironment(tmpdir.joinpath("venv_orig"), venv_type=venv_type) # Install setuptools and pip. install_egg_link(venv, "setuptools", setuptools_install) pip_editable = Path(str(tmpdir_factory.mktemp("pip"))) / "pip" shutil.copytree(pip_src, pip_editable, symlinks=True) # noxfile.py is Python 3 only assert compileall.compile_dir( str(pip_editable), quiet=1, rx=re.compile("noxfile.py$"), ) subprocess.check_call( [venv.bin / "python", "setup.py", "-q", "develop"], cwd=pip_editable ) # Install coverage and pth file for executing it in any spawned processes # in this virtual environment. install_egg_link(venv, "coverage", coverage_install) # zz prefix ensures the file is after easy-install.pth. with open(venv.site / "zz-coverage-helper.pth", "a") as f: f.write("import coverage; coverage.process_startup()") # Drop (non-relocatable) launchers. for exe in os.listdir(venv.bin): if not ( exe.startswith("python") or exe.startswith("libpy") # Don't remove libpypy-c.so... ): (venv.bin / exe).unlink() # Enable user site packages. venv.user_site_packages = True # Rename original virtualenv directory to make sure # it's not reused by mistake from one of the copies. venv_template = tmpdir / "venv_template" venv.move(venv_template) yield venv
def clean_project(tmpdir_factory: pytest.TempdirFactory, data: TestData) -> Path: tmpdir = Path(str(tmpdir_factory.mktemp("clean_project"))) new_project_dir = tmpdir.joinpath("FSPkg") path = data.packages.joinpath("FSPkg") shutil.copytree(path, new_project_dir) return new_project_dir
def pip_src(tmpdir_factory: pytest.TempdirFactory) -> Path: def not_code_files_and_folders(path: str, names: List[str]) -> Iterable[str]: # In the root directory... if path == SRC_DIR: # ignore all folders except "src" folders = { name for name in names if os.path.isdir(os.path.join(path, name)) } to_ignore = folders - {"src"} # and ignore ".git" if present (which may be a file if in a linked # worktree). if ".git" in names: to_ignore.add(".git") return to_ignore # Ignore all compiled files and egg-info. ignored = set() for pattern in ("__pycache__", "*.pyc", "pip.egg-info"): ignored.update(fnmatch.filter(names, pattern)) return ignored pip_src = Path(str(tmpdir_factory.mktemp("pip_src"))).joinpath("pip_src") # Copy over our source tree so that each use is self contained shutil.copytree( SRC_DIR, pip_src.resolve(), ignore=not_code_files_and_folders, ) return pip_src
def image_dir(tmpdir_factory: pytest.TempdirFactory): """Autoremoved directory for storing images, Using \ ``TempdirFactory`` understood.""" directory = Path(tmpdir_factory.mktemp("test_image")) yield directory shutil.rmtree(directory)
def test_PNGProvider_exception(tmpdir_factory: pytest.TempdirFactory) -> None: """Testing ``clickgen.util.PNGProvider`` throwing **FileNotFoundError** \ exception on non exists file. """ directory = Path(tmpdir_factory.mktemp("tt")) with pytest.raises(FileNotFoundError) as excinfo: PNGProvider(directory) assert str(excinfo.value ) == f"'*.png' files not found in '{directory.absolute()}'" shutil.rmtree(directory)
def _common_wheel_editable_install( tmpdir_factory: pytest.TempdirFactory, common_wheels: Path, package: str ) -> Path: wheel_candidates = list(common_wheels.glob(f"{package}-*.whl")) assert len(wheel_candidates) == 1, wheel_candidates install_dir = Path(str(tmpdir_factory.mktemp(package))) / "install" Wheel(wheel_candidates[0]).install_as_egg(install_dir) (install_dir / "EGG-INFO").rename(install_dir / f"{package}.egg-info") assert compileall.compile_dir(str(install_dir), quiet=1) return install_dir
def downloads_directory(tmpdir_factory: pytest.TempdirFactory): """ Folder in which browser downloads will be stored If SELENOID_HOST env variable is provided, then no directory is created and path to selenoid downloads returned as string """ if os.environ.get("SELENOID_HOST"): return SELENOID_DOWNLOADS_PATH downloads_dirname = 'browser-downloads' return tmpdir_factory.mktemp(downloads_dirname)
def script_with_launchers( tmpdir_factory: pytest.TempdirFactory, script_factory: ScriptFactory, common_wheels: Path, pip_src: Path, ) -> PipTestEnvironment: tmpdir = Path(str(tmpdir_factory.mktemp("script_with_launchers"))) script = script_factory(tmpdir.joinpath("workspace")) # Re-install pip so we get the launchers. script.pip_install_local("-f", common_wheels, pip_src) return script
def test_WindowsPackager_with_website_url( tmpdir_factory: pytest.TempdirFactory, ) -> None: """Testing ``clickgen.packagers.WindowsPackager`` default parameters \ with custom value.""" d = Path(tmpdir_factory.mktemp("test_image")) create_test_cursor(d, "Work.ani") create_test_cursor(d, "Busy.ani") create_test_cursor(d, "Default.cur") create_test_cursor(d, "Help.cur") create_test_cursor(d, "Link.cur") create_test_cursor(d, "Move.cur") create_test_cursor(d, "Diagonal_2.cur") create_test_cursor(d, "Vertical.cur") create_test_cursor(d, "Horizontal.cur") create_test_cursor(d, "Diagonal_1.cur") create_test_cursor(d, "Handwriting.cur") create_test_cursor(d, "Cross.cur") create_test_cursor(d, "IBeam.cur") create_test_cursor(d, "Unavailiable.cur") create_test_cursor(d, "Alternate.cur") WindowsPackager( d, theme_name="test", comment="testing", author="Unknown", website_url="testing.test", ) install_file = d / "install.inf" assert install_file.exists() is True data = install_file.read_text() assert "Work.ani" in data assert "Busy.ani" in data assert "Default.cur" in data assert "Help.cur" in data assert "Link.cur" in data assert "Move.cur" in data assert "Diagonal_2.cur" in data assert "Vertical.cur" in data assert "Horizontal.cur" in data assert "Diagonal_1.cur" in data assert "Handwriting.cur" in data assert "Cross.cur" in data assert "IBeam.cur" in data assert "Unavailiable.cur" in data assert "Alternate.cur" in data # assert "testing.test" in data shutil.rmtree(d)
def simple3_nwb(simple1_nwb_metadata: Dict[str, Any], tmpdir_factory: pytest.TempdirFactory) -> str: """With a subject, but no subject_id.""" return make_nwb_file( str(tmpdir_factory.mktemp("simple2").join("simple2.nwb")), subject=pynwb.file.Subject( date_of_birth=datetime(2016, 12, 1, tzinfo=tzutc()), sex="M", species="mouse", ), **simple1_nwb_metadata, )
def test_main_modular_filename(tmpdir_factory: TempdirFactory) -> None: """Test main function on modular file with filename.""" output_directory = Path(tmpdir_factory.mktemp('output')) input_filename = OPEN_API_DATA_PATH / 'modular.yaml' output_filename = output_directory / 'model.py' assert (main( ['--input', str(input_filename), '--output', str(output_filename)]) == Exit.ERROR)
def run( *, script_file: str, tmpdir_factory: pytest.TempdirFactory, arg: str = '', setup: str = '', additional_intercepts: Iterable[str] = (), ) -> subprocess.CompletedProcess: """Invokes the given script within a temporary test environment.""" with open(script_file) as f: script_lines = f.readlines() # Create a unique temporary directory dir_path = tmpdir_factory.mktemp('tmp', numbered=True) file_path = os.path.join(dir_path, 'test-script.sh') intercepted = [ 'python', 'python3', 'pylint', 'env', 'pytest', 'mypy', 'black', *additional_intercepts, ] assert script_lines[0] == '#!/usr/bin/env bash\n' for e in intercepted: script_lines.insert(1, e + '() {\n echo INTERCEPTED ' + e + ' $@\n}\n') with open(file_path, 'w') as f: f.writelines(script_lines) cmd = r""" dir=$(git rev-parse --show-toplevel) cd {} git init --quiet --initial-branch master git config --local user.name 'Me' git config --local user.email '<>' git commit -m init --allow-empty --quiet --no-gpg-sign {} mkdir -p dev_tools touch dev_tools/pypath chmod +x ./test-script.sh ./test-script.sh {} """.format( dir_path, setup, arg ) return shell_tools.run( cmd, log_run_to_stderr=False, shell=True, check=False, capture_output=True )
def test_add_missing_xcursors_exception( data, tmpdir_factory: pytest.TempdirFactory) -> None: """Testing ``clickgen.util.add_missing_xcursors`` **NotADirectoryError** \ exception on *non exists directory* or *passing a file* inside parameter. """ d = tmpdir_factory.mktemp("ffff") tmp_dir = Path(d) shutil.rmtree(tmp_dir) with pytest.raises(NotADirectoryError) as excinfo: add_missing_xcursors(tmp_dir, data) assert str(excinfo.value) == str(tmp_dir.absolute())
def test_WindowsPackager_with_semi_animated_cursors( tmpdir_factory: pytest.TempdirFactory, ) -> None: """Testing ``clickgen.packagers.WindowsPackager`` supports 'semi-animated' \ cursors. This test generates all .ani (animated) cursors and passed them to \ **WindowsPackager**. And checks, It generates valid ``install.inf`` file \ with appropriate cursor-type or not. """ d = Path(tmpdir_factory.mktemp("test_image")) create_test_cursor(d, "Work.ani") create_test_cursor(d, "Busy.ani") create_test_cursor(d, "Default.ani") create_test_cursor(d, "Help.ani") create_test_cursor(d, "Link.ani") create_test_cursor(d, "Move.ani") create_test_cursor(d, "Diagonal_2.ani") create_test_cursor(d, "Vertical.ani") create_test_cursor(d, "Horizontal.ani") create_test_cursor(d, "Diagonal_1.ani") create_test_cursor(d, "Handwriting.ani") create_test_cursor(d, "Cross.ani") create_test_cursor(d, "IBeam.ani") create_test_cursor(d, "Unavailiable.ani") create_test_cursor(d, "Alternate.ani") WindowsPackager(d, theme_name="test", comment="testing", author="Unknown") install_file = d / "install.inf" assert install_file.exists() is True data = install_file.read_text() assert "Work.ani" in data assert "Busy.ani" in data assert "Default.ani" in data assert "Help.ani" in data assert "Link.ani" in data assert "Move.ani" in data assert "Diagonal_2.ani" in data assert "Vertical.ani" in data assert "Horizontal.ani" in data assert "Diagonal_1.ani" in data assert "Handwriting.ani" in data assert "Cross.ani" in data assert "IBeam.ani" in data assert "Unavailiable.ani" in data assert "Alternate.ani" in data shutil.rmtree(d)
def test_add_missing_xcursors_without_rename_and_force( data, tmpdir_factory: pytest.TempdirFactory) -> None: d = tmpdir_factory.mktemp("ffff") tmp_dir = Path(d) images = create_test_image(tmp_dir, 2, key="fffff0") with CursorAlias.from_bitmap(images, (0, 0)) as alias: config_file = alias.create((10, 10), delay=2) XCursor.create(config_file, tmp_dir) x_dir = tmp_dir / "cursors" add_missing_xcursors(x_dir, data) assert list(map(lambda x: x.name, x_dir.iterdir())) == ["fffff0"] shutil.rmtree(tmp_dir)
def test_main_modular(tmpdir_factory: TempdirFactory) -> None: """Test main function on modular file.""" output_directory = Path(tmpdir_factory.mktemp('output')) input_filename = OPEN_API_DATA_PATH / 'modular.yaml' output_path = output_directory / 'model' with freeze_time(TIMESTAMP): main(['--input', str(input_filename), '--output', str(output_path)]) main_modular_dir = EXPECTED_MAIN_KR_PATH / 'main_modular' for path in main_modular_dir.rglob('*.py'): result = output_path.joinpath( path.relative_to(main_modular_dir)).read_text() assert result == path.read_text()
def pip_test_package_script( tmpdir_factory: pytest.TempdirFactory, script_factory: ScriptFactory, shared_data: TestData, ) -> PipTestEnvironment: tmpdir = Path(str(tmpdir_factory.mktemp("pip_test_package"))) script = script_factory(tmpdir.joinpath("workspace")) script.pip("install", "-f", shared_data.find_links, "--no-index", "simple==1.0") script.pip( "install", "-e", "git+https://github.com/pypa/pip-test-package.git#egg=pip-test-package", ) return script
def simple_script( tmpdir_factory: pytest.TempdirFactory, script_factory: ScriptFactory, shared_data: TestData, ) -> PipTestEnvironment: tmpdir = Path(str(tmpdir_factory.mktemp("pip_test_package"))) script = script_factory(tmpdir.joinpath("workspace")) script.pip( "install", "-f", shared_data.find_links, "--no-index", "simple==1.0", "simple2==3.0", ) return script
def test_WindowsPackger_install_and_uninstall_scripts( tmpdir_factory: pytest.TempdirFactory, ) -> None: """Test the ``clickgen.packagers.WindowsPackager`` functionality.""" d = Path(tmpdir_factory.mktemp("test_image")) create_test_cursor(d, "Work.ani") create_test_cursor(d, "Busy.ani") create_test_cursor(d, "Default.cur") create_test_cursor(d, "Help.cur") create_test_cursor(d, "Link.cur") create_test_cursor(d, "Move.cur") create_test_cursor(d, "Diagonal_2.cur") create_test_cursor(d, "Vertical.cur") create_test_cursor(d, "Horizontal.cur") create_test_cursor(d, "Diagonal_1.cur") create_test_cursor(d, "Handwriting.cur") create_test_cursor(d, "Cross.cur") create_test_cursor(d, "IBeam.cur") create_test_cursor(d, "Unavailiable.cur") create_test_cursor(d, "Alternate.cur") WindowsPackager(d, theme_name="test", comment="testing", author="Unknown") install_script = d / "install.inf" assert install_script.exists() is True install_data = install_script.read_text() assert "test Cursors" in install_data assert "Work.ani" in install_data assert "Busy.ani" in install_data assert "Default.cur" in install_data assert "Help.cur" in install_data assert "Link.cur" in install_data assert "Move.cur" in install_data assert "Diagonal_2.cur" in install_data assert "Vertical.cur" in install_data assert "Horizontal.cur" in install_data assert "Diagonal_1.cur" in install_data assert "Handwriting.cur" in install_data assert "Cross.cur" in install_data assert "IBeam.cur" in install_data assert "Unavailiable.cur" in install_data assert "Alternate.cur" in install_data uninstall_script = d / "uninstall.bat" assert uninstall_script.exists() is True uninstall_data = uninstall_script.read_text() assert "test Cursors" in uninstall_data
def test_add_missing_xcursors_with_rename_and_force( data, tmpdir_factory: pytest.TempdirFactory) -> None: d = tmpdir_factory.mktemp("ffff") tmp_dir = Path(d) images = create_test_image(tmp_dir, 2, key="ffffd") with CursorAlias.from_bitmap(images, (0, 0)) as alias: config_file = alias.create((10, 10), delay=2) XCursor.create(config_file, tmp_dir) x_dir = tmp_dir / "cursors" add_missing_xcursors(x_dir, data, rename=True) with pytest.raises(FileExistsError): add_missing_xcursors(x_dir, data, rename=True) add_missing_xcursors(x_dir, data, rename=True, force=True) assert sorted(list(map(lambda x: x.name, x_dir.iterdir()))) == sorted(["ddddd", "ffffff"]) shutil.rmtree(tmp_dir)
def test_PNGProvider_get(tmpdir_factory: pytest.TempdirFactory) -> None: """Testing ``clickgen.util.PNGProvider.get`` method.""" d = tmpdir_factory.mktemp("ffff") directory = Path(d) # static images = create_test_image(directory, 1, key="static") p = PNGProvider(directory) assert p.get("static") == images[0] # animated images1 = create_test_image(directory, 4, key="animated") p1 = PNGProvider(directory) p1_output = p1.get("animated") assert isinstance(p1_output, list) assert sorted(p1_output) == sorted(images1) shutil.rmtree(d)
def existing_dir(tmpdir_factory: pytest.TempdirFactory) -> str: ed = str(tmpdir_factory.mktemp("existing_dir")) print("\n*** Created temporary dir for current session: ", ed) return ed
def shared_data(tmpdir_factory: pytest.TempdirFactory) -> TestData: return TestData.copy(Path(str(tmpdir_factory.mktemp("data"))))
def simple1_nwb(simple1_nwb_metadata: Dict[str, Any], tmpdir_factory: pytest.TempdirFactory) -> str: return make_nwb_file( str(tmpdir_factory.mktemp("simple1").join("simple1.nwb")), **simple1_nwb_metadata, )
def shared_script(tmpdir_factory: pytest.TempdirFactory, script_factory: ScriptFactory) -> PipTestEnvironment: tmpdir = Path(str(tmpdir_factory.mktemp("download_shared_script"))) script = script_factory(tmpdir.joinpath("workspace")) return script
def autocomplete_script( tmpdir_factory: pytest.TempdirFactory, script_factory: ScriptFactory ) -> PipTestEnvironment: tmpdir = Path(str(tmpdir_factory.mktemp("autocomplete_script"))) return script_factory(tmpdir.joinpath("workspace"))