mirror of
https://github.com/issadarkthing/gomu.git
synced 2025-04-25 13:48:49 +08:00
Better config/new test for start.go (#65)
* chore(tests): add test for getArgs in ./start.go * test(start): add unit test for getArgs * fix(todos): Change XDG_* to os.UserHomeDir
This commit is contained in:
parent
e8ddb8b326
commit
50f449ec58
22
start.go
22
start.go
@ -33,13 +33,14 @@ type Panel interface {
|
||||
}
|
||||
|
||||
// Default values for command line arguments.
|
||||
// TODO: change to os.UserHomeDir() calls
|
||||
const (
|
||||
configPath = "~/.config/gomu/config"
|
||||
cacheQueuePath = "~/.local/share/gomu/queue.cache"
|
||||
musicPath = "~/music"
|
||||
musicPath = "~/music" //by default this is uppercase
|
||||
)
|
||||
|
||||
// Args is the augs for gomu executable
|
||||
// Args is the args for gomu executable
|
||||
type Args struct {
|
||||
config *string
|
||||
empty *bool
|
||||
@ -48,14 +49,17 @@ type Args struct {
|
||||
}
|
||||
|
||||
func getArgs() Args {
|
||||
ar := Args{
|
||||
config: flag.String("config", configPath, "Specify config file"),
|
||||
empty: flag.Bool("empty", false, "Open gomu with empty queue. Does not override previous queue"),
|
||||
music: flag.String("music", musicPath, "Specify music directory"),
|
||||
version: flag.Bool("version", false, "Print gomu version"),
|
||||
}
|
||||
configFlag := flag.String("config", configPath, "Specify config file")
|
||||
emptyFlag := flag.Bool("empty", false, "Open gomu with empty queue. Does not override previous queue")
|
||||
musicFlag := flag.String("music", musicPath, "Specify music directory")
|
||||
versionFlag := flag.Bool("version", false, "Print gomu version")
|
||||
flag.Parse()
|
||||
return ar
|
||||
return Args{
|
||||
config: configFlag,
|
||||
empty: emptyFlag,
|
||||
music: musicFlag,
|
||||
version: versionFlag,
|
||||
}
|
||||
}
|
||||
|
||||
// built-in functions
|
||||
|
@ -1,6 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/issadarkthing/gomu/anko"
|
||||
@ -8,6 +13,82 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Test default case
|
||||
func TestGetArgsDefaults(t *testing.T) {
|
||||
args := getArgs()
|
||||
assert.Equal(t, *args.config, "~/.config/gomu/config")
|
||||
assert.Equal(t, *args.empty, false)
|
||||
assert.Equal(t, *args.music, "~/music")
|
||||
assert.Equal(t, *args.version, false)
|
||||
}
|
||||
|
||||
// Test non-standard flags/the empty/version flags
|
||||
func TestGetArgs(t *testing.T) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
cfgDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// Test setting config flag
|
||||
testConfig := filepath.Join(cfgDir, ".tmp", "gomu")
|
||||
_, err = os.Stat(testConfig)
|
||||
if os.IsNotExist(err) {
|
||||
os.MkdirAll(testConfig, 0755)
|
||||
}
|
||||
defer os.RemoveAll(testConfig)
|
||||
//create a temporary config file
|
||||
tmpCfgf, err := os.CreateTemp(testConfig, "config")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
testMusic := filepath.Join(home, ".tmp", "gomu")
|
||||
_, err = os.Stat(testMusic)
|
||||
if os.IsNotExist(err) {
|
||||
os.MkdirAll(testMusic, 0755)
|
||||
}
|
||||
defer os.RemoveAll(testMusic)
|
||||
|
||||
boolChecks := []struct {
|
||||
name string
|
||||
arg bool
|
||||
want bool
|
||||
}{
|
||||
{"empty", true, true},
|
||||
{"version", true, true},
|
||||
}
|
||||
for _, check := range boolChecks {
|
||||
t.Run("testing bool flag "+check.name, func(t *testing.T) {
|
||||
flag.CommandLine.Set(check.name, strconv.FormatBool(check.arg))
|
||||
flag.CommandLine.Parse(os.Args[1:])
|
||||
assert.Equal(t, check.arg, check.want)
|
||||
})
|
||||
}
|
||||
strChecks := []struct {
|
||||
name string
|
||||
arg string
|
||||
want string
|
||||
}{
|
||||
{"config", tmpCfgf.Name(), tmpCfgf.Name()},
|
||||
{"music", testMusic, testMusic},
|
||||
}
|
||||
for _, check := range strChecks {
|
||||
t.Run("testing string flag "+check.name, func(t *testing.T) {
|
||||
flag.CommandLine.Set(check.name, check.arg)
|
||||
flag.CommandLine.Parse(os.Args[1:])
|
||||
fmt.Println("flag value: ", check.arg)
|
||||
assert.Equal(t, check.arg, check.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupHooks(t *testing.T) {
|
||||
|
||||
gomu := newGomu()
|
||||
|
Loading…
x
Reference in New Issue
Block a user