-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.go
73 lines (57 loc) · 1.42 KB
/
tests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"errors"
"fmt"
"math"
"os"
"strconv"
"strings"
"github.com/urfave/cli/v2"
)
func readFile(path string) (string, error) {
interval := [2]int{0, math.MaxInt}
if strings.Contains(path, "#") {
s := strings.Split(path, "#")
path = s[0]
var err error
for i, v := range strings.Split(s[1], ":") {
interval[i], err = strconv.Atoi(v)
if err != nil {
return "", errors.New("invalid line selectors in file path")
}
}
}
bytes, err := os.ReadFile(path)
if err != nil {
return "", err
}
str := string(bytes)
lines := strings.Split(str, "\n")
str = strings.Join(lines[max(interval[0]-1, 0):min(interval[1], len(lines))], "\n")
return str, nil
}
func TestsCommand(ctx *cli.Context) error {
inputPath := ctx.Args().Get(0)
outPath := ctx.Args().Get(1)
if inputPath == "" || outPath == "" {
return cli.Exit("Input and output file paths must be defined", 1)
}
inputStr, err := readFile(inputPath)
if err != nil {
return err
}
s := CreateSpinner()
s.Suffix = " Generating tests for code snippet 🪄\n"
s.Start()
result, err := AskAI(fmt.Sprintf(`Generate tests code for the following code snippet based on what it does in the same language.\n\n %s`, inputStr))
s.Stop()
if err != nil {
return err
}
err = os.WriteFile(outPath, []byte(result), 0644)
if err != nil {
return err
}
fmt.Print(boldGreen("\n✅ Test cases generated successfully 🐙\n"))
return nil
}