No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Owen Mansel-Chan 0fd7e1e9a9
Merge pull request #29 from github/owen-mc/subpackes-in-modules-txt
Fix writing `vendor/modules.txt` to deal with subpackages
2025-03-21 11:19:44 +00:00
.github/workflows Use newer version of actions/checkout 2025-03-12 14:30:02 +00:00
model Fix embedded fields 2025-03-20 16:25:19 +00:00
vendor Update vendored dependencies 2021-11-24 09:16:56 -05:00
.gitignore Initial commit 2020-06-10 20:12:27 -07:00
auto-detect.go Find and include license(s) for each stubbed library (#16) 2021-02-16 17:57:33 +00:00
CODE_OF_CONDUCT.md Initial commit 2020-06-10 20:12:27 -07:00
CONTRIBUTING.md Initial commit 2020-06-10 20:12:27 -07:00
depstubber.go Do not use deprecated io/ioutil package 2025-03-20 12:45:46 +00:00
go.mod Update version of neurosnap/sentences in go.mod 2021-11-24 09:12:37 -05:00
go.sum Update version of neurosnap/sentences in go.mod 2021-11-24 09:12:37 -05:00
LICENSE Initial commit 2020-06-10 20:12:27 -07:00
license-finder.go Deduplicate license file names (#18) 2021-06-22 11:49:12 -07:00
NOTICE Initial commit 2020-06-10 20:12:27 -07:00
README.md Add known limitation about promoted methods 2025-03-21 09:32:33 +00:00
reflect.go Merge pull request #29 from github/owen-mc/subpackes-in-modules-txt 2025-03-21 11:19:44 +00:00
SECURITY.md Initial commit 2020-06-10 20:12:27 -07:00
util.go Do not use deprecated io/ioutil package 2025-03-20 12:45:46 +00:00
vendor.go Sort packages for each module 2025-03-20 21:59:20 +00:00

depstubber

CI

This is a tool that generates type-correct stubs for dependencies, for use in testing. It is particularly useful for testing static analysis applications, where including library source code may be undesirable. It was written and is currently used for testing CodeQL Go.

The general usage pattern if vendoring is desired will look something like:

PATH="$PATH:$GOPATH/bin"
GO111MODULES=off go get github.com/github/depstubber
GO111MODULES=off go install github.com/github/depstubber # Only needed for Go 1.18 and above
go mod tidy # required to generate go.sum
# generate a vendor/module.txt for the go 1.24 vendor consistency check
depstubber -write_module_txt
# Create stubs of Type1, Type2, SomeFunc, and SomeVariable. They are separated by a space
# because values must be treated differently from types in the implementation of depstubber.
depstubber -vendor github.com/my/package Type1,Type2 SomeFunc,SomeVariable

The last line can be executed using Go's built in generate subcommand, by adding a comment to any relevant Go file that looks like this:

//go:generate depstubber -vendor github.com/my/package Type1,Type2 SomeFunc,SomeVariable

Then, run go generate <package>, where <package> is the package containing the file the comment was added to. This will automatically run the depstubber command.

Limitations:

  • It is limited to a single package at a time.
  • There is no way to automatically stub all exports.
  • It does not generate memory-compatible types, as unexported types are skipped.
  • There is no way to automatically detect exports used in a program.
  • There is no way to specify specific methods on a type; all methods are automatically stubbed.
  • It cannot currently distinguish between type aliases. This is a limitation of the reflect package.
  • It makes new method declarations for promoted methods. This is a limitation of the reflect package.

Please feel free to submit a pull request for any of the above, or with any other improvements. See CONTRIBUTING.md for more information.

For information about what improvements may be in progress, see issues.

To build, simply run go build with Go 1.14 or higher.

This project contains a significant amount of code copied from the GoMock project, as well as from the Go standard library. The mock code has been adapted to generate stub versions of requested exported fields instead of generating a mock implementation of an interface. The licenses for both these codebases can be found in the NOTICE file.

It is licensed under Apache-2.0. For more information, see the LICENSE file.