From e1fecdcde2a7b27a0e65b41617b42c575c037b2f Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 24 Jun 2023 15:14:03 +0200 Subject: [PATCH] tests: improve tests --- tagliatelle_test.go | 149 ++++++++++++++++++++++++---- testdata/src/a/go.mod | 3 - testdata/src/{a => one}/b/sample.go | 0 testdata/src/one/go.mod | 3 + testdata/src/{a => one}/sample.go | 2 +- testdata/src/three/b/sample.go | 56 +++++++++++ testdata/src/three/go.mod | 3 + testdata/src/three/sample.go | 56 +++++++++++ testdata/src/two/b/sample.go | 56 +++++++++++ testdata/src/two/go.mod | 3 + testdata/src/two/sample.go | 56 +++++++++++ 11 files changed, 361 insertions(+), 26 deletions(-) delete mode 100644 testdata/src/a/go.mod rename testdata/src/{a => one}/b/sample.go (100%) create mode 100644 testdata/src/one/go.mod rename testdata/src/{a => one}/sample.go (99%) create mode 100644 testdata/src/three/b/sample.go create mode 100644 testdata/src/three/go.mod create mode 100644 testdata/src/three/sample.go create mode 100644 testdata/src/two/b/sample.go create mode 100644 testdata/src/two/go.mod create mode 100644 testdata/src/two/sample.go diff --git a/tagliatelle_test.go b/tagliatelle_test.go index 6d14298..5cc9e99 100644 --- a/tagliatelle_test.go +++ b/tagliatelle_test.go @@ -3,7 +3,6 @@ package tagliatelle_test import ( "os" "os/exec" - "path" "path/filepath" "testing" @@ -13,47 +12,153 @@ import ( ) func TestAnalyzer(t *testing.T) { - cfg := tagliatelle.Config{ - Base: tagliatelle.Base{ - Rules: map[string]string{ - "json": "camel", - "yaml": "camel", - "xml": "camel", - "bson": "camel", - "avro": "snake", - "mapstructure": "kebab", - "header": "header", - "envconfig": "upperSnake", - "env": "upperSnake", + testCases := []struct { + desc string + dir string + patterns []string + cfg tagliatelle.Config + }{ + { + desc: "simple", + dir: "one", + patterns: []string{"one"}, + cfg: tagliatelle.Config{ + Base: tagliatelle.Base{ + Rules: map[string]string{ + "json": "camel", + "yaml": "camel", + "xml": "camel", + "bson": "camel", + "avro": "snake", + "mapstructure": "kebab", + "header": "header", + "envconfig": "upperSnake", + "env": "upperSnake", + }, + UseFieldName: true, + }, + }, + }, + { + desc: "with non-applicable overrides", + dir: "one", + patterns: []string{"one/..."}, + cfg: tagliatelle.Config{ + Base: tagliatelle.Base{ + Rules: map[string]string{ + "json": "camel", + "yaml": "camel", + "xml": "camel", + "bson": "camel", + "avro": "snake", + "mapstructure": "kebab", + "header": "header", + "envconfig": "upperSnake", + "env": "upperSnake", + }, + UseFieldName: true, + }, + Overrides: []tagliatelle.Overrides{ + { + Package: "one/b/c", + Base: tagliatelle.Base{ + Rules: map[string]string{ + "json": "upperSnake", + "yaml": "upperSnake", + }, + UseFieldName: false, + }, + }, + }, + }, + }, + { + desc: "with applicable overrides", + dir: "two", + patterns: []string{"two/..."}, + cfg: tagliatelle.Config{ + Base: tagliatelle.Base{ + Rules: map[string]string{ + "json": "camel", + "yaml": "camel", + "xml": "camel", + "bson": "camel", + "avro": "snake", + "mapstructure": "kebab", + "header": "header", + "envconfig": "upperSnake", + "env": "upperSnake", + }, + UseFieldName: true, + }, + Overrides: []tagliatelle.Overrides{ + { + Package: "two/b", + Base: tagliatelle.Base{ + Rules: map[string]string{ + "json": "upperSnake", + "yaml": "upperSnake", + }, + UseFieldName: false, + }, + }, + }, }, - UseFieldName: true, }, - Overrides: []tagliatelle.Overrides{ - { - Package: "a/b/c", + { + desc: "ignore", + dir: "three", + patterns: []string{"three/..."}, + cfg: tagliatelle.Config{ Base: tagliatelle.Base{ Rules: map[string]string{ - "json": "upperSnake", - "yaml": "upperSnake", + "json": "camel", + "yaml": "camel", + "xml": "camel", + "bson": "camel", + "avro": "snake", + "mapstructure": "kebab", + "header": "header", + "envconfig": "upperSnake", + "env": "upperSnake", + }, + UseFieldName: true, + }, + Overrides: []tagliatelle.Overrides{ + { + Package: "three/b", + Base: tagliatelle.Base{ + Ignore: true, + }, }, - UseFieldName: false, }, }, }, } - runWithSuggestedFixes(t, tagliatelle.New(cfg), "a", "a") + for _, test := range testCases { + t.Run(test.desc, func(t *testing.T) { + runWithSuggestedFixes(t, tagliatelle.New(test.cfg), test.dir, test.patterns...) + }) + } } func runWithSuggestedFixes(t *testing.T, a *analysis.Analyzer, dir string, patterns ...string) []*analysistest.Result { t.Helper() + wd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + + defer func() { _ = os.Chdir(wd) }() + testdata := analysistest.TestData() // NOTE: analysistest does not yet support modules; // see https://github.com/golang/go/issues/37054 for details. - err := os.Chdir(filepath.Join(testdata, "src", path.Join(dir))) + err = os.Chdir(filepath.Join(testdata, "src", filepath.FromSlash(dir))) if err != nil { t.Fatal(err) } diff --git a/testdata/src/a/go.mod b/testdata/src/a/go.mod deleted file mode 100644 index 4c33abd..0000000 --- a/testdata/src/a/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module example.com/a - -go 1.19 diff --git a/testdata/src/a/b/sample.go b/testdata/src/one/b/sample.go similarity index 100% rename from testdata/src/a/b/sample.go rename to testdata/src/one/b/sample.go diff --git a/testdata/src/one/go.mod b/testdata/src/one/go.mod new file mode 100644 index 0000000..e5ea4ec --- /dev/null +++ b/testdata/src/one/go.mod @@ -0,0 +1,3 @@ +module example.com/one + +go 1.19 diff --git a/testdata/src/a/sample.go b/testdata/src/one/sample.go similarity index 99% rename from testdata/src/a/sample.go rename to testdata/src/one/sample.go index 2712fa7..1141169 100644 --- a/testdata/src/a/sample.go +++ b/testdata/src/one/sample.go @@ -1,4 +1,4 @@ -package a +package one type Foo struct { ID string `json:"ID"` // want `json\(camel\): got 'ID' want 'id'` diff --git a/testdata/src/three/b/sample.go b/testdata/src/three/b/sample.go new file mode 100644 index 0000000..7165b99 --- /dev/null +++ b/testdata/src/three/b/sample.go @@ -0,0 +1,56 @@ +package b + +type Foo struct { + ID string `json:"ID"` + UserID string `json:"UserID"` + Name string `json:"NAME"` + Value string `json:"VALUE,omitempty"` + Bar Bar `json:"BAR"` + Bur `json:"BUR"` + + Qiix Quux `json:",inline"` + Quux `json:",inline"` +} + +type Bar struct { + Name string `json:"-"` + Value string `json:"VALUE"` + CommonServiceFooItem *Bir `json:"COMMON_SERVICE_ITEM,omitempty"` +} + +type Bir struct { + Name string `json:"-"` + Value string `json:"VALUE"` + ReplaceAllowList []string `mapstructure:"replace-allow-list"` +} + +type Bur struct { + Name string + Value string `yaml:"Value"` + More string `json:"-"` + Also string `json:",omitempty"` + ReqPerS string `avro:"req_per_s"` + HeaderValue string `header:"Header-Value"` + WrongHeaderValue string `header:"Header_Value"` + EnvConfigValue string `envconfig:"ENV_CONFIG_VALUE"` + WrongEnvConfigValue string `envconfig:"env_config_value"` + EnvValue string `env:"ENV_VALUE"` + WrongEnvValue string `env:"env_value"` +} + +type Quux struct { + Data []byte `json:"data"` +} + +// MessedUpTags struct is to validate the tool is not doing any validation about invalid tags. +// Please read the readme about this choice. +type MessedUpTags struct { + // an invalid tag cannot be validated. + Bad string `json:"bad` + + // a tag not supported by the rules is not validated. + Whatever string `foo:whatever` + + // a tag supported by the rule cannot be validated because foo tag breaks the whole tags block + Mixed string `json:"mixed" foo:mixed` +} diff --git a/testdata/src/three/go.mod b/testdata/src/three/go.mod new file mode 100644 index 0000000..04f1b69 --- /dev/null +++ b/testdata/src/three/go.mod @@ -0,0 +1,3 @@ +module example.com/two + +go 1.19 diff --git a/testdata/src/three/sample.go b/testdata/src/three/sample.go new file mode 100644 index 0000000..0c4f41a --- /dev/null +++ b/testdata/src/three/sample.go @@ -0,0 +1,56 @@ +package two + +type Foo struct { + ID string `json:"ID"` // want `json\(camel\): got 'ID' want 'id'` + UserID string `json:"UserID"` // want `json\(camel\): got 'UserID' want 'userId'` + Name string `json:"name"` + Value string `json:"value,omitempty"` + Bar Bar `json:"bar"` + Bur `json:"bur"` + + Qiix Quux `json:",inline"` + Quux `json:",inline"` +} + +type Bar struct { + Name string `json:"-"` + Value string `json:"value"` + CommonServiceFooItem *Bir `json:"CommonServiceItem,omitempty"` // want `json\(camel\): got 'CommonServiceItem' want 'commonServiceFooItem'` +} + +type Bir struct { + Name string `json:"-"` + Value string `json:"value"` + ReplaceAllowList []string `mapstructure:"replace-allow-list"` +} + +type Bur struct { + Name string + Value string `yaml:"Value"` // want `yaml\(camel\): got 'Value' want 'value'` + More string `json:"-"` + Also string `json:",omitempty"` // want `json\(camel\): got 'Also' want 'also'` + ReqPerS string `avro:"req_per_s"` + HeaderValue string `header:"Header-Value"` + WrongHeaderValue string `header:"Header_Value"` // want `header\(header\): got 'Header_Value' want 'Wrong-Header-Value'` + EnvConfigValue string `envconfig:"ENV_CONFIG_VALUE"` + WrongEnvConfigValue string `envconfig:"env_config_value"` // want `envconfig\(upperSnake\): got 'env_config_value' want 'WRONG_ENV_CONFIG_VALUE'` + EnvValue string `env:"ENV_VALUE"` + WrongEnvValue string `env:"env_value"` // want `env\(upperSnake\): got 'env_value' want 'WRONG_ENV_VALUE'` +} + +type Quux struct { + Data []byte `json:"data"` +} + +// MessedUpTags struct is to validate the tool is not doing any validation about invalid tags. +// Please read the readme about this choice. +type MessedUpTags struct { + // an invalid tag cannot be validated. + Bad string `json:"bad` + + // a tag not supported by the rules is not validated. + Whatever string `foo:whatever` + + // a tag supported by the rule cannot be validated because foo tag breaks the whole tags block + Mixed string `json:"mixed" foo:mixed` +} diff --git a/testdata/src/two/b/sample.go b/testdata/src/two/b/sample.go new file mode 100644 index 0000000..bf3ca58 --- /dev/null +++ b/testdata/src/two/b/sample.go @@ -0,0 +1,56 @@ +package b + +type Foo struct { + ID string `json:"ID"` + UserID string `json:"UserID"` // want `json\(upperSnake\): got 'UserID' want 'USER_ID'` + Name string `json:"NAME"` + Value string `json:"VALUE,omitempty"` + Bar Bar `json:"BAR"` + Bur `json:"BUR"` + + Qiix Quux `json:",inline"` + Quux `json:",inline"` +} + +type Bar struct { + Name string `json:"-"` + Value string `json:"VALUE"` + CommonServiceFooItem *Bir `json:"COMMON_SERVICE_ITEM,omitempty"` +} + +type Bir struct { + Name string `json:"-"` + Value string `json:"VALUE"` + ReplaceAllowList []string `mapstructure:"replace-allow-list"` +} + +type Bur struct { + Name string + Value string `yaml:"Value"` // want `yaml\(upperSnake\): got 'Value' want 'VALUE'` + More string `json:"-"` + Also string `json:",omitempty"` // want `json\(upperSnake\): got 'Also' want 'ALSO'` + ReqPerS string `avro:"req_per_s"` + HeaderValue string `header:"Header-Value"` + WrongHeaderValue string `header:"Header_Value"` // want `header\(header\): got 'Header_Value' want 'Header-Value'` + EnvConfigValue string `envconfig:"ENV_CONFIG_VALUE"` + WrongEnvConfigValue string `envconfig:"env_config_value"` // want `envconfig\(upperSnake\): got 'env_config_value' want 'ENV_CONFIG_VALUE'` + EnvValue string `env:"ENV_VALUE"` + WrongEnvValue string `env:"env_value"` // want `env\(upperSnake\): got 'env_value' want 'ENV_VALUE'` +} + +type Quux struct { + Data []byte `json:"data"` // want `json\(upperSnake\): got 'data' want 'DATA'` +} + +// MessedUpTags struct is to validate the tool is not doing any validation about invalid tags. +// Please read the readme about this choice. +type MessedUpTags struct { + // an invalid tag cannot be validated. + Bad string `json:"bad` + + // a tag not supported by the rules is not validated. + Whatever string `foo:whatever` + + // a tag supported by the rule cannot be validated because foo tag breaks the whole tags block + Mixed string `json:"mixed" foo:mixed` // want `json\(upperSnake\): got 'mixed' want 'MIXED'` +} diff --git a/testdata/src/two/go.mod b/testdata/src/two/go.mod new file mode 100644 index 0000000..596a9ed --- /dev/null +++ b/testdata/src/two/go.mod @@ -0,0 +1,3 @@ +module example.com/three + +go 1.19 diff --git a/testdata/src/two/sample.go b/testdata/src/two/sample.go new file mode 100644 index 0000000..0545e2c --- /dev/null +++ b/testdata/src/two/sample.go @@ -0,0 +1,56 @@ +package three + +type Foo struct { + ID string `json:"ID"` // want `json\(camel\): got 'ID' want 'id'` + UserID string `json:"UserID"` // want `json\(camel\): got 'UserID' want 'userId'` + Name string `json:"name"` + Value string `json:"value,omitempty"` + Bar Bar `json:"bar"` + Bur `json:"bur"` + + Qiix Quux `json:",inline"` + Quux `json:",inline"` +} + +type Bar struct { + Name string `json:"-"` + Value string `json:"value"` + CommonServiceFooItem *Bir `json:"CommonServiceItem,omitempty"` // want `json\(camel\): got 'CommonServiceItem' want 'commonServiceFooItem'` +} + +type Bir struct { + Name string `json:"-"` + Value string `json:"value"` + ReplaceAllowList []string `mapstructure:"replace-allow-list"` +} + +type Bur struct { + Name string + Value string `yaml:"Value"` // want `yaml\(camel\): got 'Value' want 'value'` + More string `json:"-"` + Also string `json:",omitempty"` // want `json\(camel\): got 'Also' want 'also'` + ReqPerS string `avro:"req_per_s"` + HeaderValue string `header:"Header-Value"` + WrongHeaderValue string `header:"Header_Value"` // want `header\(header\): got 'Header_Value' want 'Wrong-Header-Value'` + EnvConfigValue string `envconfig:"ENV_CONFIG_VALUE"` + WrongEnvConfigValue string `envconfig:"env_config_value"` // want `envconfig\(upperSnake\): got 'env_config_value' want 'WRONG_ENV_CONFIG_VALUE'` + EnvValue string `env:"ENV_VALUE"` + WrongEnvValue string `env:"env_value"` // want `env\(upperSnake\): got 'env_value' want 'WRONG_ENV_VALUE'` +} + +type Quux struct { + Data []byte `json:"data"` +} + +// MessedUpTags struct is to validate the tool is not doing any validation about invalid tags. +// Please read the readme about this choice. +type MessedUpTags struct { + // an invalid tag cannot be validated. + Bad string `json:"bad` + + // a tag not supported by the rules is not validated. + Whatever string `foo:whatever` + + // a tag supported by the rule cannot be validated because foo tag breaks the whole tags block + Mixed string `json:"mixed" foo:mixed` +}