Fix Go test task when using Git submodules (#17108)

I have found an error running tests in Golang projects that use
submodules. This PR fixes the issue by accessing the directory before
running the test.

![image](https://github.com/user-attachments/assets/3790a77c-a07c-4467-be69-ee8e22675f7a)
The `commons` in the image is a git submodule in a subfolder inside a
parent folder where the workspace is set.


Release Notes:

- Fixed Go tests not being able to run in case the package (and the
`go.mod`) was in a nested folder. Pre-defined Go tasks have been changed
to now run in the package's directory. That means `go test ./package
-run MyTest` will run in `./package` and execute `go test -run MyTest`.
Also, `go test ./...` will run in the package directory, not at the root
of the Zed project, which is a small breaking change. In case one wants
to run `go test ./...` from the root, one can spawn a manual task that
does this.


![image](https://github.com/user-attachments/assets/b7c66f6a-ca29-421e-9539-737c8f719ae2)

---------

Co-authored-by: Thorsten Ball <mrnugget@gmail.com>
This commit is contained in:
Glaudiston Gomes da Silva 2024-08-30 11:11:27 -03:00 committed by GitHub
parent 89632ff5c2
commit 00eed768ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -505,6 +505,12 @@ impl ContextProvider for GoContextProvider {
_: Option<Arc<dyn language::File>>,
_: &AppContext,
) -> Option<TaskTemplates> {
let package_cwd = if GO_PACKAGE_TASK_VARIABLE.template_value() == "." {
None
} else {
Some("$ZED_DIRNAME".to_string())
};
Some(TaskTemplates(vec![
TaskTemplate {
label: format!(
@ -512,26 +518,23 @@ impl ContextProvider for GoContextProvider {
GO_PACKAGE_TASK_VARIABLE.template_value(),
VariableName::Symbol.template_value(),
),
command: "go".into(),
args: vec![
"test".into(),
GO_PACKAGE_TASK_VARIABLE.template_value(),
"-run".into(),
format!("^{}\\$", VariableName::Symbol.template_value(),),
],
command: format!("go test -run {}", VariableName::Symbol.template_value(),),
tags: vec!["go-test".to_owned()],
cwd: package_cwd.clone(),
..TaskTemplate::default()
},
TaskTemplate {
label: format!("go test {}", GO_PACKAGE_TASK_VARIABLE.template_value()),
command: "go".into(),
args: vec!["test".into(), GO_PACKAGE_TASK_VARIABLE.template_value()],
cwd: package_cwd.clone(),
..TaskTemplate::default()
},
TaskTemplate {
label: "go test ./...".into(),
command: "go".into(),
args: vec!["test".into(), "./...".into()],
cwd: package_cwd.clone(),
..TaskTemplate::default()
},
TaskTemplate {
@ -544,7 +547,6 @@ impl ContextProvider for GoContextProvider {
command: "go".into(),
args: vec![
"test".into(),
GO_PACKAGE_TASK_VARIABLE.template_value(),
"-v".into(),
"-run".into(),
format!(
@ -553,6 +555,7 @@ impl ContextProvider for GoContextProvider {
GO_SUBTEST_NAME_TASK_VARIABLE.template_value(),
),
],
cwd: package_cwd.clone(),
tags: vec!["go-subtest".to_owned()],
..TaskTemplate::default()
},
@ -571,13 +574,15 @@ impl ContextProvider for GoContextProvider {
"-bench".into(),
format!("^{}\\$", VariableName::Symbol.template_value()),
],
cwd: package_cwd.clone(),
tags: vec!["go-benchmark".to_owned()],
..TaskTemplate::default()
},
TaskTemplate {
label: format!("go run {}", GO_PACKAGE_TASK_VARIABLE.template_value(),),
command: "go".into(),
args: vec!["run".into(), GO_PACKAGE_TASK_VARIABLE.template_value()],
args: vec!["run".into(), ".".into()],
cwd: package_cwd.clone(),
tags: vec!["go-main".to_owned()],
..TaskTemplate::default()
},