diff --git a/CHANGES.md b/CHANGES.md index 40d2f73..2257aaa 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,7 @@ - deezer: fixed endless export loop if the user's listen history was empty. - dump: it is now possible to specify a file to write the text output to. - Fixed potential issues with MusicBrainz rate limiting. +- Fixed import log output duplicating. ## 0.6.0 - 2025-05-23 diff --git a/internal/backends/import.go b/internal/backends/import.go index e7a6add..97912dd 100644 --- a/internal/backends/import.go +++ b/internal/backends/import.go @@ -112,8 +112,9 @@ func process[R models.LovesResult | models.ListensResult, P ImportProcessor[R]]( return } - importResult, err := processor.Import(ctx, exportResult, result, out, progress) - result.Update(importResult) + importResult, err := processor.Import( + ctx, exportResult, result.Copy(), out, progress) + result.Update(&importResult) if err != nil { processor.ImportBackend().FinishImport() out <- handleError(result, err, progress) diff --git a/internal/models/models.go b/internal/models/models.go index 78d9965..a93a043 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -196,11 +196,21 @@ func (i *ImportResult) UpdateTimestamp(newTime time.Time) { } } -func (i *ImportResult) Update(from ImportResult) { - i.TotalCount = from.TotalCount - i.ImportCount = from.ImportCount - i.UpdateTimestamp(from.LastTimestamp) - i.ImportLog = append(i.ImportLog, from.ImportLog...) +func (i *ImportResult) Update(from *ImportResult) { + if i != from { + i.TotalCount = from.TotalCount + i.ImportCount = from.ImportCount + i.UpdateTimestamp(from.LastTimestamp) + i.ImportLog = append(i.ImportLog, from.ImportLog...) + } +} + +func (i *ImportResult) Copy() ImportResult { + return ImportResult{ + TotalCount: i.TotalCount, + ImportCount: i.ImportCount, + LastTimestamp: i.LastTimestamp, + } } func (i *ImportResult) Log(t LogEntryType, msg string) { diff --git a/internal/models/models_test.go b/internal/models/models_test.go index 5395610..47ef86f 100644 --- a/internal/models/models_test.go +++ b/internal/models/models_test.go @@ -1,5 +1,5 @@ /* -Copyright © 2023 Philipp Wolfer +Copyright © 2023-2025 Philipp Wolfer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -138,13 +138,31 @@ func TestImportResultUpdate(t *testing.T) { LastTimestamp: time.Now().Add(1 * time.Hour), ImportLog: []models.LogEntry{logEntry2}, } - result.Update(newResult) + result.Update(&newResult) assert.Equal(t, 120, result.TotalCount) assert.Equal(t, 50, result.ImportCount) assert.Equal(t, newResult.LastTimestamp, result.LastTimestamp) assert.Equal(t, []models.LogEntry{logEntry1, logEntry2}, result.ImportLog) } +func TestImportResultCopy(t *testing.T) { + logEntry := models.LogEntry{ + Type: models.Warning, + Message: "foo", + } + result := models.ImportResult{ + TotalCount: 100, + ImportCount: 20, + LastTimestamp: time.Now(), + ImportLog: []models.LogEntry{logEntry}, + } + copy := result.Copy() + assert.Equal(t, result.TotalCount, copy.TotalCount) + assert.Equal(t, result.ImportCount, copy.ImportCount) + assert.Equal(t, result.LastTimestamp, copy.LastTimestamp) + assert.Empty(t, copy.ImportLog) +} + func TestImportResultLog(t *testing.T) { result := models.ImportResult{} result.Log(models.Warning, "foo")