Skip to content

Commit

Permalink
test: add more config cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Jan 8, 2024
1 parent 34cc47c commit 5e39234
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 22 deletions.
2 changes: 2 additions & 0 deletions cmd/vale/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ func mockPath() (string, error) {
if err != nil {
return "", err
}

cfg.StylesPath = os.TempDir()
cfg.Paths = []string{cfg.StylesPath}

err = initPath(cfg)
if err != nil {
Expand Down
10 changes: 7 additions & 3 deletions internal/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
// can be set via the `--config` flag, the `VALE_CONFIG_PATH` environment
// variable, or the default search process.
//
// NOTE: The config pipeline is stored in the top-level `.config`
// NOTE: The config pipeline is stored in the top-level `.vale-config`
// directory. See `cmd/vale/sync.go`.
ConfigDir = "config"

Expand Down Expand Up @@ -172,13 +172,17 @@ func NewConfig(flags *CLIFlags) (*Config, error) {
cfg.Stylesheets = make(map[string]string)
cfg.TokenIgnores = make(map[string][]string)
cfg.FormatToLang = make(map[string]string)
cfg.Paths = []string{""}

found, err := DefaultStylesPath()
if err != nil {
return &cfg, err
}
cfg.StylesPath = found
cfg.Paths = []string{found}

if !flags.IgnoreGlobal {
cfg.StylesPath = found
cfg.Paths = []string{found}
}

return &cfg, nil
}
Expand Down
116 changes: 116 additions & 0 deletions internal/core/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package core

import (
"os"
"path/filepath"
"testing"
)

var testData = filepath.Join("..", "..", "testdata")

func TestInitCfg(t *testing.T) {
cfg, err := NewConfig(&CLIFlags{})
if err != nil {
t.Fatal(err)
}

// In v3.0, these should have defaults.
if cfg.StylesPath == "" {
t.Fatal("StylesPath is empty")
} else if len(cfg.Paths) == 0 {
t.Fatal("Paths are empty")
}
}

func TestGetIgnores(t *testing.T) {
found, err := IgnoreFiles(filepath.Join(testData, "styles"))
if err != nil {
t.Fatal(err)
} else if len(found) != 2 {
t.Fatalf("Expected 2 ignore files, found %d", len(found))
}
}

func TestFindAsset(t *testing.T) {
cfg, err := NewConfig(&CLIFlags{})
if err != nil {
t.Fatal(err)
}
cfg.StylesPath = filepath.Join(testData, "styles")
cfg.Paths = append(cfg.Paths, cfg.StylesPath)

found := FindAsset(cfg, "line.tmpl")
if found == "" {
t.Fatal("Expected to find line.tmpl")
}

found = FindAsset(cfg, "notfound")
if found != "" {
t.Fatal("Expected to not find notfound")
}
}

func TestFindAssetDefault(t *testing.T) {
cfg, err := NewConfig(&CLIFlags{})
if err != nil {
t.Fatal(err)
}

expected, err := DefaultStylesPath()
if err != nil {
t.Fatal(err)
}
target := filepath.Join(expected, "tmp.tmpl")

err = os.WriteFile(target, []byte{}, os.ModePerm)
if err != nil {
t.Fatal(err)
}

found := FindAsset(cfg, "tmp.tmpl")
if found == "" {
t.Fatal("Expected to find 'tmp.tmpl', got empty")
}

found = FindAsset(cfg, "notfound")
if found != "" {
t.Fatal("Expected to not find 'notfound', got", found)
}

err = os.Remove(target)
if err != nil {
t.Fatal(err)
}
}

func TestFallbackToDefault(t *testing.T) {
cfg, err := NewConfig(&CLIFlags{})
if err != nil {
t.Fatal(err)
}
local := filepath.Join(testData, "styles")

cfg.StylesPath = local
cfg.Paths = append(cfg.Paths, local)

expected, err := DefaultStylesPath()
if err != nil {
t.Fatal(err)
}
target := filepath.Join(expected, "tmp.tmpl")

err = os.WriteFile(target, []byte{}, os.ModePerm)
if err != nil {
t.Fatal(err)
}

found := FindAsset(cfg, "tmp.tmpl")
if found == "" {
t.Fatal("Expected to find 'tmp.tmpl', got empty", cfg.Paths)
}

err = os.Remove(target)
if err != nil {
t.Fatal(err)
}
}
6 changes: 3 additions & 3 deletions internal/core/ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var coreOpts = map[string]func(*ini.Section, *Config, []string) error{
basePath = determinePath(args[0], filepath.FromSlash(paths[1]))
mockPath = determinePath(args[1], filepath.FromSlash(paths[0]))
}
cfg.Paths = []string{basePath, mockPath}
cfg.Paths = append(cfg.Paths, []string{basePath, mockPath}...)
cfg.StylesPath = basePath
} else if len(paths) > 0 {
entry := paths[len(paths)-1]
Expand All @@ -100,7 +100,7 @@ var coreOpts = map[string]func(*ini.Section, *Config, []string) error{
cfg.Flags.Path)
}

cfg.Paths = []string{cfg.StylesPath}
cfg.Paths = append(cfg.Paths, cfg.StylesPath)
}
return nil
},
Expand Down Expand Up @@ -245,7 +245,7 @@ func loadINI(cfg *Config, dry bool) (*ini.File, error) {
return nil, NewE100("default/ini", err)
}
cfg.Flags.Local = true
sources = append(sources, []string{defaultCfg, cfg.Root}...)
sources = []string{defaultCfg, cfg.Root}
} else if base == "" {
return nil, NewE100(".vale.ini not found", errors.New("no config file found"))
}
Expand Down
36 changes: 20 additions & 16 deletions internal/core/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,38 +71,42 @@ func FromString(src string, cfg *Config, dry bool) (*ini.File, error) {

// getConfigAsset returns the path to a given asset if it exists.
// Otherwise, it returns an empty string.
func getConfigAsset(stylesPath, target string) string {
if stylesPath == "" {
return ""
}

for _, dir := range []string{DictDir, IgnoreDir, TmplDir, VocabDir} {
path := filepath.Join(stylesPath, dir, target)
if FileExists(path) {
return path
func getConfigAsset(target string, paths []string) string {
for _, p := range paths {
for _, dir := range ConfigDirs {
path := filepath.Join(p, dir, target)
if FileExists(path) {
return path
}
}
}

return ""
}

// FindAsset tries to locate a Vale-related resource by looking in the
// user-defined StylesPath.
func FindAsset(cfg *Config, path string) string {
if path == "" {
return path
return ""
}

inPath := filepath.Join(cfg.StylesPath, path)
if FileExists(inPath) {
return inPath
for _, p := range cfg.Paths {
inPath := filepath.Join(p, path)
if FileExists(inPath) {
return inPath
}
}

if p := getConfigAsset(cfg.StylesPath, path); p != "" {
if p := getConfigAsset(path, cfg.Paths); p != "" {
return p
}

return determinePath(cfg.Flags.Path, path)
p := determinePath(cfg.Flags.Path, path)
if FileExists(p) {
return p
}

return ""
}

func validateFlags(cfg *Config) error {
Expand Down

0 comments on commit 5e39234

Please sign in to comment.