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

@@ -0,0 +1,7 @@
package storage
import "github.com/qdm12/gluetun/internal/models"
func (s *storage) FlushToFile(allServers models.AllServers) error {
return flushToFile(s.filepath, allServers)
}

View File

@@ -4,7 +4,6 @@ package storage
import (
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/golibs/logging"
"github.com/qdm12/golibs/os"
)
type Storage interface {
@@ -14,14 +13,12 @@ type Storage interface {
}
type storage struct {
os os.OS
logger logging.Logger
filepath string
}
func New(logger logging.Logger, os os.OS, filepath string) Storage {
func New(logger logging.Logger, filepath string) Storage {
return &storage{
os: os,
logger: logger,
filepath: filepath,
}

View File

@@ -5,11 +5,11 @@ import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/golibs/os"
)
var (
@@ -39,7 +39,7 @@ func countServers(allServers models.AllServers) int {
func (s *storage) SyncServers(hardcodedServers models.AllServers) (
allServers models.AllServers, err error) {
serversOnFile, err := s.readFromFile(s.filepath)
serversOnFile, err := readFromFile(s.filepath)
if err != nil {
return allServers, fmt.Errorf("%w: %s", ErrCannotReadFile, err)
}
@@ -62,14 +62,14 @@ func (s *storage) SyncServers(hardcodedServers models.AllServers) (
return allServers, nil
}
if err := s.FlushToFile(allServers); err != nil {
if err := flushToFile(s.filepath, allServers); err != nil {
return allServers, fmt.Errorf("%w: %s", ErrCannotWriteFile, err)
}
return allServers, nil
}
func (s *storage) readFromFile(filepath string) (servers models.AllServers, err error) {
file, err := s.os.OpenFile(filepath, os.O_RDONLY, 0)
func readFromFile(filepath string) (servers models.AllServers, err error) {
file, err := os.Open(filepath)
if os.IsNotExist(err) {
return servers, nil
} else if err != nil {
@@ -86,13 +86,13 @@ func (s *storage) readFromFile(filepath string) (servers models.AllServers, err
return servers, file.Close()
}
func (s *storage) FlushToFile(servers models.AllServers) error {
dirPath := filepath.Dir(s.filepath)
if err := s.os.MkdirAll(dirPath, 0644); err != nil {
func flushToFile(path string, servers models.AllServers) error {
dirPath := filepath.Dir(path)
if err := os.MkdirAll(dirPath, 0644); err != nil {
return err
}
file, err := s.os.OpenFile(s.filepath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}