Skip to content

Commit

Permalink
fixed bug where deletion of an account would not work, if the it was …
Browse files Browse the repository at this point in the history
…the first account in the slice
  • Loading branch information
KonstantinGasser committed Feb 18, 2022
1 parent be99f6a commit a412425
Show file tree
Hide file tree
Showing 34 changed files with 50 additions and 2,707 deletions.
Binary file modified build/sherlock
Binary file not shown.
8 changes: 4 additions & 4 deletions internal/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ func (g group) lookup(accountName string) (*account, error) {
// delete deletes a given account from the group, returns an ErrNoSuchAccount
// if account not present
func (g *group) delete(account string) error {
var offset *int
var offset int = -1
for i, a := range g.Accounts {
if a.Name == account {
offset = &i
offset = i
}
}
if offset == nil {
if offset == -1 {
return ErrNoSuchAccount
}

g.Accounts = append(g.Accounts[:*offset], g.Accounts[*offset+1:]...)
g.Accounts = append(g.Accounts[:offset], g.Accounts[offset+1:]...)
return nil
}

Expand Down
44 changes: 44 additions & 0 deletions internal/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,47 @@ func TestFilterByTag(t *testing.T) {
}
}
}

func TestDeleteAccount(t *testing.T) {
tt := []struct {
name string
group *group
toBeDeleted string
want error
}{
{
name: "deletion of account, no error",
toBeDeleted: "acc1",
group: &group{
GID: "g1",
Accounts: []*account{{Name: "acc1"}, {Name: "acc2"}},
},
want: nil,
},
{
name: "deletion of account, with error",
toBeDeleted: "acc3",
group: &group{
GID: "g1",
Accounts: []*account{{Name: "acc1"}, {Name: "acc2"}},
},
want: ErrNoSuchAccount,
},
}

for _, tc := range tt {
err := tc.group.delete(tc.toBeDeleted)

if err != tc.want {
t.Fatalf("group.delete: %s: want: %v, have: %v", tc.name, tc.want, err)
}
// if no error is wanted, check if account got deleted
if tc.want == nil {
for _, acc := range tc.group.Accounts {
if acc.Name == tc.toBeDeleted {
t.Fatalf("group.delete: %s: account %q NOT delete", tc.name, tc.toBeDeleted)
}
}
}
}
}
4 changes: 2 additions & 2 deletions internal/sherlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (sh Sherlock) GetAccount(query string, groupKey string) (*account, error) {
//
// it allows to modify a group/account (adding accounts, changing account) through the passed StateOption.
func (sh Sherlock) UpdateState(ctx context.Context, query, groupKey string, opt StateOption) error {
gid, name, err := SplitQuery(query)
gid, account, err := SplitQuery(query)
if err != nil {
return err
}
Expand All @@ -230,7 +230,7 @@ func (sh Sherlock) UpdateState(ctx context.Context, query, groupKey string, opt
if err != nil {
return err
}
if err := opt(group, name); err != nil {
if err := opt(group, account); err != nil {
return err
}
return sh.writeGroup(ctx, gid, groupKey, group)
Expand Down
Binary file modified sherlock-darwin.tar.gz
Binary file not shown.
3 changes: 0 additions & 3 deletions sherlock-main-2/.gitignore

This file was deleted.

14 changes: 0 additions & 14 deletions sherlock-main-2/.travis.yml

This file was deleted.

202 changes: 0 additions & 202 deletions sherlock-main-2/LICENSE

This file was deleted.

4 changes: 0 additions & 4 deletions sherlock-main-2/Makefile

This file was deleted.

Loading

0 comments on commit a412425

Please sign in to comment.