Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 0 additions & 108 deletions _codegen/internal/imports/imports.go

This file was deleted.

79 changes: 70 additions & 9 deletions _codegen/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
/*
The MIT License (MIT)

Copyright (c) 2015,2016 Ernesto Jiménez
Copyright (c) 2019 Leigh McCulloch
Copyright (c) 2020 Matt Gorzka
Copyright (c) 2024 Simon Schulte
Copyright (c) 2023,2025 Olivier Mengué
Comment on lines +4 to +8
Copy link
Collaborator

@brackendawson brackendawson Sep 16, 2025

Choose a reason for hiding this comment

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

This is unusual and would require careful attention to keep up to date. Follow the same pattern as testify and allow VCS to be the source of the names:

Copyright (c) 2012-2020 Mat Ryer, Tyler Bunnell and contributors.

Suggested change
Copyright (c) 2015,2016 Ernesto Jiménez
Copyright (c) 2019 Leigh McCulloch
Copyright (c) 2020 Matt Gorzka
Copyright (c) 2024 Simon Schulte
Copyright (c) 2023,2025 Olivier Mengué
Copyright (c) 2015-2025 Ernesto Jiménez and contributors.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was tempted to do that, but to stay on the safe side, I chose to mention all contributors I found in the Git history.

As this is not really engineering, but legal stuff, I would be more confident if I you could give me some references to back the choice of a particular style of copyright mentions.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I can't give you the original decision to claim on behalf of the contributors because it's not written down, but in addition to what is already done in our own source code I found this in my research: https://opensource.stackexchange.com/questions/4960/can-a-team-be-the-copyright-holder-mit

We should be consistent with ourselves.


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

// This program reads all assertion functions from the assert package and
// automatically generates the corresponding requires and forwarded assertions

Expand All @@ -22,8 +50,6 @@ import (
"regexp"
"strings"
"text/template"

"github.com/stretchr/testify/_codegen/internal/imports"
)

var (
Expand All @@ -42,17 +68,17 @@ func main() {
log.Fatal(err)
}

importer, funcs, err := analyzeCode(scope, docs)
imports, funcs, err := analyzeCode(scope, docs)
if err != nil {
log.Fatal(err)
}

if err := generateCode(importer, funcs); err != nil {
if err := generateCode(imports, funcs); err != nil {
log.Fatal(err)
}
}

func generateCode(importer imports.Importer, funcs []testFunc) error {
func generateCode(imports *imports, funcs []testFunc) error {
buff := bytes.NewBuffer(nil)

tmplHead, tmplFunc, err := parseTemplates()
Expand All @@ -66,7 +92,7 @@ func generateCode(importer imports.Importer, funcs []testFunc) error {
Imports map[string]string
}{
*outputPkg,
importer.Imports(),
imports.imports,
}); err != nil {
return err
}
Expand Down Expand Up @@ -128,10 +154,13 @@ func outputFile() (*os.File, error) {

// analyzeCode takes the types scope and the docs and returns the import
// information and information about all the assertion functions.
func analyzeCode(scope *types.Scope, docs *doc.Package) (imports.Importer, []testFunc, error) {
func analyzeCode(scope *types.Scope, docs *doc.Package) (*imports, []testFunc, error) {
testingT := scope.Lookup("TestingT").Type().Underlying().(*types.Interface)

importer := imports.New(*outputPkg)
importer := &imports{
currentPkg: *outputPkg,
imports: map[string]string{},
}
var funcs []testFunc
// Go through all the top level functions
for _, fdocs := range docs.Funcs {
Expand Down Expand Up @@ -166,11 +195,43 @@ func analyzeCode(scope *types.Scope, docs *doc.Package) (imports.Importer, []tes
}

funcs = append(funcs, testFunc{*outputPkg, fdocs, fn})
importer.AddImportsFrom(sig.Params())
importer.addImportsFrom(sig.Params())
}
return importer, funcs, nil
}

// imports collects a map of imported packages for a source file.
//
// This code has been copied from package github.com/ernesto-jimenez/gogen/imports
type imports struct {
currentPkg string
imports map[string]string
}

func (imp *imports) addImportsFrom(t types.Type) {
switch el := t.(type) {
case *types.Basic:
case *types.Slice:
imp.addImportsFrom(el.Elem())
case *types.Pointer:
imp.addImportsFrom(el.Elem())
case *types.Named:
pkg := el.Obj().Pkg()
if pkg == nil {
return
}
if pkg.Name() == imp.currentPkg {
return
}
imp.imports[pkg.Path()] = pkg.Name()
case *types.Tuple:
for i := 0; i < el.Len(); i++ {
imp.addImportsFrom(el.At(i).Type())
}
default:
}
}

// parsePackageSource returns the types scope and the package documentation from the package
func parsePackageSource(pkg string) (*types.Scope, *doc.Package, error) {
pd, err := build.Import(pkg, ".", 0)
Expand Down