Skip to content

Commit d4e525b

Browse files
authored
feat(tty): support no tty (#33)
* feat(tty): support no tty * feat(e2e): add e2e tests
1 parent f94d178 commit d4e525b

File tree

12 files changed

+57
-32
lines changed

12 files changed

+57
-32
lines changed

.github/workflows/e2e.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ jobs:
6161
key: commitizen-${{ github.event.pull_request.number }}-${{ github.sha }}
6262
- name: Run E2E test
6363
run: |
64-
GOPATH=~/go make e2e
64+
GOPATH=~/go make e2e NO_TTY=1

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Options:
2424
This option is available when using: make release
2525
V Set to 1 enable verbose build. Default is 0.
2626
DEBUG Whether to generate debug symbols. Default is 0.
27+
NO_TTY Make sure that the TTY (terminal) is never used for
28+
any output. Default is 0.
2729
endef
2830
export USAGE_OPTIONS
2931

cmd/cz/cz.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ func New() *cobra.Command {
2727
}
2828

2929
conf := config.New()
30-
tmpl, err := conf.Run()
30+
tmpl, err := conf.Run(o.NoTTY)
3131
if err != nil {
3232
return err
3333
}
3434

35-
msg, err := tmpl.Run()
35+
msg, err := tmpl.Run(o.NoTTY)
3636
if err != nil {
3737
return err
3838
}

hack/include/test.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
GINKGO := $(shell go env GOPATH)/bin/ginkgo
1616
CLI ?= $(OUTPUT_DIR)/commitizen
17+
NO_TTY ?= 0
1718

1819
.PHONY: test.cover
1920
test.cover:
@@ -24,4 +25,4 @@ test.cover:
2425
.PHONY: test.e2e
2526
test.e2e: tools.verify.ginkgo
2627
@echo "===========> Run e2e test, CLI: $(CLI)"
27-
@$(GINKGO) -v $(REPO_ROOT)/test/e2e -- -cli=$(CLI)
28+
@$(GINKGO) -v $(REPO_ROOT)/test/e2e -- -cli=$(CLI) -no-tty=$(NO_TTY)

internal/config/config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"path/filepath"
99

10-
tea "github.com/charmbracelet/bubbletea"
1110
"github.com/shipengqi/golib/convutil"
1211
"github.com/shipengqi/golib/fsutil"
1312
"github.com/shipengqi/golib/sysutil"
@@ -67,14 +66,14 @@ func (c *Config) initialize() error {
6766
return nil
6867
}
6968

70-
func (c *Config) Run() (*render.Template, error) {
69+
func (c *Config) Run(noTTY bool) (*render.Template, error) {
7170
err := c.initialize()
7271
if err != nil {
7372
return nil, err
7473
}
7574
if len(c.others) > 0 {
7675
model := c.createTemplatesSelect("Select a template to use for this commit:")
77-
if _, err := tea.NewProgram(model).Run(); err != nil {
76+
if _, err := ui.Run(model, noTTY); err != nil {
7877
return nil, err
7978
}
8079
if model.Canceled() {

internal/options/options.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
type Options struct {
1010
DryRun bool
11+
NoTTY bool
1112
GitOptions *git.Options
1213
}
1314

@@ -22,4 +23,7 @@ func (o *Options) AddFlags(f *pflag.FlagSet) {
2223
o.GitOptions.AddFlags(f)
2324

2425
f.BoolVar(&o.DryRun, "dry-run", o.DryRun, "you can use the --dry-run flag to preview the message that would be committed, without really submitting it.")
26+
f.BoolVar(&o.NoTTY, "no-tty", o.NoTTY, "make sure that the TTY (terminal) is never used for any output.")
27+
28+
_ = f.MarkHidden("no-tty")
2529
}

internal/render/template.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"strings"
77
"text/template"
88

9-
tea "github.com/charmbracelet/bubbletea"
10-
119
"github.com/shipengqi/commitizen/internal/ui"
1210
)
1311

@@ -60,7 +58,7 @@ func NewTemplate() (*Template, error) {
6058
return t, nil
6159
}
6260

63-
func (t *Template) Run() ([]byte, error) {
61+
func (t *Template) Run(noTTY bool) ([]byte, error) {
6462
err := t.init()
6563
if err != nil {
6664
return nil, err
@@ -72,7 +70,7 @@ func (t *Template) Run() ([]byte, error) {
7270

7371
values := map[string]interface{}{}
7472
for _, v := range t.models {
75-
if _, err = tea.NewProgram(v.model).Run(); err != nil {
73+
if _, err = ui.Run(v.model, noTTY); err != nil {
7674
return nil, err
7775
}
7876
if v.model.Canceled() {

internal/ui/ui.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ui
2+
3+
import tea "github.com/charmbracelet/bubbletea"
4+
5+
func Run(model tea.Model, noTTY bool) (tea.Model, error) {
6+
var p *tea.Program
7+
if noTTY {
8+
p = tea.NewProgram(model, tea.WithInput(nil))
9+
} else {
10+
p = tea.NewProgram(model)
11+
}
12+
return p.Run()
13+
}

test/e2e/cli_options.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package e2e
33
var CliOpts CliOptions
44

55
type CliOptions struct {
6-
Cli string
6+
Cli string
7+
NoTTY int
78
}

test/e2e/cz_test.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
package e2e_test
22

3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
"github.com/onsi/gomega/gbytes"
7+
)
8+
39
func CZTest() {
4-
// Context("Check Commitizen", func() {
5-
// It("should not need to select a template", func() {
6-
// se, err = RunCLITest()
7-
// NoError(err)
8-
// ShouldContains(se, "Select the type of change that you're committing:")
9-
// ShouldNotContains(se, "Select a template to use for this commit:")
10-
// })
11-
// })
12-
//
13-
// Context("Check Commitizen Cancel", func() {
14-
// It("should output canceled", func() {
15-
// se, err = RunCLITest()
16-
// NoError(err)
17-
// se.Terminate()
18-
// ShouldContains(se, "canceled")
19-
// ExitCode(se, 0)
20-
// })
21-
// })
10+
Context("Check Commitizen", func() {
11+
It("should not need to select a template", func() {
12+
se, err = RunCLITest()
13+
NoError(err)
14+
Eventually(se.Out).Should(gbytes.Say("Select the type of"))
15+
Eventually(se.Out).Should(gbytes.Say("A new feature"))
16+
Eventually(se.Out).ShouldNot(gbytes.Say("Select a template to use for this commit:"))
17+
se.Terminate()
18+
})
19+
})
2220
}

0 commit comments

Comments
 (0)