mcbeet / lectern

Literate Minecraft data packs and resource packs.
MIT License
20 stars 3 forks source link

Snapshots do not support directives for worldgen #361

Open BPR02 opened 5 days ago

BPR02 commented 5 days ago

lectern v0.34.0 beet v0.108.5 pytest v8.3.3 pytest-insta v0.3.0

I'm trying to use lectern snapshots to test my WIP beet plugin, but it appears that overlays aren't properly working with directives for worldgen. Generating the markdown file works fine and generating a datapack from the markdown file also works fine, but when loading the snapshot, there is no overlay in the resulting Document object. Only the worldgen directives are broken. I explicitly tested the following which resulted in the error posted further down:

This is my python test code, which is basically the same as bolt test

import os
from pathlib import Path

import pytest
from beet import ProjectCache, run_beet
from lectern import Document
from pytest_insta import SnapshotFixture

EXAMPLES = [
    f
    for f in os.listdir("examples")
    if not (f.startswith("nosnap_") or f.startswith("."))
]

@pytest.mark.parametrize("directory", EXAMPLES)
def test_build(snapshot: SnapshotFixture, directory: str, tmp_path: Path):
    with run_beet(
        directory=f"examples/{directory}",
        cache=ProjectCache(tmp_path / ".beet_cache", tmp_path / "generated"),
    ) as ctx:
        expected: Document = snapshot("pack.md")
        actual = ctx.inject(Document)
        assert actual == expected

An example can be found in this branch. Running pytest will result in the error below. https://github.com/BPR02/Observer/tree/worldgen

FAILED tests/test_examples.py::test_build[overlay_adds_dimension1] - AssertionError: assert Document(assets=ResourcePack(name='overlay_adds_dimension1_resource_pack', description='', pack_format=34), data=DataPack(name='overlay_adds_dimension1_data_pack', description='', pack_format=57)) == Document(assets=ResourcePack(name='overlay_adds_dimension1_resource_pack', description='', pack_format=34), data=DataPack(name='overlay_adds_dimension1_data_pack', description='', pack_format=57))

  Differing data pack:
    assert DataPack(name='overlay_adds_dimension1_data_pack', description='', pack_format=57) == DataPack(name='overlay_adds_dimension1_data_pack', description='', pack_format=57)

    Differing file:
    ['pack.mcmeta']

    Drill down into differing file 'pack.mcmeta':
      assert {'pack': {'pack_format': 57, 'description': '', 'supported_formats': [48, 57]}, 'overlays': {'entries': [{'formats': 48, 'directory': 'overlay_48'}]}} == {'pack': {'pack_format': 57, 'description': '', 'supported_formats': [48, 57]}}

      Common items:
      {'pack': {'description': '', 'pack_format': 57, 'supported_formats': [48, 57]}}
      Left contains 1 more item:
      {'overlays': {'entries': [{'directory': 'overlay_48', 'formats': 48}]}}

      Full diff:
        {
      +     'overlays': {
      +         'entries': [
      +             {
      +                 'directory': 'overlay_48',
      +                 'formats': 48,
      +             },
      +         ],
      +     },
            'pack': {
                'description': '',
                'pack_format': 57,
                'supported_formats': [
                    48,
                    57,
                ],
            },
        }

    Left contains 1 more namespace:
    {'demo': DataPackNamespace()}

    Left contains 1 more overlay:
    {'overlay_48': DataPack(name=None, description='', pack_format=0)}

The snapshot below is generated which is valid and can generate the correct datapack, but does not work when loaded as a snapshot.

Lectern snapshot

Data pack

@data_pack pack.mcmeta

{
  "pack": {
    "pack_format": 57,
    "description": "",
    "supported_formats": [
      48,
      57
    ]
  },
  "overlays": {
    "entries": [
      {
        "formats": 48,
        "directory": "overlay_48"
      }
    ]
  }
}

demo

@dimension demo:foo

{
  "type": "demo:this-is-the-same-in-both",
  "generator": {
    "type": "minecraft:debug"
  }
}

Overlay overlay_48

@overlay overlay_48

demo

--VVV-- HERE IS THE ISSUE ---------------------------------- @dimension demo:demo

{
  "type": "demo:this-only-exists-in-the-overlay",
  "generator": {
    "type": "minecraft:debug"
  }
}

@endoverlay

BPR02 commented 4 days ago

This bug also occurs when using ctx.data instead of ctx.inject(Document) for the snapshot, so it's likely an issue with the way it handles snapshots and not with reading the markdown file (also indicated by the fact that lectern can properly generate the datapack from the markdown file).

BPR02 commented 4 days ago

With further testing and digging through the code, this seems to be a problem with worldgen directives in snapshots, regardless if they're in an overlay. Ran this sanity test which fails after generating the snapshot.

Lectern snapshot

Data pack

@data_pack pack.mcmeta

{
  "pack": {
    "pack_format": 48,
    "description": ""
  }
}

demo

@dimension demo:demo

{
  "type": "demo:in-the-base-pack",
  "generator": {
    "type": "minecraft:debug"
  }
}