github.com/mlmmr/revel-cmd@v0.21.2-0.20191112133115-68d8795776dd/revel/new_test.go (about)

     1  package main_test
     2  
     3  import (
     4  	"github.com/mlmmr/revel-cmd/model"
     5  	"github.com/mlmmr/revel-cmd/revel"
     6  	"github.com/mlmmr/revel-cmd/utils"
     7  	"github.com/stretchr/testify/assert"
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  )
    13  
    14  // test the commands
    15  func TestNew(t *testing.T) {
    16  	a := assert.New(t)
    17  	gopath := setup("revel-test-new",  a)
    18  
    19  		t.Run("New", func(t *testing.T) {
    20  			a := assert.New(t)
    21  			c := newApp("new-test", model.NEW, nil, a)
    22  			a.Nil(main.Commands[model.NEW].RunWith(c), "New failed")
    23  		})
    24  		t.Run("Path", func(t *testing.T) {
    25  			a := assert.New(t)
    26  			c := newApp("new/test/a", model.NEW, nil, a)
    27  			a.Nil(main.Commands[model.NEW].RunWith(c), "New path failed")
    28  		})
    29  		t.Run("Path-Duplicate", func(t *testing.T) {
    30  			a := assert.New(t)
    31  			c := newApp("new/test/b", model.NEW, nil, a)
    32  			a.Nil(main.Commands[model.NEW].RunWith(c), "New path failed")
    33  			c = newApp("new/test/b", model.NEW, nil, a)
    34  			a.NotNil(main.Commands[model.NEW].RunWith(c), "Duplicate path Did Not failed")
    35  		})
    36  		t.Run("Skeleton-Git", func(t *testing.T) {
    37  			a := assert.New(t)
    38  			c := newApp("new/test/c/1", model.NEW, nil, a)
    39  			c.New.SkeletonPath = "git://github.com/revel/skeletons:basicnsadnsak"
    40  			a.NotNil(main.Commands[model.NEW].RunWith(c), "Expected Failed to run with new")
    41  			// We need to pick a different path
    42  			c = newApp("new/test/c/2", model.NEW, nil, a)
    43  			c.New.SkeletonPath = "git://github.com/revel/skeletons:basic/bootstrap4"
    44  			a.Nil(main.Commands[model.NEW].RunWith(c), "Failed to run with new skeleton git")
    45  		})
    46  	if !t.Failed() {
    47  		if err := os.RemoveAll(gopath); err != nil {
    48  			a.Fail("Failed to remove test path")
    49  		}
    50  	}
    51  }
    52  
    53  // test the commands
    54  func TestNewVendor(t *testing.T) {
    55  	a := assert.New(t)
    56  	gopath := setup("revel-test-new-vendor",  a)
    57  	precall := func(c *model.CommandConfig) {
    58  		c.New.Vendored = true
    59  	}
    60  	t.Run("New", func(t *testing.T) {
    61  		a := assert.New(t)
    62  		c := newApp("onlyone/v/a", model.NEW, precall, a)
    63  		c.New.Vendored = true
    64  		a.Nil(main.Commands[model.NEW].RunWith(c), "New failed")
    65  	})
    66  	t.Run("Test", func(t *testing.T) {
    67  		a := assert.New(t)
    68  		c := newApp("onlyone/v/a", model.TEST, nil, a)
    69  		a.Nil(main.Commands[model.TEST].RunWith(c), "Test failed")
    70  	})
    71  	t.Run("Build", func(t *testing.T) {
    72  		a := assert.New(t)
    73  		c := newApp("onlyone/v/a", model.BUILD, nil, a)
    74  		c.Index = model.BUILD
    75  		c.Build.TargetPath = filepath.Join(gopath, "src/onlyone/v/a", "target")
    76  		a.Nil(main.Commands[model.BUILD].RunWith(c), " Build failed")
    77  		a.True(utils.DirExists(c.Build.TargetPath), "Target folder not made", c.Build.TargetPath)
    78  	})
    79  	t.Run("Package", func(t *testing.T) {
    80  		a := assert.New(t)
    81  		c := newApp("onlyone/v/a", model.PACKAGE, nil, a)
    82  		c.Package.TargetPath = filepath.Join(gopath, "src/onlyone/v/a", "target.tar.gz")
    83  		a.Nil(main.Commands[model.PACKAGE].RunWith(c), "Package Failed")
    84  		a.True(utils.Exists(c.Package.TargetPath), "Target package not made", c.Package.TargetPath)
    85  	})
    86  	t.Run("TestVendorDir", func(t *testing.T) {
    87  		// Check to see that no additional packages were downloaded outside the vendor folder
    88  		files, err := ioutil.ReadDir(gopath)
    89  		a.Nil(err, "Failed to read gopath folder")
    90  		// bin/     onlyone/ pkg/     src/
    91  		a.Equal(3, len(files), "Expected single file in "+gopath)
    92  		files, err = ioutil.ReadDir(filepath.Join(gopath, "src"))
    93  		a.Nil(err, "Failed to read src folder")
    94  		a.Equal(1, len(files), "Expected single file in source folder", filepath.Join(gopath, "src"))
    95  	})
    96  	if !t.Failed() {
    97  		if err := os.RemoveAll(gopath); err != nil {
    98  			a.Fail("Failed to remove test path")
    99  		}
   100  	}
   101  }