def installed_demoapp(dist=None, path=None, demoapp='demoapp'): if path is None: path = os.getcwd() path = os.path.join(path, demoapp, "dist", "{}*".format(demoapp)) if dist == 'bdist': with chdir('/'): output = untar(path) install_dirs = list() install_bin = None for line in output: if re.search(r".*/site-packages/{}.*?/$".format(demoapp), line): install_dirs.append(line) if re.search(r".*/bin/{}$".format(demoapp), line): install_bin = line elif dist == 'install': pass else: pip("install", path) try: yield finally: if dist == 'bdist': with chdir('/'): os.remove(install_bin) for path in install_dirs: rmtree(path, ignore_errors=True) else: pip("uninstall", "-y", demoapp)
def test_generated_extension(tmpfolder, venv_run): args = ["--custom-extension", "pyscaffoldext-some_extension"] opts = parse_args(args) create_project(opts) with chdir("pyscaffoldext-some_extension"): assert '' == venv_run("flake8") venv_run("python setup.py install") venv_run("putup --some-extension the_actual_project") assert path_exists("the_actual_project/setup.cfg") with chdir("the_actual_project"): assert '' == venv_run("flake8")
def test_generated_extension_flake8(tmpfolder, venv_run): args = [EXT_FLAG, "my_project"] opts = parse_args(args) opts = process_opts(opts) create_project(opts) with chdir("my_project"): assert "" == venv_run(FLAKE8) venv_run("python setup.py install") venv_run("putup {ext_flag} the_actual_project".format(ext_flag=EXT_FLAG)) assert path_exists("the_actual_project/setup.cfg") with chdir("the_actual_project"): assert "" == venv_run(FLAKE8)
def test_git_repo_with_1_0_tag(tmpfolder): create_demoapp() add_tag('demoapp', 'v1.0', 'final release') with installed_demoapp('install'), chdir('demoapp'): out = next(setup_py('--version')) exp = '1.0' check_version(out, exp, dirty=False)
def _generate(self): putup([self.name]) with chdir(self.name): demoapp_src_dir = path_join(__location__, self.name) demoapp_dst_root = self.pkg_path demoapp_dst_pkg = path_join(demoapp_dst_root, "src", self.name) copyfile( path_join(demoapp_src_dir, "runner.py"), path_join(demoapp_dst_pkg, "runner.py"), ) git("add", path_join(demoapp_dst_pkg, "runner.py")) copyfile( path_join(demoapp_src_dir, "setup.cfg"), path_join(demoapp_dst_root, "setup.cfg"), ) copyfile( path_join(demoapp_src_dir, "setup.py"), path_join(demoapp_dst_root, "setup.py"), ) git("add", path_join(demoapp_dst_root, "setup.cfg")) git("add", path_join(demoapp_dst_root, "setup.py")) if self.data: data_src_dir = path_join(demoapp_src_dir, "data") data_dst_dir = path_join(demoapp_dst_pkg, "data") os.mkdir(data_dst_dir) copyfile( path_join(data_src_dir, "hello_world.txt"), path_join(data_dst_dir, "hello_world.txt"), ) git("add", path_join(data_dst_dir, "hello_world.txt")) git("commit", "-m", "Added basic application logic") # this is needed for Windows 10 which lacks some certificats self.run("pip", "install", "-q", "certifi")
def build(self, dist='bdist'): with self.guard('built'), chdir(self.pkg_path): if 'wheel' in dist: self.run('pip', 'install', 'wheel') self.run('python', 'setup.py', dist) self.dist = dist return self
def test_git_repo(tmpdir): # noqa create_demoapp() build_demoapp('install') with chdir('demoapp'): out = list(setup_py('version'))[-1] exp = '0.0.post0.dev1' check_version(out, exp, dirty=False)
def create_demoapp(data=False): if data: demoapp = 'demoapp_data' else: demoapp = 'demoapp' putup([demoapp]) with chdir(demoapp): demoapp_src_dir = os.path.join(__location__, demoapp) demoapp_dst_dir = os.path.join(os.getcwd(), demoapp) copyfile(os.path.join(demoapp_src_dir, 'runner.py'), os.path.join(demoapp_dst_dir, 'runner.py')) git('add', os.path.join(demoapp_dst_dir, 'runner.py')) demoapp_dst_dir = os.getcwd() copyfile(os.path.join(demoapp_src_dir, 'setup.cfg'), os.path.join(demoapp_dst_dir, 'setup.cfg')) git('add', os.path.join(demoapp_dst_dir, 'setup.cfg')) if data: data_src_dir = os.path.join(demoapp_src_dir, 'data') data_dst_dir = os.path.join(os.getcwd(), demoapp, 'data') os.mkdir(data_dst_dir) copyfile(os.path.join(data_src_dir, 'hello_world.txt'), os.path.join(data_dst_dir, 'hello_world.txt')) git('add', os.path.join(data_dst_dir, 'hello_world.txt')) git('commit', '-m', 'Added basic progamme logic')
def _generate(self): putup([self.name]) with chdir(self.name): demoapp_src_dir = path_join(__location__, self.name) demoapp_dst_root = self.pkg_path demoapp_dst_pkg = path_join(demoapp_dst_root, 'src', self.name) copyfile(path_join(demoapp_src_dir, 'runner.py'), path_join(demoapp_dst_pkg, 'runner.py')) git('add', path_join(demoapp_dst_pkg, 'runner.py')) copyfile(path_join(demoapp_src_dir, 'setup.cfg'), path_join(demoapp_dst_root, 'setup.cfg')) copyfile(path_join(demoapp_src_dir, 'setup.py'), path_join(demoapp_dst_root, 'setup.py')) git('add', path_join(demoapp_dst_root, 'setup.cfg')) git('add', path_join(demoapp_dst_root, 'setup.py')) if self.data: data_src_dir = path_join(demoapp_src_dir, 'data') data_dst_dir = path_join(demoapp_dst_pkg, 'data') os.mkdir(data_dst_dir) copyfile(path_join(data_src_dir, 'hello_world.txt'), path_join(data_dst_dir, 'hello_world.txt')) git('add', path_join(data_dst_dir, 'hello_world.txt')) git('commit', '-m', 'Added basic application logic') # this is needed for Windows 10 which lacks some certificats self.run('pip', 'install', '-q', 'certifi')
def test_git_repo_with_1_0_tag_dirty(tmpdir): # noqa create_demoapp() add_tag('demoapp', 'v1.0', 'final release') make_dirty_tree() with installed_demoapp('install'), chdir('demoapp'): out = next(setup_py('--version')) exp = '1.0' check_version(out, exp, dirty=True)
def test_git_repo_with_1_0_tag(tmpdir): # noqa create_demoapp() add_tag('demoapp', 'v1.0', 'final release') build_demoapp('install') with chdir('demoapp'): out = list(setup_py('version'))[-1] exp = '1.0' check_version(out, exp, dirty=False)
def test_putup_with_update_dirty_workspace(cwd): run("putup myproj") with chdir("myproj"): with open("setup.py", "w") as fh: fh.write("DIRTY") with pytest.raises(CalledProcessError): run("putup --update myproj") run("putup --update myproj --force")
def test_putup_with_update_dirty_workspace(cwd): run('putup myproj') with chdir('myproj'): with open('setup.py', 'w') as fh: fh.write('DIRTY') with pytest.raises(CalledProcessError): run('putup --update myproj') run('putup --update myproj --force')
def test_version_of_subdir(tmpdir): # noqa projects = ["main_project", "inner_project"] for project in projects: opts = cli.parse_args([project]) opts = cli.get_default_opts(opts['project'], **opts) struct = structure.make_structure(opts) structure.create_structure(struct) repo.init_commit_repo(project, struct) shutil.rmtree(os.path.join('inner_project', '.git')) shutil.move('inner_project', 'main_project/inner_project') with utils.chdir('main_project'): main_version = subprocess.check_output([ 'python', 'setup.py', '--version']).strip() with utils.chdir('inner_project'): inner_version = subprocess.check_output([ 'python', 'setup.py', '--version']).strip() assert main_version == inner_version
def test_version_of_subdir(tmpdir): # noqa projects = ["main_project", "inner_project"] for project in projects: opts = cli.parse_args([project]) opts = cli.get_default_opts(opts['project'], **opts) struct = structure.make_structure(opts) structure.create_structure(struct) repo.init_commit_repo(project, struct) shutil.rmtree(os.path.join('inner_project', '.git')) shutil.move('inner_project', 'main_project/inner_project') with utils.chdir('main_project'): main_version = subprocess.check_output( ['python', 'setup.py', '--version']).strip() with utils.chdir('inner_project'): inner_version = subprocess.check_output( ['python', 'setup.py', '--version']).strip() assert main_version == inner_version
def test_version_of_subdir(tmpfolder): projects = ["main_project", "inner_project"] for project in projects: opts = cli.parse_args([project]) _, opts = api.get_default_options({}, opts) struct, _ = structure.define_structure({}, opts) struct, _ = structure.apply_update_rules(struct, opts) structure.create_structure(struct, {}) repo.init_commit_repo(project, struct) shutil.rmtree(os.path.join('inner_project', '.git')) shutil.move('inner_project', 'main_project/inner_project') with utils.chdir('main_project'): main_version = subprocess.check_output( ['python', 'setup.py', '--version']).strip().splitlines()[-1] with utils.chdir('inner_project'): inner_version = subprocess.check_output( ['python', 'setup.py', '--version']).strip().splitlines()[-1] assert main_version.strip() == inner_version.strip()
def test_git_archive(tmpdir): # noqa create_demoapp() add_tag('demoapp', 'v1.0', 'final release') build_demoapp('git_archive') untar(os.path.join('demoapp', 'dist', 'demoapp.tar.gz')) with chdir('demoapp_unpacked'): out = list(setup_py('version'))[-1] exp = '1.0' check_version(out, exp, dirty=False)
def test_version_of_subdir(tmpfolder): projects = ["main_project", "inner_project"] for project in projects: opts = cli.parse_args([project]) _, opts = api.get_default_options({}, opts) struct, _ = structure.define_structure({}, opts) struct, _ = structure.apply_update_rules(struct, opts) structure.create_structure(struct, {}) repo.init_commit_repo(project, struct) shutil.rmtree(os.path.join('inner_project', '.git')) shutil.move('inner_project', 'main_project/inner_project') with utils.chdir('main_project'): main_version = subprocess.check_output([ 'python', 'setup.py', '--version']).strip().splitlines()[-1] with utils.chdir('inner_project'): inner_version = subprocess.check_output([ 'python', 'setup.py', '--version']).strip().splitlines()[-1] assert main_version.strip() == inner_version.strip()
def test_git_repo_dirty(tmpfolder): create_demoapp() add_tag('demoapp', 'v0.1', 'first tag') make_dirty_tree() make_commit() make_dirty_tree() with installed_demoapp('install'), chdir('demoapp'): out = next(setup_py('--version')) exp = '0.1.post0.dev1' check_version(out, exp, dirty=True)
def install(self, edit=False): with self.guard('installed'), chdir(self.pkg_path): self.check_not_installed() if edit or self.dist is None: self.run('pip', 'install', '-e', '.') elif self.dist == 'bdist': self._install_bdist() else: self.run('pip', 'install', self.dist_file) return self
def test_get_git_root_with_nogit(tmpdir, nogit_mock): # noqa project = "my_project" struct = {project: { "my_file": "Some other content", "my_dir": {"my_file": "Some more content"}} } structure.create_structure(struct) with utils.chdir(project): git_root = repo.get_git_root(default='.') assert git_root == '.'
def run(self, cmd, with_coverage=False, **kwargs): if with_coverage: kwargs.setdefault('pytestconfig', self.pytestconfig) # change to directory where .coverage needs to be created kwargs.setdefault('cd', os.getcwd()) return self.venv.run_with_coverage(cmd, **kwargs).strip() else: with chdir(self.tmpdir): kwargs.setdefault('cwd', self.tmpdir) return self.venv.run(cmd, capture=True, **kwargs).strip()
def test_get_git_root_with_nonegit(tmpfolder, nonegit_mock): project = "my_project" struct = {project: { "my_file": "Some other content", "my_dir": {"my_file": "Some more content"}} } structure.create_structure(struct, {}) with utils.chdir(project): git_root = repo.get_git_root(default='.') assert git_root == '.'
def test_chdir(): curr_dir = os.getcwd() try: temp_dir = tempfile.mkdtemp() with utils.chdir(temp_dir): new_dir = os.getcwd() assert new_dir == os.path.realpath(temp_dir) assert curr_dir == os.getcwd() finally: os.rmdir(temp_dir)
def build(self, dist="bdist", cli_opts=()): with self.guard("built"), chdir(self.pkg_path): if "wheel" in dist: self.run("pip", "install", "wheel") else: cli_opts = cli_opts or ["--format", "gztar"] # ^ force tar.gz (Windows defaults to zip) self.run("python", "setup.py", dist, *cli_opts) self.dist = dist return self
def install(self, edit=False): with self.guard("installed"), chdir(self.pkg_path): self.check_not_installed() if edit or self.dist is None: self.run("pip", "install", "-e", ".") elif self.dist == "bdist": self._install_bdist() else: self.run("pip", "install", self.dist_file) return self
def test_parentdir(tmpdir): # noqa create_demoapp() add_tag('demoapp', 'v1.0', 'final release') build_demoapp('sdist') path = os.path.join("demoapp", "dist", "demoapp*") untar(path) with chdir('demoapp-1.0'): out = list(setup_py('version'))[-1] exp = '1.0' check_version(out, exp, dirty=False)
def test_get_git_root(tmpfolder): project = "my_project" struct = {project: { "my_file": "Some other content", "my_dir": {"my_file": "Some more content"}} } structure.create_structure(struct, {}) repo.init_commit_repo(project, struct) with utils.chdir(project): git_root = repo.get_git_root() assert os.path.basename(git_root) == project
def test_get_git_root(tmpdir): # noqa project = "my_project" struct = {project: { "my_file": "Some other content", "my_dir": {"my_file": "Some more content"}} } structure.create_structure(struct) repo.init_commit_repo(project, struct) with utils.chdir(project): git_root = repo.get_git_root() assert os.path.basename(git_root) == project
def test_chdir(caplog): curr_dir = os.getcwd() try: temp_dir = tempfile.mkdtemp() with utils.chdir(temp_dir, log=True): new_dir = os.getcwd() assert new_dir == os.path.realpath(temp_dir) assert curr_dir == os.getcwd() assert "chdir" in last_log(caplog) finally: os.rmdir(temp_dir)
def test_pretend_chdir(caplog, tmpdir): caplog.set_level(logging.INFO) curr_dir = os.getcwd() dname = uniqstr() # Use a unique name to get easily identifiable logs temp_dir = str(tmpdir.mkdir(dname)) with utils.chdir(temp_dir, pretend=True): new_dir = os.getcwd() assert new_dir == curr_dir # the directory is not changed assert curr_dir == os.getcwd() logs = caplog.text assert re.search("chdir.+" + dname, logs)
def test_pretend_chdir(caplog): curr_dir = os.getcwd() try: temp_dir = tempfile.mkdtemp() with utils.chdir(temp_dir, pretend=True): new_dir = os.getcwd() assert new_dir == curr_dir # the directory is not changed assert curr_dir == os.getcwd() assert "chdir" in last_log(caplog) finally: os.rmdir(temp_dir)
def test_pretend_chdir(caplog): caplog.set_level(logging.INFO) curr_dir = os.getcwd() try: temp_dir = tempfile.mkdtemp() with utils.chdir(temp_dir, pretend=True): new_dir = os.getcwd() assert new_dir == curr_dir # the directory is not changed assert curr_dir == os.getcwd() assert "chdir" in last_log(caplog) finally: os.rmdir(temp_dir)
def test_chdir(caplog, tmpdir, isolated_logger): caplog.set_level(logging.INFO) curr_dir = os.getcwd() dname = uniqstr() # Use a unique name to get easily identifiable logs temp_dir = str(tmpdir.mkdir(dname)) with utils.chdir(temp_dir, log=True): new_dir = os.getcwd() assert new_dir == os.path.realpath(temp_dir) assert curr_dir == os.getcwd() assert curr_dir != new_dir logs = caplog.text assert re.search("chdir.+" + dname, logs)
def test_chdir(caplog): caplog.set_level(logging.INFO) curr_dir = os.getcwd() try: temp_dir = tempfile.mkdtemp() with utils.chdir(temp_dir, log=True): new_dir = os.getcwd() assert new_dir == os.path.realpath(temp_dir) assert curr_dir == os.getcwd() assert "chdir" in last_log(caplog) finally: os.rmdir(temp_dir)
def build_demoapp(dist, path=None, demoapp='demoapp'): if path is None: path = os.getcwd() path = os.path.join(path, demoapp) with chdir(path): if dist == 'git_archive': os.mkdir('dist') filename = os.path.join('dist', '{}.tar.gz'.format(demoapp)) git('archive', '--format', 'tar.gz', '--output', filename, '--prefix', '{}_unpacked/'.format(demoapp), 'HEAD') else: setup_py(dist)
def __init__(self, tmpdir, venv, data=None): self.name = 'demoapp' if data: self.name += '_data' self.pkg_path = path_join(str(tmpdir), self.name) self.built = False self.installed = False self.venv = venv self.venv_path = str(venv.virtualenv) self.data = data self.dist = None with chdir(str(tmpdir)): self._generate()
def _generate(self): putup([self.name]) with chdir(self.name): demoapp_src_dir = path_join(__location__, self.name) demoapp_dst_root = self.pkg_path demoapp_dst_pkg = path_join(demoapp_dst_root, 'src', self.name) copyfile(path_join(demoapp_src_dir, 'runner.py'), path_join(demoapp_dst_pkg, 'runner.py')) git('add', path_join(demoapp_dst_pkg, 'runner.py')) copyfile(path_join(demoapp_src_dir, 'setup.cfg'), path_join(demoapp_dst_root, 'setup.cfg')) copyfile(path_join(demoapp_src_dir, 'setup.py'), path_join(demoapp_dst_root, 'setup.py')) git('add', path_join(demoapp_dst_root, 'setup.cfg')) git('add', path_join(demoapp_dst_root, 'setup.py')) if self.data: data_src_dir = path_join(demoapp_src_dir, 'data') data_dst_dir = path_join(demoapp_dst_pkg, 'data') os.mkdir(data_dst_dir) copyfile(path_join(data_src_dir, 'hello_world.txt'), path_join(data_dst_dir, 'hello_world.txt')) git('add', path_join(data_dst_dir, 'hello_world.txt')) git('commit', '-m', 'Added basic application logic')
def tag(self, name, message): with chdir(self.pkg_path): git('tag', '-a', name, '-m', message) return self
def _install_bdist(self): with chdir('/'): # Because of the way bdist works, the tar.gz will contain # the whole path to the current venv, starting from the # / directory ... untar(self.dist_file)
def make_commit(demoapp='demoapp'): with chdir(demoapp): git('commit', '-a', '-m', 'message')
def test_git_repo(tmpfolder): create_demoapp() with installed_demoapp('install'), chdir('demoapp'): out = next(setup_py('--version')) exp = '0.0.post0.dev2' check_version(out, exp, dirty=False)
def make_dirty_tree(demoapp='demoapp'): dirty_file = os.path.join(demoapp, 'runner.py') with chdir(demoapp): with open(dirty_file, 'a') as fh: fh.write("\n\ndirty_variable = 69\n")
def setup_py(self, *args, **kwargs): with chdir(self.pkg_path): args = ['python', 'setup.py'] + list(args) return self.run(*args, **kwargs)
def make_commit(self): with chdir(self.pkg_path): git('commit', '-a', '-m', 'message') return self