Skip to content

Commit

Permalink
Merge pull request #155 from alexwlchan/fix-tests
Browse files Browse the repository at this point in the history
Get tests running in GitHub Actions
  • Loading branch information
alexwlchan authored Jun 19, 2024
2 parents 8336dad + 2b9ef60 commit 47cd31c
Show file tree
Hide file tree
Showing 19 changed files with 388 additions and 119 deletions.
8 changes: 0 additions & 8 deletions .coveragerc

This file was deleted.

2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requirements.txt linguist-generated=true
dev_requirements.txt linguist-generated=true
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
time: "09:00"
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "daily"
time: "09:00"
129 changes: 129 additions & 0 deletions .github/install-github-bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/env bash
# Install one of my Rust binaries from GitHub.
#
# This was originally written to pull just my Rust binaries, but it
# turns out it can work reasonably well for other projects. This allows
# me to bypass Homebrew, which is generally nicer.
#
# For my CLI tools written in Rust, I use a GitHub Action [1]
# to compile binaries and add them to a release on GitHub.
#
# These are the "canonical" versions, rather than recompiling on
# each machine and having slightly different versions because I
# was using different checkouts of the code.
#
# This script gets the latest version of a binary from GitHub and
# adds it to my $PATH.
#
# This script isn't meant to be run directly (hence the underscore) --
# I have other scripts that invoke it (e.g. install-vfd), so I can find
# those scripts with autocomplete in my shell, even on a new machine.
#
# TODO: I publish macOS/Windows/Linux binaries, but this script only
# fetches the macOS binary. In theory, it could detect which OS it's
# running on and pick the right binary.
#
# [1]: https://github.com/taiki-e/upload-rust-binary-action

set -o errexit
set -o nounset

if (( "$#" != 1 ))
then
echo "Usage: _install-rust-bin <REPO_NAME>"
fi

REPO_NAME="$1"

RELEASES_API_URL="https://api.github.com/repos/$REPO_NAME/releases/latest"

# Fetch the asset URL using the GitHub Releases API [2].
#
# For the purposes of this script, these are the interesting bits of the
# API response that we want to pay attention to:
#
# {
# "assets": [
# {
# "name": "vfd-x86_64-apple-darwin.tar.gz",
# "url": "https://api.github.com/repos/alexwlchan/books.alexwlchan.net/releases/assets/64229966",
# ...
# },
# {
# "name": "vfd-x86_64-pc-windows-msvc.zip",
# "url": "https://api.github.com/repos/alexwlchan/books.alexwlchan.net/releases/assets/64229889",
# ...
# },
# {
# "name": "vfd-x86_64-unknown-linux-gnu.tar.gz",
# "url": "https://api.github.com/repos/alexwlchan/books.alexwlchan.net/releases/assets/64229611",
# ...
# }
# ],
# }
#
# [2]: https://docs.github.com/en/rest/releases/releases#get-the-latest-release
#
# Note: this will filter out ARM binaries because I'm not running on
# Apple Silicon yet; this will need updating eventually.
#
ASSET_URL=$(curl --silent "$RELEASES_API_URL" \
| jq -r '.assets | .[] | select(.name | contains("darwin")) | select(.name | contains("arm") | not) | select(.name | contains("aarch64") | not) | .url' \
| grep -v arm64
)

if [[ "$ASSET_URL" == "" ]]
then
echo "No macOS download available for the latest version! Is it still building?" >&2
exit 1
fi

# Download and unpack the asset using the GitHub Release Assets API [3].
#
# We supply the headers required by the GitHub API, and the `--location`
# flag caused curl to follow redirects.
#
# Note: this assumes the binary is packaged as a tar.gz. The Windows
# binaries are zipped instead of tar.gz-ed, so if you want to support
# Windows, inspect the "content_type" field in the Releases API response.
#
# [3]: https://docs.github.com/en/rest/releases/assets#get-a-release-asset

cd $(mktemp -d)

curl \
--header "Accept: application/octet-stream" \
--location \
--silent \
"$ASSET_URL" > "asset.tar.gz"

# Identify the name of the binary, which may be different from the repo name.
#
# We list all the files in the asset package, which should contain a single
# file, and assume that's the name of the binary.
ASSET_FILES=$(tar --list --file "asset.tar.gz")

if [[ "$REPO_NAME" == "BurntSushi/ripgrep" ]]
then
BINARY_PATH=$(echo "$ASSET_FILES" | grep "/rg$")
BINARY_NAME="rg"
else
if (( $(echo "$ASSET_FILES" | wc -l) != 1 ))
then
echo "Release asset doesn't contain exactly 1 file; not sure what to do:" >&2
echo "$ASSET_FILES" >&2
exit 1
fi

BINARY_PATH="$(echo "$ASSET_FILES" | head -n 1)"
BINARY_NAME="$BINARY_PATH"
fi

# Now actually extract the binary, make it executable, and add it to the PATH.
tar --extract --gunzip --file "asset.tar.gz"

chmod +x "$BINARY_PATH"
sudo mv "$BINARY_PATH" /usr/local/bin

which "$BINARY_NAME"
"$BINARY_NAME" --version
12 changes: 0 additions & 12 deletions .github/workflows/check_run.yml

This file was deleted.

48 changes: 48 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Test

on:
push:
branches:
- main

pull_request:
branches:
- main

jobs:
test:
# Note: this project uses some macOS-specific tools like `qlmanage`, so
# the tests need to run on macOS.
#
# This is different from my other Python projects, which usually run on
# `ubuntu-latest`.
runs-on: macos-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: 'pip'
cache-dependency-path: 'dev_requirements.txt'

- name: Install dependencies
run: |
brew install ffmpeg
.github/install-github-bin alexwlchan/dominant_colours
pip install -r dev_requirements.txt
- name: Run linting
run: |
ruff check .
ruff format --check .
# - name: Check types
# run: mypy src tests

- name: Run tests
run: |
coverage run -m pytest tests
coverage report
9 changes: 9 additions & 0 deletions dev_requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-e file:.
-r requirements.txt

bs4
pytest
pytest-cov
coverage
mypy
ruff
107 changes: 107 additions & 0 deletions dev_requirements.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[project]
name = "docstore"
version = "2.0.0"

[project.scripts]
docstore = "docstore.cli:main"

[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.package-data]
analytics = ["static/*", "templates/*"]

[tool.coverage.run]
branch = true
source = [
"docstore",
"tests",
]

[tool.coverage.report]
show_missing = true
skip_covered = true
fail_under = 90
# fail_under = 100

# [tool.pytest.ini_options]
# filterwarnings = ["error"]

[tool.mypy]
mypy_path = "src"
check_untyped_defs = true
disallow_any_generics = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
no_implicit_optional = true
no_implicit_reexport = true
show_error_codes = true
strict_equality = true
warn_redundant_casts = true
warn_return_any = true
warn_unused_configs = true
warn_unused_ignores = true
3 changes: 2 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ cattrs>=1.1.1
click>=7.1.2
hyperlink>=21.0.0
Flask>=1.1.2
rapidfuzz>=1.4.1,<2
Pillow
rapidfuzz
smartypants>=2.0.1
Unidecode>=1.1.1
wcag_contrast_ratio>=0.9
Loading

0 comments on commit 47cd31c

Please sign in to comment.