Skip to content

Commit

Permalink
Merge pull request #25 from jb55/create-test
Browse files Browse the repository at this point in the history
Add first test for project Creation
  • Loading branch information
jb55 committed Oct 11, 2019
2 parents 2a19857 + 437df20 commit f679a5c
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 34 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
@@ -0,0 +1,9 @@
language: go
script:
- env GO111MODULE=on go build
- env GO111MODULE=on go test ./test

go:
- 1.11.x
- 1.12.x
- master
3 changes: 3 additions & 0 deletions Makefile
Expand Up @@ -30,6 +30,9 @@ deps-go:
build-deps:
go install github.com/gobuffalo/packr/v2/packr2

check:
go test ./test

fmt:
go fmt ./...

Expand Down
2 changes: 2 additions & 0 deletions README.md
@@ -1,5 +1,7 @@
# Commit0 [POC]

[![Build Status](https://travis-ci.org/commitdev/commit0.svg)](https://travis-ci.org/commitdev/commit0)

Status: currently poc

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.
Expand Down
10 changes: 8 additions & 2 deletions cmd/commit0.go
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"github.com/commitdev/commit0/templator"
"github.com/gobuffalo/packr/v2"
"github.com/spf13/cobra"
"os"
)
Expand All @@ -17,8 +18,13 @@ var rootCmd = &cobra.Command{
},
}

func Execute(templates *templator.Templator) {
Templator = templates
func Init() {
templates := packr.New("templates", "../templates")
Templator = templator.NewTemplator(templates)
}

func Execute() {
Init()
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
58 changes: 32 additions & 26 deletions cmd/create.go
@@ -1,9 +1,9 @@
package cmd

import (
"fmt"
"log"
"os"
"path"

"github.com/spf13/cobra"
)
Expand All @@ -13,6 +13,36 @@ func init() {
rootCmd.AddCommand(createCmd)
}

func Create(projectName string, outDir string) string {
rootDir := path.Join(outDir, projectName)
log.Printf("Creating project %s.", projectName)
err := os.MkdirAll(rootDir, os.ModePerm)

if os.IsExist(err) {
log.Fatalf("Directory %v already exists! Error: %v", projectName, err)
} else if err != nil {
log.Fatalf("Error creating root: %v ", err)
}

commit0ConfigPath := path.Join(rootDir, "commit0.yml")
log.Printf("%s", commit0ConfigPath)

f, err := os.Create(commit0ConfigPath)
if err != nil {
log.Printf("Error creating commit0 config: %v", err)
}
Templator.Commit0.Execute(f, projectName)

gitIgnorePath := path.Join(rootDir, ".gitignore")
f, err = os.Create(gitIgnorePath)
if err != nil {
log.Printf("Error creating commit0 config: %v", err)
}
Templator.GitIgnore.Execute(f, projectName)

return rootDir
}

var createCmd = &cobra.Command{
Use: "create",
Short: "Create new project with provided name.",
Expand All @@ -23,30 +53,6 @@ var createCmd = &cobra.Command{

projectName := args[0]

rootDir := fmt.Sprintf("./%v", projectName)

log.Printf("Creating project %s.", projectName)

err := os.Mkdir(rootDir, os.ModePerm)
if os.IsExist(err) {
log.Fatalf("Directory %v already exists! Error: %v", projectName, err)
} else if err != nil {
log.Fatalf("Error creating root: %v ", err)
}

commit0ConfigPath := fmt.Sprintf("%v/commit0.yml", rootDir)

f, err := os.Create(commit0ConfigPath)
if err != nil {
log.Printf("Error creating commit0 config: %v", err)
}
Templator.Commit0.Execute(f, projectName)

gitIgnorePath := fmt.Sprintf("%v/.gitignore", rootDir)
f, err = os.Create(gitIgnorePath)
if err != nil {
log.Printf("Error creating commit0 config: %v", err)
}
Templator.GitIgnore.Execute(f, projectName)
Create(projectName, "./")
},
}
6 changes: 1 addition & 5 deletions main.go
Expand Up @@ -2,12 +2,8 @@ package main

import (
"github.com/commitdev/commit0/cmd"
"github.com/commitdev/commit0/templator"
"github.com/gobuffalo/packr/v2"
)

func main() {
templates := packr.New("templates", "./templates")
templator := templator.NewTemplator(templates)
cmd.Execute(templator)
cmd.Execute()
}
2 changes: 1 addition & 1 deletion templator/templator.go
Expand Up @@ -22,7 +22,7 @@ type GoTemplator struct {
}

type Templator struct {
Commit0 *template.Template
Commit0 *template.Template
GitIgnore *template.Template
MakefileTemplate *template.Template
ProtoHealthTemplate *template.Template
Expand Down
34 changes: 34 additions & 0 deletions test/create_test.go
@@ -0,0 +1,34 @@
package main

import (
"github.com/commitdev/commit0/cmd"
"io/ioutil"
"os"
"path"
"testing"
)

func TestInitWorks(t *testing.T) {
cmd.Init()
}

func TestCreateWorks(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "commit0-")
if err != nil {
t.Fatal(err)
}

projectName := "test-project"

root := cmd.Create(projectName, tmpdir)
defer os.RemoveAll(tmpdir)

st, err := os.Stat(path.Join(root, "commit0.yml"))
if err != nil {
t.Fatal(err)
}

if st.Size() == 0 {
t.Fatalf("commit0.yml is empty")
}
}

0 comments on commit f679a5c

Please sign in to comment.