Skip to content

Commit

Permalink
Merge pull request #38 from nao1215/add-unit-test
Browse files Browse the repository at this point in the history
Add unit test
  • Loading branch information
nao1215 committed Sep 16, 2022
2 parents a82f2df + e66a501 commit c0b1961
Show file tree
Hide file tree
Showing 7 changed files with 430 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
# vendor/
gup
dist/
cover.html
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/fatih/color v1.13.0
github.com/gen2brain/beeep v0.0.0-20210529141713-5586760f0cc1
github.com/google/go-cmp v0.5.9
github.com/mattn/go-colorable v0.1.13
github.com/spf13/cobra v1.5.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 h1:qZNfIGkIANxGv/Oq
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4/go.mod h1:kW3HQ4UdaAyrUCSSDR4xUzBKW6O2iA4uHhk7AtyYp10=
github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME=
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c h1:16eHWuMGvCjSfgRJKqIzapE78onvvTbdi1rMkU00lZw=
github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gopherjs/gopherwasm v1.1.0 h1:fA2uLoctU5+T3OhOn2vYP0DVT6pxc7xhTlBB1paATqQ=
Expand Down
23 changes: 17 additions & 6 deletions internal/print/print.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package print defines functions to accept colored standard output and user input
package print

import (
Expand All @@ -17,38 +18,48 @@ var (
Stderr = colorable.NewColorableStderr()
)

// Info print information message at STDOUT.
// Info print information message at STDOUT in green.
// This function is used to print some information (that is not error) to the user.
func Info(msg string) {
fmt.Fprintf(Stdout, "%s:%s: %s\n",
cmdinfo.Name, color.GreenString("INFO "), msg)
}

// Warn print warning message at STDERR.
// Warn print warning message at STDERR in yellow.
// This function is used to print warning message to the user.
func Warn(err interface{}) {
fmt.Fprintf(Stderr, "%s:%s: %v\n",
cmdinfo.Name, color.YellowString("WARN "), err)
}

// Err print error message at STDERR.
// Err print error message at STDERR in yellow.
// This function is used to print error message to the user.
func Err(err interface{}) {
fmt.Fprintf(Stderr, "%s:%s: %v\n",
cmdinfo.Name, color.HiYellowString("ERROR"), err)
}

// Fatal print dying message at STDERR.
// OsExit is wrapper for os.Exit(). It's for unit test.
var OsExit = os.Exit

// Fatal print dying message at STDERR in red.
// After print message, process will exit
func Fatal(err interface{}) {
fmt.Fprintf(Stderr, "%s:%s: %v\n",
cmdinfo.Name, color.RedString("FATAL"), err)
os.Exit(1)
OsExit(1)
}

// FmtScanln is wrapper for fmt.Scanln(). It's for unit test.
var FmtScanln = fmt.Scanln

// Question displays the question in the terminal and receives an answer from the user.
func Question(ask string) bool {
var response string

fmt.Fprintf(Stdout, "%s:%s: %s",
cmdinfo.Name, color.GreenString("CHECK"), ask+" [Y/n] ")
_, err := fmt.Scanln(&response)
_, err := FmtScanln(&response)
if err != nil {
// If user input only enter.
if strings.Contains(err.Error(), "expected newline") {
Expand Down
Loading

0 comments on commit c0b1961

Please sign in to comment.