go: Add ability to run testable examples (#39390)

See related discussion #39381.

<img width="724" height="488"
src="https://github.com/user-attachments/assets/4a69e13e-783f-45d7-99f4-e23c0415a781"
/>

Release Notes:

- Added ability to run Go Testable Examples
This commit is contained in:
Lev Zakharov
2025-10-08 22:55:26 +02:00
committed by GitHub
parent ca89a40df2
commit 81b98cdd4d
2 changed files with 62 additions and 0 deletions
+53
View File
@@ -635,6 +635,22 @@ impl ContextProvider for GoContextProvider {
cwd: package_cwd.clone(),
..TaskTemplate::default()
},
TaskTemplate {
label: format!(
"go test {} -run {}",
GO_PACKAGE_TASK_VARIABLE.template_value(),
VariableName::Symbol.template_value(),
),
command: "go".into(),
args: vec![
"test".into(),
"-run".into(),
format!("\\^{}\\$", VariableName::Symbol.template_value(),),
],
tags: vec!["go-example".to_owned()],
cwd: package_cwd.clone(),
..TaskTemplate::default()
},
TaskTemplate {
label: format!("go test {}", GO_PACKAGE_TASK_VARIABLE.template_value()),
command: "go".into(),
@@ -992,6 +1008,43 @@ mod tests {
);
}
#[gpui::test]
fn test_go_example_test_detection(cx: &mut TestAppContext) {
let language = language("go", tree_sitter_go::LANGUAGE.into());
let example_test = r#"
package main
import "fmt"
func Example() {
fmt.Println("Hello, world!")
// Output: Hello, world!
}
"#;
let buffer =
cx.new(|cx| crate::Buffer::local(example_test, cx).with_language(language.clone(), cx));
cx.executor().run_until_parked();
let runnables: Vec<_> = buffer.update(cx, |buffer, _| {
let snapshot = buffer.snapshot();
snapshot.runnable_ranges(0..example_test.len()).collect()
});
let tag_strings: Vec<String> = runnables
.iter()
.flat_map(|r| &r.runnable.tags)
.map(|tag| tag.0.to_string())
.collect();
assert!(
tag_strings.contains(&"go-example".to_string()),
"Should find go-example tag, found: {:?}",
tag_strings
);
}
#[gpui::test]
fn test_go_table_test_slice_detection(cx: &mut TestAppContext) {
let language = language("go", tree_sitter_go::LANGUAGE.into());
+9
View File
@@ -71,6 +71,15 @@
(#set! tag go-subtest)
)
; Functions names start with `Example`
(
(
(function_declaration name: (_) @run @_name
(#match? @_name "^Example.*"))
) @_
(#set! tag go-example)
)
; Functions names start with `Benchmark`
(
(