Skip to content

Commit

Permalink
Fix tests (69.2%)
Browse files Browse the repository at this point in the history
  • Loading branch information
koddr committed Jul 6, 2020
1 parent a9b6da0 commit 813a0ab
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Expand Up @@ -11,7 +11,7 @@ tasks:
test:
desc: Test project
cmds:
- go test -timeout 600s -cover ./...
- go test -timeout 60s -cover ./...

security:
desc: Run gosec for project
Expand Down
2 changes: 1 addition & 1 deletion pkg/cgapp/actions.go
Expand Up @@ -106,7 +106,7 @@ func CreateCLIAction(c *cli.Context) error {
stopTimer := time.Since(startTimer).String()

// END message
SendMsg(true, "FINISH", "Completed in "+stopTimer+".", "green", false)
SendMsg(true, "FINISH", "Completed in "+stopTimer+"!", "green", false)
SendMsg(true, "DOCS", "A helpful documentation here → https://create-go.app", "yellow", false)
SendMsg(false, "!", "Go to the `"+appPath+"` folder and make something beautiful! :)", "yellow", true)

Expand Down
38 changes: 30 additions & 8 deletions pkg/cgapp/create.go
Expand Up @@ -20,7 +20,7 @@ func CreateProjectFromRegistry(project *Project, registry map[string]*Registry)

// Checking for nil
if project == nil || registry == nil {
return ThrowError("Registry not found!")
return ThrowError("Project template or registry not found!")
}

// Create path in project root folder
Expand Down Expand Up @@ -71,6 +71,12 @@ func CreateProjectFromRegistry(project *Project, registry map[string]*Registry)
SendMsg(false, "OK", strings.Title(project.Type)+": created with user template `"+project.Name+"`!", "", false)
}

// Cleanup project
foldersToRemove := []string{".git", ".github"}
if err := RemoveFolders(folder, foldersToRemove); err != nil {
return ThrowError(err.Error())
}

return nil
}

Expand All @@ -79,15 +85,18 @@ func CreateProjectFromCMD(p *Project, cmd map[string]*Command) error {
// Define vars
var options []string

// Checking for nil
if p == nil || cmd == nil {
return ThrowError("Project template or commands not found!")
}

// Create path in project root folder
folder := filepath.Join(p.RootFolder, p.Type)

// Split framework name and template
project := StringSplit(":", p.Name)

// Error, when empty
if len(project) == 0 {
return ThrowError("Frontend template not set!")
project, err := StringSplit(":", p.Name)
if err != nil {
return ThrowError(err.Error())
}

// Re-define vars for more beauty view
Expand All @@ -106,19 +115,32 @@ func CreateProjectFromCMD(p *Project, cmd map[string]*Command) error {
break
case "preact":
// preact create [template] [dest] [args...]
options = []string{create, folder}
options = []string{create, "default", p.Type, args["cwd"], p.RootFolder, args["name"], "cgapp"}
if len(project) > 1 {
options = []string{create, project[1], p.Type, args["cwd"], p.RootFolder, args["name"], "cgapp"}
}
break
case "svelte":
// npx degit [template] [dest]
options = []string{create, args["template"], folder}
break
default:
return ThrowError("Frontend template" + p.Name + " not found!")
}

//
// Run execution command
if err := ExecCommand(runner, options); err != nil {
return ThrowError(err.Error())
}

// Cleanup project
folderToRemove := []string{".git"}
if err := RemoveFolders(folder, folderToRemove); err != nil {
return ThrowError(err.Error())
}

// Show success report
SendMsg(false, "OK", "Frontend: created with template `"+p.Name+"`!", "", false)

return nil
}
74 changes: 74 additions & 0 deletions pkg/cgapp/create_test.go
Expand Up @@ -75,6 +75,18 @@ func TestCreateProjectFromRegistry(t *testing.T) {
},
false,
},
{
"failed wrong template name",
args{
project: &Project{
Type: "backend",
Name: "...wrong...",
RootFolder: "../../tmp",
},
registry: registry,
},
true,
},
{
"failed create (empty Project struct)",
args{
Expand Down Expand Up @@ -114,3 +126,65 @@ func TestCreateProjectFromRegistry(t *testing.T) {
}
}
}

func TestCreateProjectFromCMD(t *testing.T) {
type args struct {
p *Project
cmd map[string]*Command
}
tests := []struct {
name string
args args
wantErr bool
}{
{
"successfully create svelte",
args{
p: &Project{
Type: "frontend",
Name: "svelte",
RootFolder: "../../tmp",
},
cmd: cmds,
},
false,
},
{
"failed create (empty Project struct)",
args{
p: nil,
cmd: cmds,
},
true,
},
{
"failed create (empty Command struct)",
args{
p: &Project{
Type: "frontend",
Name: "svelte",
RootFolder: "../../tmp",
},
cmd: nil,
},
true,
},
{
"failed create (empty Project and Command struct)",
args{},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateProjectFromCMD(tt.args.p, tt.args.cmd); (err != nil) != tt.wantErr {
t.Errorf("CreateProjectFromCMD() error = %v, wantErr %v", err, tt.wantErr)
}
})

// Clean
if tt.args.p != nil {
os.RemoveAll(tt.args.p.RootFolder)
}
}
}
13 changes: 9 additions & 4 deletions pkg/cgapp/defaults.go
Expand Up @@ -67,10 +67,15 @@ var (
Runner: "preact",
Create: "create",
Args: map[string]string{
"cwd": "--cwd",
"name": "--name",
"skip-git": "--git",
"skip-install": "--install",
"cwd": "--cwd",
"name": "--name",
},
},
"svelte": {
Runner: "npx",
Create: "degit",
Args: map[string]string{
"template": "sveltejs/template",
},
},
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/cgapp/utils.go
Expand Up @@ -112,7 +112,12 @@ func ExecCommand(command string, options []string) error {
}

// StringSplit ...
func StringSplit(pattern, match string) []string {
func StringSplit(pattern, match string) ([]string, error) {
// Error, when empty or nil
if pattern == "" || match == "" {
return nil, ThrowError("Frontend template not set!")
}

// Define empty []string{} for splitted strings
splittedStrings := []string{}

Expand All @@ -126,7 +131,7 @@ func StringSplit(pattern, match string) []string {
splittedStrings = append(splittedStrings, split[str])
}

return splittedStrings
return splittedStrings, nil
}

// MakeFiles function for massively create folders
Expand Down Expand Up @@ -185,11 +190,5 @@ func GitClone(rootFolder, templateName string) error {
return ThrowError("Repository was not cloned!")
}

// Cleanup project
foldersToRemove := []string{".git", ".github"}
if err := RemoveFolders(rootFolder, foldersToRemove); err != nil {
return ThrowError(err.Error())
}

return nil
}
24 changes: 19 additions & 5 deletions pkg/cgapp/utils_test.go
Expand Up @@ -296,9 +296,10 @@ func TestStringSplit(t *testing.T) {
match string
}
tests := []struct {
name string
args args
want []string
name string
args args
want []string
wantErr bool
}{
{
"successfully matched",
Expand All @@ -307,19 +308,32 @@ func TestStringSplit(t *testing.T) {
match: "react:redux",
},
[]string{"react", "redux"},
false,
},
{
"failed match",
"successfully not matched",
args{
pattern: "=",
match: "react:redux",
},
[]string{"react:redux"},
false,
},
{
"failed wrong pattern and match",
args{},
nil,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := StringSplit(tt.args.pattern, tt.args.match); !reflect.DeepEqual(got, tt.want) {
got, err := StringSplit(tt.args.pattern, tt.args.match)
if (err != nil) != tt.wantErr {
t.Errorf("StringSplit() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("StringSplit() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 813a0ab

Please sign in to comment.