github.com/thetechnoweenie/graven@v1.0.2/commands/commands_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path"
     7  	"testing"
     8  	"path/filepath"
     9  
    10  	"github.com/cbegin/graven/domain"
    11  	"github.com/cbegin/graven/test_fixtures/hello/version"
    12  	"github.com/cbegin/graven/util"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/urfave/cli"
    15  )
    16  
    17  func init() {
    18  	domain.FindProject = func() (*domain.Project, error) {
    19  		relativePath := "../test_fixtures/hello/project.yaml"
    20  		absolutePath, _ := filepath.Abs(relativePath)
    21  		return domain.LoadProject(absolutePath)
    22  	}
    23  	c := &cli.Context{}
    24  	_ = unfreeze(c)
    25  }
    26  
    27  func TestShouldBuildTargetDirectory(t *testing.T) {
    28  	c := &cli.Context{}
    29  
    30  	err := clean(c)
    31  	assert.NoError(t, err)
    32  
    33  	err = build(c)
    34  	assert.NoError(t, err)
    35  
    36  	assert.True(t, util.PathExists("../test_fixtures/hello/target/darwin/1.txt"))
    37  	assert.True(t, util.PathExists("../test_fixtures/hello/target/darwin/3.txt"))
    38  	assert.True(t, util.PathExists("../test_fixtures/hello/target/darwin/hello"))
    39  	assert.True(t, util.PathExists("../test_fixtures/hello/target/darwin/Readme.md"))
    40  
    41  	assert.True(t, util.PathExists("../test_fixtures/hello/target/linux/1.txt"))
    42  	assert.True(t, util.PathExists("../test_fixtures/hello/target/linux/3.txt"))
    43  	assert.True(t, util.PathExists("../test_fixtures/hello/target/linux/hello"))
    44  	assert.True(t, util.PathExists("../test_fixtures/hello/target/linux/Readme.md"))
    45  
    46  	assert.True(t, util.PathExists("../test_fixtures/hello/target/win/2.txt"))
    47  	assert.True(t, util.PathExists("../test_fixtures/hello/target/win/3.txt"))
    48  	assert.True(t, util.PathExists("../test_fixtures/hello/target/win/hello.exe"))
    49  	assert.True(t, util.PathExists("../test_fixtures/hello/target/win/Readme.md"))
    50  }
    51  
    52  func TestShouldCleanTargetDirectory(t *testing.T) {
    53  	c := &cli.Context{}
    54  
    55  	err := build(c)
    56  	assert.NoError(t, err)
    57  
    58  	err = clean(c)
    59  	assert.NoError(t, err)
    60  
    61  	assert.False(t, util.PathExists("../test_fixtures/hello/target/darwin/hello"))
    62  }
    63  
    64  func TestShouldPackageTargetDirectory(t *testing.T) {
    65  	c := &cli.Context{}
    66  
    67  	err := clean(c)
    68  	assert.NoError(t, err)
    69  
    70  	err = pkg(c)
    71  	assert.NoError(t, err)
    72  
    73  	darwinPath := fmt.Sprintf("../test_fixtures/hello/target/hello-%s-darwin.tgz", version.Version)
    74  	linuxPath := fmt.Sprintf("../test_fixtures/hello/target/hello-%s-linux.tar.gz", version.Version)
    75  	winPath := fmt.Sprintf("../test_fixtures/hello/target/hello-%s-win.zip", version.Version)
    76  
    77  	assert.True(t, util.PathExists(darwinPath))
    78  	assert.True(t, util.PathExists(linuxPath))
    79  	assert.True(t, util.PathExists(winPath))
    80  }
    81  
    82  func TestShouldInitDirectory(t *testing.T) {
    83  	tempdir := "../temp"
    84  	_ = os.RemoveAll(tempdir)
    85  	err := os.MkdirAll(tempdir, 0755)
    86  	assert.NoError(t, err)
    87  	defer os.RemoveAll(tempdir)
    88  
    89  	c := &cli.Context{}
    90  
    91  	wd, err := os.Getwd()
    92  	assert.NoError(t, err)
    93  	defer os.Chdir(wd) // making double sure we change the working directory back
    94  
    95  	err = os.Chdir(tempdir)
    96  	assert.NoError(t, err)
    97  
    98  	err = initialize(c)
    99  	assert.NoError(t, err)
   100  
   101  	err = os.Chdir(wd)
   102  	assert.NoError(t, err)
   103  
   104  	assert.True(t, util.PathExists(path.Join(tempdir, "version", "version.go")))
   105  	assert.True(t, util.PathExists(path.Join(tempdir, "main.go")))
   106  	assert.True(t, util.PathExists(path.Join(tempdir, "project.yaml")))
   107  }
   108  
   109  func TestShouldFreezeResources(t *testing.T) {
   110  	c := &cli.Context{}
   111  
   112  	_ = os.RemoveAll("../test_fixtures/hello/.freezer")
   113  
   114  	err := freeze(c)
   115  	assert.NoError(t, err)
   116  
   117  	assert.True(t, util.PathExists("../test_fixtures/hello/.freezer/github-com-davecgh-go-spew-spew-346938d642f2ec3594ed81d874461961cd0faa76.zip"))
   118  	assert.True(t, util.PathExists("../test_fixtures/hello/.freezer/github-com-fatih-color-62e9147c64a1ed519147b62a56a14e83e2be02c1.zip"))
   119  	assert.True(t, util.PathExists("../test_fixtures/hello/.freezer/github-com-mattn-go-colorable-941b50ebc6efddf4c41c8e4537a5f68a4e686b24.zip"))
   120  	assert.True(t, util.PathExists("../test_fixtures/hello/.freezer/github-com-mattn-go-isatty-fc9e8d8ef48496124e79ae0df75490096eccf6fe.zip"))
   121  	assert.True(t, util.PathExists("../test_fixtures/hello/.freezer/github-com-pmezard-go-difflib-difflib-792786c7400a136282c1664665ae0a8db921c6c2.zip"))
   122  	assert.True(t, util.PathExists("../test_fixtures/hello/.freezer/github-com-stretchr-testify-assert-f6abca593680b2315d2075e0f5e2a9751e3f431a.zip"))
   123  
   124  }
   125  
   126  func TestShouldUnfreezeResources(t *testing.T) {
   127  	c := &cli.Context{}
   128  
   129  	_ = os.RemoveAll("../test_fixtures/hello/vendor/github.com")
   130  	_ = os.RemoveAll("../test_fixtures/hello/vendor/golang.org")
   131  
   132  	err := unfreeze(c)
   133  	assert.NoError(t, err)
   134  
   135  	assert.True(t, util.PathExists("../test_fixtures/hello/vendor/github.com/fatih"))
   136  	assert.True(t, util.PathExists("../test_fixtures/hello/vendor/github.com/mattn"))
   137  	assert.True(t, util.PathExists("../test_fixtures/hello/vendor/golang.org/x"))
   138  
   139  }
   140  
   141  func TestShouldRunTests(t *testing.T) {
   142  	c := &cli.Context{}
   143  
   144  	err := tester(c)
   145  	assert.NoError(t, err)
   146  }
   147  
   148  func TestShouldBumpPatchVersion(t *testing.T) {
   149  	// clean up before and after
   150  	resetVersion()
   151  	defer resetVersion()
   152  
   153  	app := cli.NewApp()
   154  	app.Commands = []cli.Command{
   155  		BumpCommand,
   156  	}
   157  	err := app.Run([]string{"graven", "bump", "patch"})
   158  	assert.NoError(t, err)
   159  
   160  	p, err := domain.LoadProject("../test_fixtures/hello/project.yaml")
   161  	assert.NoError(t, err)
   162  	assert.Equal(t, "0.0.2", p.Version)
   163  }
   164  
   165  func TestShouldBumpMinorVersion(t *testing.T) {
   166  	// clean up before and after
   167  	resetVersion()
   168  	defer resetVersion()
   169  
   170  	app := cli.NewApp()
   171  	app.Commands = []cli.Command{
   172  		BumpCommand,
   173  	}
   174  	err := app.Run([]string{"graven", "bump", "minor"})
   175  	assert.NoError(t, err)
   176  
   177  	p, err := domain.LoadProject("../test_fixtures/hello/project.yaml")
   178  	assert.NoError(t, err)
   179  	assert.Equal(t, "0.1.0", p.Version)
   180  }
   181  
   182  func TestShouldBumpMajorVersion(t *testing.T) {
   183  	// clean up before and after
   184  	resetVersion()
   185  	defer resetVersion()
   186  
   187  	app := cli.NewApp()
   188  	app.Commands = []cli.Command{
   189  		BumpCommand,
   190  	}
   191  	err := app.Run([]string{"graven", "bump", "major"})
   192  	assert.NoError(t, err)
   193  
   194  	p, err := domain.LoadProject("../test_fixtures/hello/project.yaml")
   195  	assert.NoError(t, err)
   196  	assert.Equal(t, "1.0.0", p.Version)
   197  }
   198  
   199  func TestShouldSetVersionQualifier(t *testing.T) {
   200  	// clean up before and after
   201  	resetVersion()
   202  	defer resetVersion()
   203  
   204  	app := cli.NewApp()
   205  	app.Commands = []cli.Command{
   206  		BumpCommand,
   207  	}
   208  	err := app.Run([]string{"graven", "bump", "DEV"})
   209  	assert.NoError(t, err)
   210  
   211  	p, err := domain.LoadProject("../test_fixtures/hello/project.yaml")
   212  	assert.NoError(t, err)
   213  	assert.Equal(t, "0.0.1-DEV", p.Version)
   214  }
   215  
   216  func resetVersion() {
   217  	util.CopyFile("../test_fixtures/hello/project.fixture", "../test_fixtures/hello/project.yaml")
   218  	util.CopyFile("../test_fixtures/hello/version/version.fixture", "../test_fixtures/hello/version/version.go")
   219  }
   220