github.com/devcamcar/cli@v0.0.0-20181107134215-706a05759d18/init_test.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/fnproject/cli/commands"
    10  	"github.com/fnproject/cli/common"
    11  	"github.com/fnproject/cli/langs"
    12  	"github.com/urfave/cli"
    13  	yaml "gopkg.in/yaml.v2"
    14  )
    15  
    16  func TestInit(t *testing.T) {
    17  
    18  	testname := "test-init"
    19  	testdir, err := ioutil.TempDir("", testname)
    20  	if err != nil {
    21  		t.Fatalf("ERROR: Failed to make tmp test directory: err: %v", err)
    22  	}
    23  	defer os.RemoveAll(testdir)
    24  
    25  	err = os.Chdir(testdir)
    26  	if err != nil {
    27  		t.Fatalf("ERROR: Failed to cd to tmp test directory: err: %v", err)
    28  	}
    29  
    30  	helper := &langs.GoLangHelper{}
    31  	helper.GenerateBoilerplate(testdir)
    32  
    33  	app := newFn()
    34  	err = app.Command("init").Run(cli.NewContext(app, &flag.FlagSet{}, nil))
    35  	if err != nil {
    36  		t.Fatalf("ERROR: Failed run `init` command: err: %v", err)
    37  	}
    38  
    39  	ffname := "func.yaml"
    40  	b, err := ioutil.ReadFile(ffname)
    41  	if err != nil {
    42  		t.Fatalf("Could not open %s for parsing. Error: %v", ffname, err)
    43  	}
    44  	ff := &common.FuncFile{}
    45  	err = yaml.Unmarshal(b, ff)
    46  	if err != nil {
    47  		t.Fatalf("Could not parse %s. Error: %v", ffname, err)
    48  	}
    49  	// should have version, runtime and entrypoint
    50  	if ff.Version == "" {
    51  		t.Errorf("No version found in generated %s", ffname)
    52  	}
    53  	if ff.Runtime == "" {
    54  		t.Errorf("No runtime found in generated %s", ffname)
    55  	}
    56  	if ff.Entrypoint == "" {
    57  		t.Errorf("No entrypoint found in generated %s", ffname)
    58  	}
    59  }
    60  
    61  func funcNameValidation(name string, t *testing.T) {
    62  	err := commands.ValidateFuncName("fooFunc")
    63  	if err == nil {
    64  		t.Error("Expected validation error for function name")
    65  	}
    66  }
    67  
    68  func TestFuncNameWithUpperCase(t *testing.T) {
    69  	funcNameValidation("fooMyFunc", t)
    70  }
    71  
    72  func TestFuncNameWithColon(t *testing.T) {
    73  	funcNameValidation("foo:myfunc", t)
    74  }