Skip to content

Commit d7b3235

Browse files
authored
Merge pull request #61 from deathiop/master
Revamp tests
2 parents 7cf9c8e + 39b6ccf commit d7b3235

15 files changed

+553
-778
lines changed

go.mod

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ module github.com/ovh/go-ovh
22

33
go 1.18
44

5-
require gopkg.in/ini.v1 v1.67.0
5+
require (
6+
github.com/jarcoal/httpmock v1.3.0
7+
github.com/maxatome/go-testdeep v1.12.0
8+
gopkg.in/ini.v1 v1.67.0
9+
)
610

7-
require github.com/stretchr/testify v1.8.2 // indirect
11+
require (
12+
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/stretchr/testify v1.8.2 // indirect
14+
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/jarcoal/httpmock v1.3.0 h1:2RJ8GP0IIaWwcC9Fp2BmVi8Kog3v2Hn7VXM3fTd+nuc=
5+
github.com/jarcoal/httpmock v1.3.0/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
6+
github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g=
7+
github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
48
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
59
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
610
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

ovh/configuration.go

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ import (
44
"fmt"
55
"os"
66
"os/user"
7-
"path/filepath"
87
"strings"
98

109
"gopkg.in/ini.v1"
1110
)
1211

13-
// Use variables for easier test overload
14-
var (
15-
systemConfigPath = "/etc/ovh.conf"
16-
userConfigPath = "/.ovh.conf" // prefixed with homeDir
17-
localConfigPath = "./ovh.conf"
18-
)
12+
var configPaths = []string{
13+
// System wide configuration
14+
"/etc/ovh.com",
15+
// Configuration in user's home
16+
"~/.ovh.conf",
17+
// Configuration in local folder
18+
"./ovh.conf",
19+
}
1920

20-
// currentUserHome attempts to get current user's home directory
21+
// currentUserHome attempts to get current user's home directory.
2122
func currentUserHome() (string, error) {
2223
usr, err := user.Current()
2324
if err != nil {
@@ -31,14 +32,43 @@ func currentUserHome() (string, error) {
3132
return usr.HomeDir, nil
3233
}
3334

34-
// appendConfigurationFile only if it exists. We need to do this because
35-
// ini package will fail to load configuration at all if a configuration
36-
// file is missing. This is racy, but better than always failing.
37-
func appendConfigurationFile(cfg *ini.File, path string) {
38-
if file, err := os.Open(path); err == nil {
39-
defer file.Close()
40-
_ = cfg.Append(path)
35+
// configPaths returns configPaths, with ~/ prefix expanded.
36+
func expandConfigPaths() []interface{} {
37+
paths := []interface{}{}
38+
39+
// Will be initialized on first use
40+
var home string
41+
var homeErr error
42+
43+
for _, path := range configPaths {
44+
if strings.HasPrefix(path, "~/") {
45+
// Find home if needed
46+
if home == "" && homeErr == nil {
47+
home, homeErr = currentUserHome()
48+
}
49+
// Ignore file in HOME if we cannot find it
50+
if homeErr != nil {
51+
continue
52+
}
53+
54+
path = home + path[1:]
55+
}
56+
57+
paths = append(paths, path)
58+
}
59+
60+
return paths
61+
}
62+
63+
// loadINI builds a ini.File from the configuration paths provided in configPaths.
64+
// It's a helper for loadConfig.
65+
func loadINI() (*ini.File, error) {
66+
paths := expandConfigPaths()
67+
if len(paths) == 0 {
68+
return ini.Empty(), nil
4169
}
70+
71+
return ini.LooseLoad(paths[0], paths[1:]...)
4272
}
4373

4474
// loadConfig loads client configuration from params, environments or configuration
@@ -58,13 +88,10 @@ func appendConfigurationFile(cfg *ini.File, path string) {
5888
func (c *Client) loadConfig(endpointName string) error {
5989
// Load configuration files by order of increasing priority. All configuration
6090
// files are optional. Only load file from user home if home could be resolve
61-
cfg := ini.Empty()
62-
appendConfigurationFile(cfg, systemConfigPath)
63-
if home, err := currentUserHome(); err == nil {
64-
userConfigFullPath := filepath.Join(home, userConfigPath)
65-
appendConfigurationFile(cfg, userConfigFullPath)
91+
cfg, err := loadINI()
92+
if err != nil {
93+
return fmt.Errorf("cannot load configuration: %w", err)
6694
}
67-
appendConfigurationFile(cfg, localConfigPath)
6895

6996
// Canonicalize configuration
7097
if endpointName == "" {

0 commit comments

Comments
 (0)