spongy

A Unixy IRC client
git clone https://git.woozle.org/neale/spongy.git

spongy / spongyd
Neale Pickett  ·  2016-01-18

network_test.go

 1package main
 2
 3import (
 4	"io/ioutil"
 5	"os"
 6	"path"
 7	"strings"
 8	"testing"
 9	"time"
10)
11
12func writeFile(fn string, data string) {
13	ioutil.WriteFile(fn, []byte(data), os.ModePerm)
14}
15
16func createNetwork (t *testing.T, parent string) (base string, current string) {
17	base, err := ioutil.TempDir(parent, "spongy-test")
18	if err != nil {
19		t.Fatal(err)
20	}
21	
22	writeFile(path.Join(base, "nick"), "SpongyTest")
23	writeFile(path.Join(base, "server"), "moo.slashnet.org:6697")
24	writeFile(path.Join(base, "channels"), "#SpongyTest")
25	os.Mkdir(path.Join(base, "outq"), os.ModePerm)
26	os.Mkdir(path.Join(base, "log"), os.ModePerm)
27	
28	current = path.Join(base, "log", "current")
29	
30	return
31}
32
33func expect(t *testing.T, fpath string, needle string) {
34	for i := 0; i < 8; i += 1 {
35		if i > 0 {
36			time.Sleep(1 * time.Second)
37		}
38
39		fpBytes, err := ioutil.ReadFile(fpath)
40		if err != nil {
41			t.Log(err)
42			time.Sleep(1 * time.Second)
43			continue
44		}
45		fpString := string(fpBytes)
46		if strings.Contains(fpString, needle) {
47			return
48		}
49	}
50	t.Errorf("Could not find %#v in %s", needle, fpath)
51}
52
53func TestCreateNetwork(t *testing.T) {
54	base, _ := createNetwork(t, "")
55	
56	if fi, err := os.Stat(path.Join(base, "nick")); err != nil {
57		t.Error(err)
58	} else if fi.IsDir() {
59		t.Error("%s is not a regular file", path.Join(base, "nick"))
60	}
61	
62	os.RemoveAll(base)
63	if _, err := os.Stat(path.Join(base, "outq")); err == nil {
64		t.Error("Didn't unlink outq")
65	}
66}
67
68func TestConnect(t *testing.T) {
69	base, current := createNetwork(t, "")
70	defer os.RemoveAll(base)
71
72	n := NewNetwork(base)
73	go n.Connect()
74	
75	time.Sleep(5 * time.Second)
76	
77	expect(t, current, " 001 ")
78	expect(t, current, " JOIN " + n.Nick + " #SpongyTest")
79	
80	ioutil.WriteFile(path.Join(base, "outq", "merf"), []byte("PART #SpongyTest\n"), os.ModePerm)
81	expect(t, current, " PART ")
82	
83	n.Close()
84	return
85}