Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 4098f12

Browse files
committed
Add utils tests
Signed-off-by: Joffrey F <[email protected]>
1 parent 64e5e67 commit 4098f12

File tree

5 files changed

+131
-9
lines changed

5 files changed

+131
-9
lines changed

packager/init.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ func initFromScratch(name string) error {
4040
log.Println("init from scratch")
4141
fmt.Println(`
4242
Please indicate a list of services that will be used by your application, one per line.
43-
Examples of possible values: java, mysql, redis, ruby, postgres, rabbitmq...
44-
`)
45-
services, err := utils.ReadNewlineSeparatedList()
43+
Examples of possible values: java, mysql, redis, ruby, postgres, rabbitmq...`)
44+
services, err := utils.ReadNewlineSeparatedList(os.Stdin)
4645
if err != nil {
4746
return err
4847
}

utils/components.go

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

33
import (
4+
"regexp"
45
"strings"
56
)
67

@@ -62,11 +63,16 @@ func MatchService(inputName string) ServiceConfig {
6263
imgName = keywords["default"]
6364
}
6465
return ServiceConfig{
65-
ServiceName: strings.Replace(inputName, " ", "_", -1),
66+
ServiceName: sanitizeServiceName(inputName),
6667
ServiceImage: imgName,
6768
}
6869
}
6970

71+
func sanitizeServiceName(inputName string) string {
72+
invalidChars, _ := regexp.Compile("[^a-z_-]")
73+
return invalidChars.ReplaceAllLiteralString(inputName, "_")
74+
}
75+
7076
// ServiceConfig is a stub containing basic information about a service component
7177
type ServiceConfig struct {
7278
ServiceName string

utils/components_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package utils
2+
3+
import (
4+
"github.com/gotestyourself/gotestyourself/assert"
5+
"testing"
6+
)
7+
8+
func TestMatchService(t *testing.T) {
9+
assert.Equal(t, MatchService("redis"), ServiceConfig{
10+
ServiceImage: "redis",
11+
ServiceName: "redis",
12+
}, "incorrect service match")
13+
}
14+
15+
func TestMatchServiceSanitize(t *testing.T) {
16+
assert.Equal(t, MatchService("ruby on rails"), ServiceConfig{
17+
ServiceImage: "rails",
18+
ServiceName: "ruby_on_rails",
19+
}, "incorrect service match")
20+
}
21+
22+
func TestMatchServiceNoMatch(t *testing.T) {
23+
assert.Equal(t, MatchService("no-such_image"), ServiceConfig{
24+
ServiceImage: "alpine",
25+
ServiceName: "no-such_image",
26+
}, "incorrect default service config")
27+
}
28+
29+
func TestMatchServiceNoMatchSanitize(t *testing.T) {
30+
assert.Equal(t, MatchService("aspq[foo[bar]|"), ServiceConfig{
31+
ServiceImage: "alpine",
32+
ServiceName: "aspq_foo_bar__",
33+
}, "incorrect default service config")
34+
}

utils/io.go

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

33
import (
44
"bufio"
5+
"io"
56
"os"
7+
"strings"
68
)
79

810
// CreateFileWithData creates a new file at the given path and writes
@@ -17,13 +19,17 @@ func CreateFileWithData(fileName string, data []byte) error {
1719
return err
1820
}
1921

20-
// ReadNewlineSeparatedList reads data from stdin until reaching EOF and
21-
// returns a slice with data from each line
22-
func ReadNewlineSeparatedList() ([]string, error) {
23-
scanner := bufio.NewScanner(os.Stdin)
22+
// ReadNewlineSeparatedList reads data from the reader interface until
23+
// reaching EOF and returns a slice with data from each line
24+
func ReadNewlineSeparatedList(rd io.Reader) ([]string, error) {
25+
scanner := bufio.NewScanner(rd)
2426
var into []string
2527
for scanner.Scan() {
26-
into = append(into, scanner.Text())
28+
token := strings.TrimSpace(scanner.Text())
29+
if token == "" {
30+
continue
31+
}
32+
into = append(into, token)
2733
}
2834
return into, scanner.Err()
2935
}

utils/io_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package utils
2+
3+
import (
4+
"github.com/gotestyourself/gotestyourself/assert"
5+
"github.com/gotestyourself/gotestyourself/fs"
6+
"testing"
7+
8+
"strings"
9+
)
10+
11+
var dummyData = []byte("hello world\n")
12+
13+
func TestCreateFileWithData(t *testing.T) {
14+
tmpdir := fs.NewDir(t, "iotest")
15+
defer tmpdir.Remove()
16+
path := tmpdir.Join("file.txt")
17+
18+
err := CreateFileWithData(path, dummyData)
19+
assert.NilError(t, err, "failed to write data to file")
20+
21+
manifest := fs.Expected(t,
22+
fs.WithFile("file.txt", string(dummyData),
23+
fs.WithMode(0644),
24+
),
25+
)
26+
comp := fs.Equal(tmpdir.Path(), manifest)()
27+
assert.Assert(t, comp.Success())
28+
}
29+
30+
func TestCreateFileWithDataOverride(t *testing.T) {
31+
tmpdir := fs.NewDir(t, "iotest")
32+
defer tmpdir.Remove()
33+
path := tmpdir.Join("file.txt")
34+
35+
err := CreateFileWithData(path, []byte("oops!"))
36+
assert.NilError(t, err, "failed to write data to file")
37+
err = CreateFileWithData(path, dummyData)
38+
assert.NilError(t, err, "failed to rewrite data to file")
39+
40+
manifest := fs.Expected(t,
41+
fs.WithFile("file.txt", string(dummyData),
42+
fs.WithMode(0644),
43+
),
44+
)
45+
comp := fs.Equal(tmpdir.Path(), manifest)()
46+
assert.Assert(t, comp.Success())
47+
}
48+
49+
func TestReadNewlineSeparatedList(t *testing.T) {
50+
reader := strings.NewReader("lorem\nipsum\r\ndolor sit\namet\n")
51+
results, err := ReadNewlineSeparatedList(reader)
52+
assert.NilError(t, err)
53+
expected := []string{
54+
"lorem", "ipsum", "dolor sit", "amet",
55+
}
56+
assert.DeepEqual(t, results, expected)
57+
}
58+
59+
func TestReadNewlineSeparatedListWithEmptyLines(t *testing.T) {
60+
reader := strings.NewReader("\t\t\nlorem\n\nipsum\n \ndolor sit\r\n\n\namet\n ")
61+
results, err := ReadNewlineSeparatedList(reader)
62+
assert.NilError(t, err)
63+
expected := []string{
64+
"lorem", "ipsum", "dolor sit", "amet",
65+
}
66+
assert.DeepEqual(t, results, expected)
67+
}
68+
69+
func TestReadNewlineSeparatedListSanitize(t *testing.T) {
70+
reader := strings.NewReader("\t\tlorem\nipsum\n\t\t\ndolor sit \n \t amet \t\r\r\n")
71+
results, err := ReadNewlineSeparatedList(reader)
72+
assert.NilError(t, err)
73+
expected := []string{
74+
"lorem", "ipsum", "dolor sit", "amet",
75+
}
76+
assert.DeepEqual(t, results, expected)
77+
}

0 commit comments

Comments
 (0)