From ec3287e08dd5b90028e44c0a12f193214ca2a02c Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Fri, 5 Oct 2018 21:31:51 -0700 Subject: [PATCH] testing coverage - notifiers - removed old --- core/notifier/notifiers.go | 4 ++-- source/source.go | 1 + utils/encryption.go | 9 ------- utils/log.go | 35 +++++++++++++++++++++++---- utils/logRow.go | 49 -------------------------------------- utils/utils_test.go | 30 +++++++++++++++++++---- 6 files changed, 59 insertions(+), 69 deletions(-) delete mode 100644 utils/logRow.go diff --git a/core/notifier/notifiers.go b/core/notifier/notifiers.go index ca8518ac..5aa590d8 100644 --- a/core/notifier/notifiers.go +++ b/core/notifier/notifiers.go @@ -380,8 +380,8 @@ func isEnabled(n interface{}) bool { } // inLimits will return true if the notifier is within sending limits -func inLimits(n Notifier) bool { - notifier := n.Select() +func inLimits(n interface{}) bool { + notifier := n.(Notifier).Select() ok, _ := notifier.WithinLimits() return ok } diff --git a/source/source.go b/source/source.go index aa8852f7..ab7ff499 100644 --- a/source/source.go +++ b/source/source.go @@ -42,6 +42,7 @@ func Assets() { TmplBox = rice.MustFindBox("tmpl") } +// HelpMarkdown will return the Markdown of help.md into HTML func HelpMarkdown() string { helpSrc, err := TmplBox.Bytes("help.md") if err != nil { diff --git a/utils/encryption.go b/utils/encryption.go index a197222c..18ccd370 100644 --- a/utils/encryption.go +++ b/utils/encryption.go @@ -17,7 +17,6 @@ package utils import ( "crypto/sha1" - "encoding/hex" "fmt" "golang.org/x/crypto/bcrypt" "math/rand" @@ -52,11 +51,3 @@ func RandomString(n int) string { } return string(b) } - -// Sha256 returns a SHA256 hash as string from []byte -func Sha256(data []byte) string { - h := sha1.New() - h.Write(data) - sha1_hash := hex.EncodeToString(h.Sum(nil)) - return sha1_hash -} diff --git a/utils/log.go b/utils/log.go index 0fa11e1d..916a6711 100644 --- a/utils/log.go +++ b/utils/log.go @@ -24,6 +24,7 @@ import ( "os/signal" "sync" "syscall" + "time" ) var ( @@ -34,10 +35,6 @@ var ( LockLines sync.Mutex ) -func Logger() *lumberjack.Logger { - return ljLogger -} - // createLog will create the '/logs' directory based on a directory func createLog(dir string) error { var err error @@ -137,7 +134,7 @@ func Http(r *http.Request) string { func pushLastLine(line interface{}) { LockLines.Lock() defer LockLines.Unlock() - LastLines = append(LastLines, NewLogRow(line)) + LastLines = append(LastLines, newLogRow(line)) // We want to store max 1000 lines in memory (for /logs page). for len(LastLines) > 1000 { LastLines = LastLines[1:] @@ -153,3 +150,31 @@ func GetLastLine() *LogRow { } return nil } + +type LogRow struct { + Date time.Time + Line interface{} +} + +func newLogRow(line interface{}) (logRow *LogRow) { + logRow = new(LogRow) + logRow.Date = time.Now() + logRow.Line = line + return +} + +func (o *LogRow) lineAsString() string { + switch v := o.Line.(type) { + case string: + return v + case error: + return v.Error() + case []byte: + return string(v) + } + return "" +} + +func (o *LogRow) FormatForHtml() string { + return fmt.Sprintf("%s: %s", o.Date.Format("2006-01-02 15:04:05"), o.lineAsString()) +} diff --git a/utils/logRow.go b/utils/logRow.go deleted file mode 100644 index 08d641fa..00000000 --- a/utils/logRow.go +++ /dev/null @@ -1,49 +0,0 @@ -// Statup -// Copyright (C) 2018. Hunter Long and the project contributors -// Written by Hunter Long and the project contributors -// -// https://github.com/hunterlong/statup -// -// The licenses for most software and other practical works are designed -// to take away your freedom to share and change the works. By contrast, -// the GNU General Public License is intended to guarantee your freedom to -// share and change all versions of a program--to make sure it remains free -// software for all its users. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -package utils - -import ( - "fmt" - "time" -) - -type LogRow struct { - Date time.Time - Line interface{} -} - -func NewLogRow(line interface{}) (logRow *LogRow) { - logRow = new(LogRow) - logRow.Date = time.Now() - logRow.Line = line - return -} - -func (o *LogRow) LineAsString() string { - switch v := o.Line.(type) { - case string: - return v - case error: - return v.Error() - case []byte: - return string(v) - } - return "" -} - -func (o *LogRow) FormatForHtml() string { - return fmt.Sprintf("%s: %s", o.Date.Format("2006-01-02 15:04:05"), o.LineAsString()) -} diff --git a/utils/utils_test.go b/utils/utils_test.go index 6cc2fc87..a349d962 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -33,10 +33,27 @@ func TestInitLogs(t *testing.T) { assert.FileExists(t, "../logs/statup.log") } +func TestFileExists(t *testing.T) { + assert.True(t, FileExists("../logs/statup.log")) +} + func TestDir(t *testing.T) { assert.Contains(t, Directory, "github.com/hunterlong/statup") } +func TestCommand(t *testing.T) { + in, out, err := Command("pwd") + assert.Nil(t, err) + assert.Contains(t, in, "statup") + assert.Empty(t, out) +} + +func TestDurationReadable(t *testing.T) { + dur, _ := time.ParseDuration("1505s") + readable := DurationReadable(dur) + assert.Equal(t, "25 minutes", readable) +} + func TestLog(t *testing.T) { assert.Nil(t, Log(0, errors.New("this is a 0 level error"))) assert.Nil(t, Log(1, errors.New("this is a 1 level error"))) @@ -46,6 +63,15 @@ func TestLog(t *testing.T) { assert.Nil(t, Log(5, errors.New("this is a 5 level error"))) } +func TestFormatDuration(t *testing.T) { + dur, _ := time.ParseDuration("158s") + formatted := FormatDuration(dur) + assert.Equal(t, "3 minutes", formatted) + dur, _ = time.ParseDuration("-65s") + formatted = FormatDuration(dur) + assert.Equal(t, "1 minute", formatted) +} + func TestDeleteFile(t *testing.T) { assert.Nil(t, DeleteFile(Directory+"/logs/statup.log")) } @@ -102,10 +128,6 @@ func TestRandomString(t *testing.T) { assert.NotEmpty(t, RandomString(5)) } -func TestSha256(t *testing.T) { - assert.Equal(t, "dc724af18fbdd4e59189f5fe768a5f8311527050", Sha256([]byte("testing"))) -} - func TestDeleteDirectory(t *testing.T) { assert.Nil(t, DeleteDirectory(Directory+"/logs")) }