def test_pretend_move_old_package(tmpfolder, caplog, isolated_logger): # Given a package is already created without namespace create_project(project="proj", package="my_pkg") opts = parse_args( ["proj", "-p", "my_pkg", "--namespace", "my.ns", "--pretend"]) opts = process_opts(opts) configure_logger(opts) struct = dict(proj={'src': {'my_pkg': {'file.py': ''}}}) # when 'pretend' option is passed, struct, opts = get_default_options(struct, opts) struct, opts = enforce_namespace_options(struct, opts) struct, opts = move_old_package(struct, opts) # then nothing should happen, assert tmpfolder.join("proj/src/my_pkg/__init__.py").check() assert not tmpfolder.join("proj/src/my/ns").check() # something should be logged, log = caplog.text expected_log = ('move', 'my_pkg', 'to', 'my/ns') for text in expected_log: assert text in log # but user should see no warning, unexpected_warnings = ('A folder', 'exists in the project directory', 'a namespace option was passed', 'Please make sure') for text in unexpected_warnings: assert text not in log
def test_pretend_move_old_package(tmpfolder, caplog, isolated_logger): # Given a package is already created without namespace create_project(project="proj", package="my_pkg") opts = parse_args( ["proj", "-p", "my_pkg", "--namespace", "my.ns", "--pretend"]) opts = process_opts(opts) logger.reconfigure(opts) struct = dict(proj={"src": {"my_pkg": {"file.py": ""}}}) # when 'pretend' option is passed, struct, opts = get_default_options(struct, opts) struct, opts = enforce_namespace_options(struct, opts) struct, opts = move_old_package(struct, opts) # then nothing should happen, assert tmpfolder.join("proj/src/my_pkg/__init__.py").check() assert not tmpfolder.join("proj/src/my/ns").check() # something should be logged, log = caplog.text expected_log = ("move", "my_pkg", "to", str(Path("my/ns"))) for text in expected_log: assert text in log # but user should see no warning, unexpected_warnings = ( "A folder", "exists in the project directory", "a namespace option was passed", "Please make sure", ) for text in unexpected_warnings: assert text not in log
def test_project_with_no_setup(tmpfolder): os.mkdir("my_project") args = ["my_project"] opts = cli.parse_args(args) opts = cli.process_opts(opts) with pytest.raises(FileNotFoundError): info.project(opts)
def test_add_custom_extension(tmpfolder): args = [EXT_FLAG, "my_project", "-p", "my_package"] opts = parse_args(args) opts = process_opts(opts) create_project(opts) assert path_exists("my_project/src/my_package/__init__.py")
def test_project_with_wrong_setup(tmpfolder): os.mkdir("my_project") open("my_project/setup.py", 'a').close() args = ["my_project"] opts = cli.parse_args(args) opts = cli.process_opts(opts) with pytest.raises(FileNotFoundError): info.project(opts)
def test_add_namespace(): args = ["project", "-p", "package", "--namespace", "com.blue_yonder"] opts = parse_args(args) opts = process_opts(opts) opts["ns_list"] = prepare_namespace(opts["namespace"]) struct = {"project": {"src": {"package": {"file1": "Content"}}}} ns_struct, _ = add_namespace(struct, opts) ns_pkg_struct = ns_struct["project"]["src"] assert ["project"] == list(ns_struct.keys()) assert "package" not in list(ns_struct.keys()) assert ["com"] == list(ns_pkg_struct.keys()) assert {"blue_yonder", "__init__.py"} == set(ns_pkg_struct["com"].keys()) assert "package" in list(ns_pkg_struct["com"]["blue_yonder"].keys())
def test_project_without_args(tmpfolder): old_args = [ "my_project", "-u", "http://www.blue-yonder.com/", "-d", "my description" ] cli.main(old_args) args = ["my_project"] opts = cli.parse_args(args) opts = cli.process_opts(opts) new_opts = info.project(opts) assert new_opts['url'] == "http://www.blue-yonder.com/" assert new_opts['package'] == "my_project" assert new_opts['license'] == "mit" assert new_opts['description'] == "my description"
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_pretend_create_project_with_django(tmpfolder, caplog): # Given options with the django extension, caplog.set_level(logging.INFO) opts = parse_args([PROJ_NAME, '--pretend', '--django']) opts = process_opts(opts) # when the project is created, create_project(opts) # then files should exist assert not path_exists(PROJ_NAME) for path in DJANGO_FILES: assert not path_exists(path) # but activities should be logged logs = caplog.text assert re.search(r'run.+django', logs)
def test_pretend_create_project_with_cookiecutter(tmpfolder, caplog): # Given options with the cookiecutter extension, caplog.set_level(logging.INFO) opts = parse_args( [PROJ_NAME, "--pretend", "--cookiecutter", COOKIECUTTER_URL]) opts = process_opts(opts) # when the project is created, create_project(opts) # then files should exist assert not path_exists(PROJ_NAME) for path in COOKIECUTTER_FILES: assert not path_exists(path) # but activities should be logged logs = caplog.text assert re.search(r"run.+cookiecutter", logs)
def test_pretend_create_project_with_cookiecutter(tmpfolder, caplog): # Given options with the cookiecutter extension, caplog.set_level(logging.INFO) opts = parse_args( [PROJ_NAME, '--pretend', '--cookiecutter', COOKIECUTTER_URL]) opts = process_opts(opts) # when the project is created, create_project(opts) # then files should exist assert not path_exists(PROJ_NAME) for path in COOKIECUTTER_FILES: assert not path_exists(path) # but activities should be logged logs = caplog.text assert re.search(r'run.+cookiecutter', logs)
def test_project_with_args(tmpfolder): old_args = [ "my_project", "-u", "http://www.blue-yonder.com/", "-d", "my description", ] cli.main(old_args) args = [ "my_project", "-u", "http://www.google.com/", "-d", "other description" ] opts = cli.parse_args(args) opts = cli.process_opts(opts) new_opts = info.project(opts) assert new_opts["url"] == "http://www.google.com/" assert new_opts["package"] == "my_project" assert new_opts["description"] == "other description"
def test_version_of_subdir(tmpfolder): projects = ["main_project", "inner_project"] for project in projects: opts = cli.parse_args([project]) opts = cli.process_opts(opts) _, opts = api.get_default_options({}, opts) struct, _ = structure.define_structure({}, opts) struct, _ = update.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_version_of_subdir(tmpfolder): projects = ["main_project", "inner_project"] for project in projects: opts = cli.parse_args([project]) opts = cli.process_opts(opts) _, opts = api.get_default_options({}, opts) struct, _ = structure.define_structure({}, opts) struct, _ = update.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_version_of_subdir(tmpfolder): projects = ["main_project", "inner_project"] for project in projects: opts = cli.parse_args([project]) opts = cli.process_opts(opts) _, opts = api.get_default_options({}, opts) struct, _ = structure.define_structure({}, opts) struct, _ = update.apply_update_rules(struct, opts) structure.create_structure(struct, {}) repo.init_commit_repo(project, struct) utils.rm_rf(os.path.join("inner_project", ".git")) shutil.move("inner_project", "main_project/inner_project") with utils.chdir("main_project"): main_version = (subprocess.check_output( [sys.executable, "setup.py", "--version"]).strip().splitlines()[-1]) with utils.chdir("inner_project"): inner_version = (subprocess.check_output( [sys.executable, "setup.py", "--version"]).strip().splitlines()[-1]) assert main_version.strip() == inner_version.strip()
def test_process_opts_raises(): opts = cli.parse_args(["non-existent", "--update"]) with pytest.raises(NoPyScaffoldProject): cli.process_opts(opts)