Skip to content

Commit

Permalink
Allow options tests to pass in GitHub Actions environments
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 1f478f4 commit 1c1e257
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
3 changes: 3 additions & 0 deletions options/options.go
Expand Up @@ -100,6 +100,9 @@ func New() (*Options, error) {
}
}

// TODO(scorecard): Reset commit options. Fix this in scorecard.
opts.ScorecardOpts.Commit = options.DefaultCommit

if err := opts.ScorecardOpts.Validate(); err != nil {
return opts, fmt.Errorf("validating scorecard options: %w", err)
}
Expand Down
54 changes: 37 additions & 17 deletions options/options_test.go
Expand Up @@ -36,14 +36,22 @@ const (
)

func TestNew(t *testing.T) {
type fields struct {
EnableSarif bool
Format string
PolicyFile string
ResultsFile string
Commit string
LogLevel string
}
tests := []struct {
name string
githubEventPath string
repo string
resultsFile string
resultsFormat string
publishResults string
want options.Options
want fields
wantErr bool
}{
{
Expand All @@ -52,8 +60,7 @@ func TestNew(t *testing.T) {
repo: testRepo,
resultsFormat: "sarif",
resultsFile: testResultsFile,
want: options.Options{
Repo: testRepo,
want: fields{
EnableSarif: true,
Format: formatSarif,
PolicyFile: defaultScorecardPolicyFile,
Expand All @@ -69,8 +76,7 @@ func TestNew(t *testing.T) {
repo: testRepo,
resultsFormat: "json",
resultsFile: testResultsFile,
want: options.Options{
Repo: testRepo,
want: fields{
EnableSarif: true,
Format: options.FormatJSON,
ResultsFile: testResultsFile,
Expand All @@ -79,22 +85,23 @@ func TestNew(t *testing.T) {
},
wantErr: false,
},
/*
{
name: "FailureNoEnvVars",
want: options.Options{},
wantErr: true,
},
*/
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.githubEventPath != "" {
os.Setenv(EnvGithubEventPath, tt.githubEventPath)
_, pathEnvExists := os.LookupEnv(EnvGithubEventPath)
if !pathEnvExists {
if tt.githubEventPath != "" {
os.Setenv(EnvGithubEventPath, tt.githubEventPath)
}
}
if tt.repo != "" {
os.Setenv(EnvGithubRepository, tt.repo)

_, repoEnvExists := os.LookupEnv(EnvGithubRepository)
if !repoEnvExists {
if tt.repo != "" {
os.Setenv(EnvGithubRepository, tt.repo)
}
}

if tt.resultsFile != "" {
os.Setenv("SCORECARD_RESULTS_FILE", tt.resultsFile)
}
Expand All @@ -103,11 +110,24 @@ func TestNew(t *testing.T) {
}

opts, err := New()
got := *opts.ScorecardOpts
scOpts := *opts.ScorecardOpts
got := fields{
EnableSarif: scOpts.EnableSarif,
Format: scOpts.Format,
PolicyFile: scOpts.PolicyFile,
ResultsFile: scOpts.ResultsFile,
Commit: scOpts.Commit,
LogLevel: scOpts.LogLevel,
}

if (err != nil) != tt.wantErr {
for _, e := range os.Environ() {
t.Logf(e)
}
t.Errorf("New() error = %+v, wantErr %+v", err, tt.wantErr)
return
}

if !cmp.Equal(tt.want, got) {
t.Errorf("New(): -want, +got:\n%s", cmp.Diff(tt.want, got))
}
Expand Down

0 comments on commit 1c1e257

Please sign in to comment.