Skip to content

Resolve black box test import path from go.mod#39

Open
myzktkyk wants to merge 1 commit into
rillig:masterfrom
myzktkyk:fix-issue-33
Open

Resolve black box test import path from go.mod#39
myzktkyk wants to merge 1 commit into
rillig:masterfrom
myzktkyk:fix-issue-33

Conversation

@myzktkyk

Copy link
Copy Markdown

Summary

  • Resolve the import path written into gobco_bridge_test.go by reading the module path from go.mod and combining it with the directory's relative path inside the module, instead of scanning imports in the target directory.
  • This fixes the failure described in Finding original package path in directory with _test package may fails #33 where the previous heuristic could pick an unrelated import (e.g. an iface/target package) when no _test file imports the target package directly.
  • Extracts findInModule from main.go into a package-level function so it can be reused.
  • Adds testdata/issue33 reproducing the original failure scenario, used by the new Test_gobcoMain__issue33.

Background

This PR supersedes #34. Compared to that PR, it resolves the module path by reading go.mod directly instead of shelling out to go list -m, which avoids depending on the cwd and on go being in PATH at instrumentation time, and uses the path package so import paths are correct on Windows.

Test plan

  • go test ./... passes (including the new Test_gobcoMain__issue33)
  • sh ./test.sh equivalent: go install ./..., gobco ., gobco ./testdata/instrumenter, gobco -branch ./testdata/instrumenter all succeed
  • Verified the new testdata fails with the previous implementation (./gobco_bridge_test.go:6:16: undefined: target.GobcoCover)

Fixes #33

Credit to @1st-vil for the original approach in #34.

The generated gobco_bridge_test.go must import the target package by
its real Go import path. The previous implementation scanned imports
in the target directory and copied one whose base name matched the
target package's name. That heuristic failed when:

  - no '_test' file imported the target package directly
    (it was only reached through a helper package), and
  - another import in the same directory happened to share the same
    base name as the target package (for example, an interface package
    "iface/target" imported from "target").

In that case, the bridge file imported the wrong package and the
test build failed with 'undefined: target.GobcoCover'.

Compute the import path instead by combining the module path read
directly from go.mod with the relative path from the module root,
so the result no longer depends on which imports happen to appear
in neighbouring files.

The added testdata/issue33 reproduces the failure scenario described
in issue rillig#33.

Fixes rillig#33
@myzktkyk

Copy link
Copy Markdown
Author

Hi @rillig I took @1st-vil's approach in #34 as a starting point and opened #39 against current master.

A few differences from #34 that I hope address the points you raised:

  • The module path is read directly from go.mod instead of shelling out to go list -m, so it doesn't depend on the current working directory or on go being in PATH at instrumentation time.
  • The import path is assembled with the path package, so it stays correct on Windows.
  • No new module dependencies.

I also added testdata/issue33, which reproduces the original failure (undefined: target.GobcoCover) on the previous implementation and is exercised by a new test case.

Would you mind taking a look when you have a moment? Happy to adjust anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Finding original package path in directory with _test package may fails

1 participant