Skip to content

Commit

Permalink
refactor: create test case for symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Jan 15, 2025
1 parent 00b5b09 commit 865e851
Show file tree
Hide file tree
Showing 31 changed files with 355 additions and 228 deletions.
5 changes: 3 additions & 2 deletions cmd/vale/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/spf13/pflag"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/errata-ai/vale/v3/internal/system"
)

// Style represents an externally-hosted style.
Expand Down Expand Up @@ -80,7 +81,7 @@ func fetch(src, dst string) error {
}

resp.Body.Close()
return unarchive(tmpfile.Name(), dst)
return system.Unarchive(tmpfile.Name(), dst)
}

func install(args []string, flags *core.CLIFlags) error {
Expand All @@ -90,7 +91,7 @@ func install(args []string, flags *core.CLIFlags) error {
}

style := filepath.Join(cfg.StylesPath(), args[0])
if core.IsDir(style) {
if system.IsDir(style) {
os.RemoveAll(style) // Remove existing version
}

Expand Down
15 changes: 8 additions & 7 deletions cmd/vale/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/errata-ai/vale/v3/internal/core"
"github.com/errata-ai/vale/v3/internal/lint"
"github.com/errata-ai/vale/v3/internal/nlp"
"github.com/errata-ai/vale/v3/internal/system"
)

// TaggedWord is a word with an NLP context.
Expand Down Expand Up @@ -65,7 +66,7 @@ func fix(args []string, flags *core.CLIFlags) error {
}

alert := args[0]
if core.FileExists(args[0]) {
if system.FileExists(args[0]) {
b, err := os.ReadFile(args[0])
if err != nil {
return err
Expand Down Expand Up @@ -112,7 +113,7 @@ func sync(_ []string, flags *core.CLIFlags) error {
}

for idx, pkg := range pkgs {
name := fileNameWithoutExt(pkg)
name := system.FileNameWithoutExt(pkg)

p.UpdateTitle("Syncing " + name)
p.Increment()
Expand Down Expand Up @@ -140,7 +141,7 @@ func printConfig(_ []string, flags *core.CLIFlags) error {
func printMetrics(args []string, _ *core.CLIFlags) error {
if len(args) != 1 {
return core.NewE100("ls-metrics", errors.New("one argument expected"))
} else if !core.FileExists(args[0]) {
} else if !system.FileExists(args[0]) {
return errors.New("file not found")
}

Expand Down Expand Up @@ -272,14 +273,14 @@ func printDirs(_ []string, flags *core.CLIFlags) error {
styles, _ := core.DefaultStylesPath()

stylesFound := pterm.FgGreen.Sprint("✓")
if !core.IsDir(styles) {
if !system.IsDir(styles) {
stylesFound = pterm.FgRed.Sprint("✗")
}

cfg, _ := core.DefaultConfig()

configFound := pterm.FgGreen.Sprint("✓")
if !core.FileExists(cfg) {
if !system.FileExists(cfg) {
configFound = pterm.FgRed.Sprint("✗")
}

Expand All @@ -288,7 +289,7 @@ func printDirs(_ []string, flags *core.CLIFlags) error {
nativeExe := filepath.Join(nativeDir, getExecName("vale-native"))

nativeFound := pterm.FgGreen.Sprint("✓")
if !core.FileExists(native) {
if !system.FileExists(native) {
nativeFound = pterm.FgRed.Sprint("✗")
}

Expand All @@ -313,7 +314,7 @@ func printDirs(_ []string, flags *core.CLIFlags) error {
func transform(args []string, flags *core.CLIFlags) error {
if len(args) != 1 {
return core.NewE100("transform", errors.New("one argument expected"))
} else if !core.FileExists(args[0]) {
} else if !system.FileExists(args[0]) {
return fmt.Errorf("file not found: %s", args[0])
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/vale/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Masterminds/sprig/v3"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/errata-ai/vale/v3/internal/system"
)

// ProcessedFile represents a file that Vale has linted.
Expand All @@ -27,7 +28,7 @@ func PrintCustomAlerts(linted []*core.File, cfg *core.Config) (bool, error) {
var alertCount int

path := cfg.Flags.Output
if !core.FileExists(path) {
if !system.FileExists(path) {
path = core.FindAsset(cfg, path)
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/vale/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/errata-ai/vale/v3/internal/core"
"github.com/errata-ai/vale/v3/internal/lint"
"github.com/errata-ai/vale/v3/internal/system"
)

// version is set during the release build process.
Expand All @@ -23,8 +24,8 @@ func stat() bool {
}

func looksLikeStdin(s string) int {
isDir := core.IsDir(s)
if !(core.FileExists(s) || isDir) && s != "" {
isDir := system.IsDir(s)
if !(system.FileExists(s) || isDir) && s != "" {
return 1
} else if isDir {
return 0
Expand Down
31 changes: 17 additions & 14 deletions cmd/vale/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/adrg/xdg"
"github.com/pterm/pterm"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/errata-ai/vale/v3/internal/system"
)

const nativeHostName = "sh.vale.native"
Expand Down Expand Up @@ -60,8 +60,9 @@ func getNativeConfig() (string, error) {
if err != nil {
return "", err
}
name := system.Name()

switch runtime.GOOS {
switch name {
case "windows":
cfg, notFound := xdg.ConfigFile("vale/native/config.json")
if notFound != nil {
Expand All @@ -70,23 +71,23 @@ func getNativeConfig() (string, error) {
return cfg, nil
case "linux":
path := filepath.Join(home, ".config/vale/native/config.json")
if err = mkdir(filepath.Dir(path)); err != nil {
if err = system.Mkdir(filepath.Dir(path)); err != nil {
return "", err
}
return path, nil
case "darwin":
path := filepath.Join(home, "Library/Application Support/vale/native/config.json")
if err = mkdir(filepath.Dir(path)); err != nil {
if err = system.Mkdir(filepath.Dir(path)); err != nil {
return "", err
}
return path, nil
default:
return "", fmt.Errorf("unsupported OS: %s", runtime.GOOS)
return "", fmt.Errorf("unsupported OS: %s", name)
}
}

func getExecName(name string) string {
if runtime.GOOS == "windows" {
if system.IsWindows() {
return name + ".exe"
}
return name
Expand All @@ -99,7 +100,7 @@ func getManifestDirs() (map[string]string, error) {
}

manifests := map[string]string{}
switch runtime.GOOS {
switch system.Name() {
case "linux":
manifests = map[string]string{
"chrome": filepath.Join(home, ".config/google-chrome/NativeMessagingHosts"),
Expand Down Expand Up @@ -127,7 +128,7 @@ func getLocation(browser string) (map[string]string, error) {
}

bin := filepath.Dir(cfg)
if runtime.GOOS == "windows" {
if system.IsWindows() {
return map[string]string{
"appDir": bin,
"manifestDir": "",
Expand Down Expand Up @@ -231,18 +232,20 @@ func hostDownloadURL() (string, error) {
if err != nil {
return "", err
}
name := platformAndArch()
name := system.PlatformAndArch()
return fmt.Sprintf(releaseURL, hostVersion, name, "zip"), nil
}

func installHost(manifestJSON []byte, manifestFile, browser string) error {
switch runtime.GOOS {
name := system.Name()

switch name {
case "linux", "darwin":
return installNativeHostUnix(manifestJSON, manifestFile)
case "windows":
return installNativeHostWindows(manifestJSON, manifestFile, browser)
default:
return fmt.Errorf("unsupported OS: %s", runtime.GOOS)
return fmt.Errorf("unsupported OS: %s", name)
}
}

Expand Down Expand Up @@ -281,7 +284,7 @@ func installNativeHost(args []string, _ *core.CLIFlags) error { //nolint:funlen
oldInstall := []string{exeName, "LICENSE", "README.md"}
for _, file := range oldInstall {
fp := filepath.Join(locations["appDir"], file)
if core.FileExists(fp) {
if system.FileExists(fp) {
err = os.Remove(fp)
if err != nil {
return progressError("host-install", err, p)
Expand Down Expand Up @@ -361,7 +364,7 @@ func uninstallNativeHost(args []string, _ *core.CLIFlags) error {
exeName := getExecName("vale-native")
for _, file := range []string{"config.json", exeName, "LICENSE", "README.md", "host.log"} {
fp := filepath.Join(locations["appDir"], file)
if core.FileExists(fp) {
if system.FileExists(fp) {
err = os.Remove(filepath.Join(locations["appDir"], file))
if err != nil {
return progressError("host-uninstall", err, p)
Expand All @@ -374,7 +377,7 @@ func uninstallNativeHost(args []string, _ *core.CLIFlags) error {
p.UpdateTitle(steps[1])
manifestFile := filepath.Join(locations["manifestDir"], nativeHostName+".json")

if core.FileExists(manifestFile) {
if system.FileExists(manifestFile) {
err = os.Remove(manifestFile)
if err != nil {
return progressError("host-uninstall", err, p)
Expand Down
27 changes: 14 additions & 13 deletions cmd/vale/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/errata-ai/vale/v3/internal/system"
)

var TestData = "../../testdata/pkg"
Expand Down Expand Up @@ -54,11 +55,11 @@ func TestLibrary(t *testing.T) {
t.Fatal(err)
}

if !core.IsDir(filepath.Join(path, "write-good")) {
if !system.IsDir(filepath.Join(path, "write-good")) {
t.Fatal("unable to find 'write-good' in StylesPath")
}

if !core.FileExists(filepath.Join(path, "write-good", "E-Prime.yml")) {
if !system.FileExists(filepath.Join(path, "write-good", "E-Prime.yml")) {
t.Fatal("unable to find 'E-Prime' in StylesPath")
}
}
Expand All @@ -79,11 +80,11 @@ func TestLocalZip(t *testing.T) {
t.Fatal(err)
}

if !core.IsDir(filepath.Join(path, "write-good")) {
if !system.IsDir(filepath.Join(path, "write-good")) {
t.Fatal("unable to find 'write-good' in StylesPath")
}

if !core.FileExists(filepath.Join(path, "write-good", "E-Prime.yml")) {
if !system.FileExists(filepath.Join(path, "write-good", "E-Prime.yml")) {
t.Fatal("unable to find 'E-Prime' in StylesPath")
}
}
Expand All @@ -104,11 +105,11 @@ func TestLocalDir(t *testing.T) {
t.Fatal(err)
}

if !core.IsDir(filepath.Join(path, "write-good")) {
if !system.IsDir(filepath.Join(path, "write-good")) {
t.Fatal("unable to find 'write-good' in StylesPath")
}

if !core.FileExists(filepath.Join(path, "write-good", "E-Prime.yml")) {
if !system.FileExists(filepath.Join(path, "write-good", "E-Prime.yml")) {
t.Fatal("unable to find 'E-Prime' in StylesPath")
}
}
Expand All @@ -129,12 +130,12 @@ func TestLocalComplete(t *testing.T) { //nolint:dupl
t.Fatal(err)
}

if !core.IsDir(filepath.Join(path, "ISC")) {
if !system.IsDir(filepath.Join(path, "ISC")) {
t.Fatal("unable to find 'ISC' in StylesPath")
}

vocab := filepath.Join(path, "Vocab", "ISC_General", "accept.txt")
if !core.FileExists(vocab) {
if !system.FileExists(vocab) {
t.Fatal("unable to find 'ISC_General' in Vocab")
}

Expand Down Expand Up @@ -165,12 +166,12 @@ func TestLocalOnlyStyles(t *testing.T) { //nolint:dupl
t.Fatal(err)
}

if !core.IsDir(filepath.Join(path, "ISC")) {
if !system.IsDir(filepath.Join(path, "ISC")) {
t.Fatal("unable to find 'ISC' in StylesPath")
}

vocab := filepath.Join(path, "Vocab", "ISC_General", "accept.txt")
if !core.FileExists(vocab) {
if !system.FileExists(vocab) {
t.Fatal("unable to find 'ISC_General' in Vocab")
}

Expand Down Expand Up @@ -201,15 +202,15 @@ func TestV3Pkg(t *testing.T) {
t.Fatal(err)
}

if !core.IsDir(filepath.Join(path, "config")) {
if !system.IsDir(filepath.Join(path, "config")) {
t.Fatal("unable to find 'config' in StylesPath")
}

if !core.FileExists(filepath.Join(path, core.VocabDir, "Basic", "accept.txt")) {
if !system.FileExists(filepath.Join(path, core.VocabDir, "Basic", "accept.txt")) {
t.Fatal("unable to find 'accept.txt'")
}

if !core.FileExists(filepath.Join(path, core.TmplDir, "t.tmpl")) {
if !system.FileExists(filepath.Join(path, core.TmplDir, "t.tmpl")) {
t.Fatal("unable to find 't.tmpl'")
}
}
Loading

0 comments on commit 865e851

Please sign in to comment.