Skip to content
Open
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
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ jobs:
build:
runs-on: macos-14
env:
VERSION: 1.0.${{ github.run_number }}
VERSION: 1.1.${{ github.run_number }}
steps:
- name: Checkout code
uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f # v3.4.0
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v6
with:
go-version: '1.21'
go-version-file: go.mod
- name: Install Apple Xcode certificates
uses: apple-actions/import-codesign-certs@8f3fb608891dd2244cdab3d69cd68c0d37a7fe93 # v2.0.0
with:
Expand Down
16 changes: 2 additions & 14 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,13 @@ const launchServicesPlistPath = "Library/Preferences/com.apple.LaunchServices/co

type Client struct {
Runner osq.CmdRunner
CurrentUser string
HomeDir string
PlistLocation string
}

type Option func(*Client)
type currentUserLookup func() (*user.User, error)

func WithCurrentUser(currentUser string) Option {
return func(c *Client) {
c.CurrentUser = currentUser
}
}

func WithHomeDir(homeDir string) Option {
return func(c *Client) {
c.HomeDir = homeDir
Expand All @@ -47,17 +40,12 @@ func newClient(lookupCurrentUser currentUserLookup, opts ...Option) (Client, err
opt(&c)
}

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

if c.PlistLocation == "" {
Expand Down
57 changes: 8 additions & 49 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,36 @@ func TestNewClient(t *testing.T) {
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"
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, 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"
expectedPlistLocation := "/Volumes/test_volume/Users/testuser/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist"

client, err := newClient(
func() (*user.User, error) {
return &user.User{Username: "systemuser", HomeDir: "/Users/systemuser"}, nil
return nil, errors.New("current user lookup should not be called")
},
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")
assert.Equal(t, expectedPlistLocation, client.PlistLocation, "PlistLocation should use the provided home directory")
}

func TestNewClientDefaultCurrentUser(t *testing.T) {
expectedUser := "systemuser"
func TestNewClientDefaultHomeDir(t *testing.T) {
expectedHomeDir := "/Volumes/test_volume/Users/systemuser"

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

assert.NoError(t, err, "NewClient should not return an error")
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")
}
Expand All @@ -74,49 +54,29 @@ func TestNewClientWithPlistLocation(t *testing.T) {

client, err := newClient(
func() (*user.User, error) {
return &user.User{Username: "systemuser", HomeDir: "/Volumes/test_volume/Users/systemuser"}, nil
return nil, errors.New("current user lookup should not be called")
},
WithPlistLocation(expectedPlistLocation),
)

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

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"
func TestNewClientWithHomeDirAndPlistLocation(t *testing.T) {
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, 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")
}
Expand All @@ -131,7 +91,6 @@ func TestNewClientReturnsCurrentUserLookupError(t *testing.T) {
)

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")
}
Expand Down
Loading