Skip to content

Commit

Permalink
Add better support for glob matching
Browse files Browse the repository at this point in the history
  • Loading branch information
hudson-newey committed Nov 18, 2024
1 parent 8a484e5 commit 129f95d
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/cli/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Mark FILE(s) for deletion.
-f, --force Bypass protections
-v, --verbose Add additional information to the output
-v, --verbose Add additional information to the output
--help Display this help and (without deleting anything)
Expand Down
1 change: 1 addition & 0 deletions src/config/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestParsingConfig(t *testing.T) {
"*.partial",
},
Soft: []string{
"backups/",
"*.bak",
},
Protected: []string{
Expand Down
17 changes: 10 additions & 7 deletions src/models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,16 @@ func (config Config) InteractiveThreshold() int {
}

func matchesPattern(pattern string, path string) bool {
// we put a wildcard at the start so that we don't have to match full
// paths
isAbsolutePath := strings.HasPrefix(path, "/")
if !isAbsolutePath {
pattern = "*" + pattern
// Normalize the pattern and path
normalizedPattern := filepath.Clean(pattern)
normalizedPath := filepath.Clean(path)

// Check if the pattern matches the path
matched, _ := filepath.Match(normalizedPattern, normalizedPath)
if matched {
return true
}

matched, _ := filepath.Match(pattern, path)
return matched
hasSuffix := strings.HasSuffix(normalizedPath, normalizedPattern)
return hasSuffix
}
80 changes: 80 additions & 0 deletions src/models/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,44 @@ func TestConfig(t *testing.T) {
testedPath: "node_modules/",
expectedResult: true,
},
{
name: "HardDeleteWithPrefix",
configPath: "valid.yml",
configFunction: models.Config.ShouldHardDelete,
testedPath: "./node_modules/",
expectedResult: true,
},
{
name: "HardDeleteWithLongPrefix",
configPath: "valid.yml",
configFunction: models.Config.ShouldHardDelete,
testedPath: "./Documents/client/node_modules/",
expectedResult: true,
},
{
// while we specified node_modules/ as a hard delete inside the
// config, test.txt does not match the hard delete config pattern
// so we expect the nested file to use the default soft-delete
name: "HardDeleteWithSuffix",
configPath: "valid.yml",
configFunction: models.Config.ShouldHardDelete,
testedPath: "node_modules/test.txt",
expectedResult: false,
},
{
name: "HardDeleteWithLongSuffix",
configPath: "valid.yml",
configFunction: models.Config.ShouldHardDelete,
testedPath: "node_modules/sub_package/dist/test.txt",
expectedResult: false,
},
{
name: "HardDeleteWithSuffixAndPrefix",
configPath: "valid.yml",
configFunction: models.Config.ShouldHardDelete,
testedPath: "/home/john_doe/Documents/client/node_modules/test.txt",
expectedResult: false,
},
{
name: "NotHardDelete",
configPath: "valid.yml",
Expand Down Expand Up @@ -86,6 +124,20 @@ func TestConfig(t *testing.T) {
testedPath: "file.bak",
expectedResult: true,
},
{
name: "SoftDeleteWithPrefix",
configPath: "valid.yml",
configFunction: models.Config.ShouldSoftDelete,
testedPath: "./file.bak",
expectedResult: true,
},
{
name: "SoftDeleteWithLongPrefix",
configPath: "valid.yml",
configFunction: models.Config.ShouldSoftDelete,
testedPath: "./.local/share/2rm/file.bak",
expectedResult: true,
},
{
name: "NotSoftDelete",
configPath: "valid.yml",
Expand Down Expand Up @@ -116,6 +168,20 @@ func TestConfig(t *testing.T) {
testedPath: ".ssh/",
expectedResult: true,
},
{
name: "ProtectedWithPrefix",
configPath: "valid.yml",
configFunction: models.Config.IsProtected,
testedPath: "./.ssh/",
expectedResult: true,
},
{
name: "ProtectedWithLongPrefix",
configPath: "valid.yml",
configFunction: models.Config.IsProtected,
testedPath: "./john-doe/crypto/.ssh/",
expectedResult: true,
},
{
name: "NotProtected",
configPath: "valid.yml",
Expand Down Expand Up @@ -146,6 +212,20 @@ func TestConfig(t *testing.T) {
testedPath: ".ssh/test.pem",
expectedResult: true,
},
{
name: "OverwriteWithPrefix",
configPath: "valid.yml",
configFunction: models.Config.ShouldOverwrite,
testedPath: "./.ssh/test.pem",
expectedResult: true,
},
{
name: "OverwriteWithLongPrefix",
configPath: "valid.yml",
configFunction: models.Config.ShouldOverwrite,
testedPath: "./john-doe/crypto/.ssh/test.pem",
expectedResult: true,
},
{
name: "DontOverwrite",
configPath: "valid.yml",
Expand Down
8 changes: 6 additions & 2 deletions src/patches/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func RmPatch(arguments []string, config models.Config) {

if shouldNotify {
fileNames := strings.Join(filePaths, ", ")
err := beeep.Notify("2rm", "Finished deletion request '"+fileNames+"'", "")
err := beeep.Notify("2rm", "Completed deletion '"+fileNames+"'", "")
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -81,7 +81,11 @@ func extractFilePaths(input []string) []string {

for _, str := range input {
if !strings.HasPrefix(str, "-") {
filePaths = append(filePaths, str)
if util.IsDirectory(str) && !strings.HasSuffix(str, "/") {
filePaths = append(filePaths, str+"/")
} else {
filePaths = append(filePaths, str)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/assets/configs/valid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ hard:
- ".next/"
- "*.partial"
soft:
- "backups/"
- "*.bak"
protected:
- ".ssh/"
Expand Down

0 comments on commit 129f95d

Please sign in to comment.