Skip to content

Commit

Permalink
swarm/api: Can't use ioutil.TempFile - it returns predictable extensi…
Browse files Browse the repository at this point in the history
…on only since go1.11, a bit of copy-paste (ethersphere#527, ethersphere#944)
  • Loading branch information
nizsheanez committed Sep 24, 2018
1 parent b632a2b commit 8dc449a
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion swarm/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"io/ioutil"
"math/big"
"os"
"path/filepath"
"strconv"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -464,8 +466,23 @@ func testDetectContentType(t *testing.T, fileName, content, expectedContentType
}
}

// tempFile copy of ioutil.TempFile - because before go1.11 it adding random suffix
func tempFile(fileName string) (f *os.File, err error) {
nconflict := 0
for i := 0; i < 10000; i++ {
name := filepath.Join(os.TempDir(), strconv.Itoa(nconflict)+fileName)
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if os.IsExist(err) {
nconflict++
continue
}
break
}
return
}

func tempFileWithContent(fileName string, content string) (*os.File, error) {
f, err := ioutil.TempFile("", "*"+fileName)
f, err := tempFile(fileName)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8dc449a

Please sign in to comment.