CLI test coverage

This commit is contained in:
Ben Johnson
2022-01-11 13:05:14 -07:00
parent 3f0ec9fa9f
commit f308e0b154
154 changed files with 1619 additions and 312 deletions

View File

@@ -7,6 +7,7 @@ import (
"regexp"
"strconv"
"syscall"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
@@ -190,3 +191,30 @@ var (
Help: "The number of bytes used by replica operations",
}, []string{"replica_type", "operation"})
)
// TruncateDuration truncates d to the nearest major unit (s, ms, µs, ns).
func TruncateDuration(d time.Duration) time.Duration {
if d < 0 {
if d < -10*time.Second {
return d.Truncate(time.Second)
} else if d < -time.Second {
return d.Truncate(time.Second / 10)
} else if d < -time.Millisecond {
return d.Truncate(time.Millisecond)
} else if d < -time.Microsecond {
return d.Truncate(time.Microsecond)
}
return d
}
if d > 10*time.Second {
return d.Truncate(time.Second)
} else if d > time.Second {
return d.Truncate(time.Second / 10)
} else if d > time.Millisecond {
return d.Truncate(time.Millisecond)
} else if d > time.Microsecond {
return d.Truncate(time.Microsecond)
}
return d
}

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"reflect"
"testing"
"time"
"github.com/benbjohnson/litestream/internal"
)
@@ -59,3 +60,41 @@ func TestParseWALSegmentPath(t *testing.T) {
})
}
}
func TestTruncateDuration(t *testing.T) {
for _, tt := range []struct {
input, output time.Duration
}{
{0, 0 * time.Nanosecond},
{1, 1 * time.Nanosecond},
{12, 12 * time.Nanosecond},
{123, 123 * time.Nanosecond},
{1234, 1 * time.Microsecond},
{12345, 12 * time.Microsecond},
{123456, 123 * time.Microsecond},
{1234567, 1 * time.Millisecond},
{12345678, 12 * time.Millisecond},
{123456789, 123 * time.Millisecond},
{1234567890, 1200 * time.Millisecond},
{12345678900, 12 * time.Second},
{-1, -1 * time.Nanosecond},
{-12, -12 * time.Nanosecond},
{-123, -123 * time.Nanosecond},
{-1234, -1 * time.Microsecond},
{-12345, -12 * time.Microsecond},
{-123456, -123 * time.Microsecond},
{-1234567, -1 * time.Millisecond},
{-12345678, -12 * time.Millisecond},
{-123456789, -123 * time.Millisecond},
{-1234567890, -1200 * time.Millisecond},
{-12345678900, -12 * time.Second},
} {
t.Run(fmt.Sprint(int(tt.input)), func(t *testing.T) {
if got, want := internal.TruncateDuration(tt.input), tt.output; got != want {
t.Fatalf("duration=%s, want %s", got, want)
}
})
}
}

View File

@@ -0,0 +1,43 @@
package testingutil
import (
"os"
"testing"
)
// MustReadFile reads all data from filename. Fail on error.
func MustReadFile(tb testing.TB, filename string) []byte {
tb.Helper()
b, err := os.ReadFile(filename)
if err != nil {
tb.Fatal(err)
}
return b
}
// Getpwd returns the working directory. Fail on error.
func Getwd(tb testing.TB) string {
tb.Helper()
dir, err := os.Getwd()
if err != nil {
tb.Fatal(err)
}
return dir
}
// Setenv sets the environment variable key to value. The returned function reverts it.
func Setenv(tb testing.TB, key, value string) func() {
tb.Helper()
prevValue := os.Getenv(key)
if err := os.Setenv(key, value); err != nil {
tb.Fatal(err)
}
return func() {
if err := os.Setenv(key, prevValue); err != nil {
tb.Fatal(tb)
}
}
}