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

add CI build and test for context-py #115

Merged
merged 21 commits into from
Oct 4, 2024
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
125 changes: 125 additions & 0 deletions .github/workflows/pyo3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# This file is autogenerated by maturin v1.5.1
# To regenerate, run
#
# maturin generate-ci -m ./context-py/Cargo.toml --platform linux --platform macos --pytest --output ./.github/workflows/pyo3.yml github
#
name: Build and test context-py

on:
push:
branches:
- main
- master
tags:
- "*"
pull_request: {}
workflow_dispatch: {}

permissions:
contents: read

jobs:
linux:
runs-on: ${{ matrix.platform.runner }}
strategy:
matrix:
platform:
- runner: ubuntu-latest
target: x86_64
- runner: ubuntu-latest
target: aarch64
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist --find-interpreter --manifest-path ./context-py/Cargo.toml
manylinux: auto
container: "2_28"
before-script-linux: |
# Needed for `openssl-sys`:
# https://docs.rs/openssl/latest/openssl/
if [ -f /usr/bin/dnf ]; then
# quay.io/pypa/manylinux_2_28_x86_64:latest
/usr/bin/dnf upgrade
/usr/bin/dnf install -y pkgconf perl-IPC-Cmd openssl-devel python3-pip
/usr/bin/ln -s /usr/bin/pip3 /usr/bin/pip
/usr/bin/ln -sf /usr/bin/python3.11 /usr/bin/python3
Comment on lines +46 to +51
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

            # Needed for `openssl-sys`:
            # https://docs.rs/openssl/latest/openssl/

elif [ -f /usr/bin/apt ]; then
# ghcr.io/rust-cross/manylinux_2_28-cross:aarch64
/usr/bin/apt update
/usr/bin/apt install -y pkg-config libssl-dev
fi
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-linux-${{ matrix.platform.target }}
path: dist
- name: pytest
if: ${{ startsWith(matrix.platform.target, 'x86_64') }}
shell: bash
run: |
set -e
pip install context-py --find-links dist --force-reinstall
pip install pytest
cd ./context-py && pytest
- name: pytest
if: ${{ startsWith(matrix.platform.target, 'aarch64') }}
uses: uraimo/run-on-arch-action@v2.5.0
with:
arch: ${{ matrix.platform.target }}
distro: ubuntu22.04
githubToken: ${{ github.token }}
install: |
apt-get update
apt-get install -y --no-install-recommends python3 python3-pip
pip3 install -U pip pytest
run: |
set -e
pip3 install context-py --find-links dist --force-reinstall
cd ./context-py && pytest

macos:
runs-on: ${{ matrix.platform.runner }}
strategy:
matrix:
platform:
- runner: macos-latest
target: x86_64
- runner: macos-14
target: aarch64
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out dist --find-interpreter --manifest-path ./context-py/Cargo.toml
sccache: "true"
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-macos-${{ matrix.platform.target }}
path: dist

sdist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build sdist
uses: PyO3/maturin-action@v1
with:
command: sdist
args: --out dist --manifest-path ./context-py/Cargo.toml
- name: Upload sdist
uses: actions/upload-artifact@v4
with:
name: wheels-sdist
path: dist
2 changes: 2 additions & 0 deletions .trunk/configs/.bandit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bandit]
exclude = tests
Comment on lines +1 to +2
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asserts are used in tests and bandit says to exclude tests 🤷
https://bandit.readthedocs.io/en/latest/config.html#exclusions

2 changes: 2 additions & 0 deletions .trunk/configs/.isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
profile=black
5 changes: 5 additions & 0 deletions .trunk/configs/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generic, formatter-friendly config.
select = ["B", "D3", "E", "F"]

# Never enforce `E501` (line length violations). This should be handled by formatters.
ignore = ["E501"]
30 changes: 17 additions & 13 deletions .trunk/trunk.yaml
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upgraded to support python linters and formatters

Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,38 @@ cli:
plugins:
sources:
- id: trunk
ref: v1.6.0
ref: v1.6.3
uri: https://github.com/trunk-io/plugins
# Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes)
runtimes:
enabled:
- node@18.12.1
- python@3.10.8
- rust@1.71.1
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
lint:
enabled:
- osv-scanner@1.8.1
- actionlint@1.7.1
- checkov@3.2.164
- clippy@1.75.0
- actionlint@1.7.3
- bandit@1.7.10
- black@24.8.0
- checkov@3.2.256
- clippy@1.71.1
- git-diff-check
- markdownlint@0.41.0
- prettier@3.3.2
- rustfmt@1.75.0
- taplo@0.8.1
- trivy@0.52.2
- trufflehog@3.79.0
- isort@5.13.2
- markdownlint@0.42.0
- nixpkgs-fmt@1.3.0
- osv-scanner@1.9.0
- prettier@3.3.3
- ruff@0.6.8
- rustfmt@1.71.1
- taplo@0.9.3
- trufflehog@3.82.6
- yamllint@1.35.1
actions:
disabled:
enabled:
- trunk-announce
- trunk-check-pre-push
- trunk-fmt-pre-commit
enabled:
- trunk-upgrade-available
Comment on lines -33 to 42
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to use these

tools:
enabled:
Expand Down
1 change: 1 addition & 0 deletions context-py/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
8 changes: 7 additions & 1 deletion context-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,21 @@ fn repo_validate(bundle_repo: repo::BundleRepo) -> repo::validator::RepoValidati

#[pymodule]
fn context_py(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<env::parser::CIPlatform>()?;
m.add_class::<env::parser::BranchClass>()?;
m.add_class::<env::validator::EnvValidationLevel>()?;
m.add_function(wrap_pyfunction!(env_parse, m)?)?;
m.add_function(wrap_pyfunction!(env_validate, m)?)?;

m.add_class::<junit::bindings::BindingsReport>()?;
m.add_class::<junit::bindings::BindingsTestCaseStatusStatus>()?;
m.add_class::<junit::bindings::BindingsNonSuccessKind>()?;
m.add_class::<junit::validator::JunitValidationLevel>()?;
m.add_function(wrap_pyfunction!(junit_parse, m)?)?;
m.add_function(wrap_pyfunction!(junit_validate, m)?)?;

m.add_class::<repo::BundleRepo>()?;
m.add_class::<repo::RepoUrlParts>()?;
m.add_class::<repo::validator::RepoValidationLevel>()?;
m.add_function(wrap_pyfunction!(repo_validate, m)?)?;
Ok(())
}
81 changes: 81 additions & 0 deletions context-py/tests/test_parse_and_validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
def test_env_parse_and_validate():
from context_py import CIPlatform, EnvValidationLevel, env_parse, env_validate

env_vars = {
"GITHUB_ACTIONS": "true",
"GITHUB_REF": "abc",
"GITHUB_ACTOR": "Spikey",
"GITHUB_REPOSITORY": "analytics-cli",
"GITHUB_RUN_ID": "12345",
}

ci_info = env_parse(env_vars)
env_validation = env_validate(ci_info)

assert ci_info.platform == CIPlatform.GitHubActions
assert env_validation.max_level() == EnvValidationLevel.SubOptimal
assert [issue.error_message for issue in env_validation.issues_flat()] == [
"CI info author email too short",
"CI info author name too short",
"CI info commit message too short",
"CI info committer email too short",
"CI info committer name too short",
"CI info title too short",
], "\n" + "\n".join([issue.error_message for issue in env_validation.issues_flat()])


def test_junit_parse_and_validate():
import datetime

from context_py import JunitValidationLevel, junit_parse, junit_validate

timestamp = datetime.datetime.now().astimezone(datetime.timezone.utc).isoformat()
junit_xml = f"""
<testsuites name="my-test-run" tests="1" failures="1" errors="0">
<testsuite name="my-test-suite" tests="1" disabled="0" errors="0" failures="1" timestamp="{timestamp}">
<testcase name="failure-case" file="test.py" classname="MyClass" timestamp="{timestamp}" time="1">
<failure/>
</testcase>
</testsuite>
</testsuites>
"""

report = junit_parse(str.encode(junit_xml))
junit_report_validation = junit_validate(report[0])

assert (
junit_report_validation.max_level() == JunitValidationLevel.Valid
), "\n" + "\n".join(
[
issue.error_message
for test_suite in junit_report_validation.test_suites_owned()
for test_case in test_suite.test_cases_owned()
for issue in test_case.issues_flat()
]
)


def test_repo_validate():
import math
import time

from context_py import BundleRepo, RepoUrlParts, RepoValidationLevel, repo_validate

repo = RepoUrlParts("github", "trunk-io", "analytics-cli")
bundle_repo = BundleRepo(
repo,
".",
"https://github.com/trunk-io/analytics-cli",
"abc",
"main",
math.floor(time.time()),
"commit",
"Spikey",
"spikey@trunk.io",
)

repo_validation = repo_validate(bundle_repo)

assert repo_validation.max_level() == RepoValidationLevel.Valid, "\n" + "\n".join(
[issue.error_message for issue in repo_validation.issues_flat()]
)
10 changes: 0 additions & 10 deletions rustfmt.toml
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is not used by trunk CLI (and IMO is very divergent from normal Rust code styling)

This file was deleted.

Loading