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

@@ -3,9 +3,7 @@ package alpine
import (
"context"
"github.com/qdm12/golibs/os"
"github.com/qdm12/golibs/os/user"
"os/user"
)
type Configurator interface {
@@ -14,13 +12,17 @@ type Configurator interface {
}
type configurator struct {
openFile os.OpenFileFunc
osUser user.OSUser
alpineReleasePath string
passwdPath string
lookupID func(uid string) (*user.User, error)
lookup func(username string) (*user.User, error)
}
func NewConfigurator(openFile os.OpenFileFunc, osUser user.OSUser) Configurator {
func NewConfigurator() Configurator {
return &configurator{
openFile: openFile,
osUser: osUser,
alpineReleasePath: "/etc/alpine-release",
passwdPath: "/etc/passwd",
lookupID: user.LookupId,
lookup: user.Lookup,
}
}

View File

@@ -15,7 +15,7 @@ var (
// CreateUser creates a user in Alpine with the given UID.
func (c *configurator) CreateUser(username string, uid int) (createdUsername string, err error) {
UIDStr := strconv.Itoa(uid)
u, err := c.osUser.LookupID(UIDStr)
u, err := c.lookupID(UIDStr)
_, unknownUID := err.(user.UnknownUserIdError)
if err != nil && !unknownUID {
return "", err
@@ -28,7 +28,7 @@ func (c *configurator) CreateUser(username string, uid int) (createdUsername str
return u.Username, nil
}
u, err = c.osUser.Lookup(username)
u, err = c.lookup(username)
_, unknownUsername := err.(user.UnknownUserError)
if err != nil && !unknownUsername {
return "", err
@@ -39,7 +39,7 @@ func (c *configurator) CreateUser(username string, uid int) (createdUsername str
ErrUserAlreadyExists, username, u.Uid, uid)
}
file, err := c.openFile("/etc/passwd", os.O_APPEND|os.O_WRONLY, 0644)
file, err := os.OpenFile(c.passwdPath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return "", err
}

View File

@@ -8,7 +8,7 @@ import (
)
func (c *configurator) Version(ctx context.Context) (version string, err error) {
file, err := c.openFile("/etc/alpine-release", os.O_RDONLY, 0)
file, err := os.OpenFile(c.alpineReleasePath, os.O_RDONLY, 0)
if err != nil {
return "", err
}