Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package utils

import (
"os"
"bufio"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
)

// Paths functions
func HomeDir() string {
homeDir, err := os.UserHomeDir()
if err != nil {
Expand All @@ -15,8 +21,46 @@ func HomeDir() string {
return homeDir
}

func GetModuleRoot() string {
_, filename, _, _ := runtime.Caller(0)
rootDir, _ := strings.CutSuffix(filepath.Dir(filename), "utils")

return rootDir
}

// Custom strings functions
func CleanString(s string) string {
cleanString := strings.TrimSpace(s)
cleanString = strings.Trim(cleanString, "\"")
cleanString = strings.Trim(cleanString, "'")

return cleanString
}

func RemoveQuote(s string) string {
s = strings.ReplaceAll(s, "\"", "")
s = strings.ReplaceAll(s, "'", "")
return s
}

// Others utils functions

func AskForConfirmation(s string, reader io.Reader, writer io.Writer) bool {
scanner := bufio.NewScanner(reader)

for {
fmt.Fprintf(writer, "%s [y/n]: ", s)

if scanner.Scan() {
response := strings.ToLower(strings.TrimSpace(scanner.Text()))

if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
} else if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
}
77 changes: 77 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package utils

import (
"bytes"
"os"
"strings"
"testing"
)

const cmd = "dvopsctl"

func TestHomeDir(t *testing.T) {
if HomeDir() != os.Getenv("HOME") {
t.Errorf("The home directory is not the right one")
}
}

func TestGetModuleRoot(t *testing.T) {
rootDir := GetModuleRoot()
if !strings.HasSuffix(rootDir, cmd+"/") {
t.Errorf("%s does not contain %s, it does not seems to be the root directory of the module.", rootDir, cmd)
}
}

func TestCleanString(t *testing.T) {
cases := []struct {
str string
name string
}{
{" A string with whitespace ", "Test CleanString() with whitespace"},
{" \"A string with double quote and whitespace\" ", "Test CleanString() with whitespace and double quote"},
{" 'A string with simple quote and whitespace' ", "Test CleanString() with whitespace and simple quote"},
{"\"A string with double quote only\"", "Test CleanString() with double quote"},
{"'A string with simple quote only'", "Test CleanString() with simple quote"},
}
for _, testCase := range cases {
t.Run(testCase.name, func(t *testing.T) {
cleandStr := CleanString(testCase.str)

for _, subStr := range []string{ " ", "\"", "'" } {
if strings.HasPrefix(cleandStr, subStr) ||
strings.HasSuffix(cleandStr, subStr) {
t.Errorf("String is not clean, quote or whitespace are present")
}
}
})
}
}

func TestAskForConfirmation(t *testing.T) {
cases := []struct {
input string
expected bool
name string
}{
{"random\nn", false, "Test not valid response random then valid response n"},
{"y", true, "Test valid response y"},
{"yes", true, "Test valid response yes"},
{"n", false, "Test valid response n"},
{"no", false, "Test valid response no"},
{"YES", true, "Test valid response YES"},
{"NO", false, "Test valid response NO"},
}

for _, testCase := range cases {
t.Run(testCase.name, func(t *testing.T) {
reader := strings.NewReader(testCase.input + "\n")
writer := new(bytes.Buffer)

result := AskForConfirmation(testCase.name + "?", reader, writer)

if result != testCase.expected {
t.Errorf("AskForConfirmation(%q) = %v; want %v", testCase.input, result, testCase.expected)
}
})
}
}