github.com/awslabs/clencli@v0.0.0-20210514234156-7ecf17182a20/tests/cmd_init_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/awslabs/clencli/cobra/controller"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestInitCmd(t *testing.T) {
    12  	tests := map[string]struct {
    13  		args []string
    14  		out  string
    15  		err  string
    16  	}{
    17  		// argument
    18  		"empty":     {args: []string{"init"}, out: "", err: "this command requires one argument"},
    19  		"empty arg": {args: []string{"init", ""}, out: "", err: "invalid argument"},
    20  		"wrong arg": {args: []string{"init", "foo"}, out: "", err: "invalid argument"},
    21  
    22  		// flags
    23  		"wrong flag": {args: []string{"init", "project", "--foo"}, out: "", err: "unknown flag: --foo"},
    24  
    25  		// # projects
    26  		"no project name": {args: []string{"init", "project"}, out: "", err: "--project-name must be defined"},
    27  
    28  		// ## --project-name
    29  		"emtpy project name": {args: []string{"init", "project", "--project-name"}, out: "", err: "flag needs an argument"},
    30  
    31  		// ## --project-type
    32  		"empty project type":   {args: []string{"init", "project", "--project-type"}, out: "", err: "flag needs an argument"},
    33  		"invalid project type": {args: []string{"init", "project", "--project-type", "nil"}, out: "", err: "--project-name must be defined"},
    34  
    35  		// ## --project-name && --project-type
    36  		"with project name and empty project type":   {args: []string{"init", "project", "--project-name", "foo", "--project-type"}, out: "", err: "flag needs an argument"},
    37  		"with project name and invalid project type": {args: []string{"init", "project", "--project-name", "foo", "--project-type", "bar"}, out: "", err: "unknow project type"},
    38  	}
    39  
    40  	for name, tc := range tests {
    41  		t.Run(name, func(t *testing.T) {
    42  			out, err := executeCommand(t, controller.InitCmd(), tc.args)
    43  			assert.Contains(t, out, tc.out)
    44  			assert.Contains(t, err.Error(), tc.err)
    45  		})
    46  	}
    47  }
    48  
    49  func assertBasicProject(t *testing.T, err error) (string, string) {
    50  	sep := string(os.PathSeparator)
    51  	dir := t.Name() + sep + "foo"
    52  
    53  	assert.Nil(t, err)
    54  	assert.DirExists(t, dir)
    55  	assert.FileExists(t, dir+sep+".gitignore")
    56  	assert.DirExists(t, dir+sep+"clencli")
    57  
    58  	assert.FileExists(t, dir+sep+"clencli"+sep+"readme.tmpl")
    59  	assert.FileExists(t, dir+sep+"clencli"+sep+"readme.yaml")
    60  
    61  	return dir, sep
    62  }
    63  
    64  func assertCloudProject(t *testing.T, err error) (string, string) {
    65  	dir, sep := assertBasicProject(t, err)
    66  	assert.FileExists(t, dir+sep+"clencli"+sep+"hld.tmpl")
    67  	assert.FileExists(t, dir+sep+"clencli"+sep+"hld.yaml")
    68  	return dir, sep
    69  }
    70  
    71  /* PROJECT: BASIC */
    72  
    73  func TestInitBasicProjectWithNameOnly(t *testing.T) {
    74  	args := []string{"init", "project", "--project-name", "foo"}
    75  	out, err := executeCommand(t, controller.InitCmd(), args)
    76  	assert.Nil(t, err)
    77  	assert.Contains(t, out, "was successfully initialized as a basic project")
    78  	assertBasicProject(t, err)
    79  }
    80  
    81  func TestInitBasicProjectWithNameAndType(t *testing.T) {
    82  	args := []string{"init", "project", "--project-name", "foo", "--project-type", "basic"}
    83  	out, err := executeCommand(t, controller.InitCmd(), args)
    84  	assert.Nil(t, err)
    85  	assert.Contains(t, out, "was successfully initialized as a basic project")
    86  	assertBasicProject(t, err)
    87  }
    88  
    89  /* PROJECT: CLOUD */
    90  
    91  func TestInitCloudProjectWithName(t *testing.T) {
    92  	args := []string{"init", "project", "--project-name", "foo", "--project-type", "cloud"}
    93  	out, err := executeCommand(t, controller.InitCmd(), args)
    94  	assert.Contains(t, out, "was successfully initialized as a cloud project")
    95  	assertCloudProject(t, err)
    96  }
    97  
    98  /* PROJECT: CLOUDFORMATION */
    99  
   100  func TestInitProjectWithNameAndCloudFormationType(t *testing.T) {
   101  	args := []string{"init", "project", "--project-name", "foo", "--project-type", "cloudformation"}
   102  	out, err := executeCommand(t, controller.InitCmd(), args)
   103  	assert.Contains(t, out, "was successfully initialized as a cloudformation project")
   104  
   105  	dir, sep := assertCloudProject(t, err)
   106  	assert.DirExists(t, dir+sep+"environments"+sep+"dev")
   107  	assert.DirExists(t, dir+sep+"environments"+sep+"prod")
   108  
   109  	assert.FileExists(t, dir+sep+"skeleton.yaml")
   110  	assert.FileExists(t, dir+sep+"skeleton.json")
   111  }
   112  
   113  /* PROJECT: TERRAFORM */
   114  
   115  func TestInitProjectWithNameAndTerraformType(t *testing.T) {
   116  	args := []string{"init", "project", "--project-name", "foo", "--project-type", "terraform"}
   117  	out, err := executeCommand(t, controller.InitCmd(), args)
   118  	assert.Contains(t, out, "was successfully initialized as a terraform project")
   119  
   120  	dir, sep := assertCloudProject(t, err)
   121  
   122  	assert.FileExists(t, dir+sep+"Makefile")
   123  	assert.FileExists(t, dir+sep+"LICENSE")
   124  
   125  	assert.DirExists(t, dir+sep+"environments")
   126  	assert.FileExists(t, dir+sep+"environments"+sep+"dev.tf")
   127  	assert.FileExists(t, dir+sep+"environments"+sep+"prod.tf")
   128  
   129  	assert.FileExists(t, dir+sep+"main.tf")
   130  	assert.FileExists(t, dir+sep+"variables.tf")
   131  	assert.FileExists(t, dir+sep+"outputs.tf")
   132  }