Skip to content

Commit

Permalink
Better LWJGL version resolver & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolCat467 authored Mar 14, 2024
2 parents 889f02b + 4c69d0e commit 4d4c001
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 17 deletions.
4 changes: 0 additions & 4 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,3 @@ updates:
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
commit-message:
prefix: "[pip prod] "
prefix-development: "[pip dev] "
include: "scope"
40 changes: 40 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Tests

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
push:
branches: ["main"]
pull_request:
branches: ["main"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
test:
# The type of runner that the job will run on
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@main
with:
python-version: ${{ matrix.python-version }}
cache: pip
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
- name: Test with tox
run: tox
19 changes: 16 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true

[tool.ruff.isort]
[tool.ruff.lint.isort]
combine-as-imports = true

[tool.pycln]
Expand All @@ -84,6 +84,8 @@ line-length = 79
fix = true

include = ["*.py", "*.pyi", "**/pyproject.toml"]

[tool.ruff.lint]
select = [
"A", # flake8-builtins
"ASYNC", # flake8-async
Expand Down Expand Up @@ -121,6 +123,13 @@ extend-ignore = [
"SIM117", # Use multiple with statements at the same time
]

[tool.ruff.lint.per-file-ignores]
"tests/*" = [
"D100", # undocumented-public-module
"D103", # undocumented-public-function
"D107", # Missing docstring
]

[tool.pytest.ini_options]
addopts = "--cov-report term-missing --cov=fix_lwjgl"
testpaths = [
Expand Down Expand Up @@ -158,12 +167,16 @@ partial_branches = [
[tool.tox]
legacy_tox_ini = """
[tox]
envlist = py311, mypy
envlist = py38, py39, py310, py311, py312, mypy, pytest
isolated_build = false
[gh-actions]
python =
3.11: py311, pytest, mypy
3.8: py38, pytest, mypy
3.9: py39, pytest
3.10: py310, pytest
3.11: py311, pytest
3.12: py312, pytest, mypy
[testenv]
setenv =
Expand Down
48 changes: 38 additions & 10 deletions src/fix_lwjgl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,32 @@ async def rewrite_class_path_lwjgl2(
return class_path


def discover_lwjgl_version(version_string: str) -> int:
"""Discover version of lwjgl to use from version string."""
# Extract grouped numbers
parsed = []
current = ""
# Extra space at end means last number group will
# be added to parsed properly because digit check fails
for char in f"{version_string} ":
if char in "0123456789":
current += char
elif current:
parsed.append(int(current))
current = ""

parsed_version = tuple(parsed)

if "w" in version_string:
# The snapshot minecraft updated to lwjgl 3 is apparently 17w43b
if parsed_version >= (17, 43):
return 3
return 2
if parsed_version >= (1, 13):
return 3
return 2


async def rewrite_mc_args(
loop: asyncio.AbstractEventLoop,
mc_args: list[str],
Expand All @@ -519,17 +545,17 @@ async def rewrite_mc_args(
if "-cp" not in mc_args:
return mc_args

mc_vers = tuple(
map(int, mc_args[mc_args.index("--version") + 1].split(".")),
)
lwjgl_vers = 2 if mc_vers < (1, 13) else 3
# The snapshot minecraft updated to lwjgl 3 is apparently 17w43b
# TODO: Handle snapshots properly
raw_version = "Legacy Minecraft"
lwjgl_vers = 2
if "--version" in mc_args:
raw_version = mc_args[mc_args.index("--version") + 1]
lwjgl_vers = discover_lwjgl_version(raw_version)

lib_path = None
for arg in mc_args:
lib_path: str | None = None
for arg in reversed(mc_args):
if arg.startswith("-Dorg.lwjgl.librarypath="):
lib_path = arg.split("=", 1)[1]
break

cls_path = mc_args.index("-cp")

Expand All @@ -556,8 +582,7 @@ async def rewrite_mc_args(

mc_args[cls_path + 1] = os.pathsep.join(class_path)

mc_ver_text = ".".join(map(str, mc_vers))
log(f"Rewrote lwjgl class paths for {mc_ver_text} (LWJGL {lwjgl_vers})")
log(f"Rewrote lwjgl class paths for {raw_version} (LWJGL {lwjgl_vers})")

return mc_args

Expand Down Expand Up @@ -635,6 +660,9 @@ def run(args: list[str]) -> int:

if not args:
log("No java arguments to rewrite lwjgl class paths for!")
log(
"Make sure you are using `Wrapper Command` and not pre or post launch command!",
)
return 1

if args[0].lower() == "-noop":
Expand Down
65 changes: 65 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from __future__ import annotations

import os

import fix_lwjgl
import pytest


def test_get_paths() -> None:
assert fix_lwjgl.get_paths(
{
"": [
"main.py",
"waffles.txt",
],
"folder": {
"": [
"folder_one.txt",
],
"inner": {
"": [
"folder_two.txt",
],
"inner_two": [
"folder_three.txt",
],
},
},
},
) == [
"main.py",
"waffles.txt",
os.path.join("folder", "folder_one.txt"),
os.path.join("folder", "inner", "folder_two.txt"),
os.path.join("folder", "inner", "inner_two", "folder_three.txt"),
]


def test_get_address() -> None:
assert (
fix_lwjgl.get_address("CoolCat467", "fix-lwjgl", "HEAD", "README.md")
== "https://raw.githubusercontent.com/CoolCat467/fix-lwjgl/HEAD/README.md"
)


def test_get_lwjgl_file_url() -> None:
assert (
fix_lwjgl.get_lwjgl_file_url("bin/lwjgl/lwjgl.jar")
== "https://build.lwjgl.org/release/latest/bin/lwjgl/lwjgl.jar"
)


@pytest.mark.parametrize(
("version", "expected"),
[
("17w43b", 3),
("17w42b", 2),
("1.12.2", 2),
("1.13.0", 3),
("1.7.10", 2),
("1.20.1", 3),
],
)
def test_discover_lwjgl_version(version: str, expected: int) -> None:
assert fix_lwjgl.discover_lwjgl_version(version) == expected

0 comments on commit 4d4c001

Please sign in to comment.