mirror of https://github.com/grafana/grafana
CI: Move `CreateTempFile` - use it for `rpm`/`deb` packages (#56990)
* Move CreateTempFile - use it for rpm/deb packages * Fix typo * Fix tests:pull/57107/head
parent
fd0fcffc24
commit
c96b6a6ab0
@ -0,0 +1,26 @@ |
|||||||
|
package fsutil |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"os" |
||||||
|
) |
||||||
|
|
||||||
|
// CreateTempFile generates a temp filepath, based on the provided suffix.
|
||||||
|
// A typical generated path looks like /var/folders/abcd/abcdefg/A/1137975807.
|
||||||
|
func CreateTempFile(sfx string) (string, error) { |
||||||
|
var suffix string |
||||||
|
if sfx != "" { |
||||||
|
suffix = fmt.Sprintf("*-%s", sfx) |
||||||
|
} else { |
||||||
|
suffix = sfx |
||||||
|
} |
||||||
|
f, err := os.CreateTemp("", suffix) |
||||||
|
if err != nil { |
||||||
|
return "", err |
||||||
|
} |
||||||
|
if err := f.Close(); err != nil { |
||||||
|
return "", err |
||||||
|
} |
||||||
|
|
||||||
|
return f.Name(), nil |
||||||
|
} |
||||||
@ -0,0 +1,28 @@ |
|||||||
|
package fsutil |
||||||
|
|
||||||
|
import ( |
||||||
|
"strings" |
||||||
|
"testing" |
||||||
|
|
||||||
|
"github.com/stretchr/testify/require" |
||||||
|
) |
||||||
|
|
||||||
|
func TestCreateTempFile(t *testing.T) { |
||||||
|
t.Run("empty suffix, expects pattern like: /var/folders/abcd/abcdefg/A/1137975807", func(t *testing.T) { |
||||||
|
filePath, err := CreateTempFile("") |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
pathParts := strings.Split(filePath, "/") |
||||||
|
require.Greater(t, len(pathParts), 1) |
||||||
|
require.Len(t, strings.Split(pathParts[len(pathParts)-1], "-"), 1) |
||||||
|
}) |
||||||
|
|
||||||
|
t.Run("non-empty suffix, expects /var/folders/abcd/abcdefg/A/1137975807-foobar", func(t *testing.T) { |
||||||
|
filePath, err := CreateTempFile("foobar") |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
pathParts := strings.Split(filePath, "/") |
||||||
|
require.Greater(t, len(pathParts), 1) |
||||||
|
require.Len(t, strings.Split(pathParts[len(pathParts)-1], "-"), 2) |
||||||
|
}) |
||||||
|
} |
||||||
Loading…
Reference in new issue