github.com/inflatablewoman/deis@v1.0.1-0.20141111034523-a4511c46a6ce/contrib/bumpver/bump_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"os"
     9  	"path"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestHelp(t *testing.T) {
    15  	allArgs := [][]string{{"-h"}, {"--help"}, {"help"}}
    16  	for _, args := range allArgs {
    17  		rc, out := commandOutput(args)
    18  		if rc != 0 {
    19  			t.Errorf("Return code was %d, expected 0", rc)
    20  		}
    21  		if !strings.Contains(out, "Updates the current semantic version") {
    22  			t.Error(out)
    23  		}
    24  		if !strings.Contains(out, "Usage:") {
    25  			t.Error(out)
    26  		}
    27  	}
    28  }
    29  
    30  func TestUsage(t *testing.T) {
    31  	rc, out := commandOutput(nil)
    32  	if rc != 1 {
    33  		t.Errorf("Return code was %d, expected 1", rc)
    34  	}
    35  	if !strings.Contains(out, "Usage:") {
    36  		t.Error(out)
    37  	}
    38  }
    39  
    40  func TestVersion(t *testing.T) {
    41  	args := []string{"--version"}
    42  	rc, out := commandOutput(args)
    43  	if rc != 0 {
    44  		t.Errorf("Return code was %d, expected 0", rc)
    45  	}
    46  	if out != "bumpversion 0.1.0\n" {
    47  		t.Error(out)
    48  	}
    49  }
    50  
    51  func TestBadFilename(t *testing.T) {
    52  	rc, out := commandOutput([]string{"0.0.1", "**"})
    53  	if rc != 1 {
    54  		t.Errorf("Return code was %d, expected 1\n%v", rc, out)
    55  	}
    56  	rc, out = commandOutput([]string{"0.0.1", "beyzjVQLUaRrjpStWqTN.nonexistent"})
    57  	if rc != 1 {
    58  		t.Errorf("Return code was %d, expected 1\n%v", rc, out)
    59  	}
    60  }
    61  
    62  func TestBadSemver(t *testing.T) {
    63  	testBeforeAfter(t, "", "1.5", goFile, goFileAfter, ".go", 1)
    64  	testBeforeAfter(t, "", "0.13.0-dev", goFile, goFileAfter, ".go", 1)
    65  	testBeforeAfter(t, "", "latest", goFile, goFileAfter, ".go", 1)
    66  }
    67  
    68  func TestGo(t *testing.T) {
    69  	testBeforeAfter(t, "", "1.3.0", goFile, goFileAfter, ".go", 0)
    70  }
    71  
    72  func TestMarkdown(t *testing.T) {
    73  	testBeforeAfter(t, "", "0.15.1", md, mdAfter, ".md", 0)
    74  }
    75  
    76  func TestPython(t *testing.T) {
    77  	testBeforeAfter(t, "", "0.13.2", python, pythonAfter, ".py", 0)
    78  }
    79  
    80  func TestReStructuredText(t *testing.T) {
    81  	testBeforeAfter(t, "0.13.0-dev", "0.13.2", rst, rstAfter, ".rst", 0)
    82  }
    83  
    84  func TestSetup(t *testing.T) {
    85  	testBeforeAfter(t, "", "0.0.3", setup, setupAfter, ".py", 0)
    86  }
    87  
    88  func TestUserdata(t *testing.T) {
    89  	testBeforeAfter(t, "", "2.22.1", userdata, userdataAfter, "", 0)
    90  }
    91  
    92  func testBeforeAfter(t *testing.T, current, version, before, after, suffix string, code int) {
    93  	dir, err := ioutil.TempDir("/tmp", "bumpver_test")
    94  	if err != nil {
    95  		t.Error(err)
    96  	}
    97  	filename := path.Join(dir, "testfile"+suffix)
    98  	if err := ioutil.WriteFile(filename, []byte(before), 0644); err != nil {
    99  		t.Error(err)
   100  	}
   101  	defer os.Remove(filename)
   102  	// test bumpversion against it
   103  	commands := []string{version, filename}
   104  	if current != "" {
   105  		commands = []string{"--from=" + current, version, filename}
   106  	}
   107  	rc, out := commandOutput(commands)
   108  	if rc != code {
   109  		t.Errorf("Return code was %d, expected %d", rc, code)
   110  	}
   111  	if code != 0 {
   112  		return
   113  	}
   114  	if out != fmt.Sprintf("Bumped %s\n", filename) {
   115  		t.Error(out)
   116  	}
   117  	result, err := ioutil.ReadFile(filename)
   118  	if err != nil {
   119  		t.Error(err)
   120  	}
   121  	if string(result) != after {
   122  		t.Errorf("File contents not updated for %s:\n%s", version, string(result))
   123  	}
   124  }
   125  
   126  func commandOutput(args []string) (returnCode int, output string) {
   127  	old := os.Stdout
   128  	r, w, _ := os.Pipe()
   129  	os.Stdout = w
   130  
   131  	rc := Command(args)
   132  
   133  	outC := make(chan string)
   134  	go func() {
   135  		var buf bytes.Buffer
   136  		io.Copy(&buf, r)
   137  		outC <- buf.String()
   138  	}()
   139  
   140  	w.Close()
   141  	os.Stdout = old
   142  	output = <-outC
   143  
   144  	return rc, output
   145  }
   146  
   147  var goFile = `
   148  package version
   149  
   150  const Version = "1.2.11"
   151  `
   152  
   153  var goFileAfter = `
   154  package version
   155  
   156  const Version = "1.3.0"
   157  `
   158  
   159  var python = `
   160  #!/usr/bin/env python
   161  
   162  from docopt import docopt
   163  
   164  __version__ = '0.13.1'
   165  `
   166  
   167  var pythonAfter = `
   168  #!/usr/bin/env python
   169  
   170  from docopt import docopt
   171  
   172  __version__ = '0.13.2'
   173  `
   174  
   175  var setup = `
   176  #!/usr/bin/env python
   177  
   178  """Install the Deis command-line client."""
   179  
   180  try:
   181      APACHE_LICENSE = open('LICENSE').read()
   182  except IOError:
   183      APACHE_LICENSE = 'See http://www.apache.org/licenses/LICENSE-2.0'
   184  
   185  
   186  setup(name='deis',
   187        version='0.0.2',
   188        description='Command-line Client for Deis, the open PaaS',
   189        classifiers=[
   190            'Development Status :: 4 - Beta',
   191            'Environment :: Console',
   192            'Intended Audience :: Developers',
   193            'Programming Language :: Python :: 2.7',
   194        ],
   195        install_requires=[
   196            'docopt==0.6.2', 'python-dateutil==2.2', 'requests==2.3.0', 'termcolor==1.1.0'
   197        ],
   198        **KWARGS)
   199  `
   200  
   201  var setupAfter = `
   202  #!/usr/bin/env python
   203  
   204  """Install the Deis command-line client."""
   205  
   206  try:
   207      APACHE_LICENSE = open('LICENSE').read()
   208  except IOError:
   209      APACHE_LICENSE = 'See http://www.apache.org/licenses/LICENSE-2.0'
   210  
   211  
   212  setup(name='deis',
   213        version='0.0.3',
   214        description='Command-line Client for Deis, the open PaaS',
   215        classifiers=[
   216            'Development Status :: 4 - Beta',
   217            'Environment :: Console',
   218            'Intended Audience :: Developers',
   219            'Programming Language :: Python :: 2.7',
   220        ],
   221        install_requires=[
   222            'docopt==0.6.2', 'python-dateutil==2.2', 'requests==2.3.0', 'termcolor==1.1.0'
   223        ],
   224        **KWARGS)
   225  `
   226  
   227  var userdata = `
   228  #cloud-config
   229  ---
   230  coreos:
   231    etcd:
   232      # generate a new token for each unique cluster from https://discovery.etcd.io/new
   233      # uncomment the following line and replace it with your discovery URL
   234      # discovery: https://discovery.etcd.io/12345693838asdfasfadf13939923
   235      addr: $private_ipv4:4001
   236      peer-addr: $private_ipv4:7001
   237      # give etcd more time if it's under heavy load - prevent leader election thrashing
   238      peer-election-timeout: 2000
   239      # heartbeat interval should ideally be 1/4 or 1/5 of peer election timeout
   240      peer-heartbeat-interval: 500
   241  write_files:
   242    - path: /etc/deis-release
   243      content: |
   244        DEIS_RELEASE=2.22.0
   245    - path: /etc/profile.d/nse-function.sh
   246      permissions: '0755'
   247      content: |
   248        function nse() {
   249          sudo nsenter --pid --uts --mount --ipc --net --target $(docker inspect --format="{{ .State.Pid }}" $1)
   250        }
   251  `
   252  
   253  var userdataAfter = `
   254  #cloud-config
   255  ---
   256  coreos:
   257    etcd:
   258      # generate a new token for each unique cluster from https://discovery.etcd.io/new
   259      # uncomment the following line and replace it with your discovery URL
   260      # discovery: https://discovery.etcd.io/12345693838asdfasfadf13939923
   261      addr: $private_ipv4:4001
   262      peer-addr: $private_ipv4:7001
   263      # give etcd more time if it's under heavy load - prevent leader election thrashing
   264      peer-election-timeout: 2000
   265      # heartbeat interval should ideally be 1/4 or 1/5 of peer election timeout
   266      peer-heartbeat-interval: 500
   267  write_files:
   268    - path: /etc/deis-release
   269      content: |
   270        DEIS_RELEASE=2.22.1
   271    - path: /etc/profile.d/nse-function.sh
   272      permissions: '0755'
   273      content: |
   274        function nse() {
   275          sudo nsenter --pid --uts --mount --ipc --net --target $(docker inspect --format="{{ .State.Pid }}" $1)
   276        }
   277  `
   278  
   279  var rst = `
   280  .. code-block:: console
   281  
   282      $ pip install docopt==0.6.2 python-dateutil==2.2 requests==2.3.0 termcolor==1.1.0
   283      $ sudo ln -fs $(pwd)/client/deis.py /usr/local/bin/deis
   284      $ deis
   285      Usage: deis <command> [<args>...]
   286  
   287  If you don't have Python_ installed, you can download a binary executable
   288  version of the Deis client for Mac OS X, Linux amd64, or Windows:
   289  
   290      - https://s3-us-west-2.amazonaws.com/opdemand/deis-0.13.0-dev-darwin.tgz
   291      - https://s3-us-west-2.amazonaws.com/opdemand/deis-0.13.0-dev-linux.tgz
   292  `
   293  
   294  var rstAfter = `
   295  .. code-block:: console
   296  
   297      $ pip install docopt==0.6.2 python-dateutil==2.2 requests==2.3.0 termcolor==1.1.0
   298      $ sudo ln -fs $(pwd)/client/deis.py /usr/local/bin/deis
   299      $ deis
   300      Usage: deis <command> [<args>...]
   301  
   302  If you don't have Python_ installed, you can download a binary executable
   303  version of the Deis client for Mac OS X, Linux amd64, or Windows:
   304  
   305      - https://s3-us-west-2.amazonaws.com/opdemand/deis-0.13.2-darwin.tgz
   306      - https://s3-us-west-2.amazonaws.com/opdemand/deis-0.13.2-linux.tgz
   307  `
   308  
   309  var md = `
   310  # Deis
   311  
   312  Deis (pronounced DAY-iss) is an open source PaaS that makes it easy to deploy and manage applications on your own servers. Deis builds upon [Docker](http://docker.io/) and [CoreOS](http://coreos.com) to provide a lightweight PaaS with a [Heroku-inspired](http://heroku.com) workflow.
   313  
   314  [![Current Release](http://img.shields.io/badge/release-v0.12.0-blue.svg)](https://github.com/deis/deis/releases/tag/v0.12.0)
   315  
   316  ![Deis Graphic](https://s3-us-west-2.amazonaws.com/deis-images/deis-graphic.png)
   317  `
   318  
   319  var mdAfter = `
   320  # Deis
   321  
   322  Deis (pronounced DAY-iss) is an open source PaaS that makes it easy to deploy and manage applications on your own servers. Deis builds upon [Docker](http://docker.io/) and [CoreOS](http://coreos.com) to provide a lightweight PaaS with a [Heroku-inspired](http://heroku.com) workflow.
   323  
   324  [![Current Release](http://img.shields.io/badge/release-v0.15.1-blue.svg)](https://github.com/deis/deis/releases/tag/v0.15.1)
   325  
   326  ![Deis Graphic](https://s3-us-west-2.amazonaws.com/deis-images/deis-graphic.png)
   327  `