蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Eclipse 工作空间三层资源树、.metadata 配置、新建向导校验与切换隔离离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-workspaces.py
explore-workspaces.py148 行
"""Offline Eclipse Workspace hierarchy, .metadata prefs & workspace-switch verifier for Ch08 (workspaces)."""
import json
import tempfile
from pathlib import Path

VERSION = "CPython 3.12.14 offline Eclipse workspace-resource verifier (desktop Eclipse SWT GUI absent)"


def check(name, summary, expected, actual):
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def create_resource(workspace_root, rel_path, is_folder):
    if ".." in Path(rel_path).parts:
        raise ValueError("Path traversal not allowed")
    target = workspace_root / rel_path
    if not target.parent.exists():
        raise FileNotFoundError("Parent container does not exist")
    if target.exists():
        raise FileExistsError("Resource already exists")
    if is_folder:
        target.mkdir()
    else:
        target.write_text("", encoding="utf-8")
    return target


def main():
    cases = []

    # Case 1: Workspace 3-tier resource hierarchy (Workspace -> Project -> Folder -> File)
    with tempfile.TemporaryDirectory() as tmp:
        ws = Path(tmp) / "ws-main"
        proj = ws / "demo-project"
        (proj / "src" / "config").mkdir(parents=True, exist_ok=True)
        (proj / ".project").write_text("<projectDescription><name>demo-project</name></projectDescription>", encoding="utf-8")
        (proj / "src" / "config" / "app.properties").write_text("env=local\n", encoding="utf-8")
        projects = [p.name for p in ws.iterdir() if p.is_dir() and (p / ".project").is_file()]
        folders = [p.relative_to(proj).as_posix() for p in proj.rglob("*") if p.is_dir()]
        files = [p.relative_to(proj).as_posix() for p in proj.rglob("*") if p.is_file() and p.name != ".project"]
    cases.append(
        check(
            "workspace_resource_hierarchy",
            "Verify Workspace top-level project containment of folders and files",
            {
                "projects": ["demo-project"],
                "folders": ["src", "src/config"],
                "files": ["src/config/app.properties"],
            },
            {
                "projects": projects,
                "folders": sorted(folders),
                "files": sorted(files),
            },
        )
    )

    # Case 2: Workspace .metadata preferences (org.eclipse.core.resources.prefs & org.eclipse.ui.ide.prefs)
    with tempfile.TemporaryDirectory() as tmp:
        ws = Path(tmp) / "ws-prefs"
        settings = ws / ".metadata" / ".plugins" / "org.eclipse.core.runtime" / ".settings"
        settings.mkdir(parents=True, exist_ok=True)
        (settings / "org.eclipse.core.resources.prefs").write_text(
            "eclipse.preferences.version=1\nencoding=UTF-8\nrefresh.enabled=true\n",
            encoding="utf-8",
        )
        (settings / "org.eclipse.ui.ide.prefs").write_text(
            "SHOW_WORKSPACE_SELECTION_DIALOG=false\nMAX_RECENT_WORKSPACES=5\n",
            encoding="utf-8",
        )
        res_lines = (settings / "org.eclipse.core.resources.prefs").read_text(encoding="utf-8")
        ide_lines = (settings / "org.eclipse.ui.ide.prefs").read_text(encoding="utf-8")
    cases.append(
        check(
            "workspace_metadata_and_prefs",
            "Verify Workspace preferences under .metadata/.plugins/org.eclipse.core.runtime/.settings",
            {
                "encoding_utf8": True,
                "auto_refresh_enabled": True,
                "skip_startup_prompt": True,
            },
            {
                "encoding_utf8": "encoding=UTF-8" in res_lines,
                "auto_refresh_enabled": "refresh.enabled=true" in res_lines,
                "skip_startup_prompt": "SHOW_WORKSPACE_SELECTION_DIALOG=false" in ide_lines,
            },
        )
    )

    # Case 3: File > New > Folder and File > New > File wizard validation
    with tempfile.TemporaryDirectory() as tmp:
        ws = Path(tmp) / "ws-wiz"
        (ws / "app").mkdir(parents=True)
        f_dir = create_resource(ws, "app/docs", is_folder=True)
        f_file = create_resource(ws, "app/docs/readme.txt", is_folder=False)
        dup_rejected = False
        try:
            create_resource(ws, "app/docs/readme.txt", is_folder=False)
        except FileExistsError:
            dup_rejected = True
        missing_parent_rejected = False
        try:
            create_resource(ws, "app/nope/a.txt", is_folder=False)
        except FileNotFoundError:
            missing_parent_rejected = True
        created_ok = f_dir.is_dir() and f_file.is_file()
    cases.append(
        check(
            "new_folder_and_file_wizard_ops",
            "Validate New Folder and New File wizard creation, duplicate rejection, and missing-parent guards",
            {"created_ok": True, "dup_rejected": True, "missing_parent_rejected": True},
            {"created_ok": created_ok, "dup_rejected": dup_rejected, "missing_parent_rejected": missing_parent_rejected},
        )
    )

    # Case 4: File -> Switch Workspace isolation across two workspaces
    with tempfile.TemporaryDirectory() as tmp:
        ws_a = Path(tmp) / "workspace-a"
        ws_b = Path(tmp) / "workspace-b"
        (ws_a / "proj-alpha").mkdir(parents=True)
        (ws_b / "proj-beta").mkdir(parents=True)
        list_a = [p.name for p in ws_a.iterdir() if p.is_dir()]
        list_b = [p.name for p in ws_b.iterdir() if p.is_dir()]
    cases.append(
        check(
            "switch_workspace_isolation",
            "Verify switching workspaces isolates active project sets cleanly",
            {"ws_a_projects": ["proj-alpha"], "ws_b_projects": ["proj-beta"], "disjoint": True},
            {
                "ws_a_projects": list_a,
                "ws_b_projects": list_b,
                "disjoint": set(list_a).isdisjoint(set(list_b)),
            },
        )
    )

    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/eclipse-workspaces-lab/explore-workspaces.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-workspaces.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Eclipse

课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a2259.zip

下载课程关联工程包:Eclipse

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2293.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习