diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index bbf2b64..b8a6437 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -39,7 +39,10 @@ jobs: # desktop release has yet. # v1.0.1 adds the Windows archive on top of 1.0.0 and needs the same host, # but is published as a stable release so the Store tracker lists it. - PRERELEASE_TAGS: v0.0.1 v1.0.0 + # v2.0.0 declares buildSystems runConfigurationTypes (1lck/Lithe-IDEA#2148, + # Lithe-core-rust#94), which no desktop release has yet (desktop-v0.8.5 + # pins Core 5c48a67, before #94). + PRERELEASE_TAGS: v0.0.1 v1.0.0 v2.0.0 run: | prerelease=false for tag in $PRERELEASE_TAGS; do diff --git a/README.md b/README.md index 23bafc6..0911823 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,14 @@ 为 [Lithe](https://github.com/1lck/Lithe-IDEA) 提供 Go 支持:用你电脑上的 `gopls` 提供代码智能, 用声明式构建系统接口(`buildSystems`)识别 Go 模块,在构建窗口里列出包,运行 `go build`、`go vet`、 -`go test` 和 `go run`,查看模块依赖;用你电脑上的 Delve(`dlv`)调试,在「新建项目」里创建 Go 模块。 +`go test` 和 `go run`,查看模块依赖;在「运行/调试配置」里新建 Go 构建和 Go 测试配置;用你电脑上的 Delve(`dlv`)调试, +在「新建项目」里创建 Go 模块。 这是一个**社区插件**,按插件契约 v0 编写。插件只有一份 `plugin.json` 和几份项目模板,不附带任何可执行文件, 也没有后端进程。 > English: Go support for Lithe — code intelligence through your own `gopls`, the Go Modules build system > (package model, build, vet, test, run and the module list) through the declarative `buildSystems` contract, -> debugging through your own Delve, and a Go module entry in New Project. +> Go Build and Go Test run configurations, debugging through your own Delve, and a Go module entry in New Project. > The plugin is a `plugin.json` and a few project templates, with no executables and no backend process. > **兼容性**:按插件契约 v0(`schemaVersion: 0`、`pluginAPIVersion: 0`)编写,只能装进新版 Lithe 桌面端 @@ -19,7 +20,10 @@ > ([1lck/Lithe-IDEA#1971](https://github.com/1lck/Lithe-IDEA/pull/1971))。1.0.1 起作为正式版本发布并收录到 Lithe Store; > 没有这些能力的桌面端会从 Store 装到 0.0.1。语言服务器的 `indexingExclusions` 声明需要固定了包含 > [Lithe-core-rust#68](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/68) 的 Core 的桌面端, -> 更早的桌面端不认识这个字段,会拒绝整份清单。 +> 更早的桌面端不认识这个字段,会拒绝整份清单。2.0.0 的 Go 构建和 Go 测试运行配置需要支持插件声明运行配置类型的桌面端 +> ([1lck/Lithe-IDEA#2148](https://github.com/1lck/Lithe-IDEA/pull/2148),固定了 +> [Lithe-core-rust#94](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/94) 的 Core),还没有桌面端发布版本包含它, +> 所以 2.0.0 是预发布版本;更早的桌面端会拒绝安装 2.0.0,继续用 Store 里的 1.0.1。 ## 功能 @@ -30,6 +34,7 @@ | 构建 | Build(`go build ./...`)作用于模块;Vet(`go vet`)可作用于模块或单个包 | | 运行 | 每个 `main` 包在构建窗口里有「运行 <包名>」按钮,执行 `go run <导入路径>`,输出显示在构建输出里 | | 测试 | `go test` 作用于模块或单个包,可用 `-run` 只跑一个测试;输出显示在构建输出里,插件不提供测试报告文件 | +| 运行配置 | 「运行/调试配置」的「+」里有 Go 构建(`go run` 一个软件包、文件或目录)和 Go 测试(`go test` 一个软件包或目录,可按模式过滤),可存为项目文件、允许多个实例,见下文“运行配置” | | 诊断 | 把 `文件:行:列: 消息` 形式的编译输出显示为问题 | | 依赖 | `go list -m -json all` 列出模块依赖(含版本和是否间接依赖) | | 同步 | `go.mod`、`go.sum`、`go.work` 变化后重新读取模块 | @@ -90,6 +95,36 @@ 已经忽略它。 - 目前只支持启动调试(launch),不支持附加到运行中的进程,也不支持调试测试。 +## 运行配置 + +打开「运行/调试配置」,点「+」,选 **Go 构建** 或 **Go 测试**。名称、存为项目文件、允许多个实例、程序参数、 +工作目录、环境变量和启动前步骤由 Lithe 统一提供,下面是插件自己的字段: + +**Go 构建**:执行 `go run [Go 工具实参] <要运行的内容> [程序参数]`,程序输出显示在 Run 窗口里。 + +| 字段 | 说明 | +| --- | --- | +| 运行种类 | 软件包、文件或目录,决定下面显示哪一项,默认是软件包 | +| 软件包路径 | main 软件包的导入路径(如 `example.com/app/cmd/server`),或相对工作目录的路径(如 `./cmd/server`) | +| 文件 | main 软件包里的一个 `.go` 文件,`go run` 只构建这个文件 | +| 目录 | main 软件包所在的目录 | +| Go 工具实参 | 放在要运行的内容前面传给 `go run`,例如 `-mod=vendor`、`-race` | + +**Go 测试**:执行 `go test [Go 工具实参] [-run <模式>] <软件包或目录> [程序参数]`。 + +| 字段 | 说明 | +| --- | --- | +| 测试种类 | 目录或软件包,默认是软件包 | +| 软件包路径 | 导入路径,或相对工作目录的软件包模式,默认 `./...`(工作目录下的所有软件包) | +| 目录 | 测试这个目录及其子目录里的所有软件包(传给 go 的是 `<目录>/...`) | +| 模式 | 要运行的测试的正则表达式,作为 `-run` 传入,例如 `^TestName$`;留空运行所有测试 | +| Go 工具实参 | 放在软件包前面传给 `go test`,例如 `-v`、`-count=1` | + +文件和目录在项目里存为相对项目根目录的路径,运行时传给 go 的是绝对路径。存为项目文件的配置写在 +`.lithe/run/plugin/io.github.lithe-idea.go-support.toml`,可以提交到仓库。两种配置用的 `go` 和构建窗口相同 +(见上文“工具链”),构建选项里的 Build Tags 和 Race Detector 也会加进命令。目前还不能调试这两种配置, +调试请用「调试」窗口。 + ## 新建项目 「新建项目」对话框左侧有 **Go**。可以填写: @@ -213,7 +248,7 @@ CI 在 macOS、Windows x64 和 Windows ARM64 上跑这个脚本,Windows 上测 ```bash node scripts/package.mjs -LITHE_TEST_GO_PLUGIN_ARCHIVE="$PWD/dist/go-support-1.0.1-darwin.zip" node scripts/e2e-host.mjs +LITHE_TEST_GO_PLUGIN_ARCHIVE="$PWD/dist/go-support-2.0.0-darwin.zip" node scripts/e2e-host.mjs ``` 插件包路径要写绝对路径。脚本把固定版本的宿主(只取 `desktop/` 和 `scripts/`)检出到 `.artifacts/lithe-host`, @@ -239,7 +274,8 @@ Lithe 主仓库的功能矩阵不收录插件能力,改动插件行为时在 3. `Release` 工作流会核对标签与版本一致、打包并发布 Release(说明里附 SHA-256 和 `docs/releases/v<版本>.md`)。`release.yml` 里 `PRERELEASE_TAGS` 列出的版本会标为预发布版本: - 目前是只能装进新版桌面端的 `v0.0.1`,以及依赖宿主 #1954、#1964、#1971 的 `v1.0.0`;`v1.0.1` 起作为正式版本发布。 + 目前是只能装进新版桌面端的 `v0.0.1`、依赖宿主 #1954、#1964、#1971 的 `v1.0.0`,以及依赖宿主 #2148 的 `v2.0.0`; + `v1.0.1` 是正式版本。 4. Lithe Store 的自动追踪会发现稳定版本并开合并请求,合并后用户即可在 Lithe 中更新。自动追踪会跳过预发布版本; 需要收录时在 Store 中手工提交条目,并使用已发布 ZIP 的实际 SHA-256。 diff --git a/docs/feature-matrix.md b/docs/feature-matrix.md index bc9e110..c30e038 100644 --- a/docs/feature-matrix.md +++ b/docs/feature-matrix.md @@ -22,6 +22,7 @@ | --- | --- | --- | --- | --- | | `go-support-plugin` | gopls 代码智能、Go Modules 构建、运行与 `go test` | implemented / pending | implemented / pending | 2026-11-15 macOS 宿主端到端测试通过;Windows ARM64、x64 [原生 CI 通过](https://github.com/Lithe-IDEA/lithe-plugin-go/actions/runs/43728078241) | | `go-debug-delve` | 用用户的 Delve 调试 Go 包 | implemented / pending | partial / pending | 2026-11-15 macOS 宿主端到端测试通过;Windows x64 [原生 CI 通过](https://github.com/Lithe-IDEA/lithe-plugin-go/actions/runs/43728078241);Windows ARM64 上 Delve 不支持 | +| `go-run-configuration-types` | 「运行/调试配置」里的 Go 构建和 Go 测试 | implemented / pending | implemented / pending | 2026-11-18 macOS 宿主端到端测试通过(宿主 a8797ae);Windows 只有命令层面的 `scripts/test.mjs` | | `go-project-generator` | 「新建项目」里的 Go 模块,GOROOT 选择和 Go 版本默认值 | implemented / pending | implemented / pending | 2026-11-15 macOS 宿主集成测试通过;Windows ARM64、x64 [原生 CI 通过](https://github.com/Lithe-IDEA/lithe-plugin-go/actions/runs/43728078241)(模板生成的模块能构建和测试) | ## go-support-plugin @@ -129,6 +130,58 @@ Go 1.27.4、Delve 1.27.2。`node scripts/e2e-host.mjs` 通过 `TestOfficialGoPlu 6. 没有 `dlv` 时,插件详情「功能」页的调试器卡片显示“未找到 dlv”和安装按钮;安装后开始调试能停在断点。 7. 整个过程中插件安装目录不被写入。 +## go-run-configuration-types + +**能力**:Go 构建和 Go 测试两种运行配置类型,出现在「运行/调试配置」的「+」菜单里,名称、说明、字段标题和选项来自插件的中文翻译。 +Go 构建按“运行种类”执行 `go run` 一个软件包(导入路径或相对路径)、一个 `.go` 文件或一个目录;Go 测试执行 `go test` +一个软件包模式(默认 `./...`)或一个目录下的所有软件包(`<目录>/...`),可用“模式”作为 `-run` 过滤。两种都有“Go 工具实参”, +程序参数、工作目录、环境变量、启动前步骤、存为项目文件和允许多个实例由宿主统一提供。存为项目文件时写进 +`.lithe/run/plugin/io.github.lithe-idea.go-support.toml`。 + +| 平台 | 实现 | 验证 | 说明 | +| --- | --- | --- | --- | +| macOS | implemented | pending | 需要支持 `runConfigurationTypes` 的桌面端(1lck/Lithe-IDEA#2148,Core Lithe-core-rust#94);旧宿主不认识这个字段,会拒绝安装本版本 | +| Windows | implemented | pending | 命令与平台无关,文件和目录以绝对路径传给 `go.exe`;还没有在 Windows 版 Lithe 里跑过 | + +**证据**: + +- `io.github.lithe-idea.go-support/plugin.json`:`buildSystems` 里 `go` 的 `runConfigurationTypes`(`build`、`test`)和 + `requires.runConfigurationTypes`;`l10n/zh-Hans.json` 的 `runConfigurationTypes.go`。 +- `scripts/test.mjs`:按契约的展开规则把两种类型的字段拼成命令,用真实的 go 运行软件包、文件、目录的 Go 构建, + 以及软件包、目录、模式过滤的 Go 测试。 +- `e2e/host/desktop/internal/workbench/official_go_plugin_run_types_e2e_test.go`:宿主端到端测试。 + +**依赖的宿主能力**:`runConfigurationTypes`(API-29,Core Lithe-core-rust#94 的表单、保存和 `build.runConfigurationPlan`; +桌面端 1lck/Lithe-IDEA#2148 的「+」菜单、插件字段和翻译、`.lithe/run/plugin/.toml`、“允许多个实例”)。 + +**限制**: + +- 不能调试:宿主拒绝调试插件声明的运行类型,调试用「调试」窗口的 Go (Delve)。 +- 字段排在宿主的工作目录和环境变量前面,选项是分段按钮而不是下拉框;目录字段不能有默认值,Go 测试的目录不会默认填项目根目录; + 名称不会按所选软件包自动生成;类型图标按执行类别显示宿主图标,不显示插件包里的 Go 图标。 +- 还没有的字段:输出目录、构建后运行、模拟终端、重定向输入、以 sudo 运行、模块、测试框架、使用所有自定义构建标记。 + +**最近验证**:2026-11-18,macOS(Apple Silicon),插件 2.0.0 发布包,宿主 `a8797ae`(1lck/Lithe-IDEA main,包含 #2148), +Go 1.27.4。`node scripts/e2e-host.mjs` 通过 `TestOfficialGoPluginRunsGoBuildAndGoTestConfigurations`:两种类型以“Go 构建” +“Go 测试”列出;在对话框里新建 Go 构建,运行种类默认软件包、只显示软件包路径,填 `./cmd/echo` 和程序参数 `one two` +后运行,输出 `echo: one|two`;新建 Go 测试,切到目录后显示目录、隐藏软件包路径,填项目根目录、模式 `^TestGreet$` 和 +`-v -count=1` 后运行,只跑了 TestGreet;两条配置都写进插件的运行文档,安装目录逐字节不变。`scripts/test.mjs` 另外用真实的 go +覆盖文件和目录的 Go 构建、软件包模式的 Go 测试。没有覆盖存为项目文件后重新打开项目、允许多个实例、启动前步骤, +也没有做下面的手工验收。 + +**验证方法**(macOS): + +1. 安装插件包,打开含 `go.mod` 的项目,打开「运行/调试配置」点「+」:内置类型之后有 Go 构建和 Go 测试,图标和说明清楚。 +2. 新建 Go 构建:运行种类在软件包、文件、目录间切换时只显示对应的一项;三种分别选 `main` 包、`main.go` 和包目录后运行, + 程序输出显示在 Run 窗口里,程序参数传给程序;Go 工具实参填 `-race` 时命令里带上它。 +3. 新建 Go 测试:默认 `./...` 运行全部测试;改成某个包并填模式后只跑匹配的测试;切到目录后测试这个目录下的所有包; + 测试失败时 Run 窗口显示失败和非零退出码。 +4. 勾选存为项目文件后,`.lithe/run/plugin/io.github.lithe-idea.go-support.toml` 里有这条配置;关闭并重新打开项目后配置还在, + 文件和目录字段仍指向原来的路径。 +5. 勾选允许多个实例后连续运行两次,Run 窗口有两个标签;不勾选时第二次运行先停掉第一次。 +6. 在构建选项里设 Build Tags 后运行两种配置,命令带上 `-tags`。 +7. 整个过程中插件安装目录不被写入。 + ## go-project-generator **能力**:「新建项目」对话框里的 Go。GOROOT 下拉列出 `go` 工具定义在电脑上找到的 Go,默认是版本最新的一个, diff --git a/docs/releases/v2.0.0.md b/docs/releases/v2.0.0.md new file mode 100644 index 0000000..34de5db --- /dev/null +++ b/docs/releases/v2.0.0.md @@ -0,0 +1,23 @@ +2.0.0 在「运行/调试配置」里加入 Go 构建和 Go 测试两种运行配置,并把 gopls 的索引排除改为由清单声明。这个版本需要还没有发布的新版桌面端,所以是预发布版本。 + +功能: +- Go 构建:点「运行/调试配置」的「+」,选 Go 构建。运行种类可选软件包、文件或目录,执行 `go run`;软件包填导入路径(如 `example.com/app/cmd/server`)或相对路径(如 `./cmd/server`)。“Go 工具实参”放在 `go run` 后面,例如 `-mod=vendor`;程序参数传给程序。 +- Go 测试:测试种类可选软件包或目录,执行 `go test`。软件包默认 `./...`;目录会测试它和子目录里的所有软件包;“模式”作为 `-run` 传入,只运行匹配的测试。 +- 两种配置都可以存为项目文件(写进 `.lithe/run/plugin/io.github.lithe-idea.go-support.toml`)、允许多个实例,并使用 Lithe 统一的程序参数、工作目录、环境变量和启动前步骤。用的 `go` 和构建窗口相同,构建选项里的 Build Tags 和 Race Detector 也会加进命令。名称、说明和字段有中文翻译。 +- 用户排除在索引之外的目录由 Lithe 按清单里的 `indexingExclusions` 写进 gopls 的 `directoryFilters`,不再写死在 Lithe 里。 + +已知限制: +- 这两种配置还不能调试,调试请用「调试」窗口的 Go (Delve)。 +- 插件字段排在工作目录和环境变量前面,运行种类和测试种类显示为分段按钮;Go 测试的目录不会默认填项目根目录,名称不会自动生成。 +- 还没有输出目录、构建后运行、模拟终端、重定向输入、以 sudo 运行、模块、测试框架和使用所有自定义构建标记这些选项。 + +验证: +- 在 macOS 上用包含 1lck/Lithe-IDEA#2148 的桌面端跑宿主端到端测试:在对话框里新建 Go 构建和 Go 测试,运行结果和保存的配置都符合预期,插件安装目录不被写入。 +- `scripts/test.mjs` 用真实的 go 运行两种配置拼出的命令,覆盖软件包、文件、目录的 Go 构建和软件包、目录、模式过滤的 Go 测试;CI 在 macOS、Windows x64 和 Windows ARM64 上运行它。 +- 还没有在 Lithe 里手工验收,状态见 `docs/feature-matrix.md`。 + +权限:不变。 + +兼容性: +- 需要支持插件声明运行配置类型的桌面端(1lck/Lithe-IDEA#2148,固定了 Lithe-core-rust#94 的 Core)。已发布的桌面端 0.8.5 及更早版本都不包含它,会拒绝安装 2.0.0;请继续使用 1.0.1。 +- `indexingExclusions` 需要 Lithe-core-rust#68,桌面端 0.8.5 已包含。 diff --git a/e2e/host/desktop/internal/workbench/official_go_plugin_e2e_test.go b/e2e/host/desktop/internal/workbench/official_go_plugin_e2e_test.go index ae7fa13..2e23618 100644 --- a/e2e/host/desktop/internal/workbench/official_go_plugin_e2e_test.go +++ b/e2e/host/desktop/internal/workbench/official_go_plugin_e2e_test.go @@ -151,32 +151,36 @@ func awaitBuildModel(t *testing.T, h *harness, p *project, buildSystemID string, return instance } -// runConsoleText is the console of the Run window's shown session, which holds the -// output of the latest execution. -func runConsoleText(p *project) string { - if run := p.activeRun(); run != nil { - return run.output.all() +// awaitExecutionToEnd waits for op to end and returns the console of its Run window session, +// failing unless it succeeded. Runs execute in parallel, so the execution's own session holds +// its output and result, not the window's shown session or its last status line. +func awaitExecutionToEnd(t *testing.T, h *harness, p *project, op *buildOperation, name string, limit time.Duration) string { + t.Helper() + awaitStateWithin(t, h, limit, "the "+name+" execution", func() bool { return !p.build.owns(op) }) + session := op.session + if session == nil { + t.Fatalf("%s has no Run window session", name) + } + output := session.output.all() + if session.outcome != runSucceeded { + t.Fatalf("%s did not succeed: outcome=%v summary=%q output=%q", name, session.outcome, session.summary, output) } - return "" + return output } // runBuildTaskToEnd runs one task of the selected instance and waits for it to end. func runBuildTaskToEnd(t *testing.T, h *harness, p *project, projectID, taskID string, limit time.Duration) string { t.Helper() p.build.projectID, p.build.targetID, p.build.taskID = projectID, "", taskID - if _, err := h.app.runBuildTask(p, projectID, "", taskID, nil); err != nil { + op, err := h.app.runBuildTask(p, projectID, "", taskID, nil) + if err != nil { t.Fatalf("start %s: %v", taskID, err) } - awaitStateWithin(t, h, limit, "the "+taskID+" task", func() bool { return p.build.operation == nil }) - output := runConsoleText(p) - if p.build.status != h.app.text.Build.Succeeded { - t.Fatalf("%s did not succeed: status=%q output=%q", taskID, p.build.status, output) - } - return output + return awaitExecutionToEnd(t, h, p, op, taskID, limit) } // runTargetEntryToEnd runs the target's only command Run entry, as its Run button in the -// Build window does, waits for the program to exit and returns the window output. +// Build window does, waits for the program to exit and returns its console. func runTargetEntryToEnd(t *testing.T, h *harness, p *project, instance *buildInstance, targetID string, limit time.Duration) string { t.Helper() entries := buildTargetRunEntries(instance, targetID) @@ -185,15 +189,11 @@ func runTargetEntryToEnd(t *testing.T, h *harness, p *project, instance *buildIn } entry := entries[0] h.app.runBuildEntry(p, entry.ID) - if p.build.operation == nil { + op := p.build.runOf("entry:" + instance.ID + "/" + entry.ID) + if op == nil { t.Fatalf("run %s did not start", entry.Name) } - awaitStateWithin(t, h, limit, "the "+entry.Name+" run", func() bool { return p.build.operation == nil }) - output := runConsoleText(p) - if p.build.status != h.app.text.Build.Succeeded { - t.Fatalf("run %s did not succeed: status=%q output=%q", entry.Name, p.build.status, output) - } - return output + return awaitExecutionToEnd(t, h, p, op, entry.Name, limit) } func modelProjectNamed(model core.BuildModel, name string) *core.BuildProject { diff --git a/e2e/host/desktop/internal/workbench/official_go_plugin_run_types_e2e_test.go b/e2e/host/desktop/internal/workbench/official_go_plugin_run_types_e2e_test.go new file mode 100644 index 0000000..9cb8463 --- /dev/null +++ b/e2e/host/desktop/internal/workbench/official_go_plugin_run_types_e2e_test.go @@ -0,0 +1,229 @@ +package workbench + +import ( + "encoding/json" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/1lck/Lithe-IDEA/desktop/internal/core" +) + +// This opt-in integration test covers the run configuration types the Go Support archive +// declares, through the real installer and the Run/Debug Configurations dialog. It runs the +// user's own go, so it skips unless an archive is named. + +const officialGoPluginID = "io.github.lithe-idea.go-support" + +// officialGoRunTypeDraft opens the dialog, adds a configuration of kind as its + menu does and +// waits for the form. +func officialGoRunTypeDraft(t *testing.T, h *harness, p *project, kind core.RunTypeKey) *runConfigForm { + t.Helper() + d := &p.runConfigs.dialog + h.app.post(func(a *App) { + a.openRunConfigDialog(p, "") + a.addRunConfigDraft(p, declaredKind(kind), nil) + }) + var f *runConfigForm + awaitStateWithin(t, h, 30*time.Second, "the form of "+kind.Type, func() bool { + if draft := d.drafts[d.selected]; d.open && draft != nil && draft.form != nil { + f = draft.form + } + return f != nil && f.ready() && f.fields != nil + }) + if f.failure != "" { + t.Fatalf("form failure %q", f.failure) + } + return f +} + +// officialGoEditForm sets texts and flags on form f, as typing into its fields does, and waits +// until the form follows the edit. +func officialGoEditForm(t *testing.T, h *harness, p *project, f *runConfigForm, texts map[string]string, flags map[string]bool) { + t.Helper() + edited := false + h.app.post(func(a *App) { + for id, value := range texts { + *f.texts[id] = value + } + for id, value := range flags { + *f.flags[id] = value + } + a.runConfigFormEdited(p, f) + edited = true + }) + awaitStateWithin(t, h, 30*time.Second, "the edited form", func() bool { return edited && !f.refreshing }) +} + +// officialGoFieldShown reports whether form f shows the field id for the values being edited. +func officialGoFieldShown(f *runConfigForm, id string) bool { + for _, field := range f.fields { + if field.ID == id { + return visibleRunConfigField(field, f.collect()) + } + } + return false +} + +// officialGoValues prints the values being edited, for a failure. +func officialGoValues(f *runConfigForm) string { + text, _ := json.Marshal(f.collect()) + return string(text) +} + +// officialGoConditions prints when each field shows, for a failure. +func officialGoConditions(f *runConfigForm) string { + conditions := map[string]*core.RunFieldCondition{} + for _, field := range f.fields { + if field.VisibleWhen != nil { + conditions[field.ID] = field.VisibleWhen + } + } + text, _ := json.Marshal(conditions) + return string(text) +} + +// officialGoFieldLabels maps the form's field IDs to their labels. +func officialGoFieldLabels(f *runConfigForm) map[string]core.RunEditorField { + fields := map[string]core.RunEditorField{} + for _, field := range f.fields { + fields[field.ID] = field + } + return fields +} + +// officialGoAwaitRun waits for the run of the configuration named label to end and returns its +// console, failing unless the program exited with 0. +func officialGoAwaitRun(t *testing.T, h *harness, p *project, label string) string { + t.Helper() + var found *runEntry + defer func() { + if found == nil { + for _, entry := range p.build.runs { + t.Logf("session %q running %v outcome %v summary %q console:\n%s", entry.label, entry.running, entry.outcome, entry.summary, entry.output.all()) + } + t.Logf("configurations %+v failure %q notice %q", p.runConfigs.list, p.runConfigs.failure, lastNotice(h)) + } + }() + awaitStateWithin(t, h, 2*time.Minute, "the run of "+label, func() bool { + for _, entry := range p.build.runs { + if entry.label == label && !entry.running && !p.build.active() { + found = entry + return true + } + } + return false + }) + if found.outcome != runSucceeded || found.summary != h.app.text.Run.Exited(0) { + t.Fatalf("run %s ended %v: %s; console:\n%s", label, found.outcome, found.summary, found.output.all()) + } + return found.output.all() +} + +// TestOfficialGoPluginRunsGoBuildAndGoTestConfigurations covers the Go Build and Go Test kinds: +// both list under the package's Chinese names, a Go Build of a package with program arguments +// and a Go Test of a directory with a pattern are created in the dialog, saved to the plugin's +// run document, and run the user's go with the arguments the kinds make of the fields. +func TestOfficialGoPluginRunsGoBuildAndGoTestConfigurations(t *testing.T) { + archive := os.Getenv("LITHE_TEST_GO_PLUGIN_ARCHIVE") + if archive == "" { + t.Skip("set LITHE_TEST_GO_PLUGIN_ARCHIVE to a go-support--darwin.zip") + } + if _, err := exec.LookPath("go"); err != nil { + t.Fatalf("go is not on PATH: %v", err) + } + h := newHarness(t, &testKernel{Client: core.NewClient()}) + installed := installOfficialPluginArchive(t, h, archive, officialGoPluginID) + before := packageDigest(t, installed) + h.app.reloadPluginLanguages() + root := t.TempDir() + writePluginFixture(t, root, map[string]string{ + "go.mod": "module example.com/hello\n\ngo 1.21\n", + "cmd/echo/main.go": "package main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"strings\"\n)\n\nfunc main() { fmt.Println(\"echo:\", strings.Join(os.Args[1:], \"|\")) }\n", + "greet/greet.go": "package greet\n\nfunc Hello(name string) string { return \"Hello, \" + name + \"!\" }\n", + "greet/greet_test.go": "package greet\n\nimport \"testing\"\n\n" + + "func TestGreet(t *testing.T) { if Hello(\"Go\") != \"Hello, Go!\" { t.Fatal(\"wrong greeting\") } }\n\n" + + "func TestOther(t *testing.T) { t.Fatal(\"the pattern must leave this test out\") }\n", + }) + h.open(root) + p := h.app.project + if p == nil { + t.Fatal("the module did not open") + } + h.app.loadBuildSystems(p) + awaitBuildModel(t, h, p, "go", time.Minute) + build := core.RunTypeKey{PluginID: officialGoPluginID, BuildSystemID: "go", Type: "build"} + test := core.RunTypeKey{PluginID: officialGoPluginID, BuildSystemID: "go", Type: "test"} + s := &p.runConfigs + h.app.post(func(a *App) { a.loadTitleRunConfigs(p) }) + awaitStateWithin(t, h, time.Minute, "the Go kinds", func() bool { + return p.runConfigsSettled() && !s.typesLoading && s.types != nil && p.hasRunTypeInstance(build) + }) + for key, want := range map[core.RunTypeKey]string{build: "Go 构建", test: "Go 测试"} { + if kind := s.runTypeKind(key); kind == nil || kind.DisplayName != want { + t.Fatalf("kind %s = %+v, want %q among %+v", key.Type, kind, want, s.types) + } + } + + // Go Build: the run kind starts on a package; its path and the program arguments run + // `go run ./cmd/echo one two`. + f := officialGoRunTypeDraft(t, h, p, build) + fields := officialGoFieldLabels(f) + if kind := fields["field:runKind"]; kind.Label != "运行种类" || kind.OptionLabels["package"] != "软件包" || kind.OptionLabels["file"] != "文件" { + t.Fatalf("run kind field = %+v", kind) + } + if pkg := fields["field:package"]; pkg.Label != "软件包路径" || !pkg.Required { + t.Fatalf("package field = %+v", pkg) + } + if !officialGoFieldShown(f, "field:package") || officialGoFieldShown(f, "field:file") || officialGoFieldShown(f, "field:directory") { + t.Fatalf("a package run shows other fields than the package path: values %s, conditions %s", officialGoValues(f), officialGoConditions(f)) + } + officialGoEditForm(t, h, p, f, map[string]string{"name": "Echo", "field:package": "./cmd/echo", "programArguments": "one two"}, + map[string]bool{"storeAsProjectFile": true}) + applyDialog(t, h, p, runConfigDialogRun) + if s.dialog.open { + t.Fatalf("the dialog stayed open: errors %v failure %q / %q", f.errors, f.failure, s.dialog.failure) + } + if output := officialGoAwaitRun(t, h, p, "Echo"); !strings.Contains(output, "echo: one|two") { + t.Fatalf("go run did not pass the program arguments: %q", output) + } + + // Go Test of a directory: the directory field replaces the package path, and the pattern + // leaves the failing TestOther out of `go test -v -run ^TestGreet$ /...`. + f = officialGoRunTypeDraft(t, h, p, test) + officialGoEditForm(t, h, p, f, map[string]string{"name": "Greet tests", "field:testKind": "directory"}, nil) + fields = officialGoFieldLabels(f) + if directory := fields["field:directory"]; directory.Label != "目录" || directory.Kind != runFieldDirectory { + t.Fatalf("directory field = %+v", directory) + } + if !officialGoFieldShown(f, "field:directory") || officialGoFieldShown(f, "field:package") { + t.Fatalf("a directory test shows the package path instead of the directory: values %s, conditions %s", officialGoValues(f), officialGoConditions(f)) + } + if pattern := fields["field:pattern"]; pattern.Label != "模式" { + t.Fatalf("pattern field = %+v", pattern) + } + officialGoEditForm(t, h, p, f, map[string]string{ + "field:directory": root, "field:pattern": "^TestGreet$", "field:goToolArguments": "-v -count=1", + }, map[string]bool{"storeAsProjectFile": true}) + applyDialog(t, h, p, runConfigDialogRun) + if s.dialog.open { + t.Fatalf("the dialog stayed open: errors %v failure %q / %q", f.errors, f.failure, s.dialog.failure) + } + output := officialGoAwaitRun(t, h, p, "Greet tests") + if !strings.Contains(output, "--- PASS: TestGreet") || strings.Contains(output, "TestOther") { + t.Fatalf("go test did not run only TestGreet: %q", output) + } + + // Both are project configurations of the plugin's kinds, kept in its run document. + data, err := os.ReadFile(filepath.Join(root, ".lithe", "run", "plugin", officialGoPluginID+".toml")) + if err != nil || !strings.Contains(string(data), "Echo") || !strings.Contains(string(data), "Greet tests") { + t.Fatalf("plugin run document = %s, %v", data, err) + } + h.app.closeProject() + awaitStateWithin(t, h, 30*time.Second, "the closed project", func() bool { return h.app.project == nil }) + h.settle() + assertPackageUnchanged(t, installed, before) +} diff --git a/e2e/host/lithe-host.json b/e2e/host/lithe-host.json index d1463e1..ad112b7 100644 --- a/e2e/host/lithe-host.json +++ b/e2e/host/lithe-host.json @@ -1,12 +1,12 @@ { "repository": "https://github.com/1lck/Lithe-IDEA.git", - "revision": "a9bddd922e28b4f4dcda4e92b581cbbe7b0f4eb7", + "revision": "a8797ae9aa40d10cbaa6460b48b213c87fe8ad48", "packages": { "./internal/projectgen": "^TestGoPlugin", - "./internal/workbench": "^TestOfficialGoPlugin(BuildsAndServesAModule|DebugsEachProjectOfAWorkspaceWithDelve)$" + "./internal/workbench": "^TestOfficialGoPlugin(BuildsAndServesAModule|DebugsEachProjectOfAWorkspaceWithDelve|RunsGoBuildAndGoTestConfigurations)$" }, "requiredEnvironment": { - "LITHE_TEST_GO_PLUGIN_ARCHIVE": "go-support--darwin.zip to install, for example dist/go-support-1.0.0-darwin.zip" + "LITHE_TEST_GO_PLUGIN_ARCHIVE": "go-support--darwin.zip to install, for example dist/go-support-2.0.0-darwin.zip" }, "repositoryEnvironment": { "LITHE_PLUGIN_GO_DIR": "io.github.lithe-idea.go-support" diff --git a/io.github.lithe-idea.go-support/l10n/zh-Hans.json b/io.github.lithe-idea.go-support/l10n/zh-Hans.json index 041ab1e..755f520 100644 --- a/io.github.lithe-idea.go-support/l10n/zh-Hans.json +++ b/io.github.lithe-idea.go-support/l10n/zh-Hans.json @@ -31,5 +31,70 @@ }, "sections": { "Go toolchain": "Go 工具链" + }, + "runConfigurationTypes": { + "go": { + "build": { + "displayName": "Go 构建", + "description": "用 go run 构建并运行一个 Go 软件包、文件或目录。", + "fields": { + "runKind": { + "title": "运行种类", + "options": { + "package": "软件包", + "file": "文件", + "directory": "目录" + } + }, + "package": { + "title": "软件包路径", + "description": "main 软件包的导入路径,或相对工作目录的路径,例如 ./cmd/server。", + "placeholder": "example.com/app/cmd/server" + }, + "file": { + "title": "文件", + "description": "main 软件包里的一个 .go 文件;go run 只构建这个文件。" + }, + "directory": { + "title": "目录", + "description": "main 软件包所在的目录。" + }, + "goToolArguments": { + "title": "Go 工具实参", + "description": "放在软件包前传给 go run,例如 -mod=vendor。" + } + } + }, + "test": { + "displayName": "Go 测试", + "description": "对一个软件包,或一个目录下的所有软件包运行 go test。", + "fields": { + "testKind": { + "title": "测试种类", + "options": { + "directory": "目录", + "package": "软件包" + } + }, + "package": { + "title": "软件包路径", + "description": "导入路径,或相对工作目录的软件包模式;./... 测试它下面的所有软件包。" + }, + "directory": { + "title": "目录", + "description": "测试这个目录及其子目录里的所有软件包。" + }, + "pattern": { + "title": "模式", + "description": "要运行的测试的正则表达式,作为 -run 传入;留空运行所有测试。", + "placeholder": "^TestName$" + }, + "goToolArguments": { + "title": "Go 工具实参", + "description": "放在软件包前传给 go test,例如 -v 或 -count=1。" + } + } + } + } } } diff --git a/io.github.lithe-idea.go-support/plugin.json b/io.github.lithe-idea.go-support/plugin.json index db53d73..db6640a 100644 --- a/io.github.lithe-idea.go-support/plugin.json +++ b/io.github.lithe-idea.go-support/plugin.json @@ -2,7 +2,7 @@ "schemaVersion": 0, "id": "io.github.lithe-idea.go-support", "displayName": "Go Support", - "version": "1.0.1", + "version": "2.0.0", "pluginAPIVersion": 0, "requires": { "buildSystems": 0, @@ -11,6 +11,7 @@ "languageServers": 0, "languages": 0, "projectGenerators": 0, + "runConfigurationTypes": 0, "settings": 0, "toolDefinitions": 0 }, @@ -337,6 +338,168 @@ ] } ], + "runConfigurationTypes": [ + { + "id": "build", + "displayName": "Go Build", + "description": "Builds and runs a Go package, file or directory with go run.", + "icon": { + "path": "icons/go.svg", + "mode": "original" + }, + "fields": [ + { + "id": "runKind", + "title": "Run kind", + "kind": "choice", + "default": "package", + "choices": [ + { + "value": "package", + "title": "Package" + }, + { + "value": "file", + "title": "File" + }, + { + "value": "directory", + "title": "Directory" + } + ] + }, + { + "id": "package", + "title": "Package path", + "description": "The import path of a main package, or a path relative to the working directory such as ./cmd/server.", + "kind": "text", + "placeholder": "example.com/app/cmd/server", + "required": true, + "visibleWhen": { + "field": "runKind", + "equals": "package" + } + }, + { + "id": "file", + "title": "File", + "description": "A .go file of a main package; go run builds only this file.", + "kind": "file", + "required": true, + "visibleWhen": { + "field": "runKind", + "equals": "file" + } + }, + { + "id": "directory", + "title": "Directory", + "description": "The directory of a main package.", + "kind": "directory", + "required": true, + "visibleWhen": { + "field": "runKind", + "equals": "directory" + } + }, + { + "id": "goToolArguments", + "title": "Go tool arguments", + "description": "Passed to go run before the package, such as -mod=vendor.", + "kind": "arguments" + } + ], + "arguments": [ + "run", + "${optionArguments}", + "${field:goToolArguments}", + "${field:package}", + "${field:file}", + "${field:directory}", + "${userArguments}" + ] + }, + { + "id": "test", + "displayName": "Go Test", + "description": "Runs go test on a package or on every package below a directory.", + "icon": { + "path": "icons/go.svg", + "mode": "original" + }, + "execution": "task", + "fields": [ + { + "id": "testKind", + "title": "Test kind", + "kind": "choice", + "default": "package", + "choices": [ + { + "value": "directory", + "title": "Directory" + }, + { + "value": "package", + "title": "Package" + } + ] + }, + { + "id": "package", + "title": "Package path", + "description": "An import path or a package pattern relative to the working directory; ./... tests every package below it.", + "kind": "text", + "default": "./...", + "required": true, + "visibleWhen": { + "field": "testKind", + "equals": "package" + } + }, + { + "id": "directory", + "title": "Directory", + "description": "Tests every package in this directory and below it.", + "kind": "directory", + "required": true, + "visibleWhen": { + "field": "testKind", + "equals": "directory" + }, + "arguments": [ + "${value}/..." + ] + }, + { + "id": "pattern", + "title": "Pattern", + "description": "A regular expression of the tests to run, passed as -run; empty runs every test.", + "kind": "text", + "placeholder": "^TestName$", + "arguments": [ + "-run", + "${value}" + ] + }, + { + "id": "goToolArguments", + "title": "Go tool arguments", + "description": "Passed to go test before the package, such as -v or -count=1.", + "kind": "arguments" + } + ], + "arguments": [ + "test", + "${optionArguments}", + "${field:goToolArguments}", + "${field:pattern}", + "${field:package}", + "${field:directory}", + "${userArguments}" + ] + } + ], "test": { "scopes": [ "project", diff --git a/scripts/test.mjs b/scripts/test.mjs index 6a9fba9..cd72810 100644 --- a/scripts/test.mjs +++ b/scripts/test.mjs @@ -2,7 +2,8 @@ // Runs the commands plugin.json declares with the real go, the way Lithe runs // them, on a module rendered from the project templates under a path with a // space: the version probe, the model and dependency commands (decoded as JSON -// streams), the build, vet and filtered test tasks, the directories the +// streams), the build, vet and filtered test tasks, the Go Build and Go Test +// run configuration types with their fields filled in, the directories the // command installers read from `go env`, and the managed installs of gopls and // dlv into a GOBIN of their own. The installed programs are then found the // way Lithe finds a system command (on PATH, so gopls.exe on Windows), gopls @@ -51,6 +52,35 @@ function expand(list, { target, testCase } = {}) { }); } +/** + * Expands a declared run configuration type the way Core plans a saved + * configuration of it: a field passes nothing while hidden or empty, a field + * with arguments passes them with `${value}` replaced (a checkbox when + * checked), a field without them passes its value, which is an absolute path + * for a file or directory and a list for an arguments field. No option has a + * value. + */ +function expandRunType(type, values, userArguments = []) { + const valueOf = (field) => values[field.id] ?? field.default; + const shown = (field) => !field.visibleWhen + || valueOf(type.fields.find((entry) => entry.id === field.visibleWhen.field)) === field.visibleWhen.equals; + const passed = (field) => { + const value = valueOf(field); + if (!shown(field) || value === undefined || value === "" || value === false) return []; + if (field.kind === "arguments") return value; + if (!field.arguments) return [value]; + return field.arguments.map((argument) => argument.replaceAll("${value}", value)); + }; + return type.arguments.flatMap((argument) => { + if (argument === "${optionArguments}") return []; + if (argument === "${userArguments}") return userArguments; + const field = argument.match(/^\$\{field:([A-Za-z0-9]+)\}$/); + if (field) return passed(type.fields.find((entry) => entry.id === field[1])); + assert.doesNotMatch(argument, /\$\{/, `the test does not expand ${argument}`); + return [argument]; + }); +} + function run(command, args, { cwd, env = process.env } = {}) { const result = spawnSync(command, args, { cwd, env, encoding: "utf8", timeout: COMMAND_TIMEOUT_MS, maxBuffer: 64 * 1024 * 1024, windowsHide: true, @@ -144,6 +174,45 @@ try { assert.match(output, /^ok\s+lithe-demo\/greet/m); }); + const runType = (id) => buildSystem.runConfigurationTypes.find((entry) => entry.id === id); + // A second program that prints its arguments, for the program arguments. + mkdirSync(join(root, "cmd", "echo"), { recursive: true }); + writeFileSync(join(root, "cmd", "echo", "main.go"), [ + "package main", "", 'import (\n\t"fmt"\n\t"os"\n\t"strings"\n)', "", + 'func main() { fmt.Println("echo:", strings.Join(os.Args[1:], "|")) }', "", + ].join("\n")); + + await check("Go Build runs a package by import path or relative path, with the program arguments", () => { + const build = runType("build"); + assert.match(run(go, expandRunType(build, { package: "lithe-demo" }), { cwd: root }), /^Hello, Gopher!$/m); + const output = run(go, expandRunType(build, { package: "./cmd/echo", goToolArguments: ["-mod=mod"] }, ["one two", "three"]), { cwd: root }); + assert.match(output, /^echo: one two\|three$/m); + }); + + await check("Go Build runs a file and a directory given as absolute paths", () => { + const build = runType("build"); + const file = expandRunType(build, { runKind: "file", package: "./cmd/echo", file: join(root, "main.go") }); + assert.deepEqual(file, ["run", join(root, "main.go")], "the hidden package field passes nothing"); + assert.match(run(go, file, { cwd: root }), /^Hello, Gopher!$/m); + const directory = expandRunType(build, { runKind: "directory", directory: join(root, "cmd", "echo") }, ["x"]); + assert.match(run(go, directory, { cwd: root }), /^echo: x$/m); + }); + + await check("Go Test runs the selected tests of a package and of every package below a directory", () => { + const test = runType("test"); + const pattern = "^TestGreet$"; + const output = run(go, expandRunType(test, { package: "./greet", pattern, goToolArguments: ["-v", "-count=1"] }), { cwd: root }); + assert.match(output, /^--- PASS: TestGreet/m); + assert.doesNotMatch(output, /TestOther/, "the pattern leaves TestOther out"); + const byDirectory = expandRunType(test, { testKind: "directory", directory: root, pattern }); + assert.equal(byDirectory.at(-1), `${root}/...`); + assert.match(run(go, byDirectory, { cwd: root }), /^ok\s+lithe-demo\/greet/m); + }); + + await check("Go Test with the default package and no pattern runs every test", () => { + assert.throws(() => run(go, expandRunType(runType("test"), { goToolArguments: ["-count=1"] }), { cwd: root }), /--- FAIL: TestOther/); + }); + await check("the installers' default directories come from go env", () => { for (const installer of manifest.commandInstallers) { const [gobin, gopath] = installer.targets.toolDefault.directory;