From 4bee8980219c100b4fdac2eaeea3f6bd22f99246 Mon Sep 17 00:00:00 2001 From: Pontus Leitzler Date: Fri, 16 Aug 2024 12:01:19 +0200 Subject: [PATCH] Ensure generated imports doesn't start with digit --- pkg/fixtures/12345678/http/http.go | 3 +++ pkg/fixtures/same_name_imports.go | 2 ++ pkg/generator.go | 2 +- pkg/generator_test.go | 1 + 4 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 pkg/fixtures/12345678/http/http.go diff --git a/pkg/fixtures/12345678/http/http.go b/pkg/fixtures/12345678/http/http.go new file mode 100644 index 00000000..ce7968b9 --- /dev/null +++ b/pkg/fixtures/12345678/http/http.go @@ -0,0 +1,3 @@ +package http + +type MyStruct struct{} diff --git a/pkg/fixtures/same_name_imports.go b/pkg/fixtures/same_name_imports.go index c00b3365..e88a4413 100644 --- a/pkg/fixtures/same_name_imports.go +++ b/pkg/fixtures/same_name_imports.go @@ -3,6 +3,7 @@ package test import ( "net/http" + number_dir_http "github.com/vektra/mockery/v2/pkg/fixtures/12345678/http" my_http "github.com/vektra/mockery/v2/pkg/fixtures/http" ) @@ -10,4 +11,5 @@ import ( type Example interface { A() http.Flusher B(fixtureshttp string) my_http.MyStruct + C(fixtureshttp string) number_dir_http.MyStruct } diff --git a/pkg/generator.go b/pkg/generator.go index 23e461c7..2941b48b 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -25,7 +25,7 @@ import ( const mockConstructorParamTypeNamePrefix = "mockConstructorTestingT" -var invalidIdentifierChar = regexp.MustCompile("[^[:digit:][:alpha:]_]") +var invalidIdentifierChar = regexp.MustCompile("^[[:digit:]]|[^[:digit:][:alpha:]_]") func DetermineOutputPackageName( interfaceFileName string, diff --git a/pkg/generator_test.go b/pkg/generator_test.go index 9afb4006..d99a27df 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -260,6 +260,7 @@ func (s *GeneratorSuite) TestGeneratorPrologueWithMultipleImportsSameName() { expected := `package mocks +import _2345678http "github.com/vektra/mockery/v2/pkg/fixtures/12345678/http" import fixtureshttp "github.com/vektra/mockery/v2/pkg/fixtures/http" import http "net/http" import mock "github.com/stretchr/testify/mock"