Skip to content

Commit 2e9dc3e

Browse files
author
Florian Heinze
committed
TASK: parsing tests
1 parent 17c1dcd commit 2e9dc3e

8 files changed

Lines changed: 144 additions & 40 deletions

File tree

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func Execute(version, commit string) {
4343
})
4444
addDevScriptTasksAsCommands(RootCmd)
4545
RootCmd.AddCommand(buildInitCommand())
46+
RootCmd.AddCommand(buildTestTaskCommand())
4647
// TODO: fix autocompletion
4748
if err := RootCmd.Execute(); err != nil {
4849
fmt.Println(err)

cmd/task.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"github.com/gookit/color"
45
"github.com/spf13/cobra"
56
"log"
67
"main/utils"
@@ -13,9 +14,9 @@ import (
1314
func buildTaskCommand(task utils.DevScriptTask, devScriptPath string) *cobra.Command {
1415
var cmd = &cobra.Command{
1516
// IMPORTANT: never color Use! You will not be able to run the command otherwise.
16-
Use: task.Usage,
17-
Short: task.Short,
18-
Long: task.Long,
17+
Use: task.Name,
18+
Short: task.Title,
19+
Long: color.Bold.Text(task.Title) + "\n" + task.Description,
1920
Args: cobra.ArbitraryArgs,
2021
GroupID: utils.GROUP_ID_TASKS,
2122
// If this is true all flags will be passed to the command as arguments.
@@ -33,7 +34,7 @@ func buildTaskCommand(task utils.DevScriptTask, devScriptPath string) *cobra.Com
3334
if argsAsString == "--help" || argsAsString == "-h" {
3435
cmd.Help()
3536
} else {
36-
execDevScriptWithArguments(devScriptPath, append([]string{task.Usage}, args...))
37+
execDevScriptWithArguments(devScriptPath, append([]string{task.Name}, args...))
3738
}
3839
},
3940
}

cmd/test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
// This is a hack add a hint to cobra rendering
8+
func buildTestTaskCommand() *cobra.Command {
9+
return &cobra.Command{
10+
Use: "test",
11+
Short: "Test Task",
12+
Run: func(cmd *cobra.Command, args []string) {
13+
},
14+
}
15+
}

utils/DevScriptTask.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package utils
22

33
type DevScriptTask struct {
4-
Usage string
5-
Short string
6-
Long string
4+
Name string
5+
Title string
6+
Description string
77
}

utils/parse_comments.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package utils
2+
3+
import (
4+
"regexp"
5+
)
6+
7+
func taskTitleFromComments(comments string) string {
8+
reqex := regexp.MustCompile(`^.*`)
9+
return reqex.FindString(comments)
10+
}
11+
12+
func taskDescriptionFromComments(comments string) string {
13+
//reqex := regexp.MustCompile(`(?ms)(?:\n|\r\n|\r)(.*?)((?:\n|\r\n|\r)^Usage:|Examples:|\n$)`)
14+
reqex := regexp.MustCompile(`(?ms)(?:\n|\r\n|\r)(.*)`)
15+
match := reqex.FindStringSubmatch(comments)
16+
if len(match) > 1 {
17+
// return capture group `(.*?)`
18+
return match[1]
19+
}
20+
return ""
21+
}

utils/parse_comments_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package utils
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestParseComments_TitleAndDescription(t *testing.T) {
9+
// title only
10+
comment := "some title"
11+
titleExpected := "some title"
12+
titleActual := taskTitleFromComments(comment)
13+
assert.Equal(t, titleExpected, titleActual)
14+
descriptionExpected := ""
15+
descriptionActual := taskDescriptionFromComments(comment)
16+
assert.Equal(t, descriptionExpected, descriptionActual)
17+
18+
// title only with linebreak
19+
comment = "some title\n"
20+
titleExpected = "some title"
21+
titleActual = taskTitleFromComments(comment)
22+
assert.Equal(t, titleExpected, titleActual)
23+
descriptionExpected = ""
24+
descriptionActual = taskDescriptionFromComments(comment)
25+
assert.Equal(t, descriptionExpected, descriptionActual)
26+
27+
// title with comment
28+
comment = ""
29+
comment += "some title\n"
30+
comment += "Description Line 1\n"
31+
comment += "Description Line 2\n"
32+
titleExpected = "some title"
33+
titleActual = taskTitleFromComments(comment)
34+
assert.Equal(t, titleExpected, titleActual)
35+
descriptionExpected = "Description Line 1\n"
36+
descriptionExpected += "Description Line 2\n"
37+
descriptionActual = taskDescriptionFromComments(comment)
38+
assert.Equal(t, descriptionExpected, descriptionActual)
39+
40+
// title with comment and Usage or Examples
41+
//
42+
/*
43+
comment = ""
44+
comment += "some title\n"
45+
comment += "Description Line 1\n"
46+
comment += "Description Line 2\n"
47+
comment += "Usage:\n"
48+
titleExpected = "some title"
49+
titleActual = taskTitleFromComments(comment)
50+
assert.Equal(t, titleExpected, titleActual)
51+
descriptionExpected = "Description Line 1\n"
52+
descriptionExpected += "Description Line 2"
53+
descriptionActual = taskDescriptionFromComments(comment)
54+
assert.Equal(t, descriptionExpected, descriptionActual)
55+
56+
// title with comment and "Examples:"
57+
comment = ""
58+
comment += "some title\n"
59+
comment += "Description Line 1\n"
60+
comment += "Description Line 2\n"
61+
comment += "Examples:\n"
62+
titleExpected = "some title"
63+
titleActual = taskTitleFromComments(comment)
64+
assert.Equal(t, titleExpected, titleActual)
65+
descriptionExpected = "Description Line 1\n"
66+
// TODO: fix reqex to exclude \n of last line
67+
descriptionExpected += "Description Line 2\n"
68+
descriptionActual = taskDescriptionFromComments(comment)
69+
assert.Equal(t, descriptionExpected, descriptionActual)
70+
*/
71+
}
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ func ParseDevScriptTasks(devScriptPath string) []DevScriptTask {
2525
var results = []DevScriptTask{}
2626
for _, match := range matches {
2727
task := match[taskIndex]
28-
comments := processComments(match[commentsIndex])
28+
comments := prepareComments(match[commentsIndex])
2929

3030
if !strings.HasPrefix(task, "_") {
3131
results = append(results, DevScriptTask{
32-
Usage: task,
33-
Short: extractShortFromComments(comments),
34-
Long: comments,
32+
Name: task,
33+
Title: taskTitleFromComments(comments),
34+
Description: taskDescriptionFromComments(comments),
3535
})
3636
}
3737
}
@@ -40,12 +40,7 @@ func ParseDevScriptTasks(devScriptPath string) []DevScriptTask {
4040

4141
// https://regex101.com/r/Jo4uSX/1
4242
// `|\n$` matches new line and end of string
43-
func processComments(comments string) string {
43+
func prepareComments(comments string) string {
4444
reqex := regexp.MustCompile(`(?m)^# ?|\n$`)
4545
return reqex.ReplaceAllString(comments, "")
4646
}
47-
48-
func extractShortFromComments(comments string) string {
49-
reqex := regexp.MustCompile(`^.*`)
50-
return reqex.FindString(comments)
51-
}
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ import (
77

88
func TestParseDevScriptTasks_TasksSyntax(t *testing.T) {
99
expected := []DevScriptTask{
10-
{Usage: "task-syntax-1a"},
11-
{Usage: "task-syntax-1b"},
12-
{Usage: "task-syntax-1c"},
13-
{Usage: "task-syntax-2a"},
14-
{Usage: "task-syntax-2b"},
15-
{Usage: "task-syntax-2c"},
16-
{Usage: "task-syntax-3a"},
10+
{Name: "task-syntax-1a"},
11+
{Name: "task-syntax-1b"},
12+
{Name: "task-syntax-1c"},
13+
{Name: "task-syntax-2a"},
14+
{Name: "task-syntax-2b"},
15+
{Name: "task-syntax-2c"},
16+
{Name: "task-syntax-3a"},
1717
}
1818
actual := ParseDevScriptTasks("./fixtures/tasks_syntax.sh")
1919
assert.Equal(t, expected, actual)
2020
}
2121

2222
func TestParseDevScriptTasks_IgnoreTasksStartingWithUnderscore(t *testing.T) {
2323
expected := []DevScriptTask{
24-
{Usage: "task-not-starting-with-underscore"},
25-
{Usage: "task-not-starting-with-underscore_"},
26-
{Usage: "task-not-starting-with_underscore"},
24+
{Name: "task-not-starting-with-underscore"},
25+
{Name: "task-not-starting-with-underscore_"},
26+
{Name: "task-not-starting-with_underscore"},
2727
// should not be parsed
2828
// {Usage: "_task-without-underscore"},
2929
// {Usage: "__task-without-underscore"},
@@ -35,7 +35,7 @@ func TestParseDevScriptTasks_IgnoreTasksStartingWithUnderscore(t *testing.T) {
3535

3636
func TestParseDevScriptTasks_IgnoreCommentedOutTasks(t *testing.T) {
3737
expected := []DevScriptTask{
38-
{Usage: "task"},
38+
{Name: "task"},
3939
// should not be parsed
4040
// {Usage: "task-commented-out"},
4141
}
@@ -46,31 +46,31 @@ func TestParseDevScriptTasks_IgnoreCommentedOutTasks(t *testing.T) {
4646
func TestParseDevScriptTasks_TaskCommentsShouldBeParsed(t *testing.T) {
4747
expected := []DevScriptTask{
4848
{
49-
Usage: "one-line-comment-block",
50-
Short: "one line comment block - 1",
49+
Name: "one-line-comment-block",
50+
Title: "one line comment block - 1",
5151
// TODO: fix trailing \n
52-
Long: "one line comment block - 1",
52+
Description: "one line comment block - 1",
5353
},
5454
{
55-
Usage: "multiline-comment-block",
56-
Short: "multiline comment block - 1",
55+
Name: "multiline-comment-block",
56+
Title: "multiline comment block - 1",
5757
// TODO: fix trailing \n
58-
Long: "multiline comment block - 1\nmultiline comment block - 2\nmultiline comment block - 3",
58+
Description: "multiline comment block - 1\nmultiline comment block - 2\nmultiline comment block - 3",
5959
},
6060
{
61-
Usage: "empty-lines-in-comment-block",
62-
Short: "multiline comment block with empty lines - 1",
61+
Name: "empty-lines-in-comment-block",
62+
Title: "multiline comment block with empty lines - 1",
6363
// TODO: fix trailing \n
64-
Long: "multiline comment block with empty lines - 1\n\nmultiline comment block with empty lines - 2\n\nmultiline comment block with empty lines - 3",
64+
Description: "multiline comment block with empty lines - 1\n\nmultiline comment block with empty lines - 2\n\nmultiline comment block with empty lines - 3",
6565
},
6666
{
67-
Usage: "leading-spaces-or-none-in-comment-block",
68-
Short: "leading space missing",
67+
Name: "leading-spaces-or-none-in-comment-block",
68+
Title: "leading space missing",
6969
// TODO: fix trailing \n
7070
// remove # and one space always if present
7171
// keep \n but remove spaces if they are the only chars after #
7272
// also keep empty lines
73-
Long: "leading space missing\n keep leading-spaces used for indents\n\n\n\ncomment block end",
73+
Description: "leading space missing\n keep leading-spaces used for indents\n\n\n\ncomment block end",
7474
},
7575
}
7676
actual := ParseDevScriptTasks("./fixtures/tasks_with_comments.sh")

0 commit comments

Comments
 (0)