From aae99b50b6ded62523ea6ea2cdd250118b731256 Mon Sep 17 00:00:00 2001 From: tiulpin Date: Thu, 28 Sep 2023 17:57:47 +0200 Subject: [PATCH] :children_crossing: Do not validate token when it's not needed --- cmd/init.go | 4 +++- core/core_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++ core/options.go | 11 +++++++++- core/product_info.go | 2 +- core/system.go | 4 +++- 5 files changed, 68 insertions(+), 4 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 60e05c08..d0b17aa8 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -75,7 +75,9 @@ func newInitCommand() *cobra.Command { } core.PrintFile(filepath.Join(options.ProjectDir, options.YamlName)) options.Linter = qodanaYaml.Linter - options.ValidateToken(force) + if options.RequiresToken() { + options.ValidateToken(force) + } }, } flags := cmd.Flags() diff --git a/core/core_test.go b/core/core_test.go index 50d7ad55..6f66af88 100644 --- a/core/core_test.go +++ b/core/core_test.go @@ -1048,3 +1048,54 @@ func TestSetupLicenseToken(t *testing.T) { }) } } + +func TestQodanaOptions_RequiresToken(t *testing.T) { + tests := []struct { + name string + linter string + ide string + expected bool + }{ + { + "QDPYC docker", + Image(QDPYC), + "", + false, + }, + { + "QDJVMC ide", + "", + QDJVMC, + false, + }, + { + QodanaLicense, + "", + "", + false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.name == QodanaLicense { + err := os.Setenv(QodanaLicense, "test") + if err != nil { + t.Fatal(err) + } + defer func() { + err := os.Unsetenv(QodanaLicense) + if err != nil { + t.Fatal(err) + } + }() + } + o := QodanaOptions{ + Linter: tt.linter, + Ide: tt.ide, + } + result := o.RequiresToken() + assert.Equal(t, tt.expected, result) + }) + } +} diff --git a/core/options.go b/core/options.go index c07fc010..6d81ae33 100644 --- a/core/options.go +++ b/core/options.go @@ -213,7 +213,16 @@ func (o *QodanaOptions) properties() (map[string]string, []string) { } func (o *QodanaOptions) RequiresToken() bool { - return (o.Linter != Image(QDPYC) && o.Linter != Image(QDJVMC)) && o.Ide != QDJVMC && o.Ide != QDPYC + if os.Getenv(QodanaLicense) == "" { + return false + } + + if o.Linter != Image(QDPYC) && o.Linter != Image(QDJVMC) && + o.Ide != QDJVMC && o.Ide != QDPYC { + return false + } + + return !Prod.IsCommunity() && !Prod.EAP } func (o *QodanaOptions) fixesSupported() bool { diff --git a/core/product_info.go b/core/product_info.go index 76ac1b2c..6693ef4d 100644 --- a/core/product_info.go +++ b/core/product_info.go @@ -129,7 +129,7 @@ func (p *product) parentPrefix() string { } func (p *product) IsCommunity() bool { - return p.Code == QDJVMC || p.Code == QDPYC + return p.Code == QDJVMC || p.Code == QDPYC || p.Code == "" } func (p *product) getProductNameFromCode() string { diff --git a/core/system.go b/core/system.go index 5ebb2f65..7dbf8696 100644 --- a/core/system.go +++ b/core/system.go @@ -223,7 +223,9 @@ func LookUpLinterSystemDir(opts *QodanaOptions) string { // prepareHost gets the current user, creates the necessary folders for the analysis. func prepareHost(opts *QodanaOptions) { - opts.ValidateToken(false) + if opts.RequiresToken() { + opts.ValidateToken(false) + } if opts.ClearCache { err := os.RemoveAll(opts.CacheDir)