Skip to content

Commit 07b400a

Browse files
author
Florian Heinze
committed
TASK: dev script parsing test kickstart
1 parent f4db1d9 commit 07b400a

12 files changed

+257
-114
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ go 1.19
55
require (
66
github.com/gookit/color v1.5.2
77
github.com/spf13/cobra v1.6.1
8+
github.com/stretchr/testify v1.8.0
89
)
910

1011
require (
12+
github.com/davecgh/go-spew v1.1.1 // indirect
1113
github.com/inconshreveable/mousetrap v1.1.0 // indirect
14+
github.com/pmezard/go-difflib v1.0.0 // indirect
1215
github.com/spf13/pflag v1.0.5 // indirect
1316
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
1417
golang.org/x/sys v0.4.0 // indirect
18+
gopkg.in/yaml.v3 v3.0.1 // indirect
1519
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZ
2626
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2727
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
2828
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
29+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2930
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3031
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3132
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

test/dev.sh

Lines changed: 0 additions & 59 deletions
This file was deleted.

test/dev_setup.sh

Lines changed: 0 additions & 8 deletions
This file was deleted.

utils/constants.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package utils
2+
3+
const DEV_SCRIPT_MARKER = "DEV_SCRIPT_MARKER"
4+
const DEV_SCRIPT_NAME = "dev.sh"
5+
const MAX_DEPTH = 100
6+
const SECTION_SEPARATOR = "---------------------------------------------------------------------------------------------"
7+
const GROUP_ID_TASKS = "tasks"
8+
const GROUP_ID_UTILS = "utils"

utils/utils.go renamed to utils/dev_script.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,13 @@
11
package utils
22

33
import (
4-
"embed"
5-
"github.com/gookit/color"
64
"log"
75
"os"
86
"regexp"
97
"sort"
108
"strings"
119
)
1210

13-
const DEV_SCRIPT_MARKER = "DEV_SCRIPT_MARKER"
14-
const DEV_SCRIPT_NAME = "dev.sh"
15-
const MAX_DEPTH = 100
16-
const SECTION_SEPARATOR = "---------------------------------------------------------------------------------------------"
17-
const GROUP_ID_TASKS = "tasks"
18-
const GROUP_ID_UTILS = "utils"
19-
20-
// Assets represents the embedded files.
21-
//
22-
//go:embed templates/dev.sh templates/dev_setup.sh
23-
var Assets embed.FS
24-
25-
func FileExists(path string) bool {
26-
info, err := os.Stat(path)
27-
if os.IsNotExist(err) {
28-
return false
29-
}
30-
return !info.IsDir()
31-
}
32-
33-
func FileContains(filePath string, needle string) bool {
34-
b, err := os.ReadFile(filePath)
35-
if err != nil {
36-
log.Fatalf("Failed to execute: '%s'", err.Error())
37-
}
38-
markerIsPresent, err := regexp.Match(needle, b)
39-
if err != nil {
40-
log.Fatalf("Failed to execute: '%s'", err.Error())
41-
}
42-
return markerIsPresent
43-
}
44-
45-
func CopyAssetToPath(embedPath string, targetPath string) {
46-
data, err := Assets.ReadFile(embedPath)
47-
if err != nil {
48-
log.Fatalf("Failed to execute: '%s'", err.Error())
49-
}
50-
// Shell scripts need to be executable -> 0755
51-
err = os.WriteFile(targetPath, []byte(data), 0755)
52-
if err != nil {
53-
log.Fatalf("Failed to execute: '%s'", err.Error())
54-
}
55-
color.Magenta.Println(targetPath + " was created.")
56-
}
57-
5811
func ParseDevScriptTasks(devScriptPath string) []DevScriptTask {
5912
// https://regex101.com/r/5LVRcP/1 -> Iteration 1 without comments before
6013
// https://regex101.com/r/XyB410/1 -> Final Iteration with comments before ;)

utils/dev_script_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package utils
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestParseDevScriptTasks_TasksSyntax(t *testing.T) {
9+
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"},
17+
}
18+
actual := ParseDevScriptTasks("./fixtures/tasks_syntax.sh")
19+
assert.Equal(t, expected, actual)
20+
}
21+
22+
func TestParseDevScriptTasks_IgnoreTasksStartingWithUnderscore(t *testing.T) {
23+
expected := []DevScriptTask{
24+
{Usage: "task-not-starting-with-underscore"},
25+
{Usage: "task-not-starting-with-underscore_"},
26+
{Usage: "task-not-starting-with_underscore"},
27+
// should not be parsed
28+
// {Usage: "_task-without-underscore"},
29+
// {Usage: "__task-without-underscore"},
30+
// {Usage: "___task-without-underscore"},
31+
}
32+
actual := ParseDevScriptTasks("./fixtures/tasks_with_underscores.sh")
33+
assert.Equal(t, expected, actual)
34+
}
35+
36+
func TestParseDevScriptTasks_IgnoreCommentedOutTasks(t *testing.T) {
37+
expected := []DevScriptTask{
38+
{Usage: "task"},
39+
// should not be parsed
40+
// {Usage: "task-commented-out"},
41+
}
42+
actual := ParseDevScriptTasks("./fixtures/tasks_commented_out.sh")
43+
assert.Equal(t, expected, actual)
44+
}
45+
46+
func TestParseDevScriptTasks_TaskCommentsShouldBeParsed(t *testing.T) {
47+
expected := []DevScriptTask{
48+
{
49+
Usage: "one-line-comment-block",
50+
Short: "one line comment block - 1",
51+
// TODO: fix trailing \n
52+
Long: "one line comment block - 1\n",
53+
},
54+
{
55+
Usage: "multiline-comment-block",
56+
Short: "multiline comment block - 1",
57+
// TODO: fix trailing \n
58+
Long: "multiline comment block - 1\nmultiline comment block - 2\nmultiline comment block - 3\n",
59+
},
60+
{
61+
Usage: "empty-lines-in-comment-block",
62+
Short: "multiline comment block with empty lines - 1",
63+
// 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",
65+
},
66+
{
67+
Usage: "leading-spaces-or-none-in-comment-block",
68+
Short: "leading space missing",
69+
// TODO: fix trailing \n
70+
// remove # and one space always if present
71+
// keep \n but remove spaces if they are the only chars after #
72+
// also keep empty lines
73+
Long: "leading space missing\n keep leading-spaces used for indents\n\n\n\ncomment block end",
74+
},
75+
}
76+
actual := ParseDevScriptTasks("./fixtures/tasks_with_comments.sh")
77+
assert.Equal(t, expected, actual)
78+
}

utils/file_io.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package utils
2+
3+
import (
4+
"embed"
5+
"github.com/gookit/color"
6+
"log"
7+
"os"
8+
"regexp"
9+
)
10+
11+
// Assets represents the embedded files.
12+
//
13+
//go:embed templates/dev.sh templates/dev_setup.sh
14+
var Assets embed.FS
15+
16+
func FileExists(path string) bool {
17+
info, err := os.Stat(path)
18+
if os.IsNotExist(err) {
19+
return false
20+
}
21+
return !info.IsDir()
22+
}
23+
24+
func FileContains(filePath string, needle string) bool {
25+
b, err := os.ReadFile(filePath)
26+
if err != nil {
27+
log.Fatalf("Failed to execute: '%s'", err.Error())
28+
}
29+
markerIsPresent, err := regexp.Match(needle, b)
30+
if err != nil {
31+
log.Fatalf("Failed to execute: '%s'", err.Error())
32+
}
33+
return markerIsPresent
34+
}
35+
36+
func CopyAssetToPath(embedPath string, targetPath string) {
37+
data, err := Assets.ReadFile(embedPath)
38+
if err != nil {
39+
log.Fatalf("Failed to execute: '%s'", err.Error())
40+
}
41+
// Shell scripts need to be executable -> 0755
42+
err = os.WriteFile(targetPath, []byte(data), 0755)
43+
if err != nil {
44+
log.Fatalf("Failed to execute: '%s'", err.Error())
45+
}
46+
color.Magenta.Println(targetPath + " was created.")
47+
}

utils/fixtures/tasks_commented_out.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
############################## DEV_SCRIPT_MARKER ##############################
3+
4+
set -e
5+
6+
######### TASKS #########
7+
8+
function task() {
9+
echo "Some Code"
10+
}
11+
12+
#function task-commented-out() {
13+
# echo "Some Code"
14+
#}
15+
16+

utils/fixtures/tasks_syntax.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
############################## DEV_SCRIPT_MARKER ##############################
3+
4+
set -e
5+
6+
######### TASKS #########
7+
8+
function task-syntax-1a() {
9+
echo "Some Code"
10+
}
11+
12+
function task-syntax-1b () {
13+
echo "Some Code"
14+
}
15+
16+
function task-syntax-1c(){
17+
echo "Some Code"
18+
}
19+
20+
task-syntax-2a() {
21+
echo "Some Code"
22+
}
23+
24+
task-syntax-2b () {
25+
echo "Some Code"
26+
}
27+
28+
task-syntax-2c (){
29+
echo "Some Code"
30+
}
31+
32+
function task-syntax-3a {
33+
echo "Some Code"
34+
}
35+
36+

utils/fixtures/tasks_with_comments.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
############################## DEV_SCRIPT_MARKER ##############################
3+
4+
set -e
5+
6+
######### TASKS #########
7+
8+
# one line comment block - 1
9+
function one-line-comment-block() {
10+
echo "Some Code"
11+
}
12+
13+
# multiline comment block - 1
14+
# multiline comment block - 2
15+
# multiline comment block - 3
16+
function multiline-comment-block() {
17+
echo "Some Code"
18+
}
19+
20+
# multiline comment block with empty lines - 1
21+
#
22+
# multiline comment block with empty lines - 2
23+
#
24+
# multiline comment block with empty lines - 3
25+
function empty-lines-in-comment-block() {
26+
echo "Some Code"
27+
}
28+
29+
#leading space missing
30+
# keep leading-spaces used for indents
31+
#
32+
#
33+
#
34+
# comment block end
35+
function leading-spaces-or-none-in-comment-block() {
36+
echo "Some Code"
37+
}

0 commit comments

Comments
 (0)