Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const launchServicesPlistPath = "Library/Preferences/com.apple.LaunchServices/co
type Client struct {
Runner osq.CmdRunner
CurrentUser string
HomeDir string
PlistLocation string
}

Expand All @@ -23,6 +24,12 @@ func WithCurrentUser(currentUser string) Option {
}
}

func WithHomeDir(homeDir string) Option {
return func(c *Client) {
c.HomeDir = homeDir
}
}

func WithPlistLocation(plistLocation string) Option {
return func(c *Client) {
c.PlistLocation = plistLocation
Expand All @@ -40,21 +47,26 @@ func newClient(lookupCurrentUser currentUserLookup, opts ...Option) (Client, err
opt(&c)
}

if c.CurrentUser == "" {
if c.CurrentUser == "" || c.HomeDir == "" {
currentUser, err := lookupCurrentUser()
if err != nil {
return c, err
}
c.CurrentUser = currentUser.Username
if c.CurrentUser == "" {
c.CurrentUser = currentUser.Username
}
if c.HomeDir == "" {
c.HomeDir = currentUser.HomeDir
}
}

if c.PlistLocation == "" {
c.PlistLocation = defaultLaunchServicesPlistLocation(c.CurrentUser)
c.PlistLocation = defaultLaunchServicesPlistLocation(c.HomeDir)
}

return c, nil
}

func defaultLaunchServicesPlistLocation(currentUser string) string {
return "/Users/" + currentUser + "/" + launchServicesPlistPath
func defaultLaunchServicesPlistLocation(homeDir string) string {
return homeDir + "/" + launchServicesPlistPath
}
115 changes: 96 additions & 19 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,135 @@ import (

func TestNewClient(t *testing.T) {
client, err := NewClient()

assert.NoError(t, err, "NewClient should not return an error")
assert.NotNil(t, client.Runner, "Runner should not be nil")
assert.IsType(t, &osq.ExecCmdRunner{}, client.Runner, "Runner should be of type osq.Runner")
assert.NotEmpty(t, client.CurrentUser, "CurrentUser should not be empty")
assert.NotEmpty(t, client.HomeDir, "HomeDir should not be empty")
assert.Equal(t, defaultLaunchServicesPlistLocation(client.HomeDir), client.PlistLocation, "PlistLocation should use the current user's actual home directory")
}

func TestNewClientWithCurrentUser(t *testing.T) {
expectedUser := "testuser"
client, err := NewClient(WithCurrentUser(expectedUser))
expectedHomeDir := "/Volumes/test_volume/Users/systemuser"

client, err := newClient(
func() (*user.User, error) {
return &user.User{Username: "systemuser", HomeDir: expectedHomeDir}, nil
},
WithCurrentUser(expectedUser),
)

assert.NoError(t, err, "NewClient should not return an error")
assert.Equal(t, expectedUser, client.CurrentUser, "CurrentUser should be set to the provided value")
assert.Equal(t, "/Users/testuser/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist", client.PlistLocation, "PlistLocation should use the provided current user")
assert.Equal(t, expectedHomeDir, client.HomeDir, "HomeDir should be set from the current user lookup")
assert.Equal(t, defaultLaunchServicesPlistLocation(expectedHomeDir), client.PlistLocation, "PlistLocation should use the current user's actual home directory")
}

func TestNewClientWithHomeDir(t *testing.T) {
expectedHomeDir := "/Volumes/test_volume/Users/testuser"

client, err := newClient(
func() (*user.User, error) {
return &user.User{Username: "systemuser", HomeDir: "/Users/systemuser"}, nil
},
WithHomeDir(expectedHomeDir),
)

assert.NoError(t, err, "NewClient should not return an error")
assert.Equal(t, "systemuser", client.CurrentUser, "CurrentUser should be set from the current user lookup")
assert.Equal(t, expectedHomeDir, client.HomeDir, "HomeDir should be set to the provided value")
assert.Equal(t, defaultLaunchServicesPlistLocation(expectedHomeDir), client.PlistLocation, "PlistLocation should use the provided home directory")
}

func TestNewClientDefaultCurrentUser(t *testing.T) {
client, err := newClient(func() (*user.User, error) {
return &user.User{Username: "systemuser"}, nil
})
expectedUser := "systemuser"
expectedHomeDir := "/Volumes/test_volume/Users/systemuser"

client, err := newClient(
func() (*user.User, error) {
return &user.User{Username: expectedUser, HomeDir: expectedHomeDir}, nil
},
)

assert.NoError(t, err, "NewClient should not return an error")
assert.Equal(t, "systemuser", client.CurrentUser, "CurrentUser should be set to the system's current user")
assert.Equal(t, "/Users/systemuser/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist", client.PlistLocation, "PlistLocation should use the system's current user")
assert.Equal(t, expectedUser, client.CurrentUser, "CurrentUser should be set to the system's current user")
assert.Equal(t, expectedHomeDir, client.HomeDir, "HomeDir should be set to the system's current user's home directory")
assert.Equal(t, defaultLaunchServicesPlistLocation(expectedHomeDir), client.PlistLocation, "PlistLocation should use the current user's actual home directory")
}

func TestNewClientWithPlistLocation(t *testing.T) {
expectedPlistLocation := "/tmp/test.plist"
client, err := NewClient(WithPlistLocation(expectedPlistLocation))

client, err := newClient(
func() (*user.User, error) {
return &user.User{Username: "systemuser", HomeDir: "/Volumes/test_volume/Users/systemuser"}, nil
},
WithPlistLocation(expectedPlistLocation),
)

assert.NoError(t, err, "NewClient should not return an error")
assert.Equal(t, expectedPlistLocation, client.PlistLocation, "PlistLocation should be set to the provided value")
}

func TestNewClientWithCurrentUserSkipsCurrentUserLookup(t *testing.T) {
client, err := newClient(func() (*user.User, error) {
return nil, errors.New("current user lookup should not be called")
}, WithCurrentUser("testuser"))
func TestNewClientWithCurrentUserAndHomeDirSkipsCurrentUserLookup(t *testing.T) {
expectedUser := "testuser"
expectedHomeDir := "/Volumes/test_volume/Users/testuser"

client, err := newClient(
func() (*user.User, error) {
return nil, errors.New("current user lookup should not be called")
},
WithCurrentUser(expectedUser),
WithHomeDir(expectedHomeDir),
)

assert.NoError(t, err, "NewClient should not return an error")
assert.Equal(t, expectedUser, client.CurrentUser, "CurrentUser should be set to the provided value")
assert.Equal(t, expectedHomeDir, client.HomeDir, "HomeDir should be set to the provided value")
assert.Equal(t, defaultLaunchServicesPlistLocation(expectedHomeDir), client.PlistLocation, "PlistLocation should use the provided home directory")
}

func TestNewClientWithCurrentUserHomeDirAndPlistLocationSkipsCurrentUserLookup(t *testing.T) {
expectedUser := "testuser"
expectedHomeDir := "/Volumes/test_volume/Users/testuser"
expectedPlistLocation := "/tmp/test.plist"

client, err := newClient(
func() (*user.User, error) {
return nil, errors.New("current user lookup should not be called")
},
WithCurrentUser(expectedUser),
WithHomeDir(expectedHomeDir),
WithPlistLocation(expectedPlistLocation),
)

assert.NoError(t, err, "NewClient should not return an error")
assert.Equal(t, "testuser", client.CurrentUser, "CurrentUser should be set to the provided value")
assert.Equal(t, "/Users/testuser/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist", client.PlistLocation, "PlistLocation should use the provided current user")
assert.Equal(t, expectedUser, client.CurrentUser, "CurrentUser should be set to the provided value")
assert.Equal(t, expectedHomeDir, client.HomeDir, "HomeDir should be set to the provided value")
assert.Equal(t, expectedPlistLocation, client.PlistLocation, "PlistLocation should be set to the provided value")
}

func TestNewClientReturnsCurrentUserLookupError(t *testing.T) {
expectedErr := errors.New("current user lookup failed")

client, err := newClient(func() (*user.User, error) {
return nil, expectedErr
})
client, err := newClient(
func() (*user.User, error) {
return nil, expectedErr
},
)

assert.ErrorIs(t, err, expectedErr, "NewClient should return the current user lookup error")
assert.Empty(t, client.CurrentUser, "CurrentUser should not be set when lookup fails")
assert.Empty(t, client.HomeDir, "HomeDir should not be set when lookup fails")
assert.Empty(t, client.PlistLocation, "PlistLocation should not be set when lookup fails")
}

func TestDefaultLaunchServicesPlistLocation(t *testing.T) {
location := defaultLaunchServicesPlistLocation("testuser")
homeDir := "/Volumes/test_volume/Users/testuser"

location := defaultLaunchServicesPlistLocation(homeDir)

assert.Equal(t, "/Users/testuser/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist", location, "default LaunchServices plist location should match the legacy path")
assert.Equal(t, homeDir+"/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist", location, "default LaunchServices plist location should use the provided home directory")
}
Loading