Skip to content

Commit

Permalink
Implement duplicate prevention in file append function for samples
Browse files Browse the repository at this point in the history
Added logic to `SampleUpdater`'s `Append` method to check for duplicate messages in the file before appending new ones. This functionality prevents the addition of duplicate entries, with case and leading/trailing whitespace irrelevant in determining duplicates.
  • Loading branch information
umputun committed Mar 22, 2024
1 parent 8378d18 commit cb54127
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
19 changes: 17 additions & 2 deletions app/bot/sample_updater.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bot

import (
"bufio"
"fmt"
"io"
"os"
Expand All @@ -27,9 +28,23 @@ func (s *SampleUpdater) Reader() (io.ReadCloser, error) {
return fh, nil
}

// Append a message to the file
// Append a message to the file, preventing duplicates
func (s *SampleUpdater) Append(msg string) error {
fh, err := os.OpenFile(s.fileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644) //nolint:gosec // keep it readable by all
fh, err := os.Open(s.fileName)
if err != nil {
return fmt.Errorf("failed to open %s: %w", s.fileName, err)
}
defer fh.Close()

scanner := bufio.NewScanner(fh)
for scanner.Scan() {
// if a line matches the message, return right away
if strings.EqualFold(strings.TrimSpace(scanner.Text()), strings.TrimSpace(msg)) {
return nil
}
}

fh, err = os.OpenFile(s.fileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644) //nolint:gosec // keep it readable by all
if err != nil {
return fmt.Errorf("failed to open %s: %w", s.fileName, err)
}
Expand Down
26 changes: 26 additions & 0 deletions app/bot/sample_updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ func TestSampleUpdater(t *testing.T) {
assert.Equal(t, "Test message\n", string(content))
})

t.Run("dedup", func(t *testing.T) {
file, err := os.CreateTemp(os.TempDir(), "sample")
require.NoError(t, err)
defer os.Remove(file.Name())

updater := NewSampleUpdater(file.Name())
err = updater.Append("Test message")
assert.NoError(t, err)

// duplicate message
err = updater.Append(" Test message ")
assert.NoError(t, err)

// duplicate message
err = updater.Append(" TesT MessagE")
assert.NoError(t, err)

reader, err := updater.Reader()
require.NoError(t, err)
defer reader.Close()

content, err := io.ReadAll(reader)
assert.NoError(t, err)
assert.Equal(t, "Test message\n", string(content))
})

t.Run("multi-line", func(t *testing.T) {
file, err := os.CreateTemp(os.TempDir(), "sample")
require.NoError(t, err)
Expand Down

0 comments on commit cb54127

Please sign in to comment.