Skip to content

Commit

Permalink
refactor(general): minor miscellaneous cleanups (kopia#4074)
Browse files Browse the repository at this point in the history
Cleanups:

- use non-format variants of Log/Print with no additional args;
- fold in Fprintf call with no args into the following one;
- add missing arg placeholder in format strings;
- use require.Positive instead of Greater(..., 0);
- rename function to fillWithZeros to avoid collision with builtin clear;
- define type for context key to avoid collisions.
  • Loading branch information
julio-lopez authored Aug 26, 2024
1 parent 6902738 commit 948162d
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 22 deletions.
9 changes: 4 additions & 5 deletions cli/json_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (l *jsonList) begin(o *jsonOutput) {
l.o = o

if o.jsonOutput {
fmt.Fprintf(l.o.out, "[") //nolint:errcheck
fmt.Fprint(l.o.out, "[") //nolint:errcheck

if !o.jsonIndent {
l.separator = "\n "
Expand All @@ -107,16 +107,15 @@ func (l *jsonList) begin(o *jsonOutput) {
func (l *jsonList) end() {
if l.o.jsonOutput {
if !l.o.jsonIndent {
fmt.Fprintf(l.o.out, "\n") //nolint:errcheck
fmt.Fprint(l.o.out, "\n") //nolint:errcheck
}

fmt.Fprintf(l.o.out, "]") //nolint:errcheck
fmt.Fprint(l.o.out, "]") //nolint:errcheck
}
}

func (l *jsonList) emit(v interface{}) {
fmt.Fprintf(l.o.out, l.separator) //nolint:errcheck
fmt.Fprintf(l.o.out, "%s", l.o.jsonBytes(v)) //nolint:errcheck
fmt.Fprintf(l.o.out, "%s%s", l.separator, l.o.jsonBytes(v)) //nolint:errcheck

if l.o.jsonIndent {
l.separator = ","
Expand Down
2 changes: 1 addition & 1 deletion internal/gather/gather_bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func TestGatherBytesReaderAtVariableInputBufferSizes(t *testing.T) {

// write the generated data
n, err := preWrt.Write(buf)
require.NoErrorf(t, err, "Write() faiiled, inputBufferSize: %8", tc.inputBufferSize)
require.NoErrorf(t, err, "Write() faiiled, inputBufferSize: %v", tc.inputBufferSize)
require.Equalf(t, defaultAllocator.chunkSize, preWrt.alloc.chunkSize,
"this test expects that the default-allocator will be used, but we are using: %#v", preWrt.alloc)

Expand Down
2 changes: 1 addition & 1 deletion internal/repodiag/log_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestLogManager_AutoFlush(t *testing.T) {
var b [1024]byte

rand.Read(b[:])
l.Infof(hex.EncodeToString(b[:]))
l.Info(hex.EncodeToString(b[:]))
}

w.Wait(ctx)
Expand Down
2 changes: 1 addition & 1 deletion repo/blob/s3/s3_versioned_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ func compareMetadata(tb testing.TB, a, b versionMetadata) {
// deletion-marker metadata is not returned by the delete blob operation,
// and can only be retrieved later by listing versions.
if !a.IsDeleteMarker {
require.Equalf(tb, a.Version, b.Version, "blob versions do not match a:%v b:v", a, b)
require.Equalf(tb, a.Version, b.Version, "blob versions do not match a:%v b:%v", a, b)
}
}

Expand Down
2 changes: 1 addition & 1 deletion repo/content/content_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ func (bm *WriteManager) WriteContent(ctx context.Context, data gather.Bytes, pre
logbuf.AppendInt64(previousWriteTime)
}

bm.log.Debugf(logbuf.String())
bm.log.Debug(logbuf.String())

return contentID, bm.addToPackUnlocked(ctx, contentID, data, false, comp, previousWriteTime, mp)
}
Expand Down
2 changes: 1 addition & 1 deletion repo/content/content_manager_lock_free.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (sm *SharedManager) preparePackDataContent(mp format.MutableParameters, pp
sb.AppendUint32(info.PackedLength)
sb.AppendString(" d:")
sb.AppendBoolean(info.Deleted)
sm.log.Debugf(sb.String())
sm.log.Debug(sb.String())

packFileIndex.Add(info)
}
Expand Down
4 changes: 2 additions & 2 deletions repo/ecc/ecc_rs_crc.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (r *ReedSolomonCrcECC) Encrypt(input gather.Bytes, _ []byte, output *gather

// WriteBuffer does not clear the data, so we must clear the padding
if lengthSize+len(copied) < len(inputBytes) {
clear(inputBytes[lengthSize+len(copied):])
fillWithZeros(inputBytes[lengthSize+len(copied):])
}

// Compute and store ECC + checksum
Expand Down Expand Up @@ -261,7 +261,7 @@ func (r *ReedSolomonCrcECC) Decrypt(input gather.Bytes, _ []byte, output *gather

// WriteBuffer does not clear the data, so we must clear the padding
if len(copied) < len(inputBytes) {
clear(inputBytes[len(copied):])
fillWithZeros(inputBytes[len(copied):])
}

eccBytes := inputBytes[:parityPlusCrcSizeInBlock*sizes.Blocks]
Expand Down
6 changes: 3 additions & 3 deletions repo/ecc/ecc_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func applyPercent(val int, percent float32) int {
return int(math.Floor(float64(val) * float64(percent)))
}

func clear(bytes []byte) {
for i := range bytes {
bytes[i] = 0
func fillWithZeros(b []byte) {
for i := range b {
b[i] = 0
}
}

Expand Down
2 changes: 1 addition & 1 deletion snapshot/snapshotfs/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ func TestParallelUploadUploadsBlobsInParallel(t *testing.T) {

require.NoError(t, th.repo.Flush(ctx))

require.Greater(t, maxParallelCalls.Load(), int32(0))
require.Positive(t, maxParallelCalls.Load())
}

func randomBytes(n int64) []byte {
Expand Down
9 changes: 4 additions & 5 deletions tests/recovery/recovery_test/recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestConsistencyWhenKill9AfterModify(t *testing.T) {

o, err := cmd.CombinedOutput()
require.NoError(t, err)
t.Logf(string(o))
t.Log(string(o))

// create snapshot with StderrPipe
cmd = exec.Command(kopiaExe, "snap", "create", newDir, "--json", "--parallel=1")
Expand All @@ -240,9 +240,8 @@ func TestConsistencyWhenKill9AfterModify(t *testing.T) {
stdout, err := bm.RestoreGivenOrRandomSnapshot("", restoreDir)
require.NoError(t, err)

t.Logf(stdout)

t.Logf("Compare restored data and original data:")
t.Log(stdout)
t.Log("Compare restored data and original data:")
CompareDirs(t, restoreDir, cmpDir)
}

Expand All @@ -267,7 +266,7 @@ func killOnCondition(t *testing.T, cmd *exec.Cmd) {

for scanner.Scan() {
output := scanner.Text()
t.Logf(output)
t.Log(output)

// Check if the output contains the "hashing" etc.
if strings.Contains(output, "hashing") && strings.Contains(output, "hashed") && strings.Contains(output, "uploaded") {
Expand Down
4 changes: 3 additions & 1 deletion tests/robustness/multiclient_test/framework/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (

const nameLen int = 2

var clientKey = struct{}{}
type clientKeyT struct{}

var clientKey clientKeyT

// Client is a unique client for use in multiclient robustness tests.
type Client struct {
Expand Down

0 comments on commit 948162d

Please sign in to comment.