chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
@@ -0,0 +1,64 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from _pytest.mark import ParameterSet
|
||||
from pytest_examples import CodeExample, EvalExample, find_examples
|
||||
from pytest_examples.config import ExamplesConfig as BaseExamplesConfig
|
||||
|
||||
|
||||
@dataclass
|
||||
class ExamplesConfig(BaseExamplesConfig):
|
||||
known_first_party: list[str] = field(default_factory=list[str])
|
||||
|
||||
def ruff_config(self) -> tuple[str, ...]:
|
||||
config = super().ruff_config()
|
||||
if self.known_first_party:
|
||||
config = (*config, "--config", f"lint.isort.known-first-party = {self.known_first_party}")
|
||||
return config
|
||||
|
||||
|
||||
def find_doc_examples() -> Iterable[ParameterSet]:
|
||||
root_dir = Path(__file__).resolve().parents[2]
|
||||
for example in find_examples(
|
||||
root_dir / "docs",
|
||||
root_dir / "src",
|
||||
root_dir / "examples" / "dify_agent",
|
||||
):
|
||||
path = example.path.relative_to(root_dir)
|
||||
yield pytest.param(example, id=f"{path}:{example.start_line}")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("example", find_doc_examples())
|
||||
def test_documentation_examples(example: CodeExample, eval_example: EvalExample) -> None:
|
||||
prefix_settings = example.prefix_settings()
|
||||
opt_test = prefix_settings.get("test", "")
|
||||
opt_lint = prefix_settings.get("lint", "")
|
||||
line_length = int(prefix_settings.get("line_length", "120"))
|
||||
|
||||
eval_example.config = ExamplesConfig(
|
||||
ruff_ignore=["D", "Q001"],
|
||||
target_version="py312", # pyright: ignore[reportArgumentType]
|
||||
line_length=line_length,
|
||||
isort=True,
|
||||
upgrade=True,
|
||||
quotes="double",
|
||||
known_first_party=["dify_agent"],
|
||||
)
|
||||
|
||||
if not opt_lint.startswith("skip"):
|
||||
if eval_example.update_examples: # pragma: no cover
|
||||
eval_example.format_ruff(example)
|
||||
else:
|
||||
eval_example.lint_ruff(example)
|
||||
|
||||
if opt_test.startswith("skip"):
|
||||
pytest.skip(opt_test[4:].lstrip(" -") or "running code skipped")
|
||||
|
||||
if eval_example.update_examples: # pragma: no cover
|
||||
eval_example.run_print_update(example, module_globals={"__name__": "__main__"})
|
||||
else:
|
||||
eval_example.run_print_check(example, module_globals={"__name__": "__main__"})
|
||||
@@ -0,0 +1,46 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
||||
HOOKS_DIR = PROJECT_ROOT / "docs" / ".hooks"
|
||||
sys.path.append(str(HOOKS_DIR))
|
||||
|
||||
from snippets import inject_snippets, parse_file_sections, parse_snippet_directive # pyright: ignore[reportMissingImports] # noqa: E402
|
||||
|
||||
|
||||
def test_parse_snippet_directive() -> None:
|
||||
directive = parse_snippet_directive('```snippet {path="demo.py" fragment="main" hl="1"}\n```')
|
||||
|
||||
assert directive is not None
|
||||
assert directive.path == "demo.py"
|
||||
assert directive.fragment == "main"
|
||||
assert directive.extra_attrs == {"hl": "1"}
|
||||
|
||||
|
||||
def test_parse_file_sections_and_inject_snippet(tmp_path: Path) -> None:
|
||||
source = tmp_path / "demo.py"
|
||||
source.write_text(
|
||||
"""import asyncio
|
||||
|
||||
### [main]
|
||||
async def main() -> None:
|
||||
print("hello")
|
||||
### [/main]
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
parsed = parse_file_sections(source)
|
||||
assert "main" in parsed.sections
|
||||
|
||||
markdown = '```snippet {path="/examples/dify_agent/dify_agent_examples/run_server_consumer.py"}\n```'
|
||||
rendered = inject_snippets(markdown, PROJECT_ROOT / "docs")
|
||||
|
||||
assert rendered.startswith('```py {title="examples/dify_agent/dify_agent_examples/run_server_consumer.py"}')
|
||||
assert "async def main() -> None:" in rendered
|
||||
assert "asyncio.run(main())" in rendered
|
||||
Reference in new issue
Block a user