Skip to content

Commit 4480549

Browse files
authored
Merge pull request #57 from nao1215/disable-notification-by-default
Add an option to enable notifications (-N, --notify) and disable them by default.
2 parents 3b0b4b6 + 89d5aa6 commit 4480549

10 files changed

+121
-168
lines changed

cmd/check.go

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"strings"
6+
"sync"
67

78
"github.com/nao1215/gup/internal/goutil"
89
"github.com/nao1215/gup/internal/print"
@@ -49,6 +50,7 @@ func check(cmd *cobra.Command, args []string) int {
4950
func doCheck(pkgs []goutil.Package) int {
5051
result := 0
5152
countFmt := "[%" + pkgDigit(pkgs) + "d/%" + pkgDigit(pkgs) + "d]"
53+
var mu sync.Mutex
5254
needUpdatePkgs := []goutil.Package{}
5355

5456
print.Info("check binary under $GOPATH/bin or $GOBIN")
@@ -65,7 +67,9 @@ func doCheck(pkgs []goutil.Package) int {
6567
}
6668
p.Version.Latest = latestVer
6769
if !goutil.IsAlreadyUpToDate(*p.Version) {
70+
mu.Lock()
6871
needUpdatePkgs = append(needUpdatePkgs, p)
72+
mu.Unlock()
6973
}
7074
}
7175

cmd/check_test.go

+3-27
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,7 @@ func Test_check(t *testing.T) {
3535
}
3636
for _, tt := range tests {
3737
t.Run(tt.name, func(t *testing.T) {
38-
oldGoBin := os.Getenv("GOBIN")
39-
if err := os.Setenv("GOBIN", tt.gobin); err != nil {
40-
t.Fatal(err)
41-
}
42-
defer func() {
43-
if err := os.Setenv("GOBIN", oldGoBin); err != nil {
44-
t.Fatal(err)
45-
}
46-
}()
38+
t.Setenv("GOBIN", tt.gobin)
4739

4840
orgStdout := print.Stdout
4941
orgStderr := print.Stderr
@@ -85,15 +77,7 @@ func Test_check(t *testing.T) {
8577

8678
func Test_check_not_use_go_cmd(t *testing.T) {
8779
t.Run("Not found go command", func(t *testing.T) {
88-
oldPATH := os.Getenv("PATH")
89-
if err := os.Setenv("PATH", ""); err != nil {
90-
t.Fatal(err)
91-
}
92-
defer func() {
93-
if err := os.Setenv("PATH", oldPATH); err != nil {
94-
t.Fatal(err)
95-
}
96-
}()
80+
t.Setenv("PATH", "")
9781

9882
orgStdout := print.Stdout
9983
orgStderr := print.Stderr
@@ -199,15 +183,7 @@ func Test_check_gobin_is_empty(t *testing.T) {
199183

200184
for _, tt := range tests {
201185
t.Run(tt.name, func(t *testing.T) {
202-
oldGoBin := os.Getenv("GOBIN")
203-
if err := os.Setenv("GOBIN", tt.gobin); err != nil {
204-
t.Fatal(err)
205-
}
206-
defer func() {
207-
if err := os.Setenv("GOBIN", oldGoBin); err != nil {
208-
t.Fatal(err)
209-
}
210-
}()
186+
t.Setenv("GOBIN", tt.gobin)
211187

212188
orgStdout := print.Stdout
213189
orgStderr := print.Stderr

cmd/export_test.go

+2-18
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,7 @@ func Test_validPkgInfo(t *testing.T) {
5151

5252
func Test_export_not_use_go_cmd(t *testing.T) {
5353
t.Run("Not found go command", func(t *testing.T) {
54-
oldPATH := os.Getenv("PATH")
55-
if err := os.Setenv("PATH", ""); err != nil {
56-
t.Fatal(err)
57-
}
58-
defer func() {
59-
if err := os.Setenv("PATH", oldPATH); err != nil {
60-
t.Fatal(err)
61-
}
62-
}()
54+
t.Setenv("PATH", "")
6355

6456
orgStdout := print.Stdout
6557
orgStderr := print.Stderr
@@ -184,15 +176,7 @@ func Test_export(t *testing.T) {
184176

185177
for _, tt := range tests {
186178
t.Run(tt.name, func(t *testing.T) {
187-
oldGoBin := os.Getenv("GOBIN")
188-
if err := os.Setenv("GOBIN", tt.gobin); err != nil {
189-
t.Fatal(err)
190-
}
191-
defer func() {
192-
if err := os.Setenv("GOBIN", oldGoBin); err != nil {
193-
t.Fatal(err)
194-
}
195-
}()
179+
t.Setenv("GOBIN", tt.gobin)
196180

197181
if tt.name == "can not make .config directory" {
198182
oldHome := os.Getenv("HOME")

cmd/import.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Finally, you execute the export subcommand in this state.`,
2626

2727
func init() {
2828
importCmd.Flags().BoolP("dry-run", "n", false, "perform the trial update with no changes")
29+
importCmd.Flags().BoolP("notify", "N", false, "enable desktop notifications")
2930
importCmd.Flags().StringP("input", "i", config.FilePath(), "specify gup.conf file path to import")
3031
rootCmd.AddCommand(importCmd)
3132
}
@@ -43,6 +44,12 @@ func runImport(cmd *cobra.Command, args []string) int {
4344
return 1
4445
}
4546

47+
notify, err := cmd.Flags().GetBool("notify")
48+
if err != nil {
49+
print.Err(fmt.Errorf("%s: %w", "can not parse command line argument (--notify)", err))
50+
return 1
51+
}
52+
4653
if !file.IsFile(confFile) {
4754
print.Err(fmt.Errorf("%s is not found", confFile))
4855
return 1
@@ -60,5 +67,5 @@ func runImport(cmd *cobra.Command, args []string) int {
6067
}
6168

6269
print.Info("start update based on " + confFile)
63-
return update(pkgs, dryRun)
70+
return update(pkgs, dryRun, notify)
6471
}

cmd/import_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,31 @@ func Test_runImport_Error(t *testing.T) {
5151
"",
5252
},
5353
},
54+
{
55+
name: "argument parse error (--notify)",
56+
args: args{
57+
cmd: &cobra.Command{},
58+
args: []string{},
59+
},
60+
home: "",
61+
want: 1,
62+
stderr: []string{
63+
"gup:ERROR: can not parse command line argument (--notify): flag accessed but not defined: notify",
64+
"",
65+
},
66+
},
5467
}
5568
for _, tt := range tests {
5669
t.Run(tt.name, func(t *testing.T) {
5770
if tt.name == "argument parse error (--dry-run)" {
5871
tt.args.cmd.Flags().StringP("input", "i", config.FilePath(), "specify gup.conf file path to import")
72+
tt.args.cmd.Flags().BoolP("notify", "N", false, "enable desktop notifications")
5973
} else if tt.name == "argument parse error (--input)" {
6074
tt.args.cmd.Flags().BoolP("dry-run", "n", false, "perform the trial update with no changes")
75+
tt.args.cmd.Flags().BoolP("notify", "N", false, "enable desktop notifications")
76+
} else if tt.name == "argument parse error (--notify)" {
77+
tt.args.cmd.Flags().BoolP("dry-run", "n", false, "perform the trial update with no changes")
78+
tt.args.cmd.Flags().StringP("input", "i", config.FilePath(), "specify gup.conf file path to import")
6179
}
6280

6381
orgStdout := print.Stdout

cmd/list_test.go

+2-18
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,7 @@ import (
1616

1717
func Test_list_not_found_go_command(t *testing.T) {
1818
t.Run("Not found go command", func(t *testing.T) {
19-
oldPATH := os.Getenv("PATH")
20-
if err := os.Setenv("PATH", ""); err != nil {
21-
t.Fatal(err)
22-
}
23-
defer func() {
24-
if err := os.Setenv("PATH", oldPATH); err != nil {
25-
t.Fatal(err)
26-
}
27-
}()
19+
t.Setenv("PATH", "")
2820

2921
orgStdout := print.Stdout
3022
orgStderr := print.Stderr
@@ -129,15 +121,7 @@ func Test_list_gobin_is_empty(t *testing.T) {
129121

130122
for _, tt := range tests {
131123
t.Run(tt.name, func(t *testing.T) {
132-
oldGoBin := os.Getenv("GOBIN")
133-
if err := os.Setenv("GOBIN", tt.gobin); err != nil {
134-
t.Fatal(err)
135-
}
136-
defer func() {
137-
if err := os.Setenv("GOBIN", oldGoBin); err != nil {
138-
t.Fatal(err)
139-
}
140-
}()
124+
t.Setenv("GOBIN", tt.gobin)
141125

142126
orgStdout := print.Stdout
143127
orgStderr := print.Stderr

cmd/remove_test.go

+5-37
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,7 @@ func Test_remove(t *testing.T) {
100100

101101
for _, tt := range tests {
102102
t.Run(tt.name, func(t *testing.T) {
103-
oldGoBin := os.Getenv("GOBIN")
104-
if err := os.Setenv("GOBIN", tt.gobin); err != nil {
105-
t.Fatal(err)
106-
}
107-
defer func() {
108-
if err := os.Setenv("GOBIN", oldGoBin); err != nil {
109-
t.Fatal(err)
110-
}
111-
}()
103+
t.Setenv("GOBIN", tt.gobin)
112104

113105
orgStdout := print.Stdout
114106
orgStderr := print.Stderr
@@ -145,25 +137,9 @@ func Test_remove(t *testing.T) {
145137
}
146138

147139
func Test_remove_gobin_is_empty(t *testing.T) {
148-
t.Run("GOPATH and GOBIN", func(t *testing.T) {
149-
oldGoBin := os.Getenv("GOBIN")
150-
if err := os.Setenv("GOBIN", ""); err != nil {
151-
t.Fatal(err)
152-
}
153-
defer func() {
154-
if err := os.Setenv("GOBIN", oldGoBin); err != nil {
155-
t.Fatal(err)
156-
}
157-
}()
158-
159-
if err := os.Setenv("GOPATH", ""); err != nil {
160-
t.Fatal(err)
161-
}
162-
defer func() {
163-
if err := os.Setenv("GOPATH", oldGoBin); err != nil {
164-
t.Fatal(err)
165-
}
166-
}()
140+
t.Run("GOPATH and GOBIN is empty", func(t *testing.T) {
141+
t.Setenv("GOBIN", "")
142+
t.Setenv("GOPATH", "")
167143

168144
oldBuildGopath := build.Default.GOPATH
169145
build.Default.GOPATH = ""
@@ -316,15 +292,7 @@ func Test_removeLoop(t *testing.T) {
316292
if runtime.GOOS != "windows" && tt.name == "windows environment and suffix is mismatch" {
317293
GOOS = "windows"
318294
defer func() { GOOS = runtime.GOOS }()
319-
320-
if err := os.Setenv("GOEXE", ".exe"); err != nil {
321-
t.Fatal(err)
322-
}
323-
defer func() {
324-
if err := os.Setenv("GOEXE", ""); err != nil {
325-
t.Fatal(err)
326-
}
327-
}()
295+
t.Setenv("GOEXE", ".exe")
328296
}
329297

330298
if got := removeLoop(tt.args.gobin, tt.args.force, tt.args.target); got != tt.want {

cmd/root_test.go

+7-48
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,7 @@ func TestExecute_Check(t *testing.T) {
4747
} else {
4848
gobinDir = filepath.Join("testdata", "check_success")
4949
}
50-
51-
oldGoBin := os.Getenv("GOBIN")
52-
if err := os.Setenv("GOBIN", gobinDir); err != nil {
53-
t.Fatal(err)
54-
}
55-
defer func() {
56-
if err := os.Setenv("GOBIN", oldGoBin); err != nil {
57-
t.Fatal(err)
58-
}
59-
}()
50+
t.Setenv("GOBIN", gobinDir)
6051

6152
orgStdout := print.Stdout
6253
orgStderr := print.Stderr
@@ -173,15 +164,7 @@ func TestExecute_List(t *testing.T) {
173164
})
174165
}
175166
for _, tt := range tests {
176-
oldGoBin := os.Getenv("GOBIN")
177-
if err := os.Setenv("GOBIN", tt.gobin); err != nil {
178-
t.Fatal(err)
179-
}
180-
defer func() {
181-
if err := os.Setenv("GOBIN", oldGoBin); err != nil {
182-
t.Fatal(err)
183-
}
184-
}()
167+
t.Setenv("GOBIN", tt.gobin)
185168

186169
OsExit = func(code int) {}
187170
defer func() {
@@ -294,15 +277,7 @@ func TestExecute_Remove_Force(t *testing.T) {
294277
newFile.Close()
295278

296279
for _, tt := range tests {
297-
oldGoBin := os.Getenv("GOBIN")
298-
if err := os.Setenv("GOBIN", tt.gobin); err != nil {
299-
t.Fatal(err)
300-
}
301-
defer func() {
302-
if err := os.Setenv("GOBIN", oldGoBin); err != nil {
303-
t.Fatal(err)
304-
}
305-
}()
280+
t.Setenv("GOBIN", tt.gobin)
306281

307282
OsExit = func(code int) {}
308283
defer func() {
@@ -360,15 +335,7 @@ subaru = github.com/nao1215/subaru
360335
}
361336

362337
for _, tt := range tests {
363-
oldGoBin := os.Getenv("GOBIN")
364-
if err := os.Setenv("GOBIN", tt.gobin); err != nil {
365-
t.Fatal(err)
366-
}
367-
defer func() {
368-
if err := os.Setenv("GOBIN", oldGoBin); err != nil {
369-
t.Fatal(err)
370-
}
371-
}()
338+
t.Setenv("GOBIN", tt.gobin)
372339

373340
OsExit = func(code int) {}
374341
defer func() {
@@ -444,15 +411,7 @@ func TestExecute_Export_WithOutputOption(t *testing.T) {
444411
}
445412

446413
for _, tt := range tests {
447-
oldGoBin := os.Getenv("GOBIN")
448-
if err := os.Setenv("GOBIN", tt.gobin); err != nil {
449-
t.Fatal(err)
450-
}
451-
defer func() {
452-
if err := os.Setenv("GOBIN", oldGoBin); err != nil {
453-
t.Fatal(err)
454-
}
455-
}()
414+
t.Setenv("GOBIN", tt.gobin)
456415

457416
OsExit = func(code int) {}
458417
defer func() {
@@ -712,7 +671,7 @@ func TestExecute_Update(t *testing.T) {
712671
}
713672
}
714673

715-
func TestExecute_Update_DryRun(t *testing.T) {
674+
func TestExecute_Update_DryRunAndNotify(t *testing.T) {
716675
OsExit = func(code int) {}
717676
defer func() {
718677
OsExit = os.Exit
@@ -781,7 +740,7 @@ func TestExecute_Update_DryRun(t *testing.T) {
781740
print.Stdout = pw
782741
print.Stderr = pw
783742

784-
os.Args = []string{"gup", "update", "--dry-run"}
743+
os.Args = []string{"gup", "update", "--dry-run", "--notify"}
785744
Execute()
786745
pw.Close()
787746
print.Stdout = orgStdout

0 commit comments

Comments
 (0)