Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated unit tests for semver package #442

Open
wants to merge 1 commit into
base: usr/spark/unit-test-improvements
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 86 additions & 46 deletions core/semver/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,46 @@ import (
"time"
)

var (
format string
output string
export bool
tpl *template.Template
)

func init() {
if flag.Lookup("f") == nil {
flag.StringVar(
&format,
"f",
"ver",
"The output format: env, go, json, mk, rpm, ver")
}
if flag.Lookup("o") == nil {
flag.StringVar(
&output,
"o",
"",
"The output file")
}
if flag.Lookup("x") == nil {
flag.BoolVar(
&export,
"x",
false,
"Export env vars. Used with -f env")
}
}

func initFlags() {
format = flag.Lookup("f").Value.(flag.Getter).Get().(string)
output = flag.Lookup("o").Value.(flag.Getter).Get().(string)
export = flag.Lookup("x").Value.(flag.Getter).Get().(bool)
}

func main() {
var (
tpl *template.Template
format string
output string
export bool
)

flag.StringVar(
&format,
"f",
"ver",
"The output format: env, go, json, mk, rpm, ver")
flag.StringVar(
&output,
"o",
"",
"The output file")
flag.BoolVar(
&export,
"x",
false,
"Export env vars. Used with -f env")
flag.Parse()
initFlags()

if strings.EqualFold("env", format) {
format = "env"
Expand All @@ -71,10 +87,9 @@ func main() {
format = "ver"
} else {
if fileExists(format) {
buf, err := os.ReadFile(format) // #nosec G304
buf, err := ReadFile(format) // #nosec G304
if err != nil {
fmt.Fprintf(os.Stderr, "error: read tpl failed: %v\n", err)
os.Exit(1)
errorExit(fmt.Sprintf("error: read tpl failed: %v\n", err))
}
format = string(buf)
}
Expand All @@ -86,8 +101,7 @@ func main() {
if len(output) > 0 {
fout, err := os.Create(filepath.Clean(output))
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
errorExit(fmt.Sprintf("error: %v\n", err))
}
w = fout
defer func() {
Expand All @@ -102,8 +116,7 @@ func main() {
`^[^\d]*(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z].+?))?(?:-(\d+)-g(.+?)(?:-(dirty))?)?\s*$`)
m := rx.FindStringSubmatch(gitdesc)
if len(m) == 0 {
fmt.Fprintf(os.Stderr, "error: match git describe failed: %s\n", gitdesc)
os.Exit(1)
errorExit(fmt.Sprintf("error: match git describe failed: %s\n", gitdesc))
}

goos := os.Getenv("XGOOS")
Expand Down Expand Up @@ -158,8 +171,7 @@ func main() {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(ver); err != nil {
fmt.Fprintf(os.Stderr, "error: encode to json failed: %v\n", err)
os.Exit(1)
errorExit(fmt.Sprintf("error: encode to json failed: %v\n", err))
}
case "mk":
for _, v := range ver.EnvVars() {
Expand All @@ -168,22 +180,21 @@ func main() {
fmt.Fprintf(w, "%s ?=", key)
if len(p) == 1 {
fmt.Fprintln(w)
continue
} else {
val := p[1]
if strings.HasPrefix(val, `"`) &&
strings.HasSuffix(val, `"`) {
val = val[1 : len(val)-1]
}
val = strings.Replace(val, "$", "$$", -1)
fmt.Fprintf(w, " %s\n", val)
}
val := p[1]
if strings.HasPrefix(val, `"`) &&
strings.HasSuffix(val, `"`) {
val = val[1 : len(val)-1]
}
val = strings.Replace(val, "$", "$$", -1)
fmt.Fprintf(w, " %s\n", val)
}
case "rpm":
fmt.Fprintln(w, ver.RPM())
case "tpl":
if err := tpl.Execute(w, ver); err != nil {
fmt.Fprintf(os.Stderr, "error: template failed: %v\n", err)
os.Exit(1)
errorExit(fmt.Sprintf("error: template failed: %v\n", err))
}
case "ver":
fmt.Fprintln(w, ver.String())
Expand All @@ -196,22 +207,27 @@ func doExec(cmd string, args ...string) ([]byte, error) {
return c.Output()
}

func errorExit(message string) {
fmt.Fprintf(os.Stderr, "%s", message)
OSExit(1)
}

func chkErr(out []byte, err error) string {
if err == nil {
return strings.TrimSpace(string(out))
}

e, ok := err.(*exec.ExitError)
e, ok := GetExitError(err)
if !ok {
os.Exit(1)
OSExit(1)
}

st, ok := e.Sys().(syscall.WaitStatus)
status, ok := GetStatusError(e)
if !ok {
os.Exit(1)
OSExit(1)
}

os.Exit(st.ExitStatus())
OSExit(status)
return ""
}

Expand Down Expand Up @@ -327,3 +343,27 @@ func fileExists(filePath string) bool {
}
return false
}

// ReadFile is a wrapper around os.ReadFile
var ReadFile = func(file string) ([]byte, error) {
return os.ReadFile(file) // #nosec G304
}

// OSExit is a wrapper around os.Exit
var OSExit = func(code int) {
os.Exit(code)
}

// GetExitError is a wrapper around exec.ExitError
var GetExitError = func(err error) (e *exec.ExitError, ok bool) {
e, ok = err.(*exec.ExitError)
return
}

// GetStatusError is a wrapper around syscall.WaitStatus
var GetStatusError = func(exitError *exec.ExitError) (status int, ok bool) {
if e, ok := exitError.Sys().(syscall.WaitStatus); ok {
return e.ExitStatus(), true
}
return 1, false
}
Loading
Loading