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:
Marcelina Hołub 2023-01-11 10:51:42 +00:00 committed by GitHub
parent e8ddb8b326
commit 50f449ec58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 94 additions and 9 deletions

View File

@ -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

View File

@ -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()