Maint: do not mock os functions

- Use filepaths with /tmp for tests instead
- Only mock functions where filepath can't be specified such as user.Lookup
This commit is contained in:
Quentin McGaw (desktop)
2021-07-23 16:06:19 +00:00
parent e94684aa39
commit 21f4cf7ab5
48 changed files with 226 additions and 243 deletions

View File

@@ -5,19 +5,22 @@ import (
"context"
"github.com/qdm12/golibs/logging"
"github.com/qdm12/golibs/os"
"github.com/qdm12/golibs/params"
)
type CLI interface {
ClientKey(args []string, openFile os.OpenFileFunc) error
HealthCheck(ctx context.Context, env params.Env, os os.OS, logger logging.Logger) error
OpenvpnConfig(os os.OS, logger logging.Logger) error
Update(ctx context.Context, args []string, os os.OS, logger logging.Logger) error
ClientKey(args []string) error
HealthCheck(ctx context.Context, env params.Env, logger logging.Logger) error
OpenvpnConfig(logger logging.Logger) error
Update(ctx context.Context, args []string, logger logging.Logger) error
}
type cli struct{}
type cli struct {
repoServersPath string
}
func New() CLI {
return &cli{}
return &cli{
repoServersPath: "./internal/constants/servers.json",
}
}

View File

@@ -4,19 +4,19 @@ import (
"flag"
"fmt"
"io"
"os"
"strings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/golibs/os"
)
func (c *cli) ClientKey(args []string, openFile os.OpenFileFunc) error {
func (c *cli) ClientKey(args []string) error {
flagSet := flag.NewFlagSet("clientkey", flag.ExitOnError)
filepath := flagSet.String("path", constants.ClientKey, "file path to the client.key file")
if err := flagSet.Parse(args); err != nil {
return err
}
file, err := openFile(*filepath, os.O_RDONLY, 0)
file, err := os.OpenFile(*filepath, os.O_RDONLY, 0)
if err != nil {
return err
}

View File

@@ -9,15 +9,14 @@ import (
"github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/healthcheck"
"github.com/qdm12/golibs/logging"
"github.com/qdm12/golibs/os"
"github.com/qdm12/golibs/params"
)
func (c *cli) HealthCheck(ctx context.Context, env params.Env,
os os.OS, logger logging.Logger) error {
logger logging.Logger) error {
// Extract the health server port from the configuration.
config := configuration.Health{}
err := config.Read(env, os, logger)
err := config.Read(env, logger)
if err != nil {
return err
}

View File

@@ -10,17 +10,16 @@ import (
"github.com/qdm12/gluetun/internal/provider"
"github.com/qdm12/gluetun/internal/storage"
"github.com/qdm12/golibs/logging"
"github.com/qdm12/golibs/os"
"github.com/qdm12/golibs/params"
)
func (c *cli) OpenvpnConfig(os os.OS, logger logging.Logger) error {
func (c *cli) OpenvpnConfig(logger logging.Logger) error {
var allSettings configuration.Settings
err := allSettings.Read(params.NewEnv(), os, logger)
err := allSettings.Read(params.NewEnv(), logger)
if err != nil {
return err
}
allServers, err := storage.New(logger, os, constants.ServersData).
allServers, err := storage.New(logger, constants.ServersData).
SyncServers(constants.GetAllServers())
if err != nil {
return err

View File

@@ -7,7 +7,7 @@ import (
"flag"
"fmt"
"net/http"
nativeos "os"
"os"
"time"
"github.com/qdm12/gluetun/internal/configuration"
@@ -16,7 +16,6 @@ import (
"github.com/qdm12/gluetun/internal/storage"
"github.com/qdm12/gluetun/internal/updater"
"github.com/qdm12/golibs/logging"
"github.com/qdm12/golibs/os"
)
var (
@@ -26,7 +25,7 @@ var (
ErrWriteToFile = errors.New("cannot write updated information to file")
)
func (c *cli) Update(ctx context.Context, args []string, os os.OS, logger logging.Logger) error {
func (c *cli) Update(ctx context.Context, args []string, logger logging.Logger) error {
options := configuration.Updater{CLI: true}
var endUserMode, maintainerMode, updateAll bool
flagSet := flag.NewFlagSet("update", flag.ExitOnError)
@@ -65,7 +64,7 @@ func (c *cli) Update(ctx context.Context, args []string, os os.OS, logger loggin
const clientTimeout = 10 * time.Second
httpClient := &http.Client{Timeout: clientTimeout}
storage := storage.New(logger, os, constants.ServersData)
storage := storage.New(logger, constants.ServersData)
currentServers, err := storage.SyncServers(constants.GetAllServers())
if err != nil {
return fmt.Errorf("%w: %s", ErrSyncServers, err)
@@ -83,7 +82,7 @@ func (c *cli) Update(ctx context.Context, args []string, os os.OS, logger loggin
}
if maintainerMode {
if err := writeToEmbeddedJSON(os, allServers); err != nil {
if err := writeToEmbeddedJSON(c.repoServersPath, allServers); err != nil {
return fmt.Errorf("%w: %s", ErrWriteToFile, err)
}
}
@@ -91,10 +90,11 @@ func (c *cli) Update(ctx context.Context, args []string, os os.OS, logger loggin
return nil
}
func writeToEmbeddedJSON(os os.OS, allServers models.AllServers) error {
func writeToEmbeddedJSON(repoServersPath string,
allServers models.AllServers) error {
const perms = 0600
f, err := os.OpenFile("./internal/constants/servers.json",
nativeos.O_TRUNC|nativeos.O_WRONLY|nativeos.O_CREATE, perms)
f, err := os.OpenFile(repoServersPath,
os.O_TRUNC|os.O_WRONLY|os.O_CREATE, perms)
if err != nil {
return err
}