From 227bd13692380c00e4669084d1f67de72d4f0041 Mon Sep 17 00:00:00 2001 From: Jitendra Adhikari <2908547+adhocore@users.noreply.github.com> Date: Tue, 8 Nov 2022 20:36:19 +0700 Subject: [PATCH] test: test cmd/tasker fully --- cmd/tasker/main_test.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/cmd/tasker/main_test.go b/cmd/tasker/main_test.go index 7209d62..b88ff67 100644 --- a/cmd/tasker/main_test.go +++ b/cmd/tasker/main_test.go @@ -3,22 +3,45 @@ package main import ( "os" "testing" + "time" "github.com/adhocore/gronx/pkg/tasker" ) func TestMustGetOption(t *testing.T) { old := os.Args + exit = func (code int) {} t.Run("Main", func(t *testing.T) { expect := tasker.Option{File: "../../test/taskfile.txt", Out: "../../test/out.txt"} - os.Args = append(os.Args, "-verbose", "-file", expect.File, "-out", expect.Out) - opt := mustGetOption() - os.Args = old + os.Args = append(old, "-verbose", "-file", expect.File, "-out", expect.Out) + mustParseOption() if opt.File != expect.File { t.Errorf("file: expected %v, got %v", opt.File, expect.File) } if opt.Out != expect.Out { t.Errorf("out: expected %v, got %v", opt.Out, expect.Out) } + + t.Run("must parse option", func (t *testing.T) { + os.Args = append(old, "-verbose", "-out", expect.Out) + mustParseOption() + if opt.File != "" { + t.Error("opt.File must be empty "+opt.File) + } + + os.Args = append(old, "-verbose", "-file", "invalid", "-out", expect.Out) + mustParseOption() + if opt.File != "invalid" { + t.Error("opt.File must be invalid") + } + }) + + t.Run("run", func (t *testing.T) { + tick = time.Second + os.Args = append(old, "-verbose", "-file", expect.File, "-out", expect.Out, "-until", "2") + main() + }) + + os.Args = old }) }