github.com/errata-ai/vale/v3@v3.4.2/internal/core/config_test.go (about)

     1  package core
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  )
     8  
     9  var testData = filepath.Join("..", "..", "testdata")
    10  
    11  func TestInitCfg(t *testing.T) {
    12  	cfg, err := NewConfig(&CLIFlags{})
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	path := cfg.StylesPath()
    17  
    18  	// In v3.0, these should have defaults.
    19  	if path == "" {
    20  		t.Fatal("StylesPath is empty")
    21  	} else if !IsDir(path) {
    22  		t.Fatalf("%s is not a directory", path)
    23  	}
    24  }
    25  
    26  func TestGetIgnores(t *testing.T) {
    27  	found, err := IgnoreFiles(filepath.Join(testData, "styles"))
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	} else if len(found) != 2 {
    31  		t.Fatalf("Expected 2 ignore files, found %d", len(found))
    32  	}
    33  }
    34  
    35  // TestFindAsset tests that we find an asset on the user-specified StylesPath.
    36  //
    37  // This was the standard behavior in v2.0.
    38  func TestFindAsset(t *testing.T) {
    39  	cfg, err := NewConfig(&CLIFlags{})
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	cfg.AddStylesPath(filepath.Join(testData, "styles"))
    44  
    45  	found := FindAsset(cfg, "line.tmpl")
    46  	if found == "" {
    47  		t.Fatal("Expected to find line.tmpl")
    48  	}
    49  
    50  	found = FindAsset(cfg, "notfound")
    51  	if found != "" {
    52  		t.Fatal("Expected to not find notfound")
    53  	}
    54  }
    55  
    56  // TestFindAssetDefault tests that we find an asset on the default StylesPath
    57  // when there's no user-specified StylesPath.
    58  func TestFindAssetDefault(t *testing.T) {
    59  	cfg, err := NewConfig(&CLIFlags{})
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	expected, err := DefaultStylesPath()
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	target := filepath.Join(expected, "tmp.tmpl")
    69  
    70  	err = os.WriteFile(target, []byte{}, os.ModePerm)
    71  	if err != nil {
    72  		t.Fatal("Failed to create file", err)
    73  	}
    74  
    75  	found := FindAsset(cfg, "tmp.tmpl")
    76  	if found == "" {
    77  		t.Fatal("Expected to find 'tmp.tmpl', got empty")
    78  	}
    79  
    80  	found = FindAsset(cfg, "notfound")
    81  	if found != "" {
    82  		t.Fatal("Expected to not find 'notfound', got", found)
    83  	}
    84  
    85  	err = os.Remove(target)
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  }
    90  
    91  // TestFallbackToDefault tests that we find an asset on the default StylesPath
    92  // when there's a user-specified StylesPath, but the asset isn't there.
    93  func TestFallbackToDefault(t *testing.T) {
    94  	cfg, err := NewConfig(&CLIFlags{})
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	local := filepath.Join(testData, "styles")
   100  	cfg.AddStylesPath(local)
   101  
   102  	expected, err := DefaultStylesPath()
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	target := filepath.Join(expected, "tmp.tmpl")
   107  
   108  	err = os.WriteFile(target, []byte{}, os.ModePerm)
   109  	if err != nil {
   110  		t.Fatal("Failed to create file", err)
   111  	}
   112  
   113  	found := FindAsset(cfg, "tmp.tmpl")
   114  	if found == "" {
   115  		t.Fatal("Expected to find 'tmp.tmpl', got empty", cfg.Paths)
   116  	}
   117  
   118  	err = os.Remove(target)
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  }