Skip to content

Commit

Permalink
test: add tests (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Nov 16, 2023
1 parent 029c125 commit e19ed22
Show file tree
Hide file tree
Showing 12 changed files with 538 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
dist
.coverage
ghalint.yaml
3 changes: 2 additions & 1 deletion cmd/ghalint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/signal"

"github.com/spf13/afero"
"github.com/suzuki-shunsuke/ghalint/pkg/cli"
)

Expand All @@ -27,6 +28,6 @@ func core() error {
Version: version,
Commit: commit,
Date: date,
})
}, afero.NewOsFs())
return app.RunContext(ctx, os.Args) //nolint:wrapcheck
}
7 changes: 7 additions & 0 deletions cmdx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ tasks:
description: test
usage: test
script: go test ./... -race -covermode=atomic
- name: coverage
short: c
description: coverage test
usage: coverage test
script: "bash scripts/coverage.sh {{.target}}"
args:
- name: target
- name: vet
short: v
description: go vet
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/mattn/go-colorable v0.1.13
github.com/sirupsen/logrus v1.9.3
github.com/spf13/afero v1.10.0
github.com/suzuki-shunsuke/logrus-error v0.1.4
github.com/urfave/cli/v2 v2.25.7
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -17,4 +18,5 @@ require (
github.com/stretchr/testify v1.8.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.3.7 // indirect
)
435 changes: 434 additions & 1 deletion go.sum

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion pkg/cli/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"github.com/spf13/afero"
"github.com/urfave/cli/v2"
)

Expand All @@ -16,9 +17,10 @@ func (f *LDFlags) AppVersion() string {

type Runner struct {
flags *LDFlags
fs afero.Fs
}

func New(flags *LDFlags) *cli.App {
func New(flags *LDFlags, fs afero.Fs) *cli.App {
app := cli.NewApp()
app.Name = "ghalint"
app.Usage = "GitHub Actions linter"
Expand All @@ -28,6 +30,7 @@ func New(flags *LDFlags) *cli.App {
}
runner := &Runner{
flags: flags,
fs: fs,
}
app.Commands = []*cli.Command{
{
Expand Down
10 changes: 5 additions & 5 deletions pkg/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cli
import (
"errors"
"fmt"
"os"

"github.com/spf13/afero"
"gopkg.in/yaml.v3"
)

Expand All @@ -19,17 +19,17 @@ type Exclude struct {
ActionName string `yaml:"action_name"`
}

func findConfig() string {
func findConfig(fs afero.Fs) string {
for _, filePath := range []string{"ghalint.yaml", ".ghalint.yaml", "ghalint.yml", ".ghalint.yml"} {
if _, err := os.Stat(filePath); err == nil {
if _, err := fs.Stat(filePath); err == nil {
return filePath
}
}
return ""
}

func readConfig(cfg *Config, filePath string) error {
f, err := os.Open(filePath)
func readConfig(fs afero.Fs, cfg *Config, filePath string) error {
f, err := fs.Open(filePath)
if err != nil {
return fmt.Errorf("open a configuration file: %w", err)
}
Expand Down
41 changes: 41 additions & 0 deletions pkg/cli/job_secrets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cli_test

import (
"testing"

"github.com/suzuki-shunsuke/ghalint/pkg/cli"
"gopkg.in/yaml.v3"
)

func TestJobSecrets_UnmarshalYAML(t *testing.T) {
t.Parallel()
data := []struct {
name string
yaml string
inherit bool
}{
{
name: "not inherit",
yaml: `token: ${{github.token}}`,
},
{
name: "inherit",
yaml: `inherit`,
inherit: true,
},
}
for _, d := range data {
d := d
t.Run(d.name, func(t *testing.T) {
t.Parallel()
js := &cli.JobSecrets{}
if err := yaml.Unmarshal([]byte(d.yaml), js); err != nil {
t.Fatal(err)
}
inherit := js.Inherit()
if d.inherit != inherit {
t.Fatalf("got %v, wanted %v", inherit, d.inherit)
}
})
}
}
9 changes: 5 additions & 4 deletions pkg/cli/list_workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package cli

import (
"fmt"
"path/filepath"

"github.com/spf13/afero"
)

func listWorkflows() ([]string, error) {
files, err := filepath.Glob(".github/workflows/*.yml")
func listWorkflows(fs afero.Fs) ([]string, error) {
files, err := afero.Glob(fs, ".github/workflows/*.yml")
if err != nil {
return nil, fmt.Errorf("find .github/workflows/*.yml: %w", err)
}
files2, err := filepath.Glob(".github/workflows/*.yaml")
files2, err := afero.Glob(fs, ".github/workflows/*.yaml")
if err != nil {
return nil, fmt.Errorf("find .github/workflows/*.yaml: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cli/read_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package cli

import (
"fmt"
"os"

"github.com/spf13/afero"
"gopkg.in/yaml.v3"
)

func readWorkflow(p string, wf *Workflow) error {
f, err := os.Open(p)
func readWorkflow(fs afero.Fs, p string, wf *Workflow) error {
f, err := fs.Open(p)
if err != nil {
return fmt.Errorf("open a workflow file: %w", err)
}
Expand Down
16 changes: 10 additions & 6 deletions pkg/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ func (r *Runner) Run(ctx *cli.Context) error {
log.SetColor(color, logE)
}

return r.run(ctx.Context, logE)
}

func (r *Runner) run(ctx context.Context, logE *logrus.Entry) error {
cfg := &Config{}
if cfgFilePath := findConfig(); cfgFilePath != "" {
if err := readConfig(cfg, cfgFilePath); err != nil {
if cfgFilePath := findConfig(r.fs); cfgFilePath != "" {
if err := readConfig(r.fs, cfg, cfgFilePath); err != nil {
logE.WithError(err).Error("read a configuration file")
return err
}
Expand All @@ -29,7 +33,7 @@ func (r *Runner) Run(ctx *cli.Context) error {
logE.WithError(err).Error("validate a configuration file")
return err
}
filePaths, err := listWorkflows()
filePaths, err := listWorkflows(r.fs)
if err != nil {
logE.Error(err)
return err
Expand Down Expand Up @@ -57,19 +61,19 @@ func (r *Runner) Run(ctx *cli.Context) error {
return nil
}

func (r *Runner) validateWorkflow(ctx *cli.Context, logE *logrus.Entry, cfg *Config, policies []Policy, filePath string) bool {
func (r *Runner) validateWorkflow(ctx context.Context, logE *logrus.Entry, cfg *Config, policies []Policy, filePath string) bool {
wf := &Workflow{
FilePath: filePath,
}
if err := readWorkflow(filePath, wf); err != nil {
if err := readWorkflow(r.fs, filePath, wf); err != nil {
logerr.WithError(logE, err).Error("read a workflow file")
return true
}

failed := false
for _, policy := range policies {
logE := logE.WithField("policy_name", policy.Name())
if err := policy.Apply(ctx.Context, logE, cfg, wf); err != nil {
if err := policy.Apply(ctx, logE, cfg, wf); err != nil {
failed = true
continue
}
Expand Down
23 changes: 23 additions & 0 deletions scripts/coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

set -eu
set -o pipefail

cd "$(dirname "$0")/.."

if [ $# -eq 0 ]; then
target="$(go list ./... | fzf)"
profile=.coverage/$target/coverage.txt
mkdir -p .coverage/"$target"
elif [ $# -eq 1 ]; then
target=$1
mkdir -p .coverage/"$target"
profile=.coverage/$target/coverage.txt
target=./$target
else
echo "too many arguments are given: $*" >&2
exit 1
fi

go test "$target" -coverprofile="$profile" -covermode=atomic
go tool cover -html="$profile"

0 comments on commit e19ed22

Please sign in to comment.