I hate commit messages

This commit is contained in:
didier amyot committed 2014-11-15 12:07:11 -08:00
1 parent 209af0d820
commit 8654c45e35
11 files changed
+278 -35

No files matched your search

+29
View File
@@ -5,6 +5,7 @@ import (
"crypto/rsa"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/big"
@@ -123,6 +124,8 @@ func GetRSAPrivateKey(size int) *rsa.PrivateKey {
}
// return a tmp file in the default tmp directory. If fails, the function
// will panic
func GetTmpEmptyFile() *os.File {
if f, err := ioutil.TempFile("", ""); err != nil {
panic(err)
@@ -131,6 +134,32 @@ func GetTmpEmptyFile() *os.File {
}
}
// returned a closed tmp file with text in it.
func GetTmpFileWithText(text string) *os.File {
f := GetTmpEmptyFile()
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
fmt.Fprint(f, text)
return f
}
// returns a path that can be safely use as tmp file
func GetTmpFileName() string {
f := GetTmpEmptyFile()
if err := f.Close(); err != nil {
panic(err)
}
if err := os.Remove(f.Name()); err != nil {
panic(err)
}
return f.Name()
}
// create a tmp dir and return its path. If failing, the method will panic
func GetTmpEmptyDir() string {
if dirName, err := ioutil.TempDir("", ""); err != nil {
panic(err)
+33
View File
@@ -5,6 +5,7 @@ import (
"io/ioutil"
"math/big"
"math/rand"
"os"
"reflect"
"testing"
)
@@ -135,3 +136,35 @@ func TestGetReadLength(t *testing.T) {
t.Error("bad read length")
}
}
func TestGetTmpFileWithText(t *testing.T) {
f := GetTmpFileWithText("some text")
defer func() {
if err := os.Remove(f.Name()); err != nil {
t.Error(err)
}
}()
if _, err := os.Stat(f.Name()); os.IsNotExist(err) {
t.Error("tmp file does not exists")
}
}
func TestGetTmpEmptyFile(t *testing.T) {
f := GetTmpEmptyFile()
defer func() {
if err := os.Remove(f.Name()); err != nil {
t.Error(err)
}
}()
if _, err := os.Stat(f.Name()); os.IsNotExist(err) {
t.Error("tmp file does not exists")
}
}
func TestGetTmpFileName(t *testing.T) {
f := GetTmpFileName()
if _, err := os.Stat(f); !os.IsNotExist(err) {
t.Error("tmp file exists")
}
}