FedP2P/internal/testutil/greeting.go

51 lines
1.1 KiB
Go
Raw Normal View History

2014-03-20 19:02:15 +08:00
// Package testutil contains stuff for testing torrent-related behaviour.
//
// "greeting" is a single-file torrent of a file called "greeting" that
// "contains "hello, world\n".
package testutil
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/anacrolix/torrent/metainfo"
)
var Greeting = Torrent{
Files: []File{{
Data: GreetingFileContents,
}},
Name: GreetingFileName,
}
const (
GreetingFileContents = "hello, world\n"
GreetingFileName = "greeting"
)
func CreateDummyTorrentData(dirName string) string {
f, _ := os.Create(filepath.Join(dirName, "greeting"))
2014-08-22 15:40:10 +08:00
defer f.Close()
2015-02-09 21:16:01 +08:00
f.WriteString(GreetingFileContents)
return f.Name()
}
func GreetingMetaInfo() *metainfo.MetaInfo {
return Greeting.Metainfo(5)
}
2014-03-20 19:02:15 +08:00
// Gives a temporary directory containing the completed "greeting" torrent,
// and a corresponding metainfo describing it. The temporary directory can be
// cleaned away with os.RemoveAll.
func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) {
tempDir, err := ioutil.TempDir(os.TempDir(), "")
if err != nil {
panic(err)
}
CreateDummyTorrentData(tempDir)
metaInfo = GreetingMetaInfo()
return
}