Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
ff7037a60c | |||
1f94120384 |
27
.vscode/launch.json
vendored
27
.vscode/launch.json
vendored
@@ -32,6 +32,28 @@
|
|||||||
"cookassistant.yml",
|
"cookassistant.yml",
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "Launch Package (Quasimorph cookfile)",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "auto",
|
||||||
|
"program": "${workspaceFolder}",
|
||||||
|
"cwd": "C:/Users/Administrator/Seafile/Games-Quasimorph",
|
||||||
|
"args": [
|
||||||
|
"cook.yml",
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Launch Package (Rimworld cookfile)",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "auto",
|
||||||
|
"program": "${workspaceFolder}",
|
||||||
|
"cwd": "C:/Users/Administrator/Seafile/Games-Rimworld/294100",
|
||||||
|
"args": [
|
||||||
|
"cookVehicles.yml",
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "Launch Package (Workspace)",
|
"name": "Launch Package (Workspace)",
|
||||||
"type": "go",
|
"type": "go",
|
||||||
@@ -39,10 +61,7 @@
|
|||||||
"mode": "auto",
|
"mode": "auto",
|
||||||
"program": "${workspaceFolder}",
|
"program": "${workspaceFolder}",
|
||||||
"args": [
|
"args": [
|
||||||
"-loglevel",
|
"tester.yml",
|
||||||
"trace",
|
|
||||||
"-cook",
|
|
||||||
"cookscoop.yml",
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
6
main.go
6
main.go
@@ -8,12 +8,12 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"modify/processor"
|
"cook/processor"
|
||||||
"modify/utils"
|
"cook/utils"
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
|
|
||||||
"modify/logger"
|
"cook/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GlobalStats struct {
|
type GlobalStats struct {
|
||||||
|
@@ -6,7 +6,7 @@ import (
|
|||||||
|
|
||||||
lua "github.com/yuin/gopher-lua"
|
lua "github.com/yuin/gopher-lua"
|
||||||
|
|
||||||
"modify/logger"
|
"cook/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Maybe we make this an interface again for the shits and giggles
|
// Maybe we make this an interface again for the shits and giggles
|
||||||
|
@@ -9,8 +9,8 @@ import (
|
|||||||
|
|
||||||
lua "github.com/yuin/gopher-lua"
|
lua "github.com/yuin/gopher-lua"
|
||||||
|
|
||||||
"modify/logger"
|
"cook/logger"
|
||||||
"modify/utils"
|
"cook/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CaptureGroup struct {
|
type CaptureGroup struct {
|
||||||
|
@@ -2,8 +2,8 @@ package processor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"cook/utils"
|
||||||
"io"
|
"io"
|
||||||
"modify/utils"
|
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
@@ -2,7 +2,7 @@ package processor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"modify/logger"
|
"cook/logger"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
package regression
|
package regression
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"modify/processor"
|
"cook/processor"
|
||||||
"modify/utils"
|
"cook/utils"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
@@ -10,7 +10,6 @@ var (
|
|||||||
// Deprecated
|
// Deprecated
|
||||||
ResetFlag = flag.Bool("reset", false, "Reset files to their original state")
|
ResetFlag = flag.Bool("reset", false, "Reset files to their original state")
|
||||||
LogLevel = flag.String("loglevel", "INFO", "Set log level: ERROR, WARNING, INFO, DEBUG, TRACE")
|
LogLevel = flag.String("loglevel", "INFO", "Set log level: ERROR, WARNING, INFO, DEBUG, TRACE")
|
||||||
Cookfile = flag.String("cook", "**/cook.yml", "Path to cook config files, can be globbed")
|
|
||||||
ParallelFiles = flag.Int("P", 100, "Number of files to process in parallel")
|
ParallelFiles = flag.Int("P", 100, "Number of files to process in parallel")
|
||||||
Filter = flag.String("filter", "", "Filter commands before running them")
|
Filter = flag.String("filter", "", "Filter commands before running them")
|
||||||
)
|
)
|
||||||
|
@@ -1,14 +1,14 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cook/logger"
|
||||||
"fmt"
|
"fmt"
|
||||||
"modify/logger"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/object"
|
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
|
"github.com/go-git/go-git/v5/plumbing/object"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@@ -1,10 +1,9 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cook/logger"
|
||||||
"fmt"
|
"fmt"
|
||||||
"modify/logger"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bmatcuk/doublestar/v4"
|
"github.com/bmatcuk/doublestar/v4"
|
||||||
@@ -75,15 +74,7 @@ func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[s
|
|||||||
}
|
}
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
for _, glob := range command.Files {
|
for _, glob := range command.Files {
|
||||||
_, pattern, err := FigureOutGlobRoot(glob)
|
matches, err := Matches(file, glob)
|
||||||
if err != nil {
|
|
||||||
logger.Trace("Failed to figure out glob root for %s: %v", glob, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
file = filepath.Clean(file)
|
|
||||||
file = strings.ReplaceAll(file, "\\", "/")
|
|
||||||
|
|
||||||
matches, err := Matches(file, pattern)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err)
|
logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err)
|
||||||
continue
|
continue
|
||||||
@@ -119,7 +110,7 @@ func AggregateGlobs(commands []ModifyCommand) map[string]struct{} {
|
|||||||
globs := make(map[string]struct{})
|
globs := make(map[string]struct{})
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
for _, glob := range command.Files {
|
for _, glob := range command.Files {
|
||||||
glob = strings.ReplaceAll(glob, "~", os.Getenv("USERPROFILE"))
|
glob = strings.Replace(glob, "~", os.Getenv("HOME"), 1)
|
||||||
glob = strings.ReplaceAll(glob, "\\", "/")
|
glob = strings.ReplaceAll(glob, "\\", "/")
|
||||||
globs[glob] = struct{}{}
|
globs[glob] = struct{}{}
|
||||||
}
|
}
|
||||||
@@ -128,87 +119,21 @@ func AggregateGlobs(commands []ModifyCommand) map[string]struct{} {
|
|||||||
return globs
|
return globs
|
||||||
}
|
}
|
||||||
|
|
||||||
func FigureOutGlobRoot(inputPattern string) (root, pattern string, err error) {
|
|
||||||
logger.Debug("Starting to figure out glob root for input pattern: %s", inputPattern)
|
|
||||||
|
|
||||||
cwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("Failed to get current working directory: %v", err)
|
|
||||||
return "", inputPattern, fmt.Errorf("failed to get current working directory: %w", err)
|
|
||||||
}
|
|
||||||
logger.Trace("Current working directory: %s", cwd)
|
|
||||||
|
|
||||||
root = inputPattern
|
|
||||||
if !filepath.IsAbs(inputPattern) {
|
|
||||||
root = filepath.Join(cwd, inputPattern)
|
|
||||||
logger.Info("Input pattern is not absolute. Using combined path: %s", root)
|
|
||||||
}
|
|
||||||
root = filepath.Clean(root)
|
|
||||||
logger.Debug("Cleaned root path: %s", root)
|
|
||||||
|
|
||||||
// In either case (whatever our root may be), we have to figure out
|
|
||||||
// Where to start, what our FS will be
|
|
||||||
// The best place would be the last sure entry
|
|
||||||
// That is to say the final directory that is not a wildcard
|
|
||||||
|
|
||||||
finalroot := ""
|
|
||||||
// TODO: This will probably explode on linux because oooooooooo we have to be clever oooooooooo / on linux \\ on windows ooooooooooo
|
|
||||||
parts := strings.Split(root, "\\")
|
|
||||||
lastIndex := len(parts) - 1
|
|
||||||
logger.Debug("Split root into parts: %v", parts)
|
|
||||||
|
|
||||||
// In the case our pattern ends with a file (and many of them do)
|
|
||||||
// Look for only the folders, we cannot mount a file as a FS
|
|
||||||
// In any case we have to match files so they have to be the last part
|
|
||||||
for i := 0; i < len(parts)-1; i++ {
|
|
||||||
part := parts[i]
|
|
||||||
logger.Trace("Processing part: %s", part)
|
|
||||||
if part == "*" || part == "**" || part == "?" || part == "[" {
|
|
||||||
lastIndex = i
|
|
||||||
logger.Debug("Found wildcard part: %s, updating lastIndex to: %d", part, lastIndex)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
// We can't use join here because it joins C: and Users as C:Users
|
|
||||||
// Instead of C:/Users/
|
|
||||||
// God damn it
|
|
||||||
if finalroot != "" {
|
|
||||||
finalroot = finalroot + "/" + part
|
|
||||||
} else {
|
|
||||||
finalroot = finalroot + part
|
|
||||||
}
|
|
||||||
}
|
|
||||||
finalroot = filepath.Clean(finalroot)
|
|
||||||
logger.Debug("Final root after processing: %s", finalroot)
|
|
||||||
|
|
||||||
// After all this juggling our pattern is whatever is left after the finalroot
|
|
||||||
// Which is, in "worst" case, only a file
|
|
||||||
pattern = strings.Join(parts[lastIndex:], "/")
|
|
||||||
logger.Info("Determined pattern: %s", pattern)
|
|
||||||
|
|
||||||
return finalroot, pattern, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
|
func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
|
||||||
var files []string
|
var files []string
|
||||||
filesMap := make(map[string]bool)
|
filesMap := make(map[string]bool)
|
||||||
|
|
||||||
for pattern := range patterns {
|
cwd, err := os.Getwd()
|
||||||
root, pattern, err := FigureOutGlobRoot(pattern)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to figure out glob root: %w", err)
|
return nil, fmt.Errorf("failed to get current working directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Debug("Expanding patterns from directory: %s", cwd)
|
||||||
|
for pattern := range patterns {
|
||||||
logger.Trace("Processing pattern: %s", pattern)
|
logger.Trace("Processing pattern: %s", pattern)
|
||||||
matches, err := doublestar.Glob(os.DirFS(root), pattern)
|
matches, _ := doublestar.Glob(os.DirFS(cwd), pattern)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to glob pattern %s: %w", pattern, err)
|
|
||||||
}
|
|
||||||
logger.Debug("Found %d matches for pattern %s", len(matches), pattern)
|
logger.Debug("Found %d matches for pattern %s", len(matches), pattern)
|
||||||
for _, m := range matches {
|
for _, m := range matches {
|
||||||
m = filepath.Join(root, m)
|
|
||||||
m = filepath.Clean(m)
|
|
||||||
m = strings.ReplaceAll(m, "\\", "/")
|
|
||||||
|
|
||||||
info, err := os.Stat(m)
|
info, err := os.Stat(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warning("Error getting file info for %s: %v", m, err)
|
logger.Warning("Error getting file info for %s: %v", m, err)
|
||||||
@@ -230,69 +155,38 @@ func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
|
|||||||
func LoadCommands(args []string) ([]ModifyCommand, error) {
|
func LoadCommands(args []string) ([]ModifyCommand, error) {
|
||||||
commands := []ModifyCommand{}
|
commands := []ModifyCommand{}
|
||||||
|
|
||||||
logger.Info("Loading commands from cook files: %s", *Cookfile)
|
logger.Info("Loading commands from cook files: %s", args)
|
||||||
newcommands, err := LoadCommandsFromCookFiles(*Cookfile)
|
for _, arg := range args {
|
||||||
|
newcommands, err := LoadCommandsFromCookFiles(arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to load commands from cook files: %w", err)
|
return nil, fmt.Errorf("failed to load commands from cook files: %w", err)
|
||||||
}
|
}
|
||||||
logger.Info("Successfully loaded %d commands from cook files", len(newcommands))
|
logger.Info("Successfully loaded %d commands from cook iles", len(newcommands))
|
||||||
commands = append(commands, newcommands...)
|
commands = append(commands, newcommands...)
|
||||||
logger.Info("Now total commands: %d", len(commands))
|
logger.Info("Now total commands: %d", len(commands))
|
||||||
|
|
||||||
logger.Info("Loading commands from arguments: %v", args)
|
|
||||||
newcommands, err = LoadCommandFromArgs(args)
|
|
||||||
if err != nil {
|
|
||||||
if len(commands) == 0 {
|
|
||||||
return nil, fmt.Errorf("failed to load commands from args: %w", err)
|
|
||||||
}
|
}
|
||||||
logger.Warning("Failed to load commands from args: %v", err)
|
|
||||||
}
|
|
||||||
logger.Info("Successfully loaded %d commands from args", len(newcommands))
|
|
||||||
commands = append(commands, newcommands...)
|
|
||||||
logger.Info("Now total commands: %d", len(commands))
|
|
||||||
|
|
||||||
|
logger.Info("Loaded %d commands from all cook f", len(commands))
|
||||||
return commands, nil
|
return commands, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) {
|
func LoadCommandsFromCookFiles(pattern string) ([]ModifyCommand, error) {
|
||||||
// Cannot reset without git, right?
|
root, pattern, err := FigureOutGlobRoot(pattern)
|
||||||
if *ResetFlag {
|
|
||||||
*GitFlag = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(args) < 3 {
|
|
||||||
return nil, fmt.Errorf("at least %d arguments are required", 3)
|
|
||||||
}
|
|
||||||
|
|
||||||
command := ModifyCommand{
|
|
||||||
Regex: args[0],
|
|
||||||
Lua: args[1],
|
|
||||||
Files: args[2:],
|
|
||||||
Git: *GitFlag,
|
|
||||||
Reset: *ResetFlag,
|
|
||||||
LogLevel: *LogLevel,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := command.Validate(); err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid command: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return []ModifyCommand{command}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func LoadCommandsFromCookFiles(s string) ([]ModifyCommand, error) {
|
|
||||||
cwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get current working directory: %w", err)
|
return nil, fmt.Errorf("failed to figure out glob root: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
commands := []ModifyCommand{}
|
commands := []ModifyCommand{}
|
||||||
cookFiles, err := doublestar.Glob(os.DirFS(cwd), *Cookfile)
|
cookFiles, err := doublestar.Glob(os.DirFS(root), pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to glob cook files: %w", err)
|
return nil, fmt.Errorf("failed to glob cook files: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, cookFile := range cookFiles {
|
for _, cookFile := range cookFiles {
|
||||||
|
cookFile = filepath.Clean(cookFile)
|
||||||
|
cookFile = strings.ReplaceAll(cookFile, "\\", "/")
|
||||||
|
logger.Info("Loading commands from cook file: %s", cookFile)
|
||||||
|
|
||||||
cookFileData, err := os.ReadFile(cookFile)
|
cookFileData, err := os.ReadFile(cookFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to read cook file: %w", err)
|
return nil, fmt.Errorf("failed to read cook file: %w", err)
|
||||||
|
@@ -3,7 +3,6 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -1270,164 +1269,3 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) {
|
|||||||
// t.Errorf("Expected 0 commands, got: %d", len(commands))
|
// t.Errorf("Expected 0 commands, got: %d", len(commands))
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Absolute path without wildcards returns correct root and pattern
|
|
||||||
func TestFigureOutGlobRootWithAbsolutePathNoWildcards(t *testing.T) {
|
|
||||||
// Setup
|
|
||||||
absPath := filepath.Join("C:\\Users", "test", "documents", "file.txt")
|
|
||||||
|
|
||||||
// Execute
|
|
||||||
root, pattern, err := FigureOutGlobRoot(absPath)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert.NoError(t, err)
|
|
||||||
expectedRoot := filepath.Clean(filepath.Join("C:\\Users", "test", "documents"))
|
|
||||||
assert.Equal(t, expectedRoot, root)
|
|
||||||
assert.Equal(t, "file.txt", pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Empty input pattern handling - we expect our "Patter", such as it is, to be a folder
|
|
||||||
func TestFigureOutGlobRootWithEmptyPattern(t *testing.T) {
|
|
||||||
// Setup
|
|
||||||
emptyPattern := ""
|
|
||||||
|
|
||||||
// Execute
|
|
||||||
root, pattern, err := FigureOutGlobRoot(emptyPattern)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert.NoError(t, err)
|
|
||||||
cwd, _ := os.Getwd()
|
|
||||||
cwdParts := strings.Split(filepath.Clean(cwd), "\\")
|
|
||||||
expectedRoot := strings.Join(cwdParts[:len(cwdParts)-1], "\\")
|
|
||||||
expectedPattern := cwdParts[len(cwdParts)-1]
|
|
||||||
assert.Equal(t, expectedRoot, root)
|
|
||||||
assert.Equal(t, expectedPattern, pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Relative path is correctly joined with current working directory
|
|
||||||
func TestFigureOutGlobRootWithRelativePath(t *testing.T) {
|
|
||||||
// Setup
|
|
||||||
cwd, err := os.Getwd()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
relPath := "test/documents/file.txt"
|
|
||||||
|
|
||||||
// Execute
|
|
||||||
root, pattern, err := FigureOutGlobRoot(relPath)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert.NoError(t, err)
|
|
||||||
expectedRoot := filepath.Clean(filepath.Join(cwd, "test/documents"))
|
|
||||||
assert.Equal(t, expectedRoot, root)
|
|
||||||
assert.Equal(t, "file.txt", pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Path with wildcards correctly identifies the last non-wildcard directory as root
|
|
||||||
func TestFigureOutGlobRootWithWildcards(t *testing.T) {
|
|
||||||
// Setup
|
|
||||||
inputPattern := filepath.Join("C:\\Users", "test", "documents", "*", "file.txt")
|
|
||||||
|
|
||||||
// Execute
|
|
||||||
root, pattern, err := FigureOutGlobRoot(inputPattern)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert.NoError(t, err)
|
|
||||||
expectedRoot := filepath.Clean(filepath.Join("C:\\Users", "test", "documents"))
|
|
||||||
assert.Equal(t, expectedRoot, root)
|
|
||||||
assert.Equal(t, "*/file.txt", pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Windows-style paths are properly handled and converted
|
|
||||||
func TestFigureOutGlobRootWithRelativePathAndWildcards(t *testing.T) {
|
|
||||||
// Setup
|
|
||||||
inputPattern := "documents\\*\\file?.txt"
|
|
||||||
cwd, _ := os.Getwd()
|
|
||||||
|
|
||||||
// Execute
|
|
||||||
root, pattern, err := FigureOutGlobRoot(inputPattern)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert.NoError(t, err)
|
|
||||||
expectedRoot := filepath.Clean(filepath.Join(cwd, "documents"))
|
|
||||||
assert.Equal(t, expectedRoot, root)
|
|
||||||
assert.Equal(t, "*/file?.txt", pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Path with only wildcards (e.g., "*" or "**")
|
|
||||||
func TestFigureOutGlobRootWithOnlyWildcards(t *testing.T) {
|
|
||||||
// Setup
|
|
||||||
wildcardPattern := "*"
|
|
||||||
|
|
||||||
// Execute
|
|
||||||
root, pattern, err := FigureOutGlobRoot(wildcardPattern)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert.NoError(t, err)
|
|
||||||
expectedRoot, _ := os.Getwd()
|
|
||||||
assert.Equal(t, expectedRoot, root)
|
|
||||||
assert.Equal(t, "*", pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Multiple path segments are correctly processed and joined
|
|
||||||
func TestFigureOutGlobRootWithRelativePathAndWildcards2(t *testing.T) {
|
|
||||||
// Setup
|
|
||||||
cwd, err := os.Getwd()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
relPath := filepath.Join("test", "data", "*", "file?.txt")
|
|
||||||
|
|
||||||
// Execute
|
|
||||||
root, pattern, err := FigureOutGlobRoot(relPath)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert.NoError(t, err)
|
|
||||||
expectedRoot := filepath.Clean(filepath.Join(cwd, "test", "data"))
|
|
||||||
assert.Equal(t, expectedRoot, root)
|
|
||||||
assert.Equal(t, "*/file?.txt", pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Path with mixed forward and backward slashes
|
|
||||||
func TestFigureOutGlobRootWithMixedSlashes(t *testing.T) {
|
|
||||||
// Setup
|
|
||||||
mixedPath := "C:\\Users/test\\documents\\file.txt"
|
|
||||||
|
|
||||||
// Execute
|
|
||||||
root, pattern, err := FigureOutGlobRoot(mixedPath)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert.NoError(t, err)
|
|
||||||
expectedRoot := filepath.Clean(filepath.Join("C:\\Users", "test", "documents"))
|
|
||||||
assert.Equal(t, expectedRoot, root)
|
|
||||||
assert.Equal(t, "file.txt", pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Path with wildcards in the first segment
|
|
||||||
func TestFigureOutGlobRootWithWildcardInFirstSegment(t *testing.T) {
|
|
||||||
// Setup
|
|
||||||
inputPattern := "*\\Users\\test\\documents\\file.txt"
|
|
||||||
cwd, _ := os.Getwd()
|
|
||||||
|
|
||||||
// Execute
|
|
||||||
root, pattern, err := FigureOutGlobRoot(inputPattern)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert.NoError(t, err)
|
|
||||||
expectedRoot := filepath.Clean(cwd)
|
|
||||||
assert.Equal(t, expectedRoot, root)
|
|
||||||
assert.Equal(t, "*/Users/test/documents/file.txt", pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handling of relative paths with ".." or "." components
|
|
||||||
func TestFigureOutGlobRootWithRelativePathAndDotComponents(t *testing.T) {
|
|
||||||
// Setup
|
|
||||||
cwd, err := os.Getwd()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
relPath := filepath.Join("..", ".", "test", "documents", "file.txt")
|
|
||||||
|
|
||||||
// Execute
|
|
||||||
root, pattern, err := FigureOutGlobRoot(relPath)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
assert.NoError(t, err)
|
|
||||||
expectedRoot := filepath.Clean(filepath.Join(cwd, "..", "test", "documents"))
|
|
||||||
assert.Equal(t, expectedRoot, root)
|
|
||||||
assert.Equal(t, "file.txt", pattern)
|
|
||||||
}
|
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cook/logger"
|
||||||
"fmt"
|
"fmt"
|
||||||
"modify/logger"
|
|
||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user