Skip to content
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
16 changes: 2 additions & 14 deletions cmd/toolset/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/kazhuravlev/toolset/internal/humanize"
"github.com/kazhuravlev/toolset/internal/timeh"

"github.com/kazhuravlev/optional"
Expand Down Expand Up @@ -679,7 +680,7 @@ func cmdInfo(_ *cli.Context, wd *workdir.Workdir) error {

rows := []table.Row{
{"Version:", version},
{"Cache Size:", humanizeBytes(info.Storage.TotalBytes)},
{"Cache Size:", humanize.Bytes(info.Storage.TotalBytes)},
{"Cache dir:", info.Locations.CacheDir},
{"Toolset File:", info.Locations.ToolsetFile},
{"Toolset Lock File:", info.Locations.ToolsetLockFile},
Expand Down Expand Up @@ -747,16 +748,3 @@ func cmdEnsureModuleVersion(c *cli.Context, wd *workdir.Workdir) error {

return nil
}

func humanizeBytes(bytes int64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}
84 changes: 84 additions & 0 deletions internal/fsh/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package fsh_test

import (
"testing"

"github.com/kazhuravlev/toolset/internal/fsh"
"github.com/stretchr/testify/require"
)

func TestExt(t *testing.T) {
tests := []struct {
name string
filename string
exp string
}{
{
name: "simple zip file",
filename: "archive.zip",
exp: ".zip",
},
{
name: "tar.gz file",
filename: "archive.tar.gz",
exp: ".tar.gz",
},
{
name: "tgz file",
filename: "archive.tgz",
exp: ".tgz",
},
{
name: "tar.bz2 file",
filename: "archive.tar.bz2",
exp: ".tar.bz2",
},
{
name: "tar.xz file",
filename: "archive.tar.xz",
exp: ".tar.xz",
},
{
name: "with path",
filename: "/path/to/archive.tar.gz",
exp: ".tar.gz",
},
{
name: "uppercase extension",
filename: "archive.TAR.GZ",
exp: ".tar.gz",
},
{
name: "mixed case extension",
filename: "archive.Tar.Gz",
exp: ".tar.gz",
},
{
name: "no extension",
filename: "noext",
exp: "",
},
{
name: "hidden file with extension",
filename: ".hidden.tar.gz",
exp: ".tar.gz",
},
{
name: "multiple dots",
filename: "my.archive.file.tar.gz",
exp: ".tar.gz",
},
{
name: "exe file",
filename: "tool.exe",
exp: ".exe",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := fsh.Ext(tt.filename)
require.Equal(t, tt.exp, got)
})
}
}
11 changes: 10 additions & 1 deletion internal/fsh/real_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,20 @@ func TestLocks(t *testing.T) {

fs := fsh.NewRealFS()

const filename = "/tmp/test.lock"
// Use a unique temporary file for this test
tmpDir := t.TempDir()
filename := tmpDir + "/test.lock"

// Ensure clean state
_ = os.Remove(filename)

// Lock file
unlock, err := fs.Lock(ctx, filename)
require.NoError(t, err)
defer func() {
// Cleanup: ensure the lock is released and file is removed
_ = os.Remove(filename)
}()

ch := make(chan struct{})
go func() {
Expand Down
16 changes: 16 additions & 0 deletions internal/humanize/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package humanize

import "fmt"

func Bytes(bytes int64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}
63 changes: 63 additions & 0 deletions internal/humanize/bytes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package humanize_test

import (
"testing"

"github.com/kazhuravlev/toolset/internal/humanize"
"github.com/stretchr/testify/require"
)

func TestHumanizeBytes(t *testing.T) {
tests := []struct {
bytes int64
want string
}{
{
bytes: 0,
want: "0 B",
},
{
bytes: 512,
want: "512 B",
},
{
bytes: 1024,
want: "1.0 KB",
},
{
bytes: 1536,
want: "1.5 KB",
},
{
bytes: 1024 * 1024,
want: "1.0 MB",
},
{
bytes: 1024*1024*2 + 1024*512,
want: "2.5 MB",
},
{
bytes: 1024 * 1024 * 1024,
want: "1.0 GB",
},
{
bytes: 1024*1024*1024*3 + 1024*1024*700,
want: "3.7 GB",
},
{
bytes: 1024 * 1024 * 1024 * 1024,
want: "1.0 TB",
},
{
bytes: 5*1024*1024*1024 + 256*1024*1024,
want: "5.2 GB",
},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
got := humanize.Bytes(tt.bytes)
require.Equal(t, tt.want, got)
})
}
}
Loading
Loading