def check_dist(dist_file, test_cmd=""): """Check a Python package locally (not as a cli)""" dist_file = util.normalize_path(dist_file) util.run(f"twine check {dist_file}") if not test_cmd: # Get the package name from the dist file name name = re.match(r"(\S+)-\d", osp.basename(dist_file)).groups()[0] name = name.replace("-", "_") test_cmd = f'python -c "import {name}"' # Create venvs to install dist file # run the test command in the venv with TemporaryDirectory() as td: env_path = util.normalize_path(osp.abspath(td)) if os.name == "nt": # pragma: no cover bin_path = f"{env_path}/Scripts/" else: bin_path = f"{env_path}/bin" # Create the virtual env, upgrade pip, # install, and run test command util.run(f"python -m venv {env_path}") util.run(f"{bin_path}/python -m pip install -U pip") util.run(f"{bin_path}/pip install -q {dist_file}") util.run(f"{bin_path}/{test_cmd}")
def test_create_release_commit(py_package, build_mock): util.bump_version("0.0.2a0") version = util.get_version() util.run("python -m build .") shas = util.create_release_commit(version) assert util.normalize_path("dist/foo-0.0.2a0.tar.gz") in shas assert util.normalize_path("dist/foo-0.0.2a0-py3-none-any.whl") in shas
def test_extract_dist_npm(npm_dist, runner, mocker, open_mock, tmp_path): os.makedirs("staging") shutil.move(f"{util.CHECKOUT_NAME}/dist", "staging") def helper(path, **kwargs): return MockRequestResponse(f"staging/dist/{path}") get_mock = mocker.patch("requests.get", side_effect=helper) dist_names = [osp.basename(f) for f in glob("staging/dist/*.tgz")] url = normalize_path(osp.join(os.getcwd(), util.CHECKOUT_NAME)) tag_name = f"v{VERSION_SPEC}" releases = [ dict( tag_name=tag_name, target_commitish="main", assets=[dict(name=dist_name, url=dist_name) for dist_name in dist_names], ) ] sha = run("git rev-parse HEAD", cwd=util.CHECKOUT_NAME) tags = [dict(ref=f"refs/tags/{tag_name}", object=dict(sha=sha))] open_mock.side_effect = [ MockHTTPResponse(releases), MockHTTPResponse(tags), MockHTTPResponse(dict(html_url=url)), ] runner(["extract-release", HTML_URL]) assert len(open_mock.mock_calls) == 3 assert len(get_mock.mock_calls) == len(dist_names) == 3
def prep_git(branch, repo, auth, username, url): """Set up git""" repo = repo or util.get_repo() user_name = "" try: user_name = util.run("git config --global user.email") except Exception as e: pass if not user_name: # Use email address for the GitHub Actions bot # https://github.community/t/github-actions-bot-email-address/17204/6 util.run( 'git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"' ) util.run('git config --global user.name "GitHub Action"') # Set up the repository checkout_dir = os.environ.get("RH_CHECKOUT_DIR", util.CHECKOUT_NAME) checkout_exists = False if osp.exists(osp.join(checkout_dir, ".git")): print("Git checkout already exists", file=sys.stderr) checkout_exists = True if not checkout_exists: util.run(f"git init {checkout_dir}") orig_dir = os.getcwd() os.chdir(checkout_dir) if not url: if auth: url = f"https://{username}:{auth}@github.com/{repo}.git" else: url = f"https://github.com/{repo}.git" if osp.exists(url): url = util.normalize_path(url) if not checkout_exists: util.run(f"git remote add origin {url}") branch = branch or util.get_default_branch() util.run(f"git fetch origin {branch}") # Make sure we have *all* tags util.run("git fetch origin --tags") util.run(f"git checkout {branch}") # Install the package with test deps if util.SETUP_PY.exists(): util.run('pip install ".[test]"') os.chdir(orig_dir) return branch
def create_npm_package(git_repo): npm = util.normalize_path(shutil.which("npm")) run(f"{npm} init -y") git_repo.joinpath("index.js").write_text('console.log("hello")', encoding="utf-8") run("git add .") run('git commit -m "initial npm package"') run("git checkout foo") run("git pull origin bar") run("git checkout bar") return git_repo
def test_create_release_commit_hybrid(py_package, build_mock): # Add an npm package and test with that util.bump_version("0.0.2a0") version = util.get_version() testutil.create_npm_package(py_package) pkg_json = py_package / "package.json" data = json.loads(pkg_json.read_text(encoding="utf-8")) data["version"] = version pkg_json.write_text(json.dumps(data, indent=4), encoding="utf-8") txt = (py_package / "tbump.toml").read_text(encoding="utf-8") txt += testutil.TBUMP_NPM_TEMPLATE (py_package / "tbump.toml").write_text(txt, encoding="utf-8") util.run("python -m build .") shas = util.create_release_commit(version) assert len(shas) == 2 assert util.normalize_path("dist/foo-0.0.2a0.tar.gz") in shas
def test_extract_dist_py(py_package, runner, mocker, open_mock, tmp_path, git_prep): changelog_entry = mock_changelog_entry(py_package, runner, mocker) # Create the dist files run("python -m build .", cwd=util.CHECKOUT_NAME) # Finalize the release runner(["tag-release"]) os.makedirs("staging") shutil.move(f"{util.CHECKOUT_NAME}/dist", "staging") def helper(path, **kwargs): return MockRequestResponse(f"staging/dist/{path}") get_mock = mocker.patch("requests.get", side_effect=helper) tag_name = f"v{VERSION_SPEC}" dist_names = [osp.basename(f) for f in glob("staging/dist/*.*")] releases = [ dict( tag_name=tag_name, target_commitish=util.get_branch(), assets=[ dict(name=dist_name, url=dist_name) for dist_name in dist_names ], ) ] sha = run("git rev-parse HEAD", cwd=util.CHECKOUT_NAME) tags = [dict(ref=f"refs/tags/{tag_name}", object=dict(sha=sha))] url = normalize_path(osp.join(os.getcwd(), util.CHECKOUT_NAME)) open_mock.side_effect = [ MockHTTPResponse(releases), MockHTTPResponse(tags), MockHTTPResponse(dict(html_url=url)), ] runner(["extract-release", HTML_URL]) assert len(open_mock.mock_calls) == 3 assert len(get_mock.mock_calls) == len(dist_names) == 2
def prep_git(ref, branch, repo, auth, username, url, install=True): """Set up git""" repo = repo or util.get_repo() user_name = "" try: user_name = util.run("git config --global user.email") except Exception as e: pass if not user_name: # Use email address for the GitHub Actions bot # https://github.community/t/github-actions-bot-email-address/17204/6 util.run( 'git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"' ) util.run('git config --global user.name "GitHub Action"') # Set up the repository checkout_dir = os.environ.get("RH_CHECKOUT_DIR", util.CHECKOUT_NAME) checkout_exists = False if osp.exists(osp.join(checkout_dir, ".git")): util.log("Git checkout already exists") checkout_exists = True if not checkout_exists: util.run(f"git init {checkout_dir}") orig_dir = os.getcwd() os.chdir(checkout_dir) if not url: if auth: url = f"https://{username}:{auth}@github.com/{repo}.git" else: url = f"https://github.com/{repo}.git" if osp.exists(url): url = util.normalize_path(url) if not checkout_exists: util.run(f"git remote add origin {url}") branch = branch or util.get_default_branch() ref = ref or "" # Make sure we have *all* tags util.run("git fetch origin --tags") # Handle the ref if ref.startswith("refs/pull/"): pull = ref[len("refs/pull/") :] ref_alias = f"refs/pull/{pull}" else: ref = None # Reuse existing branch if possible if ref: util.run(f"git fetch origin +{ref}:{ref_alias}") util.run(f"git fetch origin {ref}") checkout_cmd = f"git checkout -B {branch} {ref_alias}" else: util.run(f"git fetch origin {branch}") checkout_cmd = f"git checkout {branch}" if checkout_exists: try: util.run(f"git checkout {branch}") except Exception: util.run(checkout_cmd) else: util.run(checkout_cmd) # Install the package with test deps if util.SETUP_PY.exists() and install: util.run('pip install ".[test]"') os.chdir(orig_dir) return branch
def test_get_version_npm(npm_package): assert util.get_version() == "1.0.0" npm = util.normalize_path(shutil.which("npm")) run(f"{npm} version patch") assert util.get_version() == "1.0.1"