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

add lint on Github action #77

Merged
merged 5 commits into from
Jul 11, 2022
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
7 changes: 5 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
steps:

- name: Set up Go 1.x
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: ^1.18

- name: Check out code into the Go module directory
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Get dependencies
run: |
Expand All @@ -32,5 +32,8 @@ jobs:
- name: Build
run: go build -v -o larry-build ./cmd/larry/.

- name: Run golangci-lint
uses: golangci/[email protected]

- name: Test
run: go test -v ./...
32 changes: 25 additions & 7 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ func TestGetKey(t *testing.T) {
t.Error("No value expected for key not saved in cache")
}

r.Set("key", "v", 1)
err := r.Set("key", "v", 1)
if err != nil {
t.Error("could not set key")
}

_, e = r.Get("key")

Expand All @@ -40,7 +43,10 @@ func TestSetKey(t *testing.T) {

r := NewClient(ro)

r.Set("key", "v", 1)
err := r.Set("key", "v", 1)
if err != nil {
t.Error("could not set key")
}

v, e := r.Get("key")

Expand All @@ -61,9 +67,15 @@ func TestDetKey(t *testing.T) {

r := NewClient(ro)

r.Set("key", "v", 1)
err := r.Set("key", "v", 1)
if err != nil {
t.Error("could not set key")
}

r.Del("key")
err = r.Del("key")
if err != nil {
t.Error("could not del key")
}

_, e := r.Get("key")

Expand All @@ -80,17 +92,23 @@ func TestScanKey(t *testing.T) {

r := NewClient(ro)

r.Set("key", "v", 1)
err := r.Set("key", "v", 1)
if err != nil {
t.Error("could not set key")
}

err := r.Scan("key", func(ctx context.Context, key string) error {
err = r.Scan("key", func(ctx context.Context, key string) error {
return errors.New("some error")
})

if err == nil {
t.Error("expected error but got none")
}

r.Set("key", "v", 1)
err = r.Set("key", "v", 1)
if err != nil {
t.Error("could not set key")
}

err = r.Scan("key", func(ctx context.Context, key string) error {
return nil
Expand Down
9 changes: 7 additions & 2 deletions larry/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@ import (
type Service struct {
Publishers map[string]Publisher
Provider Provider
Logger log.Logger
Logger *log.Logger
}

// Run executes the application
func (s Service) Run() error {
content, err := s.Provider.GetContentToPublish()
if err != nil {
s.Logger.Println("error on getting content for publishing", err)
return err
}

if content != nil {
for _, pub := range s.Publishers {
pub.PublishContent(content)
_, err := pub.PublishContent(content)
if err != nil {
s.Logger.Println("error on publishing", err)
return err
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions larry/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package larry

import (
"errors"
"log"
"testing"

"github.com/ezeoleaf/larry/domain"
Expand Down Expand Up @@ -49,6 +50,7 @@ func TestRun(t *testing.T) {
s := Service{
Provider: tc.mockProvider,
Publishers: tc.mockPublishers,
Logger: log.Default(),
}

resp := s.Run()
Expand Down
12 changes: 9 additions & 3 deletions provider/contentfile/csvfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,16 @@ title-1,subtitle-1,url-1,extradata-1-1,extradata-1-2
cc := cache.NewClient(ro)

for _, item := range tc.CachedItems {
cc.Set(item, "1", 0)
err := cc.Set(item, "1", 0)
if err != nil {
t.Error("could not set key")
}
}
for _, item := range tc.BlacklistedItems {
cc.Set("blacklist-"+item, "1", 0)
err := cc.Set("blacklist-"+item, "1", 0)
if err != nil {
t.Error("could not set key")
}
}

cfg := config.Config{SkipCsvHeader: tc.SkipHeader, ContentFile: "./test.csv"}
Expand All @@ -179,7 +185,7 @@ title-1,subtitle-1,url-1,extradata-1-1,extradata-1-2
} else if content != nil && tc.ExpectedContent == nil {
t.Errorf("expected nil as value, got %v instead", *content.Title)
} else if *content.Title != *tc.ExpectedContent.Title {
t.Errorf("expected %v as value, got %v instead", *&tc.ExpectedContent.Title, *content.Title)
t.Errorf("expected %v as value, got %v instead", tc.ExpectedContent.Title, *content.Title)
} else {
// compare returned object
expected, _ := json.Marshal(tc.ExpectedContent)
Expand Down
12 changes: 9 additions & 3 deletions provider/contentfile/jsonfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ func TestGetJsonContentFromReader(t *testing.T) {
cc := cache.NewClient(ro)

for _, item := range tc.CachedItems {
cc.Set(item, "1", 0)
err := cc.Set(item, "1", 0)
if err != nil {
t.Error("could not set key")
}
}
for _, item := range tc.BlacklistedItems {
cc.Set("blacklist-"+item, "1", 0)
err := cc.Set("blacklist-"+item, "1", 0)
if err != nil {
t.Error("could not set key")
}
}

cfg := config.Config{ContentFile: "./test.json"}
Expand All @@ -104,7 +110,7 @@ func TestGetJsonContentFromReader(t *testing.T) {
} else if content != nil && tc.ExpectedContent == nil {
t.Errorf("expected nil as value, got %v instead", *content.Title)
} else if *content.Title != *tc.ExpectedContent.Title {
t.Errorf("expected %v as value, got %v instead", *&tc.ExpectedContent.Title, *content.Title)
t.Errorf("expected %v as value, got %v instead", tc.ExpectedContent.Title, *content.Title)
} else {
// compare returned object
expected, _ := json.Marshal(tc.ExpectedContent)
Expand Down
16 changes: 8 additions & 8 deletions provider/contentfile/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ func (p Provider) getContentFromFile(fileName string) (*domain.Content, error) {
if content, err := p.FileReader.getContentFromReader(f, p.skipCachedRecord); err != nil {
return nil, err
} else {
p.addToCache(*content.Title)
err := p.addToCache(*content.Title)
if err != nil {
return nil, err
}
return content, nil
}
}

return nil, fmt.Errorf("No content file specified")
return nil, fmt.Errorf("no content file specified")
}

func StringToPointer(in string) *string {
Expand All @@ -88,10 +91,7 @@ func (p Provider) skipCachedRecord(title string) bool {
func (p Provider) isCached(title string) bool {
key := cacheKey(p.Config.GetCacheKeyPrefix(), title)
_, err := p.CacheClient.Get(key)
if err != redis.Nil {
return true
}
return false
return err != redis.Nil
}

func (p Provider) isBlacklisted(title string) bool {
Expand All @@ -113,7 +113,7 @@ func cacheKey(cacheKeyPrefix string, title string) string {
return cacheKeyPrefix + title
}

func (p Provider) addToCache(title string) {
func (p Provider) addToCache(title string) error {
key := cacheKey(p.Config.GetCacheKeyPrefix(), title)
p.CacheClient.Set(key, true, p.cacheExpirationMinutes())
return p.CacheClient.Set(key, true, p.cacheExpirationMinutes())
}
12 changes: 9 additions & 3 deletions provider/contentfile/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,16 @@ func TestGetContentFromFile(t *testing.T) {
cc := cache.NewClient(ro)

for _, item := range tc.CachedItems {
cc.Set(item, "1", 0)
err := cc.Set(item, "1", 0)
if err != nil {
t.Error("could not set key")
}
}
for _, item := range tc.BlacklistedItems {
cc.Set("blacklist-"+item, "1", 0)
err := cc.Set("blacklist-"+item, "1", 0)
if err != nil {
t.Error("could not set key")
}
}

cfg := config.Config{ContentFile: fmt.Sprintf("./testdata/%s", tc.ContentFile)}
Expand All @@ -100,7 +106,7 @@ func TestGetContentFromFile(t *testing.T) {
} else if content != nil && tc.ExpectedContent == nil {
t.Errorf("expected nil as value, got %v instead", *content.Title)
} else if *content.Title != *tc.ExpectedContent.Title {
t.Errorf("expected %v as value, got %v instead", *&tc.ExpectedContent.Title, *content.Title)
t.Errorf("expected %v as value, got %v instead", tc.ExpectedContent.Title, *content.Title)
} else {
// compare returned object
expected, _ := json.Marshal(tc.ExpectedContent)
Expand Down