testenv.go

 1package testenv
 2
 3import (
 4	"testing"
 5
 6	"github.com/fatih/color"
 7	"github.com/stretchr/testify/require"
 8
 9	"github.com/git-bug/git-bug/commands/execenv"
10	"github.com/git-bug/git-bug/entity"
11)
12
13const (
14	testUserName  = "John Doe"
15	testUserEmail = "jdoe@example.com"
16)
17
18func NewTestEnvAndUser(t *testing.T) (*execenv.Env, entity.Id) {
19	t.Helper()
20
21	// The Go testing framework either uses os.Stdout directly or a buffer
22	// depending on how the command is initially launched.  This results
23	// in os.Stdout.Fd() sometimes being a Terminal, and other times not
24	// being a Terminal which determines whether the ANSI library sends
25	// escape sequences to colorize the text.
26	//
27	// The line below disables all colorization during testing so that the
28	// git-bug command output is consistent in all test scenarios.
29	//
30	// See:
31	// - https://github.com/git-bug/git-bug/issues/926
32	// - https://github.com/golang/go/issues/57671
33	// - https://github.com/golang/go/blob/f721fa3be9bb52524f97b409606f9423437535e8/src/cmd/go/internal/test/test.go#L1180-L1208
34	// - https://github.com/golang/go/issues/34877
35	color.NoColor = true
36
37	testEnv := execenv.NewTestEnv(t)
38
39	i, err := testEnv.Backend.Identities().New(testUserName, testUserEmail)
40	require.NoError(t, err)
41
42	err = testEnv.Backend.SetUserIdentity(i)
43	require.NoError(t, err)
44
45	return testEnv, i.Id()
46}
47
48const (
49	testBugTitle   = "this is a bug title"
50	testBugMessage = "this is a bug message"
51)
52
53func NewTestEnvAndBug(t *testing.T) (*execenv.Env, entity.Id) {
54	t.Helper()
55
56	testEnv, _ := NewTestEnvAndUser(t)
57
58	b, _, err := testEnv.Backend.Bugs().New(testBugTitle, testBugMessage)
59	require.NoError(t, err)
60
61	return testEnv, b.Id()
62}
63
64const (
65	testCommentMessage = "this is a bug comment"
66)
67
68func NewTestEnvAndBugWithComment(t *testing.T) (*execenv.Env, entity.Id, entity.CombinedId) {
69	t.Helper()
70
71	env, bugID := NewTestEnvAndBug(t)
72
73	b, err := env.Backend.Bugs().Resolve(bugID)
74	require.NoError(t, err)
75
76	commentId, _, err := b.AddComment(testCommentMessage)
77	require.NoError(t, err)
78
79	return env, bugID, commentId
80}