summaryrefslogtreecommitdiffhomepage
path: root/commands/list_test.go
blob: 6f3d6c74df361fcb38f557193c12e848872f4085 (plain)
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
package commands

import (
	"bytes"
	"encoding/csv"
	"io"
	"os"
	"path/filepath"
	"strings"
	"testing"

	qt "github.com/frankban/quicktest"
)

func captureStdout(f func() error) (string, error) {
	old := os.Stdout
	r, w, _ := os.Pipe()
	os.Stdout = w

	err := f()

	w.Close()
	os.Stdout = old

	var buf bytes.Buffer
	io.Copy(&buf, r)
	return buf.String(), err
}

func TestListAll(t *testing.T) {
	c := qt.New(t)
	dir, clean, err := createSimpleTestSite(t, testSiteConfig{})
	defer clean()

	c.Assert(err, qt.IsNil)

	hugoCmd := newCommandsBuilder().addAll().build()
	cmd := hugoCmd.getCommand()

	defer func() {
		os.RemoveAll(dir)
	}()

	cmd.SetArgs([]string{"-s=" + dir, "list", "all"})

	out, err := captureStdout(func() error {
		_, err := cmd.ExecuteC()
		return err
	})
	c.Assert(err, qt.IsNil)

	r := csv.NewReader(strings.NewReader(out))

	header, err := r.Read()

	c.Assert(err, qt.IsNil)
	c.Assert(header, qt.DeepEquals, []string{
		"path", "slug", "title",
		"date", "expiryDate", "publishDate",
		"draft", "permalink",
	})

	record, err := r.Read()

	c.Assert(err, qt.IsNil)
	c.Assert(record, qt.DeepEquals, []string{
		filepath.Join("content", "p1.md"), "", "P1",
		"0001-01-01T00:00:00Z", "0001-01-01T00:00:00Z", "0001-01-01T00:00:00Z",
		"false", "https://example.org/p1/",
	})
}