Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yottahmd committed May 6, 2022
1 parent a76b2ce commit 455f9d4
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
3 changes: 0 additions & 3 deletions internal/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ func (cl *Loader) loadGlobalConfig(file string) (*Config, error) {
return nil, err
}

if def.Env == nil {
def.Env = map[string]string{}
}
for k, v := range utils.DefaultEnv() {
if _, ok := def.Env[v]; !ok {
def.Env[k] = v
Expand Down
16 changes: 16 additions & 0 deletions internal/config/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ func TestLoadGlobalConfig(t *testing.T) {
assert.Equal(t, want, cfg)
}

func TestLoadGlobalConfigError(t *testing.T) {
for _, path := range []string{
path.Join(testDir, "config_err_decode.yaml"),
path.Join(testDir, "config_err_parse.yaml"),
} {
l := &Loader{HomeDir: utils.MustGetUserHomeDir()}
_, err := l.loadGlobalConfig(path)
require.Error(t, err)
}
}

func TestLoadDeafult(t *testing.T) {
l := &Loader{
HomeDir: utils.MustGetUserHomeDir(),
Expand Down Expand Up @@ -199,6 +210,11 @@ steps:
require.Equal(t, step.Name, "1")
require.Equal(t, step.Command, "true")

// error
dat = `invalidyaml`
_, err = l.LoadData([]byte(dat))
require.Error(t, err)

// error
dat = `invalidkey: test DAG`
_, err = l.LoadData([]byte(dat))
Expand Down
2 changes: 1 addition & 1 deletion internal/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestSchedulerCancel(t *testing.T) {

g, _ := scheduler.NewExecutionGraph(
step("1", testCommand),
step("2", "sleep 60", "1"),
step("2", "sleep 1000", "1"),
step("3", testCommandFail, "2"),
)
sc := scheduler.New(&scheduler.Config{
Expand Down
22 changes: 8 additions & 14 deletions internal/sock/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/http"
"os"
"strings"

"github.com/yohamta/dagu/internal/utils"
)

type Server struct {
Expand Down Expand Up @@ -50,7 +52,7 @@ func (svr *Server) Serve(listen chan error) error {
}
log.Printf("server is running at \"%v\"\n", svr.Addr)
defer func() {
svr.listener.Close()
svr.Shutdown()
os.Remove(svr.Addr)
}()
for {
Expand All @@ -61,16 +63,11 @@ func (svr *Server) Serve(listen chan error) error {
if err == nil {
go func() {
request, err := http.ReadRequest(bufio.NewReader(conn))
if err != nil {
log.Printf("Failed to read request %v", err)
return
utils.LogIgnoreErr("read request", err)
if err == nil {
svr.HandlerFunc(NewHttpResponseWriter(&conn), request)
}
svr.HandlerFunc(NewHttpResponseWriter(&conn), request)
conn.Close()

if svr.quit {
svr.Shutdown()
}
}()
}
}
Expand All @@ -79,11 +76,8 @@ func (svr *Server) Serve(listen chan error) error {
func (svr *Server) Shutdown() {
if !svr.quit {
svr.quit = true
if svr.listener != nil {
if err := svr.listener.Close(); err != nil {
log.Printf("failed to close listener: %s", err)
}
}
err := svr.listener.Close()
utils.LogIgnoreErr("close listener", err)
}
}

Expand Down
11 changes: 5 additions & 6 deletions internal/sock/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ import (
"io/ioutil"
"net/http"
"os"
"path"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/yohamta/dagu/internal/sock"
"github.com/yohamta/dagu/internal/utils"
)

var (
testsDir = path.Join(utils.MustGetwd(), "../../tests/testdata")
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -68,3 +62,8 @@ func TestStartAndShutdownServer(t *testing.T) {
_, err = client.Request(http.MethodPost, "/")
assert.True(t, errors.Is(err, sock.ErrFileNotExist))
}

func TestResponseWriter(t *testing.T) {
w := sock.NewHttpResponseWriter(nil)
require.Equal(t, make(http.Header), w.Header())
}
1 change: 1 addition & 0 deletions tests/testdata/config_err_parse.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalidyaml

0 comments on commit 455f9d4

Please sign in to comment.