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:
7
internal/storage/flush.go
Normal file
7
internal/storage/flush.go
Normal 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)
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user