Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default datastore.github.path: #4

Merged
merged 4 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
linters-settings:
staticcheck:
go: 1.16
issues:
exclude:
- SA3000
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

const defaultBranch = "main"
const defaultReportDir = "report"
const defaultReportDir = "reports"

var DefaultConfigFilePaths = []string{".octocov.yml", "octocov.yml"}

Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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))
Expand Down
134 changes: 134 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package config

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/k1LoW/octocov/pkg/coverage"
"github.com/k1LoW/octocov/report"
)

func TestMain(m *testing.M) {
envCache := os.Environ()

m.Run()

if err := revertEnv(envCache); err != nil {
panic(err)
}
}

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)
}
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 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
}
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
}

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
}
6 changes: 3 additions & 3 deletions docs/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions testdata/config/.octocov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage:
acceptable: 75%
badge: docs/coverage.svg