go(algo/util): add createFolder func
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
35d433e847
commit
b24f7db46f
22
algo/util.go
22
algo/util.go
@ -3,8 +3,28 @@
|
|||||||
|
|
||||||
package algo
|
package algo
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
func sanitiseFName(s string) string {
|
func sanitiseFName(s string) string {
|
||||||
return strings.ReplaceAll(s, " ", "_")
|
return strings.ReplaceAll(s, " ", "_")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createFolder creates a folder at the specified path, if it doesn't already
|
||||||
|
// exist.
|
||||||
|
func createFolder(path string) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if _, err = os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
||||||
|
err = os.Mkdir(path, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
24
algo/util_test.go
Normal file
24
algo/util_test.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
package algo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// nolint: ifshort
|
||||||
|
func TestCreateFolder(t *testing.T) {
|
||||||
|
// let's assume this will never exist in a clean project...
|
||||||
|
testPath := "whatever-path"
|
||||||
|
got := createFolder(testPath)
|
||||||
|
|
||||||
|
var want error
|
||||||
|
|
||||||
|
if want != got {
|
||||||
|
t.Errorf("issue creating folder: got %q", got.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
os.RemoveAll(testPath)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user