Skip to content

Imports in mockery generated files do not comply with goimports standard when package name does not match directoryname #1162

Description

@ArnoSen

Description

At our company we use mockery extensively and it is a great tool. Thanks a lot for all the effort that you have put in!

When you import a package whose package name does not match the directory name, mockery generates go files that are changed after running 'goimports'. In our case, this makes our pipelines fail if I do not 'post process' generated files with 'goimports'

Reproducer

  1. Install mockery 3.7.0 .
  2. Create a new 'mockery' go project in an empty directory with 'go mod init mockery'
  3. Create the below directories and files:
    ├── foo
    │   └── bar.go
    ├── .mockery
    └── pkg
    └── pkg.go

foo/bar.go:

package bar // NB: note that the package name is different from the directory name

type Bar struct{}

pkg/pkg.go:

package pkg

import (
        bar "mockery/foo"
)

type Getter interface {
        Get() *bar.Bar
}

.mockery:

packages:
  mockery/pkg:
    interfaces:
      Getter:
        config:
          filename: "pkg_mock_test.go"
  1. Run 'mockery'
  2. You should see a new file pkg/pkg_mock_test.go and get the first lines:
$ head pkg/pkg_mock_test.go
// Code generated by mockery; DO NOT EDIT.
// github.com/vektra/mockery
// template: testify

package pkg

import (
        "mockery/foo"

        mock "github.com/stretchr/testify/mock"
  1. Run 'goimports' on the generated file:
$ goimports pkg/pkg_mock_test.go | head
// Code generated by mockery; DO NOT EDIT.
// github.com/vektra/mockery
// template: testify

package pkg

import (
        bar "mockery/foo"

        mock "github.com/stretchr/testify/mock"
  1. Note that there is a change:
    Mockery generated "mockery/foo"
    Goimports changed that to bar "mockery/foo"

Expected behavior

I would expect after generation of the mocks with mockery, the format complies with 'goimports'.
In this specific example I would expect the import line to be bar "mockery/foo" instead of "mockery/foo"

  1. If you think this could be improved/fixed, I would be happy to create a PR for it.

Mockery version

v3.7.0

Installation Mechanism

  • go get
  • Pre-built release
  • homebrew
  • Other: [please describe]

Go version

Version of Go used to build mockery from source (if applicable).

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions