From 425a5694e4dddf4be3f464cbd9d6f1671e22e340 Mon Sep 17 00:00:00 2001 From: Stephen Augustus Date: Sun, 6 Mar 2022 22:44:45 -0500 Subject: [PATCH] Rewrite unit tests (3/n) Signed-off-by: Stephen Augustus --- options/options.go | 19 ++++++++------- options/options_test.go | 54 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/options/options.go b/options/options.go index 609ee87d..3fb3e20e 100644 --- a/options/options.go +++ b/options/options.go @@ -18,7 +18,6 @@ import ( "encoding/json" "errors" "fmt" - "io" "io/ioutil" "os" "strconv" @@ -112,7 +111,10 @@ func New() (*Options, error) { return opts, errResultsPathEmpty } - // TODO(options): Consider running Validate() before returning. + if err := opts.Validate(); err != nil { + return opts, fmt.Errorf("validating scorecard-action options: %w", err) + } + return opts, nil } @@ -135,15 +137,14 @@ func (o *Options) Initialize() error { } // Validate validates the scorecard configuration. -func (o *Options) Validate(writer io.Writer) error { +func (o *Options) Validate() error { if os.Getenv(EnvGithubAuthToken) == "" { - fmt.Fprintf(writer, "The 'repo_token' variable is empty.\n") + fmt.Printf("The 'repo_token' variable is empty.\n") if o.IsForkStr == trueStr { - fmt.Fprintf(writer, "We have detected you are running on a fork.\n") + fmt.Printf("We have detected you are running on a fork.\n") } - fmt.Fprintf( - writer, + fmt.Printf( "Please follow the instructions at https://github.com/ossf/scorecard-action#authentication to create the read-only PAT token.\n", //nolint:lll ) @@ -152,8 +153,8 @@ func (o *Options) Validate(writer io.Writer) error { if strings.Contains(os.Getenv(o.GithubEventName), "pull_request") && os.Getenv(o.GithubRef) == o.DefaultBranch { - fmt.Fprintf(writer, "%s not supported with %s event.\n", os.Getenv(o.GithubRef), os.Getenv(o.GithubEventName)) - fmt.Fprintf(writer, "Only the default branch %s is supported.\n", o.DefaultBranch) + fmt.Printf("%s not supported with %s event.\n", os.Getenv(o.GithubRef), os.Getenv(o.GithubEventName)) + fmt.Printf("Only the default branch %s is supported.\n", o.DefaultBranch) return errOnlyDefaultBranchSupported } diff --git a/options/options_test.go b/options/options_test.go index da20ab55..19253050 100644 --- a/options/options_test.go +++ b/options/options_test.go @@ -20,13 +20,13 @@ import ( "testing" "github.com/google/go-cmp/cmp" - "github.com/ossf/scorecard/v4/options" ) const ( testRepo = "good/repo" testResultsFile = "results.sarif" + testToken = "test-token" githubEventPathNonFork = "testdata/non-fork.json" githubEventPathFork = "testdata/fork.json" @@ -88,6 +88,11 @@ func TestNew(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + _, tokenEnvExists := os.LookupEnv(EnvGithubAuthToken) + if !tokenEnvExists { + os.Setenv(EnvGithubAuthToken, testToken) + } + _, pathEnvExists := os.LookupEnv(EnvGithubEventPath) if !pathEnvExists { if tt.githubEventPath != "" { @@ -203,3 +208,50 @@ func TestInitialize(t *testing.T) { }) } } + +func TestPrint(t *testing.T) { + type fields struct { + ScorecardOpts *options.Options + EnabledChecks string + EnableLicense string + EnableDangerousWorkflow string + GithubEventName string + GithubEventPath string + GithubRef string + GithubRepository string + GithubWorkspace string + DefaultBranch string + IsForkStr string + PrivateRepoStr string + } + tests := []struct { + name string + fields fields + }{ + { + name: "Success", + fields: fields{ + ScorecardOpts: options.New(), + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + o := &Options{ + ScorecardOpts: tt.fields.ScorecardOpts, + EnabledChecks: tt.fields.EnabledChecks, + EnableLicense: tt.fields.EnableLicense, + EnableDangerousWorkflow: tt.fields.EnableDangerousWorkflow, + GithubEventName: tt.fields.GithubEventName, + GithubEventPath: tt.fields.GithubEventPath, + GithubRef: tt.fields.GithubRef, + GithubRepository: tt.fields.GithubRepository, + GithubWorkspace: tt.fields.GithubWorkspace, + DefaultBranch: tt.fields.DefaultBranch, + IsForkStr: tt.fields.IsForkStr, + PrivateRepoStr: tt.fields.PrivateRepoStr, + } + o.Print() + }) + } +}