]> Sergey Matveev's repositories - btrtrc.git/blob - internal/testutil/greeting.go
Begin restructuring testutil to support testing torrents with various properties
[btrtrc.git] / internal / testutil / greeting.go
1 // Package testutil contains stuff for testing torrent-related behaviour.
2 //
3 // "greeting" is a single-file torrent of a file called "greeting" that
4 // "contains "hello, world\n".
5
6 package testutil
7
8 import (
9         "io/ioutil"
10         "os"
11         "path/filepath"
12
13         "github.com/anacrolix/torrent/metainfo"
14 )
15
16 var Greeting = Torrent{
17         Files: []File{{
18                 Data: GreetingFileContents,
19         }},
20         Name: GreetingFileName,
21 }
22
23 const (
24         GreetingFileContents = "hello, world\n"
25         GreetingFileName     = "greeting"
26 )
27
28 func CreateDummyTorrentData(dirName string) string {
29         f, _ := os.Create(filepath.Join(dirName, "greeting"))
30         defer f.Close()
31         f.WriteString(GreetingFileContents)
32         return f.Name()
33 }
34
35 func GreetingMetaInfo() *metainfo.MetaInfo {
36         return Greeting.Metainfo(5)
37 }
38
39 // Gives a temporary directory containing the completed "greeting" torrent,
40 // and a corresponding metainfo describing it. The temporary directory can be
41 // cleaned away with os.RemoveAll.
42 func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) {
43         tempDir, err := ioutil.TempDir(os.TempDir(), "")
44         if err != nil {
45                 panic(err)
46         }
47         CreateDummyTorrentData(tempDir)
48         metaInfo = GreetingMetaInfo()
49         return
50 }