aboutsummaryrefslogtreecommitdiffstats
path: root/queue/config_test.go
blob: f5c19cc85d060d5c164a3361ca7a6ecc807c1a73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package queue

import (
	"os"
	"reflect"
	"strings"
	"testing"
	"time"
)

func TestLoad(t *testing.T) {
	cfg := Config{
		LocalDirs: []LocalDir{
			{
				Name:   "d1",
				Parser: "show",
				Dir:    "/tmp/{{ .Name }}",
				Replacements: []Replacement{
					{
						Pattern:     "\\.the\\.",
						Replacement: ".The.",
					}},
			},
		},
		Sites: []Site{{
			Name:         "foo",
			Dirs:         []string{"/site"},
			MaxAge:       "24h",
			Patterns:     []string{"^match"},
			Filters:      []string{"^skip"},
			SkipSymlinks: true,
			LocalDir:     "d1",
			Priorities:   []string{"important"},

			PostCommand: "xargs echo",
		}},
	}
	if err := cfg.load(); err != nil {
		t.Fatal(err)
	}

	site := cfg.Sites[0]
	if want := time.Duration(24) * time.Hour; site.maxAge != want {
		t.Errorf("Expected %s, got %s", want, site.maxAge)
	}
	if len(site.patterns) == 0 {
		t.Error("Expected non-empty patterns")
	}
	if len(site.filters) == 0 {
		t.Error("Expected non-empty filters")
	}
	if len(site.priorities) == 0 {
		t.Error("Expected non-empty priorities")
	}
	if site.localDir.Template == nil {
		t.Error("Expected template to be compiled")
	}
	if site.localDir.parser == nil {
		t.Error("Expected parser to be set")
	}
	if len(site.localDir.Replacements) == 0 {
		t.Error("Expected non-empty replacements")
	}
	if want := []string{"xargs", "echo"}; !reflect.DeepEqual(want, site.postCommand.Args) {
		t.Fatalf("Expected %+v, got %+v", want, site.postCommand.Args)
	}
}

func TestReadConfig(t *testing.T) {
	jsonConfig := `
{
  "LocalDirs": [
    {
      "Name": "d1",
      "Parser": "show",
      "Dir": "/tmp/d1/"
    },
    {
      "Name": "d2",
      "Parser": "movie",
      "Dir": "/tmp/d2/"
    }
  ],
  "Default": {
    "LocalDir": "d1"
  },
  "Sites": [
    {
      "Name": "foo"
    },
    {
      "Name": "bar",
      "LocalDir": "d2"
    },
    {
      "Name": "baz",
      "LocalDir": ""
    }
  ]
}
`
	cfg, err := readConfig(strings.NewReader(jsonConfig))
	if err != nil {
		t.Fatal(err)
	}
	// Test that defaults are applied and can be overridden
	var tests = []struct {
		i   int
		out string
	}{
		{0, "d1"},
		{1, "d2"},
		{2, ""},
	}
	for _, tt := range tests {
		site := cfg.Sites[tt.i]
		if got := site.LocalDir; got != tt.out {
			t.Errorf("got LocalDir=%q, want %q for Name=%q", got, tt.out, site.Name)
		}
	}
}

func TestSetLocalDir(t *testing.T) {
	jsonConfig := `
{
  "LocalDirs": [
    {
      "Name": "d1",
      "Parser": "show",
      "Dir": "/tmp/d1/"
    },
    {
      "Name": "d2",
      "Parser": "movie",
      "Dir": "/tmp/d2/"
    }
  ],
  "Default": {
    "LocalDir": "d1",
    "MaxAge": "24h"
  },
  "Sites": [
    {
      "Name": "foo"
    },
    {
      "Name": "bar"
    }
  ]
}
`
	cfg, err := readConfig(strings.NewReader(jsonConfig))
	if err != nil {
		t.Fatal(err)
	}
	if err := cfg.load(); err != nil {
		t.Fatal(err)
	}
	if err := cfg.SetLocalDir("invalid"); err == nil {
		t.Fatal("want error")
	}
	// Config remains unchanged after invalid local dir
	for _, s := range cfg.Sites {
		if want := "d1"; s.LocalDir != want {
			t.Errorf("got %q, want %q", s.LocalDir, want)
		}
	}
	if err := cfg.SetLocalDir("d2"); err != nil {
		t.Fatal(err)
	}
	for _, s := range cfg.Sites {
		if want := "d2"; s.LocalDir != want {
			t.Errorf("got %q, want %q", s.LocalDir, want)
		}
	}
}

func TestExpandUser(t *testing.T) {
	home := "/home/foo"
	os.Setenv("HOME", home)
	var tests = []struct {
		in  string
		out string
	}{
		{"", ""},
		{"/d", "/d"},
		{"~", home},
		{"~/", home},
		{"~bar", "/home/bar"},
		{"~bar/baz", "/home/bar/baz"},
		{"~/bar", home + "/bar"},
		{"/foo/~/bar", "/foo/~/bar"},
	}
	for i, tt := range tests {
		out := expandUser(tt.in)
		if out != tt.out {
			t.Errorf("#%d: expandUser(%q) = %q, want %q", i, tt.in, out, tt.out)
		}
	}
}