Skip to content

Commit

Permalink
🚸 Do not validate token when it's not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
tiulpin committed Sep 28, 2023
1 parent 9cf09fa commit aae99b5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
51 changes: 51 additions & 0 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
11 changes: 10 additions & 1 deletion core/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/product_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion core/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit aae99b5

Please sign in to comment.