From c7baa9795247618b6fae67413e3ebbc2438f11da Mon Sep 17 00:00:00 2001 From: hudson-newey Date: Mon, 30 Sep 2024 20:53:50 +1000 Subject: [PATCH] Add config model tests --- src/commands/commands_test.go | 9 ++- src/config/parse_test.go | 5 +- src/models/config_test.go | 105 +++++++++++++++++++++-------- src/patches/rm_test.go | 1 - src/util/util_test.go | 11 +-- tests/assets/configs/abs_path.yml | 4 ++ tests/assets/configs/only_soft.yml | 3 + 7 files changed, 99 insertions(+), 39 deletions(-) delete mode 100644 src/patches/rm_test.go create mode 100644 tests/assets/configs/abs_path.yml create mode 100644 tests/assets/configs/only_soft.yml diff --git a/src/commands/commands_test.go b/src/commands/commands_test.go index 0f9e0a2..7fc6d4c 100644 --- a/src/commands/commands_test.go +++ b/src/commands/commands_test.go @@ -1,7 +1,10 @@ -package commands +package commands_test -import "testing" +import ( + "hudson-newey/2rm/src/commands" + "testing" +) func TestCommandsEcho(t *testing.T) { - Execute("echo 'Hello World'") + commands.Execute("echo 'Hello World'") } diff --git a/src/config/parse_test.go b/src/config/parse_test.go index b5f9006..587a2c4 100644 --- a/src/config/parse_test.go +++ b/src/config/parse_test.go @@ -1,6 +1,7 @@ -package config +package config_test import ( + "hudson-newey/2rm/src/config" "hudson-newey/2rm/src/models" "path/filepath" "reflect" @@ -14,7 +15,7 @@ func assertConfig(t *testing.T, configPath string, expectedConfig models.Config) t.Errorf("Failed to get absolute path") } - realizedConfig := ParseConfig(absolutePath) + realizedConfig := config.ParseConfig(absolutePath) if !reflect.DeepEqual(expectedConfig, realizedConfig) { t.Errorf("Expected %v but got %v", expectedConfig, realizedConfig) diff --git a/src/models/config_test.go b/src/models/config_test.go index c9f4b21..c0006e8 100644 --- a/src/models/config_test.go +++ b/src/models/config_test.go @@ -1,55 +1,102 @@ -package models +package models_test -import "testing" +import ( + "hudson-newey/2rm/src/config" + "hudson-newey/2rm/src/models" + "path/filepath" + "testing" +) + +func loadConfig(path string) models.Config { + testConfigDir := "../../tests/assets/configs/" + absolutePath, _ := filepath.Abs(testConfigDir + path) + return config.ParseConfig(absolutePath) +} func TestShouldHardDelete(t *testing.T) { - t.Fatal("This test needs to be implemented!") + testedConfig := loadConfig("valid.yml") + + expected := true + realized := testedConfig.ShouldHardDelete("node_modules/") + + if expected != realized { + t.Fatalf("Expected %v but got %v", expected, realized) + } } -func TestShouldHardDeleteFalse(t *testing.T) { - t.Fatal("This test needs to be implemented!") +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) { - t.Fatal("This test needs to be implemented!") + 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) { - t.Fatal("This test needs to be implemented!") + testedConfig := loadConfig("valid.yml") + + expected := true + realized := testedConfig.ShouldSoftDelete("file.bak") + + if expected != realized { + t.Fatalf("Expected %v but got %v", expected, realized) + } } -func TestShouldSoftDeleteFalse(t *testing.T) { - t.Fatal("This test needs to be implemented!") +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) { - t.Fatal("This test needs to be implemented!") -} + testedConfig := loadConfig("only_backups.yml") -func TestSoftDeleteDir(t *testing.T) { - t.Fatal("This test needs to be implemented!") -} + expected := false + realized := testedConfig.ShouldSoftDelete("file.bak") -func TestSoftDeleteDirDefault(t *testing.T) { - t.Fatal("This test needs to be implemented!") + if expected != realized { + t.Fatalf("Expected %v but got %v", expected, realized) + } } -func TestMatchesPatternFile(t *testing.T) { - t.Fatal("This test needs to be implemented!") -} +func TestHardMatchesAbsolutePath(t *testing.T) { + testedConfig := loadConfig("abs_path.yml") -func TestMatchesPatternDirectory(t *testing.T) { - t.Fatal("This test needs to be implemented!") -} + expected := true + realized := testedConfig.ShouldHardDelete("/tmp/2rm/") -func TestMatchesPatternGlobPostfix(t *testing.T) { - t.Fatal("This test needs to be implemented!") + if expected != realized { + t.Fatalf("Expected %v but got %v", expected, realized) + } } -func TestMatchesPatternGlobPrefix(t *testing.T) { - t.Fatal("This test needs to be implemented!") -} +func TestSoftMatchesAbsolutePath(t *testing.T) { + testedConfig := loadConfig("abs_path.yml") + + expected := true + realized := testedConfig.ShouldSoftDelete("/home/john-doe/.local/share/2rm/config.yml") -func TestMatchesAbsolutePath(t *testing.T) { - t.Fatal("This test needs to be implemented!") + if expected != realized { + t.Fatalf("Expected %v but got %v", expected, realized) + } } diff --git a/src/patches/rm_test.go b/src/patches/rm_test.go deleted file mode 100644 index 8411d5b..0000000 --- a/src/patches/rm_test.go +++ /dev/null @@ -1 +0,0 @@ -package patches diff --git a/src/util/util_test.go b/src/util/util_test.go index 168f64b..0e7eebc 100644 --- a/src/util/util_test.go +++ b/src/util/util_test.go @@ -1,10 +1,13 @@ -package util +package util_test -import "testing" +import ( + "hudson-newey/2rm/src/util" + "testing" +) func TestInArrayTrue(t *testing.T) { expectedResult := true - realizedResult := InArray([]int{1, 2, 3}, 1) + realizedResult := util.InArray([]int{1, 2, 3}, 1) if realizedResult != expectedResult { t.Fatalf("Expected %v but got %v", expectedResult, realizedResult) @@ -13,7 +16,7 @@ func TestInArrayTrue(t *testing.T) { func TestInArrayFalse(t *testing.T) { expectedResult := false - realizedResult := InArray([]int{1, 2, 3}, 4) + realizedResult := util.InArray([]int{1, 2, 3}, 4) if realizedResult != expectedResult { t.Fatalf("Expected %v but got %v", expectedResult, realizedResult) diff --git a/tests/assets/configs/abs_path.yml b/tests/assets/configs/abs_path.yml new file mode 100644 index 0000000..de5064c --- /dev/null +++ b/tests/assets/configs/abs_path.yml @@ -0,0 +1,4 @@ +hard: + - "/tmp/2rm/" +soft: + - "/home/john-doe/.local/share/2rm/config.yml" diff --git a/tests/assets/configs/only_soft.yml b/tests/assets/configs/only_soft.yml new file mode 100644 index 0000000..d5929c2 --- /dev/null +++ b/tests/assets/configs/only_soft.yml @@ -0,0 +1,3 @@ +soft: + - "*.bak" + - "backups/"