github.com/buildtool/scaffold@v0.0.2/pkg/scaffold_test.go (about)

     1  package pkg
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  	"github.com/buildtool/scaffold/pkg/config"
     8  	"github.com/buildtool/scaffold/pkg/config/ci"
     9  	"github.com/buildtool/scaffold/pkg/config/vcs"
    10  	"github.com/buildtool/scaffold/pkg/stack"
    11  	"github.com/buildtool/scaffold/pkg/templating"
    12  	"github.com/stretchr/testify/assert"
    13  	"io"
    14  	"io/ioutil"
    15  	"os"
    16  	"path/filepath"
    17  	"testing"
    18  )
    19  
    20  var name string
    21  
    22  func TestMain(m *testing.M) {
    23  	tempDir := setup()
    24  	code := m.Run()
    25  	teardown(tempDir)
    26  	os.Exit(code)
    27  }
    28  
    29  func setup() string {
    30  	name, _ = ioutil.TempDir(os.TempDir(), "scaffold")
    31  	_ = os.Chdir(name)
    32  
    33  	return name
    34  }
    35  
    36  func teardown(tempDir string) {
    37  	_ = os.RemoveAll(tempDir)
    38  }
    39  
    40  func TestSetup_NoArgs(t *testing.T) {
    41  	out := bytes.Buffer{}
    42  
    43  	exitCode := Setup(name, &out)
    44  
    45  	assert.Equal(t, -1, exitCode)
    46  	assert.Equal(t, "\x1b[0mUsage: service-setup [options] <name>\n\nFor example \x1b[34m`service-setup --stack go gosvc`\x1b[39m would create a new repository and scaffold it as a Go-project\n\nOptions:\n\x1b[0m", out.String())
    47  }
    48  
    49  func TestSetup_NonExistingStack(t *testing.T) {
    50  	out := bytes.Buffer{}
    51  
    52  	exitCode := Setup(name, &out, "-s", "missing", "project")
    53  
    54  	assert.Equal(t, -2, exitCode)
    55  	assert.Equal(t, "\x1b[0m\x1b[31mProvided stack does not exist yet. Available stacks are: \x1b[39m\x1b[97m\x1b[1m(go, none, scala)\x1b[0m\x1b[97m\x1b[39m\n\x1b[0m", out.String())
    56  }
    57  
    58  func TestSetup_BrokenConfig(t *testing.T) {
    59  	os.Clearenv()
    60  	yaml := `ci: [] `
    61  	file := filepath.Join(name, ".scaffold.yaml")
    62  	_ = ioutil.WriteFile(file, []byte(yaml), 0777)
    63  	defer func() { _ = os.Remove(file) }()
    64  
    65  	out := bytes.Buffer{}
    66  
    67  	exitCode := Setup(name, &out, "project")
    68  
    69  	assert.Equal(t, -3, exitCode)
    70  	assert.Equal(t, fmt.Sprintf("\x1b[0mParsing config from file: \x1b[32m'%s'\x1b[39m\x1b[0m\n\x1b[0m\x1b[31myaml: unmarshal errors:\n  line 1: cannot unmarshal !!seq into config.CIConfig\x1b[39m\x1b[0m\n", file), out.String())
    71  }
    72  
    73  func TestSetup_NoVCS(t *testing.T) {
    74  	out := bytes.Buffer{}
    75  
    76  	exitCode := Setup(name, &out, "project")
    77  
    78  	assert.Equal(t, -4, exitCode)
    79  	assert.Equal(t, "\x1b[0m\x1b[31mno VCS configured\x1b[39m\x1b[0m\n", out.String())
    80  }
    81  
    82  func TestScaffold_Missing_Token(t *testing.T) {
    83  	yaml := `
    84  ci:
    85    buildkite:
    86      organisation: example
    87      token: abc
    88  vcs:
    89    github:  
    90      organisation: example
    91      token: abc
    92  `
    93  	file := filepath.Join(name, ".scaffold.yaml")
    94  	_ = ioutil.WriteFile(file, []byte(yaml), 0777)
    95  	defer func() { _ = os.Remove(file) }()
    96  
    97  	out := bytes.Buffer{}
    98  
    99  	exitCode := Setup(name, &out, "project")
   100  
   101  	assert.Equal(t, -6, exitCode)
   102  	assert.Equal(t, fmt.Sprintf("\x1b[0mParsing config from file: \x1b[32m'%s'\x1b[39m\x1b[0m\n\x1b[0m\x1b[31mGET https://api.buildkite.com/v2/user: 401 Authentication required. Please supply a valid API Access Token: https://buildkite.com/docs/apis/rest-api#authentication\x1b[39m\x1b[0m\n", file), out.String())
   103  }
   104  
   105  func TestScaffold_Configure_Error(t *testing.T) {
   106  	cfg := config.InitEmptyConfig()
   107  	cfg.CurrentCI = &mockCi{configErr: errors.New("config error")}
   108  	cfg.CurrentVCS = &mockVcs{}
   109  	out := &bytes.Buffer{}
   110  	exitCode := scaffold(cfg, name, "project", &stack.None{}, out)
   111  	assert.Equal(t, -5, exitCode)
   112  	assert.Equal(t, "\x1b[0m\x1b[31mconfig error\x1b[39m\x1b[0m\n", out.String())
   113  }
   114  
   115  func TestScaffold_Ok(t *testing.T) {
   116  	cfg := config.InitEmptyConfig()
   117  	cfg.CurrentCI = &mockCi{}
   118  	cfg.CurrentVCS = &mockVcs{}
   119  	out := &bytes.Buffer{}
   120  	exitCode := scaffold(cfg, name, "project", &stack.None{}, out)
   121  	assert.Equal(t, 0, exitCode)
   122  	assert.Equal(t, "\x1b[0m\x1b[94mCreating new service \x1b[39m\x1b[97m\x1b[1m'project'\x1b[0m\x1b[97m\x1b[39m \x1b[94musing stack \x1b[39m\x1b[97m\x1b[1m'none'\x1b[0m\x1b[97m\x1b[39m\n\x1b[0m\x1b[0m\x1b[94mCreating repository at \x1b[39m\x1b[97m\x1b[1m'mock'\x1b[0m\x1b[97m\x1b[39m\n\x1b[0m\x1b[0m\x1b[32mCreated repository \x1b[39m\x1b[97m\x1b[1m'git@git'\x1b[0m\x1b[97m\x1b[39m\n\x1b[0m\x1b[0m\x1b[94mCreating build pipeline for \x1b[39m\x1b[97m\x1b[1m'project'\x1b[0m\x1b[97m\x1b[39m\n\x1b[0m", out.String())
   123  }
   124  
   125  type mockCi struct {
   126  	configErr error
   127  }
   128  
   129  func (m mockCi) Name() string {
   130  	panic("implement me")
   131  }
   132  
   133  func (m mockCi) ValidateConfig() error {
   134  	panic("implement me")
   135  }
   136  
   137  func (m mockCi) Validate(name string) error {
   138  	return nil
   139  }
   140  
   141  func (m mockCi) Scaffold(dir string, data templating.TemplateData) (*string, error) {
   142  	return nil, nil
   143  }
   144  
   145  func (m mockCi) Badges(name string) ([]templating.Badge, error) {
   146  	return nil, nil
   147  }
   148  
   149  func (m mockCi) Configure() error {
   150  	return m.configErr
   151  }
   152  
   153  var _ ci.CI = &mockCi{}
   154  
   155  type mockVcs struct {
   156  }
   157  
   158  func (m mockVcs) Name() string {
   159  	return "mock"
   160  }
   161  
   162  func (m mockVcs) ValidateConfig() error {
   163  	panic("implement me")
   164  }
   165  
   166  func (m mockVcs) Configure() {
   167  }
   168  
   169  func (m mockVcs) Validate(name string) error {
   170  	return nil
   171  }
   172  
   173  func (m mockVcs) Scaffold(name string) (*vcs.RepositoryInfo, error) {
   174  	return &vcs.RepositoryInfo{
   175  		SSHURL:  "git@git",
   176  		HTTPURL: "https://git",
   177  	}, nil
   178  }
   179  
   180  func (m mockVcs) Webhook(name, url string) error {
   181  	panic("implement me")
   182  }
   183  
   184  func (m mockVcs) Clone(dir, name, url string, out io.Writer) error {
   185  	return nil
   186  }
   187  
   188  var _ vcs.VCS = &mockVcs{}