Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore comments when parsing recipient files #2394

Merged
merged 2 commits into from
Nov 8, 2022
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
12 changes: 9 additions & 3 deletions internal/recipients/recipients.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ func Marshal(r []string) []byte {

out := bytes.Buffer{}
for _, k := range set.Sorted(r) {
_, _ = out.WriteString(k)
_, _ = out.WriteString(strings.TrimSpace(k))
_, _ = out.WriteString("\n")
}

return out.Bytes()
}

// Unmarshal Recipients line by line from a io.Reader. Handles Unix, Windows and Mac line endings.
// Note: Does not preserve comments!
func Unmarshal(buf []byte) []string {
in := strings.ReplaceAll(string(buf), "\r", "\n")

return set.Apply(set.SortedFiltered(strings.Split(in, "\n"), func(k string) bool {
return k != ""
return k != "" && !strings.HasPrefix(k, "#")
}), func(k string) string {
return strings.TrimSpace(k)
out := strings.TrimSpace(k)
if strings.Contains(out, " #") {
out = out[:strings.Index(k, " #")]
}

return out
})
}
35 changes: 34 additions & 1 deletion internal/recipients/recipients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,32 @@ import (
func TestMarshal(t *testing.T) {
t.Parallel()

t.Skip("implement this")
for _, tc := range []struct {
in []string
want string
}{
{
want: "foo@bar.com\n",
in: []string{"foo@bar.com\n\r"},
},
{
want: "baz@bar.com\nfoo@bar.com\n",
in: []string{"baz@bar.com", "foo@bar.com"},
},
{
want: "baz@bar.com\nzab@zab.com\n",
in: []string{"baz@bar.com", "zab@zab.com"},
},
} {
tc := tc
t.Run(tc.want, func(t *testing.T) {
t.Parallel()

sort.Strings(tc.in)
got := string(Marshal(tc.in))
assert.Equal(t, tc.want, got, tc.want)
})
}
}

func TestUnmarshal(t *testing.T) {
Expand All @@ -36,6 +61,14 @@ func TestUnmarshal(t *testing.T) {
in: "foo@bar.com\rbaz@bar.com\r",
want: []string{"baz@bar.com", "foo@bar.com"},
},
{
in: "# foo@bar.com\nbaz@bar.com\nzab@zab.com # comment",
want: []string{"baz@bar.com", "zab@zab.com"},
},
dominikschulz marked this conversation as resolved.
Show resolved Hide resolved
{
in: "# foo@bar.com\nbaz@bar.com\n# comment\nzab@zab.com\n",
want: []string{"baz@bar.com", "zab@zab.com"},
},
} {
tc := tc
t.Run(tc.in, func(t *testing.T) {
Expand Down