|
| 1 | +import os |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +from pathlib import Path |
| 5 | +from tempfile import TemporaryDirectory |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from dagster._utils.env import environ |
| 10 | +from docs_beta_snippets_tests.snippet_checks.guides.components.utils import ( |
| 11 | + DAGSTER_ROOT, |
| 12 | + EDITABLE_DIR, |
| 13 | + MASK_EDITABLE_DAGSTER, |
| 14 | + MASK_JAFFLE_PLATFORM, |
| 15 | + MASK_SLING_DOWNLOAD_DUCKDB, |
| 16 | + MASK_SLING_PROMO, |
| 17 | + MASK_SLING_WARNING, |
| 18 | + MASK_TIME, |
| 19 | +) |
| 20 | +from docs_beta_snippets_tests.snippet_checks.utils import ( |
| 21 | + _run_command, |
| 22 | + check_file, |
| 23 | + compare_tree_output, |
| 24 | + create_file, |
| 25 | + re_ignore_after, |
| 26 | + re_ignore_before, |
| 27 | + run_command_and_snippet_output, |
| 28 | +) |
| 29 | + |
| 30 | +MASK_MY_EXISTING_PROJECT = (r" \/.*?\/my-existing-project", " /.../my-existing-project") |
| 31 | + |
| 32 | + |
| 33 | +COMPONENTS_SNIPPETS_DIR = ( |
| 34 | + DAGSTER_ROOT |
| 35 | + / "examples" |
| 36 | + / "docs_beta_snippets" |
| 37 | + / "docs_beta_snippets" |
| 38 | + / "guides" |
| 39 | + / "components" |
| 40 | + / "migrating-definitions" |
| 41 | +) |
| 42 | + |
| 43 | + |
| 44 | +MY_EXISTING_PROJECT = Path(__file__).parent / "my-existing-project" |
| 45 | + |
| 46 | + |
| 47 | +def test_components_docs_migrating_definitions(update_snippets: bool) -> None: |
| 48 | + snip_no = 0 |
| 49 | + |
| 50 | + def next_snip_no(): |
| 51 | + nonlocal snip_no |
| 52 | + snip_no += 1 |
| 53 | + return snip_no |
| 54 | + |
| 55 | + with ( |
| 56 | + TemporaryDirectory() as tempdir, |
| 57 | + environ( |
| 58 | + { |
| 59 | + "COLUMNS": "90", |
| 60 | + "NO_COLOR": "1", |
| 61 | + "HOME": "/tmp", |
| 62 | + "DAGSTER_GIT_REPO_DIR": str(DAGSTER_ROOT), |
| 63 | + } |
| 64 | + ), |
| 65 | + ): |
| 66 | + os.chdir(tempdir) |
| 67 | + |
| 68 | + _run_command(f"cp -r {MY_EXISTING_PROJECT} . && cd my-existing-project") |
| 69 | + _run_command(r"find . -type d -name __pycache__ -exec rm -r {} \+") |
| 70 | + _run_command( |
| 71 | + r"find . -type d -name my_existing_project.egg-info -exec rm -r {} \+" |
| 72 | + ) |
| 73 | + |
| 74 | + run_command_and_snippet_output( |
| 75 | + cmd="tree", |
| 76 | + snippet_path=COMPONENTS_SNIPPETS_DIR / f"{next_snip_no()}-tree.txt", |
| 77 | + update_snippets=update_snippets, |
| 78 | + custom_comparison_fn=compare_tree_output, |
| 79 | + ) |
| 80 | + |
| 81 | + check_file( |
| 82 | + Path("my_existing_project") / "definitions.py", |
| 83 | + COMPONENTS_SNIPPETS_DIR / f"{next_snip_no()}-definitions-before.py", |
| 84 | + update_snippets=update_snippets, |
| 85 | + ) |
| 86 | + |
| 87 | + check_file( |
| 88 | + Path("my_existing_project") / "elt" / "definitions.py", |
| 89 | + COMPONENTS_SNIPPETS_DIR / f"{next_snip_no()}-inner-definitions-before.py", |
| 90 | + update_snippets=update_snippets, |
| 91 | + ) |
| 92 | + |
| 93 | + _run_command(cmd="uv venv") |
| 94 | + _run_command(cmd="uv sync") |
| 95 | + _run_command( |
| 96 | + f"uv add --editable '{EDITABLE_DIR / 'dagster-components'!s}' '{DAGSTER_ROOT / 'python_modules' / 'dagster'!s}' '{DAGSTER_ROOT / 'python_modules' / 'dagster-webserver'!s}'" |
| 97 | + ) |
| 98 | + |
| 99 | + run_command_and_snippet_output( |
| 100 | + cmd="dg component scaffold 'definitions@dagster_components' elt-definitions", |
| 101 | + snippet_path=COMPONENTS_SNIPPETS_DIR / f"{next_snip_no()}-scaffold.txt", |
| 102 | + update_snippets=update_snippets, |
| 103 | + snippet_replace_regex=[MASK_MY_EXISTING_PROJECT], |
| 104 | + ) |
| 105 | + |
| 106 | + create_file( |
| 107 | + Path("my_existing_project") |
| 108 | + / "components" |
| 109 | + / "elt-definitions" |
| 110 | + / "component.yaml", |
| 111 | + """type: definitions@dagster_components |
| 112 | +
|
| 113 | +params: |
| 114 | + definitions_path: definitions.py |
| 115 | +""", |
| 116 | + COMPONENTS_SNIPPETS_DIR / f"{next_snip_no()}-component-yaml.txt", |
| 117 | + ) |
| 118 | + |
| 119 | + run_command_and_snippet_output( |
| 120 | + cmd="mv my_existing_project/elt/definitions.py my_existing_project/components/elt-definitions && rm -rf my_existing_project/elt", |
| 121 | + snippet_path=COMPONENTS_SNIPPETS_DIR / f"{next_snip_no()}-mv.txt", |
| 122 | + update_snippets=update_snippets, |
| 123 | + ) |
| 124 | + |
| 125 | + create_file( |
| 126 | + Path("my_existing_project") / "definitions.py", |
| 127 | + """from pathlib import Path |
| 128 | +
|
| 129 | +import dagster_components as dg_components |
| 130 | +
|
| 131 | +import dagster as dg |
| 132 | +from my_existing_project.analytics import definitions as analytics_definitions |
| 133 | +
|
| 134 | +defs = dg.Definitions.merge( |
| 135 | + dg.load_definitions_from_module(analytics_definitions), |
| 136 | + dg_components.build_component_defs(Path(__file__).parent / "components"), |
| 137 | +) |
| 138 | +""", |
| 139 | + COMPONENTS_SNIPPETS_DIR / f"{next_snip_no()}-definitions-after.py", |
| 140 | + ) |
| 141 | + |
| 142 | + _run_command(r"find . -type d -name __pycache__ -exec rm -r {} \+") |
| 143 | + _run_command( |
| 144 | + r"find . -type d -name my_existing_project.egg-info -exec rm -r {} \+" |
| 145 | + ) |
| 146 | + |
| 147 | + run_command_and_snippet_output( |
| 148 | + cmd="tree", |
| 149 | + snippet_path=COMPONENTS_SNIPPETS_DIR / f"{next_snip_no()}-tree-after.txt", |
| 150 | + update_snippets=update_snippets, |
| 151 | + custom_comparison_fn=compare_tree_output, |
| 152 | + ) |
| 153 | + |
| 154 | + # validate loads |
| 155 | + _run_command( |
| 156 | + "uv run dagster asset materialize --select '*' -m 'my_existing_project.definitions'" |
| 157 | + ) |
0 commit comments