Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Pants to test, lint and typecheck in this repo. #142

Merged
merged 12 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 14 additions & 25 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,12 @@ jobs:
steps:
- name: Noop
run: "true"
checks:
name: Lint and check formatting
needs: org-check
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Shellcheck
run: shellcheck pantsup.sh
- name: Run Checks
uses: pantsbuild/actions/run-tox@v2
with:
tox-env: format-check,lint,typecheck
unit-tests:
name: (${{ matrix.os }}) Test
tests:
# It's only necessary to typecheck and lint on one platform, but those goals are so fast
# in this repo that a separate job for them would be almost entirely wasteful overhead,
# and logic to only run them in one of these matrixed jobs would be more complicated
# than it's worth.
name: (${{ matrix.os }}) Test, Typecheck and Lint
needs: org-check
runs-on: ${{ matrix.os }}
strategy:
Expand All @@ -40,15 +27,17 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Pants
uses: pantsbuild/actions/init-pants@v4-scie-pants
with:
gha-cache-key: v2
named-caches-hash: ${{ hashFiles('tests/requirements.lock') }}
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Expose Pythons
uses: pantsbuild/actions/expose-pythons@v2
- name: Run Unit Tests
uses: pantsbuild/actions/run-tox@v2
with:
# We need to force pytest to use a terse base for its temp dirs or we hit errors like:
# 2020-02-23T16:14:47,808: [0x1139575c0] /private/var/folders/17/5mc7816d3mndxjqgplq6057w0000gn/T/pytest-of-travis/pytest-0/test_pants_1_260/project_dir/.pids/watchman/watchman.sock: path is too long
tox-env: test -- --basetemp=${HOME}/tmp
- name: Run Tests
run: |
pants test check lint ::
3 changes: 3 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
shell_sources(name="scripts")

resources(name="scripts_as_resources", sources=["one_step_setup.sh", "pants", "pantsup.sh"])
File renamed without changes.
60 changes: 28 additions & 32 deletions one_step_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,51 @@
set -e

function fail {
# Print a newline to stderr and exit.
echo > /dev/stderr
exit 1
# Print a newline to stderr and exit.
echo > /dev/stderr
exit 1
}

# Make sure we don't override an existing Pants installation.
if [ -f "pants.toml" ]
then
echo A \`pants.toml\` file already exists in this directory. This installation script \
needs to create a new \`pants.toml\` file. > /dev/stderr
echo If you are trying to upgrade to a newer version of Pants, set the \`pants_version\` \
setting in your existing \`pants.toml\` file. > /dev/stderr
echo If you are trying to run the install process from scratch, delete the \`pants.toml\` \
file and start again. > /dev/stderr
fail
if [ -f "pants.toml" ]; then
echo A \`pants.toml\` file already exists in this directory. This installation script \
needs to create a new \`pants.toml\` file. > /dev/stderr
echo If you are trying to upgrade to a newer version of Pants, set the \`pants_version\` \
setting in your existing \`pants.toml\` file. > /dev/stderr
echo If you are trying to run the install process from scratch, delete the \`pants.toml\` \
file and start again. > /dev/stderr
fail
fi

if [ -f "pants" ]
then
echo A file called \`pants\` already exists in this directory. This installation script \
will overwrite this file. To proceed, either rename this file, or delete the \
\`pants\` file. > /dev/stderr
fail
if [ -f "pants" ]; then
echo A file called \`pants\` already exists in this directory. This installation script \
will overwrite this file. To proceed, either rename this file, or delete the \
\`pants\` file. > /dev/stderr
fail
fi

# Find the latest stable version from PyPI if not set at the command line.
if [ -z "$PANTS_VERSION" ]
then
PANTS_VERSION=`curl https://pypi.org/pypi/pantsbuild.pants/json | \
grep -o '"version":"[^"]*"' | \
grep -o "[0-9]*\\.[0-9]*\\.[0-9]*"`
if [ -z "$PANTS_VERSION" ]; then
PANTS_VERSION=$(curl https://pypi.org/pypi/pantsbuild.pants/json |
grep -o '"version":"[^"]*"' |
grep -o "[0-9]*\.[0-9]*\.[0-9]*")
fi

# Create enough of a pants.toml file that our bootstrap process can run
printf '[GLOBAL]\npants_version = "'$PANTS_VERSION'"\n' > pants.toml
printf '[GLOBAL]\npants_version = "%s"\n' "$PANTS_VERSION" > pants.toml

# Fetch the Pants bootstrap script and run it to verify that we fetched
# the correct version
curl -L -O https://static.pantsbuild.org/setup/pants
curl -L -O https://static.pantsbuild.org/setup/pants
chmod +x ./pants
PANTS_EXEC_VERSION=`./pants --version`
PANTS_EXEC_VERSION=$(./pants --version)

# Verify that the correct version of Pants was installed.
if [ $PANTS_VERSION != $PANTS_EXEC_VERSION ]
then
echo Pants was installed, but the version was $PANTS_EXEC_VERSION, not \
$PANTS_VERSION. > /dev/stderr
echo Consider filing an issue at https://github.com/pantsbuild/setup/issues/new > /dev/stderr
fail
if [ "$PANTS_VERSION" != "$PANTS_EXEC_VERSION" ]; then
echo Pants was installed, but the version was "$PANTS_EXEC_VERSION", not \
"$PANTS_VERSION". > /dev/stderr
echo Consider filing an issue at https://github.com/pantsbuild/setup/issues/new > /dev/stderr
fail
fi

# Let the user know that everything worked
Expand Down
42 changes: 42 additions & 0 deletions pants.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[GLOBAL]
pants_version = "2.15.0rc5"

backend_packages.add = [
"pants.backend.python",
"pants.backend.python.lint.black",
"pants.backend.python.lint.flake8",
"pants.backend.python.lint.isort",
"pants.backend.python.typecheck.mypy",
"pants.backend.shell",
"pants.backend.shell.lint.shellcheck",
"pants.backend.shell.lint.shfmt",
]

[anonymous-telemetry]
enabled = true
repo_id = "9F90394C-959F-4045-9718-7EB1AEB2A9C7"

[source]
root_patterns = ["/", "tests"]

[test]
extra_env_vars = [
"PYENV_ROOT",
"HOME",
"PATH",
]
timeout_default = 600

[shellcheck]
args = ["--external-sources"]

[shfmt]
# See https://github.com/mvdan/sh/blob/master/cmd/shfmt/shfmt.1.scd#printer-flags.
args = ["-i 2", "-ci", "-sr"]

[python]
interpreter_constraints = ["CPython==3.8.*"]
enable_resolves = true

[python.resolves]
python-default = "tests/requirements.lock"
7 changes: 7 additions & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
python_requirements(name="reqs")

python_test_utils(name="test_utils")

python_sources(name="helpers")

python_tests(name="tests", dependencies=["//:scripts_as_resources"])
20 changes: 3 additions & 17 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,13 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import shutil
import subprocess
from pathlib import Path, PurePath
from pathlib import Path
from typing import Optional

import pytest
from typing_extensions import Protocol


@pytest.fixture(scope="session")
def project_root() -> PurePath:
return PurePath(
subprocess.run(
["git", "rev-parse", "--show-toplevel"],
stdout=subprocess.PIPE,
encoding="utf-8",
check=True,
cwd=str(Path(__file__).parent),
).stdout.strip()
)


# There are no stubs for pytest and it is not in the typeshed so we model the type of the one
# API we use on this rather large class:
# https://docs.pytest.org/en/latest/reference.html#_pytest.monkeypatch.MonkeyPatch.setenv
Expand All @@ -32,13 +18,13 @@ def setenv(self, name: str, value: str, prepend: Optional[str] = None) -> None:


@pytest.fixture
def build_root(project_root: PurePath, tmp_path: Path, monkeypatch: MonkeyPatch) -> Path:
def build_root(tmp_path: Path, monkeypatch: MonkeyPatch) -> Path:
monkeypatch.setenv("PANTS_SETUP_CACHE", str(tmp_path / "PANTS_SETUP_CACHE"))

# NB: Unlike the install guide's instruction to curl the `./pants` script, we directly
# copy it to ensure we are using the branch's version of the script and to avoid
# network pings.
build_root = tmp_path / "project_dir"
build_root.mkdir()
shutil.copy(str(project_root / "pants"), str(build_root / "pants"))
shutil.copy("./pants", str(build_root / "pants"))
return build_root
Loading