Skip to content

Commit

Permalink
Merge pull request #44 from kazhuravlev/ka/tests-2
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
kazhuravlev authored Jan 3, 2025
2 parents 909ed63 + 9ef75cc commit 2fd4ff9
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 124 deletions.
1 change: 1 addition & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ tasks:
cmds:
- task: "init"
- "{{.TOOLSET}} sync"
- "{{.TOOLSET}} which goimports gofumpt"
- "{{.TOOLSET}} list"
- "{{.TOOLSET}} remove goimports gofumpt"
- "{{.TOOLSET}} list"
Expand Down
153 changes: 32 additions & 121 deletions cmd/toolset/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

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

"github.com/kazhuravlev/optional"
"github.com/kazhuravlev/toolset/internal/fsh"

Expand Down Expand Up @@ -69,7 +71,7 @@ func main() {
At this point tool will not be installed. In order to install added tool please run
$ toolset sync`,
Action: cmdAdd,
Action: withWorkdir(cmdAdd),
Flags: []cli.Flag{
&cli.StringFlag{
Name: keyCopyFrom,
Expand All @@ -92,7 +94,7 @@ At this point tool will not be installed. In order to install added tool please
{
Name: "sync",
Usage: "install all required tools from toolset file",
Action: cmdSync,
Action: withWorkdir(cmdSync),
Flags: []cli.Flag{
flagParallel,
&cli.StringSliceFlag{
Expand All @@ -105,13 +107,13 @@ At this point tool will not be installed. In order to install added tool please
{
Name: "run",
Usage: "run installed tool by its name",
Action: cmdRun,
Action: withWorkdir(cmdRun),
Args: true,
},
{
Name: "upgrade",
Usage: "upgrade deps to the latest versions",
Action: cmdUpgrade,
Action: withWorkdir(cmdUpgrade),
Flags: []cli.Flag{
flagParallel,
&cli.StringSliceFlag{
Expand All @@ -132,18 +134,18 @@ At this point tool will not be installed. In order to install added tool please
Value: false,
},
},
Action: cmdList,
Action: withWorkdir(cmdList),
},
{
Name: "which",
Usage: "show path to the actual binary",
Action: cmdWhich,
Action: withWorkdir(cmdWhich),
Args: true,
},
{
Name: "remove",
Usage: "remove tool",
Action: cmdRemove,
Action: withWorkdir(cmdRemove),
Args: true,
},
{
Expand All @@ -156,13 +158,13 @@ At this point tool will not be installed. In order to install added tool please
Description: `Install runtime in local project dir.
$ toolset runtime add [email protected]`,
Action: cmdRuntimeAdd,
Action: withWorkdir(cmdRuntimeAdd),
Args: true,
},
{
Name: "list",
Usage: "list all runtimes",
Action: cmdRuntimeList,
Action: withWorkdir(cmdRuntimeList),
},
},
},
Expand Down Expand Up @@ -210,15 +212,21 @@ func cmdInit(c *cli.Context) error {
return nil
}

func cmdAdd(c *cli.Context) error {
fs := fsh.NewRealFS()
func withWorkdir(fn func(c *cli.Context, wd *workdir.Workdir) error) func(c *cli.Context) error {
return func(c *cli.Context) error {
fs := fsh.NewRealFS()

tags := c.StringSlice(keyTags)
wd, err := workdir.New(c.Context, fs, "./")
if err != nil {
return fmt.Errorf("new workdir: %w", err)
}

wd, err := workdir.New(c.Context, fs, "./")
if err != nil {
return fmt.Errorf("new workdir: %w", err)
return fn(c, wd)
}
}

func cmdAdd(c *cli.Context, wd *workdir.Workdir) error {
tags := c.StringSlice(keyTags)

if val := c.String(keyCopyFrom); val != "" {
count, err := wd.CopySource(c.Context, val, tags)
Expand Down Expand Up @@ -277,14 +285,7 @@ func cmdAdd(c *cli.Context) error {
return nil
}

func cmdRuntimeAdd(c *cli.Context) error {
fs := fsh.NewRealFS()

wd, err := workdir.New(c.Context, fs, "./")
if err != nil {
return fmt.Errorf("new workdir: %w", err)
}

func cmdRuntimeAdd(c *cli.Context, wd *workdir.Workdir) error {
runtime := c.Args().First()

if err := wd.RuntimeAdd(c.Context, runtime); err != nil {
Expand All @@ -298,14 +299,7 @@ func cmdRuntimeAdd(c *cli.Context) error {
return nil
}

func cmdRuntimeList(c *cli.Context) error {
fs := fsh.NewRealFS()

wd, err := workdir.New(c.Context, fs, "./")
if err != nil {
return fmt.Errorf("new workdir: %w", err)
}

func cmdRuntimeList(c *cli.Context, wd *workdir.Workdir) error {
t := table.NewWriter()
t.AppendHeader(table.Row{
"Runtime",
Expand All @@ -326,19 +320,12 @@ func cmdRuntimeList(c *cli.Context) error {
return nil
}

func cmdRun(c *cli.Context) error {
fs := fsh.NewRealFS()

func cmdRun(c *cli.Context, wd *workdir.Workdir) error {
target := c.Args().First()
if target == "" {
return fmt.Errorf("target is required")
}

wd, err := workdir.New(c.Context, fs, "./")
if err != nil {
return fmt.Errorf("new workdir: %w", err)
}

if err := wd.RunTool(c.Context, target, c.Args().Tail()...); err != nil {
if errors.Is(err, workdir.ErrToolNotFoundInSpec) {
fmt.Println("tool not added. Run `toolset add --help` to add this tool")
Expand All @@ -364,19 +351,12 @@ func cmdRun(c *cli.Context) error {
return nil
}

func cmdSync(c *cli.Context) error {
fs := fsh.NewRealFS()

func cmdSync(c *cli.Context, wd *workdir.Workdir) error {
ctx := c.Context

maxWorkers := c.Int(keyParallel)
tags := c.StringSlice(keyTags)

wd, err := workdir.New(ctx, fs, "./")
if err != nil {
return fmt.Errorf("new workdir: %w", err)
}

if err := wd.Sync(ctx, maxWorkers, tags); err != nil {
return fmt.Errorf("sync: %w", err)
}
Expand All @@ -388,19 +368,12 @@ func cmdSync(c *cli.Context) error {
return nil
}

func cmdUpgrade(c *cli.Context) error {
fs := fsh.NewRealFS()

func cmdUpgrade(c *cli.Context, wd *workdir.Workdir) error {
ctx := c.Context

maxWorkers := c.Int(keyParallel)
tags := c.StringSlice(keyTags)

wd, err := workdir.New(ctx, fs, "./")
if err != nil {
return fmt.Errorf("new workdir: %w", err)
}

if err := wd.Upgrade(c.Context, tags); err != nil {
return fmt.Errorf("upgrade: %w", err)
}
Expand All @@ -420,18 +393,11 @@ func cmdUpgrade(c *cli.Context) error {
return nil
}

func cmdList(c *cli.Context) error {
fs := fsh.NewRealFS()

func cmdList(c *cli.Context, wd *workdir.Workdir) error {
ctx := c.Context

onlyUnused := c.Bool(keyUnused)

wd, err := workdir.New(ctx, fs, "./")
if err != nil {
return fmt.Errorf("new workdir: %w", err)
}

tools, err := wd.GetTools(ctx)
if err != nil {
return fmt.Errorf("get tools: %w", err)
Expand All @@ -454,7 +420,7 @@ func cmdList(c *cli.Context) error {
for _, ts := range tools {
lastUse := "---"
if val, ok := ts.LastUse.Get(); ok {
lastUse = duration(time.Since(val))
lastUse = timeh.Duration(time.Since(val))
}

rows = append(rows, table.Row{
Expand Down Expand Up @@ -491,19 +457,12 @@ func cmdList(c *cli.Context) error {
return nil
}

func cmdWhich(c *cli.Context) error {
fs := fsh.NewRealFS()

func cmdWhich(c *cli.Context, wd *workdir.Workdir) error {
targets := c.Args().Slice()
if len(targets) == 0 {
return fmt.Errorf("target is required")
}

wd, err := workdir.New(c.Context, fs, "./")
if err != nil {
return fmt.Errorf("new workdir: %w", err)
}

for _, target := range targets {
ts, err := wd.FindTool(target)
if err != nil {
Expand All @@ -528,19 +487,12 @@ func cmdWhich(c *cli.Context) error {
return nil
}

func cmdRemove(c *cli.Context) error {
fs := fsh.NewRealFS()

func cmdRemove(c *cli.Context, wd *workdir.Workdir) error {
targets := c.Args().Slice()
if len(targets) == 0 {
return fmt.Errorf("target is required")
}

wd, err := workdir.New(c.Context, fs, "./")
if err != nil {
return fmt.Errorf("new workdir: %w", err)
}

for _, target := range targets {
if err := wd.RemoveTool(c.Context, target); err != nil {
return fmt.Errorf("remove tool (%s): %w", target, err)
Expand All @@ -553,44 +505,3 @@ func cmdRemove(c *cli.Context) error {

return nil
}

func duration(d time.Duration) string {
if d == 0 {
return "0s"
}

days := d / (24 * time.Hour)
d -= days * 24 * time.Hour

hours := d / time.Hour
d -= hours * time.Hour

minutes := d / time.Minute
d -= minutes * time.Minute

seconds := d / time.Second

// Build the human-readable string
var result string
if days > 0 {
result += fmt.Sprintf("%dd ", days)
}

if hours > 0 {
result += fmt.Sprintf("%dh ", hours)
}

if minutes > 0 {
result += fmt.Sprintf("%dm ", minutes)
}

if seconds > 0 {
result += fmt.Sprintf("%ds ", seconds)
}

if result == "" {
return "1s"
}

return result
}
5 changes: 5 additions & 0 deletions internal/fsh/filesystem_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fsh_test

import (
"os"
"runtime"
"testing"

"github.com/kazhuravlev/toolset/internal/fsh"
Expand Down Expand Up @@ -69,6 +70,10 @@ func TestWriteJson(t *testing.T) {

func TestReadOrCreateJson(t *testing.T) {
t.Run("auto_create_file_when_not_exists", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skip for Windows")
}

fs := fsh.NewMemFS(nil)

// 1. file not exists
Expand Down
41 changes: 41 additions & 0 deletions internal/fsh/mem_fs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package fsh_test

import (
"runtime"
"testing"

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

func TestMemFS_GetTree(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skip for Windows")
}

fs := fsh.NewMemFS(map[string]string{
"/dir/file1.txt": `Hello 1!`,
"/dir/file2.txt": `Hello 2!`,
"/dir/dir2/file3.txt": `Hello 3!`,
})

tree, err := fs.GetTree("/dir")
require.NoError(t, err)
require.Equal(t, []string{
"/dir",
"/dir/dir2",
"/dir/dir2/file3.txt",
"/dir/file1.txt",
"/dir/file2.txt",
}, tree)
}

func TestMemFS_SymlinkIfPossible(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skip for Windows")
}

fs := fsh.NewMemFS(nil)

require.NoError(t, fs.SymlinkIfPossible("/tmp/a", "/tmp/a"))
}
Loading

0 comments on commit 2fd4ff9

Please sign in to comment.