Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix go test not found problem #6634

Merged

Conversation

ufo22940268
Copy link
Contributor

@ufo22940268 ufo22940268 commented Aug 10, 2024

Checklist

  • I have filed an issue about this change and discussed potential changes with the maintainers.
  • I have received the approval from the maintainers to make this change.
  • This is not a stylistic, refactoring, or cleanup change.

Please note that the maintainers will not be reviewing this change until all checkboxes are ticked. See
the Contributions section in the README for more
details.

Discussion thread for this change

Issue number: #6427 #5455

Description of this change

When run go test with testify framework, the test won't be found with the test_filter parameter. But if we changed to use --test_arg=-testify.m=^TestUpdateSimpleTasksTeam_NoTeamIDSet$ then the test can be found correctly.

image

Copy link

google-cla bot commented Aug 10, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added product: GoLand GoLand plugin awaiting-review Awaiting review from Bazel team on PRs labels Aug 10, 2024
@sgowroji sgowroji added awaiting-user-response Awaiting response from author on PRs and removed awaiting-review Awaiting review from Bazel team on PRs labels Aug 12, 2024
@sgowroji
Copy link
Member

Hi @ufo22940268, Could you please Sign the CLA. Thanks!

@ufo22940268
Copy link
Contributor Author

@sgowroji Seems like i have signed.

@sgowroji sgowroji added awaiting-review Awaiting review from Bazel team on PRs and removed awaiting-user-response Awaiting response from author on PRs labels Aug 12, 2024
Copy link
Collaborator

@tpasternak tpasternak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thank you for the contribution, but now running non-testify single test cases is broken

@tpasternak
Copy link
Collaborator

@ufo22940268
you could try doing it this way

diff --git a/golang/src/com/google/idea/blaze/golang/run/producers/GoTestContextProvider.java b/golang/src/com/google/idea/blaze/golang/run/producers/GoTestContextProvider.java
--- a/golang/src/com/google/idea/blaze/golang/run/producers/GoTestContextProvider.java	(revision ebd81d99b6a07a8e4043478a82a332a584a367d5)
+++ b/golang/src/com/google/idea/blaze/golang/run/producers/GoTestContextProvider.java	(date 1723542217361)
@@ -17,8 +17,10 @@
 
 import com.goide.execution.testing.GoTestFinder;
 import com.goide.execution.testing.GoTestRunConfigurationProducerBase;
+import com.goide.execution.testing.frameworks.testify.GoTestifySupport;
 import com.goide.psi.GoFile;
 import com.goide.psi.GoFunctionOrMethodDeclaration;
+import com.goide.psi.GoMethodDeclaration;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.idea.blaze.base.dependencies.TargetInfo;
@@ -103,6 +105,11 @@
       return null;
     }
     GoFunctionOrMethodDeclaration function = GoTestFinder.findTestFunctionInContext(element);
+    if(function instanceof GoMethodDeclaration method) {
+      if (GoTestifySupport.getTestifySuiteTypeSpec(method) != null) {
+        // is testify
+      }
+    }
     if (function == null) {
       return TestContext.builder(/* sourceElement= */ file, ExecutorType.DEBUG_SUPPORTED_TYPES)
           .addTestEnv(GO_TEST_WRAP_TESTV)

@ufo22940268
Copy link
Contributor Author

I will try today

@ufo22940268
Copy link
Contributor Author

@tpasternak I updated code and make the change compatible with existing cases. But i can't add test for testify, here is the the test i write, but it still won't recognize the gofile as testify test

  @Test
  public void testTestifyFile() throws Throwable {
    PsiFile goFile = setupWithSingleFile(
        """
            package yourpackage
            
            import (
                "testing"
                "github.com/stretchr/testify/suite"
            )
            
            // Define the test suite struct
            type YourTestSuite struct {
                suite.Suite
            }
            
            // Example test method
            func (suite *YourTestSuite) T<caret>estExample() {
                suite.T().Log("This is an example test")
                suite.Equal(1, 1, "They should be equal")
            }
            
            // Run the test suite
            func TestYourTestSuite(t *testing.T) {
                suite.Run(t, new(YourTestSuite))
            } 
            """
    );

    PsiElement element = getElementAtCaret(0, goFile);
    String expectedTestFilter = "^TestExample$";
    ConfigurationContext context = createContextFromPsi(element);
    List<ConfigurationFromContext> configurations = context.getConfigurationsFromContext();
    assertThat(configurations).isNotNull();
    assertThat(configurations).hasSize(1);

    ConfigurationFromContext fromContext = configurations.get(0);
    assertThat(fromContext.isProducedBy(TestContextRunConfigurationProducer.class)).isTrue();
    assertThat(fromContext.getConfiguration()).isInstanceOf(BlazeCommandRunConfiguration.class);

    BlazeCommandRunConfiguration config =
        (BlazeCommandRunConfiguration)fromContext.getConfiguration();
    assertThat(config.getTargets())
        .containsExactly(TargetExpression.fromStringSafe("//foo/bar:foo_test"));
    assertThat(Objects.requireNonNull(getTestArgsContents(config)).get(0)).isEqualTo(
        "-testify.m=" + expectedTestFilter);
    assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
  }

@tpasternak
Copy link
Collaborator

but does it work when you test it manually?

@ufo22940268
Copy link
Contributor Author

yes

@tpasternak
Copy link
Collaborator

So I think it could indeed be non-trivial to use an external dependency in the current test framework, however we could try to create an empty go package that uses testify's importpath, and exposes a dummy Suite symbol.

TargetMapBuilder.builder()
.addTarget(
TargetIdeInfo.builder()
.setBuildFile(src("binary/BUILD"))
.setLabel("//binary:main")
.setKind("go_binary")
.addSource(src("binary/main.go"))
.addDependency("//library:foo")
.addDependency("//library/bar:go_default_library")
.setGoInfo(
GoIdeInfo.builder()
.addSource(src("binary/main.go"))
.setImportPath("github.com/user/binary/main")))

@ufo22940268 ufo22940268 force-pushed the match-go-testcase-when-using-testify branch from 4a063be to 1ad1a0f Compare August 18, 2024 11:03
@ufo22940268
Copy link
Contributor Author

@tpasternak test added. please take another look.

@tpasternak
Copy link
Collaborator

@ufo22940268 thank you for the contribution!

@tpasternak tpasternak merged commit 1bfda0e into bazelbuild:master Aug 20, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
product: GoLand GoLand plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants