Skip to content

Commit 84151e4

Browse files
authored
feat(config): initialize config automatically (#31)
* feat(config): initialize config automatically change Initialize to initialize move initialize to Run * fix(e2e): skip tty test * fix(linter): update golangci-lint version * update dependencies * update go version * update go version
1 parent 93d1671 commit 84151e4

File tree

12 files changed

+243
-28
lines changed

12 files changed

+243
-28
lines changed

.github/workflows/e2e.yaml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: "e2e test"
2+
on:
3+
push:
4+
branches: [ main ]
5+
pull_request:
6+
branches: [ main ]
7+
# Do not run when the change only includes these directories.
8+
paths-ignore:
9+
- "example/**"
10+
- "docs/**"
11+
- "README.md"
12+
jobs:
13+
# Build the commitizen and cache it so the workers can get it.
14+
build:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Set up Go
18+
uses: actions/setup-go@v5
19+
with:
20+
go-version: 1.22
21+
id: go
22+
# Look for a CLI that's made for this PR
23+
- name: Fetch built CLI
24+
id: cli-cache
25+
uses: actions/cache@v4
26+
with:
27+
path: ./_output/linux/amd64/bin/commitizen
28+
# The cache key a combination of the current PR number and the commit SHA
29+
key: commitizen-${{ github.event.pull_request.number }}-${{ github.sha }}
30+
- name: Fetch cached go modules
31+
uses: actions/cache@v4
32+
if: steps.cli-cache.outputs.cache-hit != 'true'
33+
with:
34+
path: ~/go/pkg/mod
35+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
36+
restore-keys: |
37+
${{ runner.os }}-go-
38+
- name: Check out the code
39+
uses: actions/checkout@v4
40+
if: steps.cli-cache.outputs.cache-hit != 'true'
41+
# If no binaries were built for this PR, build it now.
42+
- name: Build CLI
43+
if: steps.cli-cache.outputs.cache-hit != 'true'
44+
run: |
45+
make build
46+
run-e2e-test:
47+
needs: build
48+
runs-on: ubuntu-latest
49+
steps:
50+
- name: Set up Go
51+
uses: actions/setup-go@v5
52+
with:
53+
go-version: 1.22
54+
- name: Check out the code
55+
uses: actions/checkout@v4
56+
- name: Fetch built CLI
57+
id: cli-cache
58+
uses: actions/cache@v4
59+
with:
60+
path: ./_output/linux/amd64/bin/commitizen
61+
key: commitizen-${{ github.event.pull_request.number }}-${{ github.sha }}
62+
- name: Run E2E test
63+
run: |
64+
GOPATH=~/go make e2e

.github/workflows/lint.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ jobs:
3333
- name: golangci-lint
3434
uses: golangci/golangci-lint-action@v4
3535
with:
36-
args: --timeout=10m
36+
args: --timeout=10m
37+
version: latest

.github/workflows/release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
fetch-depth: 0 # it is required fot the changelog to work correctly
1515
- uses: actions/setup-go@v5
1616
with:
17-
go-version: 1.21
17+
go-version: 1.22
1818
- name: Run GoReleaser
1919
uses: goreleaser/goreleaser-action@v5
2020
env:

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ lint:
6565
test:
6666
@$(MAKE) test.cover
6767

68-
## test-e2e: run e2e test.
69-
.PHONY: test-e2e
70-
test-e2e:
68+
## e2e: run e2e test.
69+
.PHONY: e2e
70+
e2e:
7171
@$(MAKE) test.e2e
7272

7373
## help: show help information.

cmd/cz/cz.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ func New() *cobra.Command {
2626
}
2727

2828
conf := config.New()
29-
err = conf.Initialize()
30-
if err != nil {
31-
return err
32-
}
3329
tmpl, err := conf.Run()
3430
if err != nil {
3531
return err

go.mod

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
module github.com/shipengqi/commitizen
22

3-
go 1.21
3+
go 1.22
44

55
require (
66
github.com/charmbracelet/bubbles v0.18.0
77
github.com/charmbracelet/bubbletea v0.25.0
88
github.com/charmbracelet/lipgloss v0.10.0
9+
github.com/onsi/ginkgo/v2 v2.16.0
910
github.com/onsi/gomega v1.31.1
1011
github.com/shipengqi/component-base v0.2.4
1112
github.com/shipengqi/golib v0.2.11
@@ -19,7 +20,10 @@ require (
1920
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
2021
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
2122
github.com/fatih/color v1.16.0 // indirect
23+
github.com/go-logr/logr v1.4.1 // indirect
24+
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
2225
github.com/google/go-cmp v0.6.0 // indirect
26+
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
2327
github.com/gosuri/uitable v0.0.4 // indirect
2428
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2529
github.com/kr/text v0.2.0 // indirect
@@ -34,9 +38,10 @@ require (
3438
github.com/muesli/termenv v0.15.2 // indirect
3539
github.com/rivo/uniseg v0.4.7 // indirect
3640
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
37-
golang.org/x/net v0.19.0 // indirect
38-
golang.org/x/sync v0.1.0 // indirect
41+
golang.org/x/net v0.20.0 // indirect
42+
golang.org/x/sync v0.6.0 // indirect
3943
golang.org/x/sys v0.18.0 // indirect
40-
golang.org/x/term v0.15.0 // indirect
44+
golang.org/x/term v0.16.0 // indirect
4145
golang.org/x/text v0.14.0 // indirect
46+
golang.org/x/tools v0.17.0 // indirect
4247
)

go.sum

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,31 @@ github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt
88
github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
99
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
1010
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
11+
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
12+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
13+
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
1114
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
1215
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
1316
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
1417
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
18+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1519
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1620
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1721
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
1822
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
19-
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
20-
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
23+
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
24+
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
2125
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
2226
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
27+
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
28+
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
2329
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2430
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
2531
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
2632
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
2733
github.com/gosuri/uitable v0.0.4 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY=
2834
github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo=
35+
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
2936
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
3037
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
3138
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
@@ -54,8 +61,8 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
5461
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
5562
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
5663
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
57-
github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY=
58-
github.com/onsi/ginkgo/v2 v2.15.0/go.mod h1:HlxMHtYF57y6Dpf+mc5529KKmSq9h2FpCF+/ZkwUxKM=
64+
github.com/onsi/ginkgo/v2 v2.16.0 h1:7q1w9frJDzninhXxjZd+Y/x54XNjG/UlRLIYPZafsPM=
65+
github.com/onsi/ginkgo/v2 v2.16.0/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
5966
github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo=
6067
github.com/onsi/gomega v1.31.1/go.mod h1:y40C95dwAD1Nz36SsEnxvfFe8FFfNxzI5eJ0EYGyAy0=
6168
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -75,25 +82,31 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
7582
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
7683
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
7784
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
85+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
86+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
7887
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
7988
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
80-
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
81-
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
82-
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
83-
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
89+
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
90+
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
91+
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
92+
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
93+
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8494
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8595
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8696
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8797
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
8898
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
89-
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
90-
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
99+
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
100+
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
91101
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
92102
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
93-
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
94-
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
103+
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
104+
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
105+
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
106+
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
95107
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
96108
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
97109
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
110+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
98111
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
99112
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/config/config.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func New() *Config {
2828
return &Config{}
2929
}
3030

31-
func (c *Config) Initialize() error {
31+
func (c *Config) initialize() error {
3232
var fpath string
3333
if fsutil.IsExists(RCFilename) {
3434
fpath = RCFilename
@@ -55,7 +55,7 @@ func (c *Config) Initialize() error {
5555
}
5656
c.others = append(c.others, v)
5757
}
58-
// If the user has not configured a default template, the built-in template is used
58+
// If the user has not configured a default template, use the built-in template as the default template
5959
if c.defaultTmpl == nil {
6060
defaults, err := Load(convutil.S2B(DefaultCommitTemplate))
6161
if err != nil {
@@ -68,8 +68,12 @@ func (c *Config) Initialize() error {
6868
}
6969

7070
func (c *Config) Run() (*render.Template, error) {
71+
err := c.initialize()
72+
if err != nil {
73+
return nil, err
74+
}
7175
if len(c.others) > 0 {
72-
model := c.createTemplatesSelect("Select the template to be used for this commit")
76+
model := c.createTemplatesSelect("Select a template to use for this commit:")
7377
if _, err := tea.NewProgram(model).Run(); err != nil {
7478
return nil, err
7579
}

test/e2e/cli_options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package e2e
2+
3+
var CliOpts CliOptions
4+
5+
type CliOptions struct {
6+
Cli string
7+
}

test/e2e/cz_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package e2e_test
2+
3+
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+
// })
22+
}

test/e2e/e2e_suite_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package e2e_test
2+
3+
import (
4+
"flag"
5+
"os/exec"
6+
"testing"
7+
8+
. "github.com/onsi/ginkgo/v2"
9+
. "github.com/onsi/gomega"
10+
"github.com/onsi/gomega/gexec"
11+
"github.com/shipengqi/golib/fsutil"
12+
13+
. "github.com/shipengqi/commitizen/test/e2e"
14+
)
15+
16+
var (
17+
se *gexec.Session
18+
err error
19+
)
20+
21+
func init() {
22+
flag.StringVar(&CliOpts.Cli, "cli", "", "path to the commitizen command to use.")
23+
}
24+
25+
var _ = Describe("Sorted Tests", func() {
26+
Describe("Version Command", VersionTest)
27+
Describe("CZ Command", CZTest)
28+
})
29+
30+
func TestE2e(t *testing.T) {
31+
// Skip running E2E tests when running only "short" tests because:
32+
// 1. E2E tests are long-running tests involving generation of skeletons.
33+
if testing.Short() {
34+
t.Skip("Skipping E2E tests")
35+
}
36+
37+
RegisterFailHandler(Fail)
38+
RunSpecs(t, "E2e Suite")
39+
}
40+
41+
var _ = BeforeSuite(func() {
42+
flag.Parse()
43+
44+
if CliOpts.Cli == "" {
45+
CliOpts.Cli = "./commitizen"
46+
}
47+
})
48+
49+
// ===================================================
50+
// Helpers
51+
52+
func RunCLITest(args ...string) (*gexec.Session, error) {
53+
cmd := exec.Command(CliOpts.Cli, args...)
54+
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
55+
return session.Wait(), err
56+
}
57+
58+
func NoError(err error) {
59+
Expect(err).To(BeNil())
60+
}
61+
62+
func ExitCode(session *gexec.Session, expected int) {
63+
Ω(session.ExitCode()).Should(Equal(expected))
64+
}
65+
66+
func ShouldContains(session *gexec.Session, expected string) {
67+
Ω(session.Out.Contents()).Should(ContainSubstring(expected))
68+
}
69+
70+
func ShouldNotContains(session *gexec.Session, expected string) {
71+
Ω(session.Out.Contents()).ShouldNot(ContainSubstring(expected))
72+
}
73+
74+
func ShouldExists(fpath string) {
75+
exists := fsutil.IsExists(fpath)
76+
Expect(exists).To(Equal(true))
77+
}
78+
79+
func ShouldNotExists(fpath string) {
80+
exists := fsutil.IsExists(fpath)
81+
Expect(exists).To(Equal(false))
82+
}

test/e2e/version_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package e2e_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
)
6+
7+
func VersionTest() {
8+
Context("Check Version Message", func() {
9+
It("should output the all version messages", func() {
10+
se, err = RunCLITest("version")
11+
NoError(err)
12+
ShouldContains(se, "Version:")
13+
ShouldContains(se, "Commit:")
14+
ShouldContains(se, "GitTreeState:")
15+
ShouldContains(se, "BuildTime:")
16+
ShouldContains(se, "GoVersion:")
17+
ShouldContains(se, "Compiler:")
18+
ShouldContains(se, "Platform:")
19+
})
20+
})
21+
}

0 commit comments

Comments
 (0)