-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
141 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type testApp struct { | ||
done chan struct{} | ||
runError bool | ||
userErrorReturn bool | ||
} | ||
|
||
func (a *testApp) Run() error { | ||
<-a.done | ||
if a.runError { | ||
return errors.New(("run error!")) | ||
} | ||
return nil | ||
} | ||
|
||
func (a testApp) UsageError() bool { | ||
return a.userErrorReturn | ||
} | ||
|
||
func (a testApp) Quit() { | ||
close(a.done) | ||
} | ||
|
||
func TestRun(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := map[string]struct { | ||
runError bool | ||
usageError bool | ||
|
||
wantReturnCode int | ||
}{ | ||
"Run and exit successfully": {}, | ||
"Run and exit error": {runError: true, wantReturnCode: 1}, | ||
"Run and exit with usage error": {usageError: true, runError: true, wantReturnCode: 2}, | ||
"Run and return with usage error but no run error": {usageError: true, wantReturnCode: 0}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
a := testApp{ | ||
done: make(chan struct{}), | ||
runError: tc.runError, | ||
userErrorReturn: tc.usageError, | ||
} | ||
|
||
var rc int | ||
wait := make(chan struct{}) | ||
|
||
go func() { | ||
rc = run(&a) | ||
close(wait) | ||
}() | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
|
||
a.Quit() | ||
<-wait | ||
|
||
if rc != tc.wantReturnCode { | ||
t.Errorf("run() = %v, want %v", rc, tc.wantReturnCode) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,82 @@ | ||
package constants | ||
package constants_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/ubuntu/ubuntu-insights/internal/constants" | ||
) | ||
|
||
func Test_userConfigDir(t *testing.T) { | ||
func Test_GetUserConfigDir(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
|
||
tests := map[string]struct { | ||
want string | ||
mock func() (string, error) | ||
}{ | ||
{ | ||
name: "os.UserConfigDir success", | ||
want: "abc/def", | ||
"os.UserConfigDir success": { | ||
want: "abc/def" + string(os.PathSeparator) + constants.DefaultAppFolder, | ||
mock: func() (string, error) { | ||
return "abc/def", nil | ||
}, | ||
}, | ||
{ | ||
name: "os.UserConfigDir error", | ||
want: "", | ||
"os.UserConfigDir error": { | ||
want: string(os.PathSeparator) + constants.DefaultAppFolder, | ||
mock: func() (string, error) { | ||
return "", fmt.Errorf("error") | ||
}, | ||
}, | ||
{ | ||
name: "os.UserConfigDir error 2", | ||
want: "", | ||
"os.UserConfigDir error 2": { | ||
want: string(os.PathSeparator) + constants.DefaultAppFolder, | ||
mock: func() (string, error) { | ||
return "abc", fmt.Errorf("error") | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := userConfigDir(tt.mock); got != tt.want { | ||
t.Errorf("userConfigDir() = %v, want %v", got, tt.want) | ||
} | ||
for name, tt := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
opts := []constants.Option{constants.WithBaseDir(tt.mock)} | ||
require.Equal(t, tt.want, constants.GetDefaultConfigPath(opts...)) | ||
}) | ||
} | ||
} | ||
|
||
func Test_userCacheDir(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
|
||
tests := map[string]struct { | ||
want string | ||
mock func() (string, error) | ||
}{ | ||
{ | ||
name: "os.UserCacheDir success", | ||
want: "def/abc", | ||
"os.UserCacheDir success": { | ||
want: "def/abc" + string(os.PathSeparator) + constants.DefaultAppFolder, | ||
mock: func() (string, error) { | ||
return "def/abc", nil | ||
}, | ||
}, | ||
{ | ||
name: "os.UserCacheDir error", | ||
want: "", | ||
"os.UserCacheDir error": { | ||
want: string(os.PathSeparator) + constants.DefaultAppFolder, | ||
mock: func() (string, error) { | ||
return "", fmt.Errorf("error") | ||
}, | ||
}, | ||
{ | ||
name: "os.UserCacheDir error 2", | ||
want: "", | ||
"os.UserCacheDir error 2": { | ||
want: string(os.PathSeparator) + constants.DefaultAppFolder, | ||
mock: func() (string, error) { | ||
return "abc", fmt.Errorf("error") | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := userCacheDir(tt.mock); got != tt.want { | ||
t.Errorf("userCacheDir() = %v, want %v", got, tt.want) | ||
} | ||
for name, tt := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
opts := []constants.Option{constants.WithBaseDir(tt.mock)} | ||
require.Equal(t, tt.want, constants.GetDefaultCachePath(opts...)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package constants | ||
|
||
type Option = option | ||
|
||
func WithBaseDir(baseDir func() (string, error)) option { | ||
return func(o *options) { | ||
o.baseDir = baseDir | ||
} | ||
} |