Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

languages: Add package-level test gutter for Go (#56699)

Xiaobo Liu53421b7f3f94
diff --git a/crates/grammars/src/go/runnables.scm b/crates/grammars/src/go/runnables.scm
index 67a85e473b3..2eecbf7aa4a 100644
--- a/crates/grammars/src/go/runnables.scm
+++ b/crates/grammars/src/go/runnables.scm
@@ -1,3 +1,8 @@
+; Package-level test runner
+((package_clause
+ (package_identifier) @run) @_
+ (#set! tag go-package-test))
+
; Functions names start with `Test`
(((function_declaration
name: (_) @run
diff --git a/crates/languages/src/go.rs b/crates/languages/src/go.rs
index d2e68be4f12..3357966da35 100644
--- a/crates/languages/src/go.rs
+++ b/crates/languages/src/go.rs
@@ -822,6 +822,14 @@ impl ContextProvider for GoContextProvider {
cwd: package_cwd.clone(),
..TaskTemplate::default()
},
+ TaskTemplate {
+ label: format!("go test {}", GO_PACKAGE_TASK_VARIABLE.template_value()),
+ command: "go".into(),
+ args: vec!["test".into()],
+ cwd: package_cwd.clone(),
+ tags: vec!["go-package-test".to_owned()],
+ ..TaskTemplate::default()
+ },
TaskTemplate {
label: "go test ./...".into(),
command: "go".into(),
@@ -2137,4 +2145,45 @@ mod tests {
let result = extract_subtest_name(input_with_double_quotes);
assert_eq!(result, Some(r#"test_with_\"double_quotes\""#.to_string()));
}
+
+ #[gpui::test]
+ fn test_go_package_test_detection(cx: &mut TestAppContext) {
+ let language = language("go", tree_sitter_go::LANGUAGE.into());
+
+ let source = r#"
+ package main
+
+ import "testing"
+
+ func TestExample(t *testing.T) {
+ // test code
+ }
+ "#;
+
+ let buffer =
+ cx.new(|cx| crate::Buffer::local(source, 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..source.len()).collect()
+ });
+
+ let tag_strings: Vec<_> = runnables
+ .iter()
+ .flat_map(|r| &r.runnable.tags)
+ .map(|tag| tag.0.to_string())
+ .collect();
+
+ assert!(
+ tag_strings.contains(&"go-package-test".to_string()),
+ "Should find go-package-test tag, found: {:?}",
+ tag_strings
+ );
+ assert!(
+ tag_strings.contains(&"go-test".to_string()),
+ "Should find go-test tag, found: {:?}",
+ tag_strings
+ );
+ }
}