Skip to content

Commit

Permalink
Lint all codebase and fix tests (#2)
Browse files Browse the repository at this point in the history
Applies pre-commit and fix tests
  • Loading branch information
ryanking13 authored Jun 11, 2024
1 parent 7f10a50 commit 2243e60
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 285 deletions.
142 changes: 71 additions & 71 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,86 +1,86 @@
name: main

on:
push:
pull_request:
push:
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.12"

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Build
run: |
python -m pip install build
python -m build .
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "20"

- name: Store the distribution packages
uses: actions/upload-artifact@v3
with:
name: python-package-distributions
path: dist/

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build
run: |
python -m pip install build
python -m build .
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Store the distribution packages
uses: actions/upload-artifact@v3
with:
name: python-package-distributions
path: dist/

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Test
run: |
python -m pip install --upgrade pip
pip install ".[test]"
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

pytest -v \
--junitxml=test-results/junit.xml \
--cov=pyodide-build \
pyodide_build
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.12"

- uses: codecov/codecov-action@v4
with:
fail_ci_if_error: false
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "20"

publish:
name: >-
Publish Python 🐍 distribution 📦 to PyPI
needs:
- build
runs-on: ubuntu-latest
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
environment:
name: pypi
url: https://pypi.org/p/pyodide-build
permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing
steps:
- name: Download all the dists
uses: actions/download-artifact@v3
with:
name: python-package-distributions
path: dist/
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
- name: Test
run: |
python -m pip install --upgrade pip
pip install ".[test]"
pytest -v \
--junitxml=test-results/junit.xml \
--cov=pyodide-build \
pyodide_build
- uses: codecov/codecov-action@v4
with:
fail_ci_if_error: false

publish:
name: >-
Publish to PyPI
needs:
- build
runs-on: ubuntu-latest
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
environment:
name: pypi
url: https://pypi.org/p/pyodide-build
permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing
steps:
- name: Download all the dists
uses: actions/download-artifact@v3
with:
name: python-package-distributions
path: dist/
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ emsdk/emsdk
geckodriver.log
node_modules
dist/
pyodide-build/**/build.log
pyodide_build/**/build.log
xbuildenv/
.pyodide-xbuildenv*
.pyodide-xbuildenv*
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ repos:
additional_dependencies: *mypy-deps

- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v4.0.0-alpha.8"
rev: "v3.1.0"
hooks:
- id: prettier

ci:
autoupdate_schedule: "quarterly"
autoupdate_schedule: "quarterly"
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

- pyodide-build is now developed under https://github.com/pyodide/pyodide-build.
- pyodide-build is now developed under https://github.com/pyodide/pyodide-build.
File renamed without changes.
118 changes: 19 additions & 99 deletions pyodide_build/tests/test_build_env.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,13 @@
# flake8: noqa
# This file contains tests that ensure build environment is properly initialized in
# both in-tree and out-of-tree builds.

import os

import pytest

from conftest import ROOT_PATH
from pyodide_build import build_env, common
from pyodide_build.xbuildenv import CrossBuildEnvManager
from pyodide_build.config import BUILD_KEY_TO_VAR

from .fixture import reset_cache, reset_env_vars, dummy_xbuildenv, dummy_xbuildenv_url
from pyodide_build.xbuildenv import CrossBuildEnvManager


class TestInTree:
def test_init_environment(self, reset_env_vars, reset_cache):
assert "PYODIDE_ROOT" not in os.environ

build_env.init_environment()

assert "PYODIDE_ROOT" in os.environ
assert os.environ["PYODIDE_ROOT"] == str(ROOT_PATH)

def test_init_environment_pyodide_root_already_set(
self, reset_env_vars, reset_cache
):
assert "PYODIDE_ROOT" not in os.environ
os.environ["PYODIDE_ROOT"] = "/set_by_user"

build_env.init_environment()

assert os.environ["PYODIDE_ROOT"] == "/set_by_user"

def test_get_pyodide_root(self, reset_env_vars, reset_cache):
assert "PYODIDE_ROOT" not in os.environ

assert build_env.get_pyodide_root() == ROOT_PATH

def test_get_pyodide_root_pyodide_root_already_set(
self, reset_env_vars, reset_cache
):
assert "PYODIDE_ROOT" not in os.environ
os.environ["PYODIDE_ROOT"] = "/set_by_user"

assert str(build_env.get_pyodide_root()) == "/set_by_user"

def test_search_pyodide_root(self, tmp_path, reset_env_vars, reset_cache):
pyproject_file = tmp_path / "pyproject.toml"
pyproject_file.write_text("[tool._pyodide]")
Expand All @@ -56,66 +18,6 @@ def test_search_pyodide_root(self, tmp_path, reset_env_vars, reset_cache):
pyproject_file.unlink()
assert build_env.search_pyodide_root(tmp_path) is None

def test_in_xbuildenv(self, reset_env_vars, reset_cache):
assert not build_env.in_xbuildenv()

def test_get_build_environment_vars(self, reset_env_vars, reset_cache):
build_vars = build_env.get_build_environment_vars(ROOT_PATH)

# extra variables that does not come from config files.
extra_vars = set(["PYODIDE", "PYODIDE_PACKAGE_ABI", "PYTHONPATH"])
all_keys = set(BUILD_KEY_TO_VAR.values()) | extra_vars
for var in build_vars:
assert var in all_keys, f"Unknown {var}"

# Additionally we set these variables
for var in extra_vars:
assert var in build_vars, f"Missing {var}"

def test_get_build_flag(self, reset_env_vars, reset_cache):
for key, val in build_env.get_build_environment_vars(
pyodide_root=ROOT_PATH
).items():
assert build_env.get_build_flag(key) == val

with pytest.raises(ValueError):
build_env.get_build_flag("UNKNOWN_VAR")

def test_get_build_environment_vars_host_env(
self, monkeypatch, reset_env_vars, reset_cache
):
# host environment variables should have precedence over
# variables defined in Makefile.envs

import os

e = build_env.get_build_environment_vars(ROOT_PATH)
assert e["PYODIDE"] == "1"

monkeypatch.setenv("HOME", "/home/user")
monkeypatch.setenv("PATH", "/usr/bin:/bin")
# We now inject PKG_CONFIG_LIBDIR inside buildpkg.py
# monkeypatch.setenv("PKG_CONFIG_LIBDIR", "/x/y/z:/c/d/e")

build_env.get_build_environment_vars.cache_clear()

e_host = build_env.get_build_environment_vars(ROOT_PATH)
assert e_host.get("HOME") == os.environ.get("HOME")
assert e_host.get("PATH") == os.environ.get("PATH")

assert e_host.get("HOME") != e.get("HOME")
assert e_host.get("PATH") != e.get("PATH")

build_env.get_build_environment_vars.cache_clear()

monkeypatch.delenv("HOME")
monkeypatch.setenv("RANDOM_ENV", "1234")

build_env.get_build_environment_vars.cache_clear()
e = build_env.get_build_environment_vars(ROOT_PATH)
assert "HOME" not in e
assert "RANDOM_ENV" not in e


class TestOutOfTree(TestInTree):
# Note: other tests are inherited from TestInTree
Expand All @@ -129,13 +31,31 @@ def test_init_environment(self, dummy_xbuildenv, reset_env_vars, reset_cache):
assert "PYODIDE_ROOT" in os.environ
assert os.environ["PYODIDE_ROOT"] == str(manager.pyodide_root)

def test_init_environment_pyodide_root_already_set(
self, reset_env_vars, reset_cache
):
assert "PYODIDE_ROOT" not in os.environ
os.environ["PYODIDE_ROOT"] = "/set_by_user"

build_env.init_environment()

assert os.environ["PYODIDE_ROOT"] == "/set_by_user"

def test_get_pyodide_root(self, dummy_xbuildenv, reset_env_vars, reset_cache):
assert "PYODIDE_ROOT" not in os.environ

pyodide_root = build_env.get_pyodide_root()
manager = CrossBuildEnvManager(dummy_xbuildenv / common.xbuildenv_dirname())
assert pyodide_root == manager.pyodide_root

def test_get_pyodide_root_pyodide_root_already_set(
self, reset_env_vars, reset_cache
):
assert "PYODIDE_ROOT" not in os.environ
os.environ["PYODIDE_ROOT"] = "/set_by_user"

assert str(build_env.get_pyodide_root()) == "/set_by_user"

def test_in_xbuildenv(self, dummy_xbuildenv, reset_env_vars, reset_cache):
assert build_env.in_xbuildenv()

Expand Down
24 changes: 6 additions & 18 deletions pyodide_build/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
# flake8: noqa

import shutil
import zipfile
from pathlib import Path
from typing import Any

import pytest
import zipfile
import typer
from typer.testing import CliRunner
from typing import Any

import pyodide_build
from pyodide_build import common, build_env, cli
from pyodide_build import build_env, cli, common
from pyodide_build.cli import (
build,
build_recipes,
config,
create_zipfile,
skeleton,
xbuildenv,
py_compile,
)

from .fixture import (
temp_python_lib,
temp_python_lib2,
dummy_xbuildenv,
mock_emscripten,
dummy_xbuildenv_url,
reset_env_vars,
reset_cache,
skeleton,
)

only_node = pytest.mark.xfail_browsers(
Expand Down Expand Up @@ -419,7 +407,7 @@ def my_get_config_vars(*args):
outdir = tmp_path / "out"
app = typer.Typer()
app.command(**build.main.typer_kwargs)(build.main) # type:ignore[attr-defined]
result = runner.invoke(app, [str(srcdir), "--outdir", str(outdir)])
runner.invoke(app, [str(srcdir), "--outdir", str(outdir)])
wheel_file = next(outdir.glob("*.whl"))
print(zipfile.ZipFile(wheel_file).namelist())
so_file = next(
Expand Down
Loading

0 comments on commit 2243e60

Please sign in to comment.