From a915a99e2bf4ad4b34d571f9672664e3e9b1eec5 Mon Sep 17 00:00:00 2001 From: yalefresne Date: Sun, 23 Mar 2025 13:51:03 +0100 Subject: [PATCH] Feat: Add new utils functions and test utils - Ask for confirmation - Get module's root directory path - Trim white space and quote from string - Add test for all functions utils --- utils/utils.go | 46 ++++++++++++++++++++++++++- utils/utils_test.go | 77 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 utils/utils_test.go diff --git a/utils/utils.go b/utils/utils.go index 6617fb3..b8f68d8 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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 { @@ -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) + } + } +} diff --git a/utils/utils_test.go b/utils/utils_test.go new file mode 100644 index 0000000..0f55040 --- /dev/null +++ b/utils/utils_test.go @@ -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) + } + }) + } +}