Skip to content

Commit f679a5c

Browse files
authored
Merge pull request #25 from jb55/create-test
Add first test for project Creation
2 parents 2a19857 + 437df20 commit f679a5c

File tree

8 files changed

+90
-34
lines changed

8 files changed

+90
-34
lines changed

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: go
2+
script:
3+
- env GO111MODULE=on go build
4+
- env GO111MODULE=on go test ./test
5+
6+
go:
7+
- 1.11.x
8+
- 1.12.x
9+
- master

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ deps-go:
3030
build-deps:
3131
go install github.com/gobuffalo/packr/v2/packr2
3232

33+
check:
34+
go test ./test
35+
3336
fmt:
3437
go fmt ./...
3538

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Commit0 [POC]
22

3+
[![Build Status](https://travis-ci.org/commitdev/commit0.svg)](https://travis-ci.org/commitdev/commit0)
4+
35
Status: currently poc
46

57
Commit0 intends to be a multi-language service generator. The intention is to create a modular monolith, which is easy to seperate at a later stage when the boundries are completely understood.

cmd/commit0.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"github.com/commitdev/commit0/templator"
6+
"github.com/gobuffalo/packr/v2"
67
"github.com/spf13/cobra"
78
"os"
89
)
@@ -17,8 +18,13 @@ var rootCmd = &cobra.Command{
1718
},
1819
}
1920

20-
func Execute(templates *templator.Templator) {
21-
Templator = templates
21+
func Init() {
22+
templates := packr.New("templates", "../templates")
23+
Templator = templator.NewTemplator(templates)
24+
}
25+
26+
func Execute() {
27+
Init()
2228
if err := rootCmd.Execute(); err != nil {
2329
fmt.Println(err)
2430
os.Exit(1)

cmd/create.go

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package cmd
22

33
import (
4-
"fmt"
54
"log"
65
"os"
6+
"path"
77

88
"github.com/spf13/cobra"
99
)
@@ -13,6 +13,36 @@ func init() {
1313
rootCmd.AddCommand(createCmd)
1414
}
1515

16+
func Create(projectName string, outDir string) string {
17+
rootDir := path.Join(outDir, projectName)
18+
log.Printf("Creating project %s.", projectName)
19+
err := os.MkdirAll(rootDir, os.ModePerm)
20+
21+
if os.IsExist(err) {
22+
log.Fatalf("Directory %v already exists! Error: %v", projectName, err)
23+
} else if err != nil {
24+
log.Fatalf("Error creating root: %v ", err)
25+
}
26+
27+
commit0ConfigPath := path.Join(rootDir, "commit0.yml")
28+
log.Printf("%s", commit0ConfigPath)
29+
30+
f, err := os.Create(commit0ConfigPath)
31+
if err != nil {
32+
log.Printf("Error creating commit0 config: %v", err)
33+
}
34+
Templator.Commit0.Execute(f, projectName)
35+
36+
gitIgnorePath := path.Join(rootDir, ".gitignore")
37+
f, err = os.Create(gitIgnorePath)
38+
if err != nil {
39+
log.Printf("Error creating commit0 config: %v", err)
40+
}
41+
Templator.GitIgnore.Execute(f, projectName)
42+
43+
return rootDir
44+
}
45+
1646
var createCmd = &cobra.Command{
1747
Use: "create",
1848
Short: "Create new project with provided name.",
@@ -23,30 +53,6 @@ var createCmd = &cobra.Command{
2353

2454
projectName := args[0]
2555

26-
rootDir := fmt.Sprintf("./%v", projectName)
27-
28-
log.Printf("Creating project %s.", projectName)
29-
30-
err := os.Mkdir(rootDir, os.ModePerm)
31-
if os.IsExist(err) {
32-
log.Fatalf("Directory %v already exists! Error: %v", projectName, err)
33-
} else if err != nil {
34-
log.Fatalf("Error creating root: %v ", err)
35-
}
36-
37-
commit0ConfigPath := fmt.Sprintf("%v/commit0.yml", rootDir)
38-
39-
f, err := os.Create(commit0ConfigPath)
40-
if err != nil {
41-
log.Printf("Error creating commit0 config: %v", err)
42-
}
43-
Templator.Commit0.Execute(f, projectName)
44-
45-
gitIgnorePath := fmt.Sprintf("%v/.gitignore", rootDir)
46-
f, err = os.Create(gitIgnorePath)
47-
if err != nil {
48-
log.Printf("Error creating commit0 config: %v", err)
49-
}
50-
Templator.GitIgnore.Execute(f, projectName)
56+
Create(projectName, "./")
5157
},
5258
}

main.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ package main
22

33
import (
44
"github.com/commitdev/commit0/cmd"
5-
"github.com/commitdev/commit0/templator"
6-
"github.com/gobuffalo/packr/v2"
75
)
86

97
func main() {
10-
templates := packr.New("templates", "./templates")
11-
templator := templator.NewTemplator(templates)
12-
cmd.Execute(templator)
8+
cmd.Execute()
139
}

templator/templator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type GoTemplator struct {
2222
}
2323

2424
type Templator struct {
25-
Commit0 *template.Template
25+
Commit0 *template.Template
2626
GitIgnore *template.Template
2727
MakefileTemplate *template.Template
2828
ProtoHealthTemplate *template.Template

test/create_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"github.com/commitdev/commit0/cmd"
5+
"io/ioutil"
6+
"os"
7+
"path"
8+
"testing"
9+
)
10+
11+
func TestInitWorks(t *testing.T) {
12+
cmd.Init()
13+
}
14+
15+
func TestCreateWorks(t *testing.T) {
16+
tmpdir, err := ioutil.TempDir("", "commit0-")
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
21+
projectName := "test-project"
22+
23+
root := cmd.Create(projectName, tmpdir)
24+
defer os.RemoveAll(tmpdir)
25+
26+
st, err := os.Stat(path.Join(root, "commit0.yml"))
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
31+
if st.Size() == 0 {
32+
t.Fatalf("commit0.yml is empty")
33+
}
34+
}

0 commit comments

Comments
 (0)