aboutsummaryrefslogtreecommitdiffstats
path: root/commands/bug/testenv/testenv.go
blob: ffa428d43cc2d37c0784f1a78f19159f1605f9c5 (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
package testenv

import (
	"testing"

	"github.com/fatih/color"
	"github.com/stretchr/testify/require"

	"github.com/MichaelMure/git-bug/commands/execenv"
	"github.com/MichaelMure/git-bug/entity"
)

const (
	testUserName  = "John Doe"
	testUserEmail = "jdoe@example.com"
)

func NewTestEnvAndUser(t *testing.T) (*execenv.Env, entity.Id) {
	t.Helper()

	// The Go testing framework either uses os.Stdout directly or a buffer
	// depending on how the command is initially launched.  This results
	// in os.Stdout.Fd() sometimes being a Terminal, and other times not
	// being a Terminal which determines whether the ANSI library sends
	// escape sequences to colorize the text.
	//
	// The line below disables all colorization during testing so that the
	// git-bug command output is consistent in all test scenarios.
	//
	// See:
	// - https://github.com/MichaelMure/git-bug/issues/926
	// - https://github.com/golang/go/issues/57671
	// - https://github.com/golang/go/blob/f721fa3be9bb52524f97b409606f9423437535e8/src/cmd/go/internal/test/test.go#L1180-L1208
	// - https://github.com/golang/go/issues/34877
	color.NoColor = true

	testEnv := execenv.NewTestEnv(t)

	i, err := testEnv.Backend.Identities().New(testUserName, testUserEmail)
	require.NoError(t, err)

	err = testEnv.Backend.SetUserIdentity(i)
	require.NoError(t, err)

	return testEnv, i.Id()
}

const (
	testBugTitle   = "this is a bug title"
	testBugMessage = "this is a bug message"
)

func NewTestEnvAndBug(t *testing.T) (*execenv.Env, entity.Id) {
	t.Helper()

	testEnv, _ := NewTestEnvAndUser(t)

	b, _, err := testEnv.Backend.Bugs().New(testBugTitle, testBugMessage)
	require.NoError(t, err)

	return testEnv, b.Id()
}

const (
	testCommentMessage = "this is a bug comment"
)

func NewTestEnvAndBugWithComment(t *testing.T) (*execenv.Env, entity.Id, entity.CombinedId) {
	t.Helper()

	env, bugID := NewTestEnvAndBug(t)

	b, err := env.Backend.Bugs().Resolve(bugID)
	require.NoError(t, err)

	commentId, _, err := b.AddComment(testCommentMessage)
	require.NoError(t, err)

	return env, bugID, commentId
}