|
| 1 | +// Copyright 2020 Security Scorecard Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package options implements Scorecard options. |
| 16 | +package options |
| 17 | + |
| 18 | +import ( |
| 19 | + "os" |
| 20 | + "testing" |
| 21 | +) |
| 22 | + |
| 23 | +// Cannot run parallel tests because of the ENV variables. |
| 24 | +//nolint |
| 25 | +func TestOptions_Validate(t *testing.T) { |
| 26 | + type fields struct { |
| 27 | + Repo string |
| 28 | + Local string |
| 29 | + Commit string |
| 30 | + LogLevel string |
| 31 | + Format string |
| 32 | + NPM string |
| 33 | + PyPI string |
| 34 | + RubyGems string |
| 35 | + PolicyFile string |
| 36 | + ResultsFile string |
| 37 | + ChecksToRun []string |
| 38 | + Metadata []string |
| 39 | + ShowDetails bool |
| 40 | + PublishResults bool |
| 41 | + EnableSarif bool |
| 42 | + EnableScorecardV5 bool |
| 43 | + EnableScorecardV6 bool |
| 44 | + } |
| 45 | + tests := []struct { |
| 46 | + name string |
| 47 | + fields fields |
| 48 | + wantErr bool |
| 49 | + }{ |
| 50 | + { |
| 51 | + name: "No options are turned on", |
| 52 | + fields: fields{}, |
| 53 | + wantErr: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "format sarif but the enable sarif flag is not set", |
| 57 | + fields: fields{ |
| 58 | + Format: "sarif", |
| 59 | + }, |
| 60 | + wantErr: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "format sarif and the enable sarif flag is set", |
| 64 | + fields: fields{ |
| 65 | + Repo: "github.com/oss/scorecard", |
| 66 | + Commit: "HEAD", |
| 67 | + Format: "sarif", |
| 68 | + EnableSarif: true, |
| 69 | + PolicyFile: "testdata/policy.yaml", |
| 70 | + }, |
| 71 | + wantErr: false, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "format sarif and the disabled but the policy file is set", |
| 75 | + fields: fields{ |
| 76 | + Repo: "github.com/oss/scorecard", |
| 77 | + Commit: "HEAD", |
| 78 | + PolicyFile: "testdata/policy.yaml", |
| 79 | + }, |
| 80 | + wantErr: true, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "format raw is not supported when V6 is not enabled", |
| 84 | + fields: fields{ |
| 85 | + Repo: "github.com/oss/scorecard", |
| 86 | + Commit: "HEAD", |
| 87 | + Format: "raw", |
| 88 | + }, |
| 89 | + wantErr: true, |
| 90 | + }, |
| 91 | + } |
| 92 | + for _, tt := range tests { |
| 93 | + tt := tt |
| 94 | + t.Run(tt.name, func(t *testing.T) { |
| 95 | + o := &Options{ |
| 96 | + Repo: tt.fields.Repo, |
| 97 | + Local: tt.fields.Local, |
| 98 | + Commit: tt.fields.Commit, |
| 99 | + LogLevel: tt.fields.LogLevel, |
| 100 | + Format: tt.fields.Format, |
| 101 | + NPM: tt.fields.NPM, |
| 102 | + PyPI: tt.fields.PyPI, |
| 103 | + RubyGems: tt.fields.RubyGems, |
| 104 | + PolicyFile: tt.fields.PolicyFile, |
| 105 | + ResultsFile: tt.fields.ResultsFile, |
| 106 | + ChecksToRun: tt.fields.ChecksToRun, |
| 107 | + Metadata: tt.fields.Metadata, |
| 108 | + ShowDetails: tt.fields.ShowDetails, |
| 109 | + PublishResults: tt.fields.PublishResults, |
| 110 | + EnableSarif: tt.fields.EnableSarif, |
| 111 | + EnableScorecardV5: tt.fields.EnableScorecardV5, |
| 112 | + EnableScorecardV6: tt.fields.EnableScorecardV6, |
| 113 | + } |
| 114 | + if o.EnableSarif { |
| 115 | + os.Setenv(EnvVarEnableSarif, "1") |
| 116 | + defer os.Unsetenv(EnvVarEnableSarif) |
| 117 | + } |
| 118 | + |
| 119 | + if err := o.Validate(); (err != nil) != tt.wantErr { |
| 120 | + t.Errorf("Options.Validate() error = %v, wantErr %v", err, tt.wantErr) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments