github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/pr/shared/templates_test.go (about) 1 package shared 2 3 import ( 4 "net/http" 5 "os" 6 "path/filepath" 7 "testing" 8 9 fd "github.com/ungtb10d/cli/v2/internal/featuredetection" 10 "github.com/ungtb10d/cli/v2/internal/ghrepo" 11 "github.com/ungtb10d/cli/v2/pkg/httpmock" 12 "github.com/ungtb10d/cli/v2/pkg/prompt" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestTemplateManager_hasAPI(t *testing.T) { 17 rootDir := t.TempDir() 18 legacyTemplateFile := filepath.Join(rootDir, ".github", "ISSUE_TEMPLATE.md") 19 _ = os.MkdirAll(filepath.Dir(legacyTemplateFile), 0755) 20 _ = os.WriteFile(legacyTemplateFile, []byte("LEGACY"), 0644) 21 22 tr := httpmock.Registry{} 23 httpClient := &http.Client{Transport: &tr} 24 defer tr.Verify(t) 25 26 tr.Register( 27 httpmock.GraphQL(`query IssueTemplates\b`), 28 httpmock.StringResponse(`{"data":{"repository":{ 29 "issueTemplates": [ 30 {"name": "Bug report", "body": "I found a problem"}, 31 {"name": "Feature request", "body": "I need a feature"} 32 ] 33 }}}`)) 34 35 m := templateManager{ 36 repo: ghrepo.NewWithHost("OWNER", "REPO", "example.com"), 37 rootDir: rootDir, 38 allowFS: true, 39 isPR: false, 40 httpClient: httpClient, 41 detector: &fd.EnabledDetectorMock{}, 42 } 43 44 hasTemplates, err := m.HasTemplates() 45 assert.NoError(t, err) 46 assert.True(t, hasTemplates) 47 48 assert.Equal(t, "LEGACY", string(m.LegacyBody())) 49 50 //nolint:staticcheck // SA1019: prompt.NewAskStubber is deprecated: use PrompterMock 51 as := prompt.NewAskStubber(t) 52 as.StubPrompt("Choose a template"). 53 AssertOptions([]string{"Bug report", "Feature request", "Open a blank issue"}). 54 AnswerWith("Feature request") 55 56 tpl, err := m.Choose() 57 58 assert.NoError(t, err) 59 assert.Equal(t, "Feature request", tpl.NameForSubmit()) 60 assert.Equal(t, "I need a feature", string(tpl.Body())) 61 } 62 63 func TestTemplateManager_hasAPI_PullRequest(t *testing.T) { 64 rootDir := t.TempDir() 65 legacyTemplateFile := filepath.Join(rootDir, ".github", "PULL_REQUEST_TEMPLATE.md") 66 _ = os.MkdirAll(filepath.Dir(legacyTemplateFile), 0755) 67 _ = os.WriteFile(legacyTemplateFile, []byte("LEGACY"), 0644) 68 69 tr := httpmock.Registry{} 70 httpClient := &http.Client{Transport: &tr} 71 defer tr.Verify(t) 72 73 tr.Register( 74 httpmock.GraphQL(`query PullRequestTemplates\b`), 75 httpmock.StringResponse(`{"data":{"repository":{ 76 "pullRequestTemplates": [ 77 {"filename": "bug_pr.md", "body": "I fixed a problem"}, 78 {"filename": "feature_pr.md", "body": "I added a feature"} 79 ] 80 }}}`)) 81 82 m := templateManager{ 83 repo: ghrepo.NewWithHost("OWNER", "REPO", "example.com"), 84 rootDir: rootDir, 85 allowFS: true, 86 isPR: true, 87 httpClient: httpClient, 88 detector: &fd.EnabledDetectorMock{}, 89 } 90 91 hasTemplates, err := m.HasTemplates() 92 assert.NoError(t, err) 93 assert.True(t, hasTemplates) 94 95 assert.Equal(t, "LEGACY", string(m.LegacyBody())) 96 97 //nolint:staticcheck // SA1019: prompt.NewAskStubber is deprecated: use PrompterMock 98 as := prompt.NewAskStubber(t) 99 as.StubPrompt("Choose a template"). 100 AssertOptions([]string{"bug_pr.md", "feature_pr.md", "Open a blank pull request"}). 101 AnswerWith("bug_pr.md") 102 103 tpl, err := m.Choose() 104 105 assert.NoError(t, err) 106 assert.Equal(t, "", tpl.NameForSubmit()) 107 assert.Equal(t, "I fixed a problem", string(tpl.Body())) 108 }