Skip to content

Commit

Permalink
Improve test coverage and API
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Oct 6, 2019
1 parent 4dee21b commit f19511f
Show file tree
Hide file tree
Showing 20 changed files with 197 additions and 166 deletions.
2 changes: 1 addition & 1 deletion _examples/bookmarks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func init() {
func run() {
var query string

// Use wf.Args() to enable Magic Actions
// Use wf.args() to enable Magic Actions
if args := wf.Args(); len(args) > 0 {
query = args[0]
}
Expand Down
4 changes: 2 additions & 2 deletions _examples/fuzzy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func run() {
// Handle CLI arguments
// ----------------------------------------------------------------

// You should always use wf.Args() in Script Filters. It contains the
// same as os.Args[1:], but the arguments are first parsed for AwGo's
// You should always use wf.args() in Script Filters. It contains the
// same as os.args[1:], but the arguments are first parsed for AwGo's
// magic actions (i.e. `workflow:*` to allow the user to easily open
// the log or data/cache directory).
if args := wf.Args(); len(args) > 0 {
Expand Down
5 changes: 5 additions & 0 deletions background_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func TestRunInBackground(t *testing.T) {
if err := wf.Kill("sleep"); err != nil {
t.Fatalf("Error killing 'sleep' job: %s", err)
}
// Job kill fails
if err := wf.Kill("sleep"); err == nil {
t.Fatal("No error killing 'sleep' job")
}

if wf.IsRunning("sleep") {
t.Fatal("'sleep' job still running")
}
Expand Down
8 changes: 4 additions & 4 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestLoadOrStore(t *testing.T) {
withTempDir(func(dir string) {
c := NewCache(dir)
n := "test.txt"
maxAge := time.Duration(time.Second * 1)
maxAge := time.Duration(time.Millisecond * 50)

// Sanity checks
p := c.path(n)
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestLoadOrStore(t *testing.T) {
t.Errorf("reload was called")
}

time.Sleep(time.Duration(time.Second * 1))
time.Sleep(time.Duration(time.Millisecond * 100))

if !c.Expired(n, maxAge) {
t.Errorf("cache hasn't expired")
Expand Down Expand Up @@ -259,7 +259,7 @@ func TestLoadOrStoreJSON(t *testing.T) {
withTempDir(func(dir string) {
n := "test.json"
c := NewCache(dir)
maxAge := time.Duration(time.Second * 1)
maxAge := time.Duration(time.Millisecond * 50)

// Sanity checks
p := c.path(n)
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestLoadOrStoreJSON(t *testing.T) {
t.Errorf("reload was called")
}

time.Sleep(time.Duration(time.Second * 1))
time.Sleep(time.Duration(time.Millisecond * 100))

if !c.Expired(n, maxAge) {
t.Errorf("cache hasn't expired")
Expand Down
35 changes: 31 additions & 4 deletions config_bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ func TestZeroValue(t *testing.T) {
}
})

t.Run("isZeroValueStruct", func(t *testing.T) {
t.Run("isNonZeroValueStruct", func(t *testing.T) {
t.Parallel()
rv := reflect.ValueOf(nonzero)
typ := rv.Type()
Expand Down Expand Up @@ -513,33 +513,59 @@ func TestZeroString(t *testing.T) {
{"", reflect.Int, true},
{"0", reflect.Int, true},
{"0000", reflect.Int, true},
{"invalid", reflect.Int, false},
{"", reflect.Int8, true},
{"0", reflect.Int8, true},
{"0000", reflect.Int8, true},
{"invalid", reflect.Int8, false},
{"", reflect.Int16, true},
{"0", reflect.Int16, true},
{"0000", reflect.Int16, true},
{"invalid", reflect.Int16, false},
{"", reflect.Int32, true},
{"0", reflect.Int32, true},
{"0000", reflect.Int32, true},
{"invalid", reflect.Int32, false},
{"", reflect.Int64, true},
{"0", reflect.Int64, true},
{"0000", reflect.Int64, true},
{"1,23", reflect.Int64, false},
{"test", reflect.Int64, false},
{"invalid", reflect.Int64, false},
// Uints
{"", reflect.Uint, true},
{"0", reflect.Uint, true},
{"0000", reflect.Uint, true},
{"invalid", reflect.Uint, false},
{"", reflect.Uint8, true},
{"0", reflect.Uint8, true},
{"0000", reflect.Uint8, true},
{"invalid", reflect.Uint8, false},
{"", reflect.Uint16, true},
{"0", reflect.Uint16, true},
{"0000", reflect.Uint16, true},
{"invalid", reflect.Uint16, false},
{"", reflect.Uint32, true},
{"0", reflect.Uint32, true},
{"0000", reflect.Uint32, true},
{"invalid", reflect.Uint32, false},
{"", reflect.Uint64, true},
{"0", reflect.Uint64, true},
{"0000", reflect.Uint64, true},
{"1,23", reflect.Uint64, false},
{"invalid", reflect.Uint64, false},
// Floats
{"", reflect.Float32, true},
{"0", reflect.Float32, true},
{"0.0", reflect.Float32, true},
{"00.00", reflect.Float32, true},
{"1,23", reflect.Float32, false},
{"test", reflect.Float32, false},
{"invalid", reflect.Float32, false},
{"", reflect.Float64, true},
{"0", reflect.Float64, true},
{"0.0", reflect.Float64, true},
{"00.00", reflect.Float64, true},
{"1,23", reflect.Float64, false},
{"test", reflect.Float64, false},
{"invalid", reflect.Float64, false},
// Booleans
{"", reflect.Bool, true},
{"0", reflect.Bool, true},
Expand All @@ -548,6 +574,7 @@ func TestZeroString(t *testing.T) {
{"f", reflect.Bool, true},
{"F", reflect.Bool, true},
{"False", reflect.Bool, true},
{"invalid", reflect.Bool, false},
// Durations
{"0s", reflect.Int64, true},
{"0m0s", reflect.Int64, true},
Expand Down
2 changes: 1 addition & 1 deletion env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
)

// Env is the datasource for configuration lookups.
// Env is the data source for configuration lookups.
//
// Pass a custom implementation to NewFromEnv() to provide a custom
// source for the required workflow configuration settings.
Expand Down
3 changes: 3 additions & 0 deletions feedback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ func TestMarshalItem(t *testing.T) {
// With match
{in: &Item{title: "title", match: p("one two three")},
x: `{"title":"title","match":"one two three","valid":false}`},
// With quicklook
{in: &Item{title: "title", ql: p("http://www.example.com")},
x: `{"title":"title","valid":false,"quicklookurl":"http://www.example.com"}`},
}

for i, td := range tests {
Expand Down
4 changes: 4 additions & 0 deletions keychain/keychain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func TestParse(t *testing.T) {
{in: `password: "hunter two"`, pw: "hunter two"},
{in: `password: 0x74C3AB73745F73C3A96372C3A974 "t\303\253st_s\303\251cr\303\251t"`, pw: "tëst_sécrét"},
{in: `password: 0x68C3BC6E74657232 "h\303\274nter2"`, pw: "hünter2"},
// Invalid
{in: ``, pw: ""},
{in: `0x`, pw: ""},
{in: `0xinvalid`, pw: ""},
}

for _, td := range data {
Expand Down
45 changes: 13 additions & 32 deletions magic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,13 @@ import (
"strings"
)

// defaultMagicActions creates a MagicActions with the default actions
// already registered.
func defaultMagicActions(wf *Workflow) *MagicActions {
ma := &MagicActions{
actions: map[string]MagicAction{},
wf: wf,
}
ma.Register(
logMA{wf},
cacheMA{wf},
clearCacheMA{wf},
dataMA{wf},
clearDataMA{wf},
resetMA{wf},
)
return ma
}

/*
MagicAction is a command that is called directly by AwGo (i.e. your workflow
code is not run) if its keyword is passed in a user query.
To use Magic Actions, it's imperative that your workflow call Workflow.Args().
To use Magic Actions, it's imperative that your workflow call Workflow.args().
Calls to Workflow.Args() check the workflow's arguments (os.Args[1:])
Calls to Workflow.args() check the workflow's arguments (os.args[1:])
for the magic prefix ("workflow:" by default), and hijack control of the
workflow if found.
Expand Down Expand Up @@ -74,8 +56,8 @@ default:
Custom Actions
To add custom MagicActions, you must register them with your Workflow
*before* you call Workflow.Args()
To add custom magicActions, you must register them with your Workflow
*before* you call Workflow.args()
To do this, configure Workflow with the AddMagic option.
Expand All @@ -99,34 +81,34 @@ type MagicAction interface {
Run() error
}

// MagicActions contains the registered magic actions. See the MagicAction
// magicActions contains the registered magic actions. See the MagicAction
// interface for full documentation.
type MagicActions struct {
type magicActions struct {
actions map[string]MagicAction
wf *Workflow
}

// Register adds a MagicAction to the mapping. Previous entries are overwritten.
func (ma *MagicActions) Register(actions ...MagicAction) {
// register adds a MagicAction to the mapping. Previous entries are overwritten.
func (ma *magicActions) register(actions ...MagicAction) {
for _, action := range actions {
ma.actions[action.Keyword()] = action
}
}

// Unregister removes a MagicAction from the mapping (based on its keyword).
func (ma *MagicActions) Unregister(actions ...MagicAction) {
// unregister removes a MagicAction from the mapping (based on its keyword).
func (ma *magicActions) unregister(actions ...MagicAction) {
for _, action := range actions {
delete(ma.actions, action.Keyword())
}
}

// Args runs a magic action or returns command-line arguments.
// args runs a magic action or returns command-line arguments.
// It parses args for magic actions. If it finds one, it takes
// control of your workflow and runs the action. Control is
// not returned to your code.
//
// If no magic actions are found, it returns args.
func (ma *MagicActions) Args(args []string, prefix string) []string {
func (ma *magicActions) args(args []string, prefix string) []string {

args, handled := ma.handleArgs(args, prefix)

Expand All @@ -136,12 +118,11 @@ func (ma *MagicActions) Args(args []string, prefix string) []string {
}

return args

}

// handleArgs checks args for the magic prefix. Returns args and true if
// it found and handled a magic argument.
func (ma *MagicActions) handleArgs(args []string, prefix string) ([]string, bool) {
func (ma *magicActions) handleArgs(args []string, prefix string) ([]string, bool) {

var handled bool

Expand Down
Loading

0 comments on commit f19511f

Please sign in to comment.