Skip to content

Commit

Permalink
Rewrite unit tests (3/n)
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <foo@auggie.dev>
  • Loading branch information
justaugustus committed Mar 7, 2022
1 parent 1c1e257 commit 425a569
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
19 changes: 10 additions & 9 deletions options/options.go
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
Expand Down Expand Up @@ -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
}

Expand All @@ -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
)

Expand All @@ -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
}
Expand Down
54 changes: 53 additions & 1 deletion options/options_test.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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()
})
}
}

0 comments on commit 425a569

Please sign in to comment.