Skip to content

Commit

Permalink
Improve config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hudson-newey committed Oct 9, 2024
1 parent 53503f9 commit 7a61b19
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 120 deletions.
1 change: 1 addition & 0 deletions src/config/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestParsingConfig(t *testing.T) {
"target/",
".angular/",
".next/",
"*.partial",
},
Soft: []string{
"*.bak",
Expand Down
8 changes: 8 additions & 0 deletions src/models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import (
"hudson-newey/2rm/src/util"
"path/filepath"
"strings"
)

type Config struct {
Expand Down Expand Up @@ -77,6 +78,13 @@ func (config Config) SoftDeleteDir() string {
}

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
}

matched, _ := filepath.Match(pattern, path)
return matched
}
267 changes: 147 additions & 120 deletions src/models/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,134 +13,161 @@ func loadConfig(path string) models.Config {
return config.ParseConfig(absolutePath)
}

func TestShouldHardDelete(t *testing.T) {
testedConfig := loadConfig("valid.yml")

expected := true
realized := testedConfig.ShouldHardDelete("node_modules/")

if expected != realized {
t.Fatalf("Expected %v but got %v", expected, realized)
}
}

func TestShouldNotHardDelete(t *testing.T) {
testedConfig := loadConfig("valid.yml")

expected := false
realized := testedConfig.ShouldHardDelete("src/")

if expected != realized {
t.Fatalf("Expected %v but got %v", expected, realized)
}
}

func TestShouldHardDeleteEmpty(t *testing.T) {
testedConfig := loadConfig("only_backups.yml")

expected := false
realized := testedConfig.ShouldHardDelete("node_modules/")

if expected != realized {
t.Fatalf("Expected %v but got %v", expected, realized)
}
}

func TestShouldSoftDelete(t *testing.T) {
testedConfig := loadConfig("valid.yml")

expected := true
realized := testedConfig.ShouldSoftDelete("file.bak")

if expected != realized {
t.Fatalf("Expected %v but got %v", expected, realized)
}
}

func TestShouldNotSoftDelete(t *testing.T) {
testedConfig := loadConfig("valid.yml")

expected := false
realized := testedConfig.ShouldSoftDelete("file.txt")

if expected != realized {
t.Fatalf("Expected %v but got %v", expected, realized)
}
}

func TestShouldSoftDeleteEmpty(t *testing.T) {
testedConfig := loadConfig("only_backups.yml")

expected := false
realized := testedConfig.ShouldSoftDelete("file.bak")

if expected != realized {
t.Fatalf("Expected %v but got %v", expected, realized)
}
}

func TestHardMatchesAbsolutePath(t *testing.T) {
testedConfig := loadConfig("abs_path.yml")

expected := true
realized := testedConfig.ShouldHardDelete("/tmp/2rm/")

if expected != realized {
t.Fatalf("Expected %v but got %v", expected, realized)
}
}

func TestSoftMatchesAbsolutePath(t *testing.T) {
testedConfig := loadConfig("abs_path.yml")

expected := true
realized := testedConfig.ShouldSoftDelete("/home/john-doe/.local/share/2rm/config.yml")

if expected != realized {
t.Fatalf("Expected %v but got %v", expected, realized)
func assertConfig(
t *testing.T,
configPath string,
configFunction func(models.Config, string) bool,
testedPath string,
expectedResult bool,
) {
expectedConfig := expectedResult

testedConfig := loadConfig(configPath)
realizedConfig := configFunction(testedConfig, testedPath)

if expectedConfig != realizedConfig {
t.Fatalf("Expected %v but got %v", expectedConfig, realizedConfig)
}
}

func TestIsProtected(t *testing.T) {
testedConfig := loadConfig("valid.yml")

expected := true
realized := testedConfig.IsProtected(".ssh/")

if expected != realized {
t.Fatalf("Expected %v but got %v", expected, realized)
func runTests(t *testing.T, tests []Test) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assertConfig(t, test.configPath, test.configFunction, test.testedPath, test.expectedResult)
})
}
}

func TestNotProtected(t *testing.T) {
testedConfig := loadConfig("valid.yml")

expected := false
realized := testedConfig.IsProtected("src/")

if expected != realized {
t.Fatalf("Expected %v but got %v", expected, realized)
}
type Test struct {
name string
configPath string
configFunction func(models.Config, string) bool
testedPath string
expectedResult bool
}

func TestShouldOverwrite(t *testing.T) {
testedConfig := loadConfig("valid.yml")

expected := true
realized := testedConfig.ShouldOverwrite(".ssh/test.pem")

if expected != realized {
t.Fatalf("Expected %v but got %v", expected, realized)
func TestConfig(t *testing.T) {
tests := []Test{
// hard deletes
{
name: "HardDelete",
configPath: "valid.yml",
configFunction: models.Config.ShouldHardDelete,
testedPath: "node_modules/",
expectedResult: true,
},
{
name: "NotHardDelete",
configPath: "valid.yml",
configFunction: models.Config.ShouldHardDelete,
testedPath: "src/",
expectedResult: false,
},
{
name: "HardDeleteEmpty",
configPath: "only_backups.yml",
configFunction: models.Config.ShouldHardDelete,
testedPath: "node_modules/",
expectedResult: false,
},
{
name: "HardMatchesAbsolutePath",
configPath: "abs_path.yml",
configFunction: models.Config.ShouldHardDelete,
testedPath: "/tmp/2rm/",
expectedResult: true,
},

// soft deletes
{
name: "SoftDelete",
configPath: "valid.yml",
configFunction: models.Config.ShouldSoftDelete,
testedPath: "file.bak",
expectedResult: true,
},
{
name: "NotSoftDelete",
configPath: "valid.yml",
configFunction: models.Config.ShouldSoftDelete,
testedPath: "file.txt",
expectedResult: false,
},
{
name: "SoftDeleteEmpty",
configPath: "only_backups.yml",
configFunction: models.Config.ShouldSoftDelete,
testedPath: "file.bak",
expectedResult: false,
},
{
name: "SoftMatchesAbsolutePath",
configPath: "abs_path.yml",
configFunction: models.Config.ShouldSoftDelete,
testedPath: "/home/john-doe/.local/share/2rm/config.yml",
expectedResult: true,
},

// protected files
{
name: "IsProtected",
configPath: "valid.yml",
configFunction: models.Config.IsProtected,
testedPath: ".ssh/",
expectedResult: true,
},
{
name: "NotProtected",
configPath: "valid.yml",
configFunction: models.Config.IsProtected,
testedPath: "src/",
expectedResult: false,
},
{
name: "ProtectedEmpty",
configPath: "only_backups.yml",
configFunction: models.Config.IsProtected,
testedPath: ".ssh/",
expectedResult: false,
},
{
name: "ProtectedMatchesAbsolutePath",
configPath: "abs_path.yml",
configFunction: models.Config.IsProtected,
testedPath: "/home/john-doe/.ssh/id_rsa",
expectedResult: true,
},

// overwrite
{
name: "Overwrite",
configPath: "valid.yml",
configFunction: models.Config.ShouldOverwrite,
testedPath: ".ssh/test.pem",
expectedResult: true,
},
{
name: "DontOverwrite",
configPath: "valid.yml",
configFunction: models.Config.ShouldOverwrite,
testedPath: "non-existent.txt",
expectedResult: false,
},
{
name: "OverwriteEmpty",
configPath: "only_backups.yml",
configFunction: models.Config.ShouldOverwrite,
testedPath: ".ssh/test.pem",
expectedResult: false,
},
{
name: "OverwriteAbsolutePath",
configPath: "abs_path.yml",
configFunction: models.Config.ShouldOverwrite,
testedPath: "/home/john-doe/.ssh/key.pem",
expectedResult: true,
},
}
}

func TestNotShouldOverwrite(t *testing.T) {
testedConfig := loadConfig("valid.yml")

expected := false
realized := testedConfig.ShouldOverwrite("non-existent.txt")

if expected != realized {
t.Fatalf("Expected %v but got %v", expected, realized)
}
runTests(t, tests)
}
4 changes: 4 additions & 0 deletions tests/assets/configs/abs_path.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ hard:
- "/tmp/2rm/"
soft:
- "/home/john-doe/.local/share/2rm/config.yml"
protected:
- "/home/john-doe/.ssh/id_rsa"
overwrite:
- "/home/john-doe/.ssh/key.pem"
1 change: 1 addition & 0 deletions tests/assets/configs/valid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ hard:
- "target/"
- ".angular/"
- ".next/"
- "*.partial"
soft:
- "*.bak"
protected:
Expand Down

0 comments on commit 7a61b19

Please sign in to comment.