From 82b61dcc69e3ff2f56975b188718e58b815f28ca Mon Sep 17 00:00:00 2001 From: RainbowMango Date: Mon, 8 Jul 2024 11:05:44 +0800 Subject: [PATCH] Fix mockgen generated files not group imports issue Signed-off-by: RainbowMango --- hack/update-import-aliases.sh | 2 +- hack/update-mocks.sh | 7 ++++++- pkg/scheduler/framework/interface.go | 5 +++++ pkg/scheduler/framework/testing/mock_interface.go | 3 ++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/hack/update-import-aliases.sh b/hack/update-import-aliases.sh index 51403866c2e2..7b284f0c16ba 100755 --- a/hack/update-import-aliases.sh +++ b/hack/update-import-aliases.sh @@ -22,7 +22,7 @@ SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. cd "${SCRIPT_ROOT}" ROOT_PATH=$(pwd) -GO111MODULE=on go install "golang.org/x/tools/cmd/goimports@v0.1.5" +GO111MODULE=on go install "golang.org/x/tools/cmd/goimports" IMPORT_ALIASES_PATH="${ROOT_PATH}/hack/.import-aliases" INCLUDE_PATH="(${ROOT_PATH}/cmd|${ROOT_PATH}/test/e2e|${ROOT_PATH}/test/helper|\ diff --git a/hack/update-mocks.sh b/hack/update-mocks.sh index 83f900590005..36ccfe97ad69 100755 --- a/hack/update-mocks.sh +++ b/hack/update-mocks.sh @@ -30,11 +30,16 @@ trap EXIT echo 'installing mockgen' source "${KARMADA_ROOT}"/hack/util.sh -echo -n "Preparing: 'mockgen' existence check - " +echo "Preparing: 'mockgen' existence check - " if [ ! $(util::cmd_exist mockgen) ]; then # install from vendor with the pinned version in go.mod file GO111MODULE=on go install "go.uber.org/mock/mockgen" fi +echo "Preparing: 'goimports' existence check - " +if [ ! $(util::cmd_exist goimports) ]; then + # install from vendor with the pinned version in go.mod file + GO111MODULE=on go install "golang.org/x/tools/cmd/goimports" +fi find_files() { find . -not \( \ diff --git a/pkg/scheduler/framework/interface.go b/pkg/scheduler/framework/interface.go index 6340386c3df1..7f660589e7da 100644 --- a/pkg/scheduler/framework/interface.go +++ b/pkg/scheduler/framework/interface.go @@ -14,7 +14,12 @@ See the License for the specific language governing permissions and limitations under the License. */ +// Given mockgen does not group imports according to our project's conventions. +// Following the mockgen command, we run 'goimports', which reformats the +// generated file to ensure that all imports are properly grouped and sorted, +// maintaining consistency with the rest of our codebase. //go:generate mockgen -source=interface.go -destination=testing/mock_interface.go -package=testing FilterPlugin ScorePlugin ScoreExtensions +//go:generate goimports -local "github.com/karmada-io/karmada" -w testing/mock_interface.go package framework diff --git a/pkg/scheduler/framework/testing/mock_interface.go b/pkg/scheduler/framework/testing/mock_interface.go index def6cb8cd6bc..f72952cf0a1b 100644 --- a/pkg/scheduler/framework/testing/mock_interface.go +++ b/pkg/scheduler/framework/testing/mock_interface.go @@ -13,10 +13,11 @@ import ( context "context" reflect "reflect" + gomock "go.uber.org/mock/gomock" + v1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" v1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" framework "github.com/karmada-io/karmada/pkg/scheduler/framework" - gomock "go.uber.org/mock/gomock" ) // MockFramework is a mock of Framework interface.