From c9021709a246ec570d2d03d57759afc7ca988256 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Wed, 5 May 2021 07:34:03 +0900 Subject: [PATCH 1/4] Fix default datastore.github.path: --- README.md | 2 +- config/config.go | 2 +- config/config_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 config/config_test.go diff --git a/README.md b/README.md index 817b6410..fac3953c 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ datastore: github: repository: owner/repo # datastore repository branch: main # default: main - path: # default: report/${GITHUB_REPOSITORY}.json + path: # default: reports/${GITHUB_REPOSITORY}.json ``` #### S3 diff --git a/config/config.go b/config/config.go index c91692e4..edefd4f9 100644 --- a/config/config.go +++ b/config/config.go @@ -14,7 +14,7 @@ import ( ) const defaultBranch = "main" -const defaultReportDir = "report" +const defaultReportDir = "reports" var DefaultConfigFilePaths = []string{".octocov.yml", "octocov.yml"} diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 00000000..23545d09 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,61 @@ +package config + +import ( + "os" + "strings" + "testing" +) + +func TestMain(m *testing.M) { + envCache := os.Environ() + + m.Run() + + if err := revertEnv(envCache); err != nil { + panic(err) + } +} + +func TestDatasourceGithubPath(t *testing.T) { + if err := clearEnv(); err != nil { + t.Fatal(err) + } + os.Setenv("GITHUB_REPOSITORY", "foo/bar") + + c := New() + c.Datastore.Github.Repository = "report/dest" + c.Build() + if got := c.DatastoreConfigReady(); got != true { + t.Errorf("got %v\nwant %v", got, true) + } + if err := c.BuildDatastoreConfig(); err != nil { + t.Fatal(err) + } + want := "reports/foo/bar.json" + if got := c.Datastore.Github.Path; got != want { + t.Errorf("got %v\nwant %v", got, want) + } +} + +func revertEnv(envCache []string) error { + if err := clearEnv(); err != nil { + return err + } + for _, e := range envCache { + splitted := strings.Split(e, "=") + if err := os.Setenv(splitted[0], splitted[1]); err != nil { + return err + } + } + return nil +} + +func clearEnv() error { + for _, e := range os.Environ() { + splitted := strings.Split(e, "=") + if err := os.Unsetenv(splitted[0]); err != nil { + return err + } + } + return nil +} From c095dd944568a573d948b1fff0882d737b1e3b5f Mon Sep 17 00:00:00 2001 From: k1LoW Date: Wed, 5 May 2021 07:42:30 +0900 Subject: [PATCH 2/4] Set .golangci.yml --- .golangci.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..76d34f2d --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,6 @@ +linters-settings: + staticcheck: + go: 1.16 +issues: + exclude: + - SA3000 From 9fbcd03f97e74f99efecfc9368bb7fb43d0ec32c Mon Sep 17 00:00:00 2001 From: k1LoW Date: Wed, 5 May 2021 10:04:18 +0900 Subject: [PATCH 3/4] Add tests --- config/config.go | 15 ++++---- config/config_test.go | 73 ++++++++++++++++++++++++++++++++++++ testdata/config/.octocov.yml | 3 ++ 3 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 testdata/config/.octocov.yml diff --git a/config/config.go b/config/config.go index edefd4f9..d0aedbdf 100644 --- a/config/config.go +++ b/config/config.go @@ -23,6 +23,8 @@ type Config struct { Coverage ConfigCoverage `yaml:"coverage,omitempty"` // CodeToTestRatio ConfigCodeToTestRatio `yaml:"codeToTestRatio,omitempty"` Datastore ConfigDatastore `yaml:"datastore,omitempty"` + + root string } type ConfigCoverage struct { @@ -47,13 +49,16 @@ type ConfigDatastoreGithub struct { } func New() *Config { - return &Config{} + p, _ := os.Getwd() + return &Config{ + root: p, + } } func (c *Config) Load(path string) error { if path == "" { for _, p := range DefaultConfigFilePaths { - if f, err := os.Stat(p); err == nil && !f.IsDir() { + if f, err := os.Stat(filepath.Join(c.root, p)); err == nil && !f.IsDir() { if path != "" { return fmt.Errorf("duplicate config file [%s, %s]", path, p) } @@ -62,11 +67,7 @@ func (c *Config) Load(path string) error { } } if path == "" { - p, err := os.Getwd() - if err != nil { - return err - } - c.Coverage.Path = p + c.Coverage.Path = c.root return nil } buf, err := ioutil.ReadFile(filepath.Clean(path)) diff --git a/config/config_test.go b/config/config_test.go index 23545d09..2067f41c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -2,8 +2,12 @@ package config import ( "os" + "path/filepath" "strings" "testing" + + "github.com/k1LoW/octocov/pkg/coverage" + "github.com/k1LoW/octocov/report" ) func TestMain(m *testing.M) { @@ -16,6 +20,31 @@ func TestMain(m *testing.M) { } } +func TestLoad(t *testing.T) { + tests := []struct { + root string + path string + wantErr bool + }{ + {testdataDir(t), "", false}, + {filepath.Join(testdataDir(t), "config"), "", false}, + {filepath.Join(testdataDir(t), "config"), "no.yml", true}, + } + for _, tt := range tests { + c := New() + c.root = tt.root + if err := c.Load(tt.path); err != nil { + if !tt.wantErr { + t.Errorf("got %v\nwantErr %v", err, tt.wantErr) + } + } else { + if tt.wantErr { + t.Errorf("got %v\nwantErr %v", nil, tt.wantErr) + } + } + } +} + func TestDatasourceGithubPath(t *testing.T) { if err := clearEnv(); err != nil { t.Fatal(err) @@ -37,6 +66,37 @@ func TestDatasourceGithubPath(t *testing.T) { } } +func TestAcceptable(t *testing.T) { + tests := []struct { + in string + wantErr bool + }{ + {"60%", true}, + {"50%", false}, + {"49.9%", false}, + } + for _, tt := range tests { + c := New() + c.Coverage.Acceptable = tt.in + c.Build() + + r := report.New() + r.Coverage = &coverage.Coverage{ + Covered: 50, + Total: 100, + } + if err := c.Accepptable(r); err != nil { + if !tt.wantErr { + t.Errorf("got %v\nwantErr %v", err, tt.wantErr) + } + } else { + if tt.wantErr { + t.Errorf("got %v\nwantErr %v", nil, tt.wantErr) + } + } + } +} + func revertEnv(envCache []string) error { if err := clearEnv(); err != nil { return err @@ -59,3 +119,16 @@ func clearEnv() error { } return nil } + +func testdataDir(t *testing.T) string { + t.Helper() + wd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + dir, err := filepath.Abs(filepath.Join(filepath.Dir(filepath.Dir(wd)), "testdata")) + if err != nil { + t.Fatal(err) + } + return dir +} diff --git a/testdata/config/.octocov.yml b/testdata/config/.octocov.yml new file mode 100644 index 00000000..42955627 --- /dev/null +++ b/testdata/config/.octocov.yml @@ -0,0 +1,3 @@ +coverage: + acceptable: 75% + badge: docs/coverage.svg From 6edd2e7243d7daefbb40c699e116204316a36f68 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Wed, 5 May 2021 01:05:36 +0000 Subject: [PATCH 4/4] Commit from GitHub Actions (build) --- docs/coverage.svg | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/coverage.svg b/docs/coverage.svg index 6f9100f1..fa132cfe 100644 --- a/docs/coverage.svg +++ b/docs/coverage.svg @@ -9,14 +9,14 @@ - + coverage - - 83.0% + + 79.9%