From 0303244e3c48b1314072f249de53340a59369367 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Sun, 27 Aug 2023 16:48:15 +0200 Subject: [PATCH] Adds doc and unit tests Former-commit-id: 6f60f97947111b4fc65cc0e72d18770ba6094088 --- pkg/obiutils/path.go | 17 ++++++++++---- pkg/obiutils/path_test.go | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 pkg/obiutils/path_test.go diff --git a/pkg/obiutils/path.go b/pkg/obiutils/path.go index 7d941d9..598afd3 100644 --- a/pkg/obiutils/path.go +++ b/pkg/obiutils/path.go @@ -5,12 +5,19 @@ import ( "strings" ) +// RemoveAllExt removes all file extensions from the given path. +// +// Parameters: +// - p: the path to remove file extensions from (string). +// +// Returns: +// - The path without any file extensions (string). func RemoveAllExt(p string) string { - + for ext := path.Ext(p); len(ext) > 0; ext = path.Ext(p) { - p = strings.TrimSuffix(p, ext) + p = strings.TrimSuffix(p, ext) } - + return p - - } \ No newline at end of file + +} diff --git a/pkg/obiutils/path_test.go b/pkg/obiutils/path_test.go new file mode 100644 index 0000000..265fb0a --- /dev/null +++ b/pkg/obiutils/path_test.go @@ -0,0 +1,49 @@ +package obiutils + +import ( + "testing" +) + +// TestRemoveAllExt is a test function for the RemoveAllExt function. +// +// It tests the RemoveAllExt function by providing different test cases +// with different file paths and expected results. It ensures that the +// RemoveAllExt function correctly removes all file extensions and returns +// the file path without any extensions. +// +// Parameter(s): +// - t: A testing.T value representing the test framework. +// +// Return type(s): None. +func TestRemoveAllExt(t *testing.T) { + tests := []struct { + name string + path string + expected string + }{ + { + name: "No extensions", + path: "path/to/file", + expected: "path/to/file", + }, + { + name: "Single extension", + path: "path/to/file.txt", + expected: "path/to/file", + }, + { + name: "Multiple extensions", + path: "path/to/file.tar.gz", + expected: "path/to/file", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := RemoveAllExt(tt.path) + if result != tt.expected { + t.Errorf("Expected %q, but got %q", tt.expected, result) + } + }) + } +}