Skip to content

Commit

Permalink
Add config parse tests and --help passthrough
Browse files Browse the repository at this point in the history
  • Loading branch information
hudson-newey committed Sep 30, 2024
1 parent a48a03d commit fdcdce6
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 5 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ In this case, you can modify your `~/.local/share/2rm/config.yml` file to always
You can specify what directories are soft-deleted anb hard-deleted by using the `~/.local/share/2rm/config.yml` file.

```yml
# ~/.local/share/2rm/config.yml

# defaults to /tmp/2rm/ if not specified
# in the config file
# any files that are soft deleted will be
# backed up in the `backups` directory
backups: /tmp/2rm
backups: /tmp/2rm/
hard:
- node_modules/
- target/
Expand All @@ -59,5 +61,5 @@ hard:
# regardless of it they are configured
# for a hard delete
soft:
- *.bak
- "*.bak"
```
3 changes: 3 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ build:
mkdir -p $(OUT_DIR)
go build -o $(OUT_DIR)/2rm ./src/main.go

test:
go test ./src/...

clean:
rm -rf $(OUT_DIR)

Expand Down
7 changes: 7 additions & 0 deletions src/commands/commands_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package commands

import "testing"

func TestCommandsEcho(t *testing.T) {
Execute("echo 'Hello World'")
}
7 changes: 7 additions & 0 deletions src/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import (
func ParseConfig(path string) models.Config {
parsedConfig := models.Config{}

_, err := os.Stat(path)
if os.IsNotExist(err) {
// if the config file does not exist, we want to return an empty config
// this will act as a default config
return parsedConfig
}

content, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
Expand Down
73 changes: 73 additions & 0 deletions src/config/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package config

import (
"hudson-newey/2rm/src/models"
"path/filepath"
"reflect"
"testing"
)

func assertConfig(t *testing.T, configPath string, expectedConfig models.Config) {
testConfigDir := "../../tests/assets/configs/"
absolutePath, err := filepath.Abs(testConfigDir + configPath)
if err != nil {
t.Errorf("Failed to get absolute path")
}

realizedConfig := ParseConfig(absolutePath)

if !reflect.DeepEqual(expectedConfig, realizedConfig) {
t.Errorf("Expected %v but got %v", expectedConfig, realizedConfig)
}
}

func TestParsingConfig(t *testing.T) {
expectedConfig := models.Config{
Backups: "/tmp/2rm/",
Hard: []string{
"node_modules/",
"target/",
".angular/",
".next/",
},
Soft: []string{
"*.bak",
},
}

assertConfig(t, "valid.yml", expectedConfig)
}

// this test asserts that we can parse a partial config
// we do not have to check every combination of partial configs
// because that would result in a massive explosion of tests
// that would not provide much value
func TestOnlyHardConfig(t *testing.T) {
expectedConfig := models.Config{
Hard: []string{
"node_modules/",
"target/",
".angular/",
".next/",
},
}

assertConfig(t, "only_hard.yml", expectedConfig)
}

func TestOnlyBackups(t *testing.T) {
expectedConfig := models.Config{
Backups: "/tmp/2rm/",
}
assertConfig(t, "only_backups.yml", expectedConfig)
}

func TestParsingEmptyConfig(t *testing.T) {
expectedConfig := models.Config{}
assertConfig(t, "empty.yml", expectedConfig)
}

func TestParsingConfigWithMissingFile(t *testing.T) {
expectedConfig := models.Config{}
assertConfig(t, "missing.yml", expectedConfig)
}
55 changes: 55 additions & 0 deletions src/models/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package models

import "testing"

func TestShouldHardDelete(t *testing.T) {
t.Fatal("This test needs to be implemented!")
}

func TestShouldHardDeleteFalse(t *testing.T) {
t.Fatal("This test needs to be implemented!")
}

func TestShouldHardDeleteEmpty(t *testing.T) {
t.Fatal("This test needs to be implemented!")
}

func TestShouldSoftDelete(t *testing.T) {
t.Fatal("This test needs to be implemented!")
}

func TestShouldSoftDeleteFalse(t *testing.T) {
t.Fatal("This test needs to be implemented!")
}

func TestShouldSoftDeleteEmpty(t *testing.T) {
t.Fatal("This test needs to be implemented!")
}

func TestSoftDeleteDir(t *testing.T) {
t.Fatal("This test needs to be implemented!")
}

func TestSoftDeleteDirDefault(t *testing.T) {
t.Fatal("This test needs to be implemented!")
}

func TestMatchesPatternFile(t *testing.T) {
t.Fatal("This test needs to be implemented!")
}

func TestMatchesPatternDirectory(t *testing.T) {
t.Fatal("This test needs to be implemented!")
}

func TestMatchesPatternGlobPostfix(t *testing.T) {
t.Fatal("This test needs to be implemented!")
}

func TestMatchesPatternGlobPrefix(t *testing.T) {
t.Fatal("This test needs to be implemented!")
}

func TestMatchesAbsolutePath(t *testing.T) {
t.Fatal("This test needs to be implemented!")
}
20 changes: 20 additions & 0 deletions src/patches/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func RmPatch(arguments []string, config models.Config) {
removeDangerousArguments(arguments),
)

if shouldPassthrough(actionedArgs) {
command := "rm " + strings.Join(actionedArgs, " ")
commands.Execute(command)
return
}

filePaths := extractFilePaths(actionedArgs)
extractedArguments := extractArguments(actionedArgs)

Expand Down Expand Up @@ -55,6 +61,20 @@ func RmPatch(arguments []string, config models.Config) {
}
}

// sometimes we want to pass through the arguments to the original rm command
// e.g. when executing --help or --version
func shouldPassthrough(arguments []string) bool {
passthroughArguments := []string{"--help", "--version"}

for _, arg := range arguments {
if util.InArray(passthroughArguments, arg) {
return true
}
}

return false
}

func removeUnNeededArguments(arguments []string) []string {
unNeededArguments := []string{"-r", HARD_DELETE_CLA, SOFT_DELETE_CLA, SILENT_CLA}
returnedArguments := []string{}
Expand Down
1 change: 1 addition & 0 deletions src/patches/rm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package patches
5 changes: 2 additions & 3 deletions src/util/util.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package util

func InArray[T comparable](arr []T, query T) bool {
for _, value := range(arr) {
if (value == query) {
for _, value := range arr {
if value == query {
return true
}
}

return false
}

21 changes: 21 additions & 0 deletions src/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package util

import "testing"

func TestInArrayTrue(t *testing.T) {
expectedResult := true
realizedResult := InArray([]int{1, 2, 3}, 1)

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

func TestInArrayFalse(t *testing.T) {
expectedResult := false
realizedResult := InArray([]int{1, 2, 3}, 4)

if realizedResult != expectedResult {
t.Fatalf("Expected %v but got %v", expectedResult, realizedResult)
}
}
Empty file added tests/assets/configs/empty.yml
Empty file.
1 change: 1 addition & 0 deletions tests/assets/configs/only_backups.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
backups: /tmp/2rm/
5 changes: 5 additions & 0 deletions tests/assets/configs/only_hard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
hard:
- node_modules/
- target/
- .angular/
- .next/
1 change: 1 addition & 0 deletions tests/assets/configs/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory contains the configs that we use in tests
8 changes: 8 additions & 0 deletions tests/assets/configs/valid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
backups: /tmp/2rm/
hard:
- node_modules/
- target/
- .angular/
- .next/
soft:
- "*.bak"

0 comments on commit fdcdce6

Please sign in to comment.