summaryrefslogtreecommitdiffstats
path: root/commands/list_test.go
blob: f2ce70010916fb80502f3258054a04c2a053d058 (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
package commands

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

	"github.com/spf13/cobra"
	"github.com/stretchr/testify/require"
)

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

	_, err := f()

	if err != nil {
		return "", err
	}

	w.Close()
	os.Stdout = old

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

func TestListAll(t *testing.T) {
	assert := require.New(t)
	dir, err := createSimpleTestSite(t, testSiteConfig{})

	assert.NoError(err)

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

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

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

	out, err := captureStdout(cmd.ExecuteC)
	assert.NoError(err)

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

	header, err := r.Read()
	assert.NoError(err)

	assert.Equal([]string{
		"path", "slug", "title",
		"date", "expiryDate", "publishDate",
		"draft", "permalink",
	}, header)

	record, err := r.Read()
	assert.Equal([]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/",
	}, record)
}