go(algo/util): add createPath func
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-06-29 16:54:55 +02:00
parent babbd4201e
commit b9a4ff3192
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 30 additions and 0 deletions

@ -28,3 +28,18 @@ func createFolder(path string) error {
return err
}
// createPath creates all folders in the provided path, if they don't already
// exist.
func createPath(path string) error {
var err error
if _, err = os.Stat(path); errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Println(err)
}
}
return err
}

@ -22,3 +22,18 @@ func TestCreateFolder(t *testing.T) {
os.RemoveAll(testPath)
}
// nolint: ifshort
func TestCreatePath(t *testing.T) {
// let's assume this, too, will never exist in a clean project...
testPath := "whatever-path/whatever-subpath/subsubpath"
got := createPath(testPath)
var want error
if want != got {
t.Errorf("issue creating folders in path, testPath: %q, full cmd: %q", testPath, got.Error())
}
os.RemoveAll(testPath)
}