Skip to content

Commit

Permalink
test: handle errors in TestMain (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
loozhengyuan authored Apr 29, 2021
1 parent 691177a commit 3a7e7a6
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -14,12 +15,24 @@ var (
testUserWorkingDir string
)

func TestMain(m *testing.M) {
if d, err := os.UserConfigDir(); err == nil {
testUserConfigDir = d
func run() error {
configDir, err := os.UserConfigDir()
if err != nil {
return fmt.Errorf("get user config dir: %w", err)
}
testUserConfigDir = configDir
workingDir, err := os.Getwd()
if err != nil {
return fmt.Errorf("get user working dir: %w", err)
}
if d, err := os.Getwd(); err == nil {
testUserWorkingDir = d
testUserWorkingDir = workingDir
return nil
}

func TestMain(m *testing.M) {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}
Expand Down

0 comments on commit 3a7e7a6

Please sign in to comment.