def test_manual_overrides_defaults(tmp_path, config): cmd = InitCommand('group', config) cmd.run(Namespace(name='my-charm', author="fred", series='xenial,precise', force=False)) with (tmp_path / "metadata.yaml").open("rt", encoding="utf8") as f: metadata = yaml.safe_load(f) assert metadata.get("series") == ['xenial', 'precise']
def test_all_the_files(tmp_path): cmd = InitCommand('group') cmd.run( Namespace(path=tmp_path, name='my-charm', author="ಅಪರಿಚಿತ ವ್ಯಕ್ತಿ", series='k8s', force=False)) assert sorted(str(p.relative_to(tmp_path)) for p in tmp_path.glob("**/*")) == [ ".flake8", ".jujuignore", "LICENSE", "README.md", "actions.yaml", "config.yaml", "metadata.yaml", "requirements-dev.txt", "requirements.txt", "run_tests", "src", "src/charm.py", "tests", "tests/__init__.py", "tests/test_charm.py", ]
def test_tests(tmp_path, config): # fix the PYTHONPATH and PATH so the tests in the initted environment use our own # virtualenv libs and bins (if any), as they need them, but we're not creating a # venv for the local tests (note that for CI doesn't use a venv) env = os.environ.copy() env_paths = [p for p in sys.path if 'env/lib/python' in p] if env_paths: if 'PYTHONPATH' in env: env['PYTHONPATH'] += ':' + ':'.join(env_paths) else: env['PYTHONPATH'] = ':'.join(env_paths) for path in env_paths: bin_path = path[:path.index('env/lib/python')] + 'env/bin' env['PATH'] = bin_path + ':' + env['PATH'] cmd = InitCommand('group', config) cmd.run(Namespace(name='my-charm', author="だれだれ", series='k8s', force=False)) # uncomment the project YAMLs that we just created from the templates, so tests run ok # (those files are commented out to avoid the developer pushing valid-but-fake config # and actions files to the Store) uncomment(tmp_path / 'actions.yaml', "fortune:") uncomment(tmp_path / 'config.yaml', "options:") subprocess.run(["./run_tests"], cwd=str(tmp_path), check=True, env=env)
def test_init_pep257(tmp_path, config): cmd = InitCommand("group", config) cmd.run( Namespace(name="my-charm", author="J Doe", series="k8s", force=False)) paths = get_python_filepaths(roots=[str(tmp_path / "src")], python_paths=[]) pep257_test(paths)
def test_executables(tmp_path, config): cmd = InitCommand(config) cmd.run(Namespace(name="my-charm", author="홍길동", force=False)) if os.name == "posix": assert (tmp_path / "run_tests").stat().st_mode & S_IXALL == S_IXALL assert (tmp_path / "src/charm.py").stat().st_mode & S_IXALL == S_IXALL
def test_init_pep8(tmp_path, config, *, author="J Doe"): cmd = InitCommand(config) cmd.run(Namespace(name="my-charm", author=author, force=False)) paths = get_python_filepaths( roots=[str(tmp_path / "src"), str(tmp_path / "tests")], python_paths=[] ) pep8_test(paths)
def test_init_pep8(tmp_path, *, author="J Doe"): cmd = InitCommand('group') cmd.run(Namespace(path=tmp_path, name='my-charm', author=author)) paths = get_python_filepaths( roots=[str(tmp_path / "src"), str(tmp_path / "tests")], python_paths=[]) pep8_test(paths)
def test_bad_name(tmp_path): cmd = InitCommand('group') with pytest.raises(CommandError): cmd.run( Namespace(path=tmp_path, name='1234', author="שראלה ישראל", series='k8s'))
def test_series_defaults(tmp_path, config): cmd = InitCommand('group', config) # series default comes from the parsing itself cmd.run(Namespace(name='my-charm', author="fred", series='k8s', force=False)) with (tmp_path / "metadata.yaml").open("rt", encoding="utf8") as f: metadata = yaml.safe_load(f) assert metadata.get("series") == ['k8s']
def test_bad_name(config): cmd = InitCommand("group", config) with pytest.raises(CommandError): cmd.run( Namespace(name="1234", author="שראלה ישראל", series="k8s", force=False))
def test_gecos_missing_user_information(config): """No information at all for the requested user.""" cmd = InitCommand(config) with patch("pwd.getpwuid") as mock_pwd: mock_pwd.side_effect = KeyError("no user") msg = "Unable to automatically determine author's name, specify it with --author" with pytest.raises(CraftError, match=msg): cmd.run(Namespace(name="my-charm", author=None, force=False))
def test_executables(tmp_path): cmd = InitCommand('group') cmd.run( Namespace(path=tmp_path, name='my-charm', author="홍길동", series='k8s', force=False)) assert (tmp_path / "run_tests").stat().st_mode & S_IXALL == S_IXALL assert (tmp_path / "src/charm.py").stat().st_mode & S_IXALL == S_IXALL
def test_gecos_missing_in_getpwuid_response(config): """No GECOS field in getpwuid response.""" import pwd cmd = InitCommand(config) with patch("pwd.getpwuid") as mock_pwd: # return a fack passwd struct with an empty gecos (5th parameter) mock_pwd.return_value = pwd.struct_passwd(("user", "pass", 1, 1, "", "dir", "shell")) msg = "Unable to automatically determine author's name, specify it with --author" with pytest.raises(CraftError, match=msg): cmd.run(Namespace(name="my-charm", author=None, force=False))
def test_force(tmp_path, config): cmd = InitCommand('group', config) tmp_file = tmp_path / 'README.md' with tmp_file.open('w') as f: f.write('This is a nonsense readme') cmd.run(Namespace(name='my-charm', author="ಅಪರಿಚಿತ ವ್ಯಕ್ತಿ", series='k8s', force=True)) # Check that init ran assert (tmp_path / 'LICENSE').exists() # Check that init did not overwrite files with tmp_file.open('r') as f: assert f.read() == 'This is a nonsense readme'
def test_gecos_missing_user_information(config): """No information at all for the requested user.""" cmd = InitCommand("group", config) with patch("pwd.getpwuid") as mock_pwd: mock_pwd.side_effect = KeyError("no user") msg = "Author not given, and nothing in GECOS field" with pytest.raises(CommandError, match=msg): cmd.run( Namespace(name="my-charm", author=None, series="k8s", force=False))
def test_force(tmp_path, config): cmd = InitCommand(config) tmp_file = tmp_path / "README.md" with tmp_file.open("w") as f: f.write("This is a nonsense readme") cmd.run(Namespace(name="my-charm", author="ಅಪರಿಚಿತ ವ್ಯಕ್ತಿ", force=True)) # Check that init ran assert (tmp_path / "LICENSE").exists() # Check that init did not overwrite files with tmp_file.open("r") as f: assert f.read() == "This is a nonsense readme"
def test_gecos_missing_in_getpwuid_response(config): """No GECOS field in getpwuid response.""" cmd = InitCommand("group", config) with patch("pwd.getpwuid") as mock_pwd: # return a fack passwd struct with an empty gecos (5th parameter) mock_pwd.return_value = pwd.struct_passwd( ("user", "pass", 1, 1, "", "dir", "shell")) msg = "Author not given, and nothing in GECOS field" with pytest.raises(CommandError, match=msg): cmd.run( Namespace(name="my-charm", author=None, series="k8s", force=False))
def test_missing_directory(tmp_path, config): """If the indicated directory does not exist, create it.""" init_dir = tmp_path / "foo" / "bar" config.set( project=Project( config_provided=False, dirpath=init_dir, started_at=datetime.datetime.utcnow(), ) ) cmd = InitCommand(config) cmd.run(Namespace(name="my-charm", author="testauthor")) # check it run ok assert (init_dir / "LICENSE").exists()
def test_tests(tmp_path): # fix the PYTHONPATH so the tests in the initted environment use our own # virtualenv (if any), as they need one, but we're not creating one for them; note # that for CI this normally doesn't run under a venv, so this may fix nothing env = os.environ.copy() env_paths = [p for p in sys.path if 'env/lib/python' in p] if env_paths: if 'PYTHONPATH' in env: env['PYTHONPATH'] += ':' + ':'.join(env_paths) else: env['PYTHONPATH'] = ':'.join(env_paths) cmd = InitCommand('group') cmd.run( Namespace(path=tmp_path, name='my-charm', author="だれだれ", series='k8s')) subprocess.run(["./run_tests"], cwd=str(tmp_path), check=True, env=env)
def test_tests(tmp_path, config): # fix the PYTHONPATH and PATH so the tests in the initted environment use our own # virtualenv libs and bins (if any), as they need them, but we're not creating a # venv for the local tests (note that for CI doesn't use a venv) env = os.environ.copy() env_paths = [p for p in sys.path if "env/lib/python" in p] if env_paths: if "PYTHONPATH" in env: env["PYTHONPATH"] += ":" + ":".join(env_paths) else: env["PYTHONPATH"] = ":".join(env_paths) for path in env_paths: bin_path = path[: path.index("env/lib/python")] + "env/bin" env["PATH"] = bin_path + ":" + env["PATH"] cmd = InitCommand("group", config) cmd.run(Namespace(name="my-charm", author="だれだれ", series="k8s", force=False)) subprocess.run(["./run_tests"], cwd=str(tmp_path), check=True, env=env)
def test_all_the_files(tmp_path, config): cmd = InitCommand(config) cmd.run(Namespace(name="my-charm", author="ಅಪರಿಚಿತ ವ್ಯಕ್ತಿ", force=False)) assert sorted(str(p.relative_to(tmp_path)) for p in tmp_path.glob("**/*")) == [ ".flake8", ".gitignore", ".jujuignore", "CONTRIBUTING.md", "LICENSE", "README.md", "actions.yaml", "charmcraft.yaml", "config.yaml", "metadata.yaml", "requirements-dev.txt", "requirements.txt", "run_tests", "src", os.path.join("src", "charm.py"), "tests", os.path.join("tests", "__init__.py"), os.path.join("tests", "test_charm.py"), ]
def test_init_pep257(tmp_path, config): cmd = InitCommand('group', config) cmd.run(Namespace(name='my-charm', author='J Doe', series='k8s', force=False)) paths = get_python_filepaths(roots=[str(tmp_path / "src")], python_paths=[]) pep257_test(paths)
def test_bad_name(config): cmd = InitCommand('group', config) with pytest.raises(CommandError): cmd.run(Namespace(name='1234', author="שראלה ישראל", series='k8s', force=False))
def test_tests(tmp_path): cmd = InitCommand('group') cmd.run(Namespace(path=tmp_path, name='my-charm', author="だれだれ")) subprocess.run(["./run_tests"], cwd=str(tmp_path), check=True)
def test_no_author_gecos(tmp_path, config, mock_pwd): cmd = InitCommand(config) cmd.run(Namespace(name="my-charm", author=None, force=False)) text = (tmp_path / "src" / "charm.py").read_text() assert "Test Gecos Author Name" in text
def test_bad_name(config): cmd = InitCommand(config) with pytest.raises(CraftError): cmd.run(Namespace(name="1234", author="שראלה ישראל", force=False))
def test_executables(tmp_path, config): cmd = InitCommand("group", config) cmd.run(Namespace(name="my-charm", author="홍길동", series="k8s", force=False)) assert (tmp_path / "run_tests").stat().st_mode & S_IXALL == S_IXALL assert (tmp_path / "src/charm.py").stat().st_mode & S_IXALL == S_IXALL