github.com/fnproject/cli@v0.0.0-20240508150455-e5d88bd86117/init_test.go (about)

     1  /*
     2   * Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package main
    18  
    19  import (
    20  	"flag"
    21  	"io/ioutil"
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/fnproject/cli/commands"
    26  	"github.com/fnproject/cli/common"
    27  	"github.com/fnproject/cli/langs"
    28  	"github.com/urfave/cli"
    29  	yaml "gopkg.in/yaml.v2"
    30  )
    31  
    32  func TestInit(t *testing.T) {
    33  
    34  	testname := "test-init"
    35  	testdir, err := ioutil.TempDir("", testname)
    36  	if err != nil {
    37  		t.Fatalf("ERROR: Failed to make tmp test directory: err: %v", err)
    38  	}
    39  	defer os.RemoveAll(testdir)
    40  
    41  	err = os.Chdir(testdir)
    42  	if err != nil {
    43  		t.Fatalf("ERROR: Failed to cd to tmp test directory: err: %v", err)
    44  	}
    45  
    46  	helper := &langs.GoLangHelper{}
    47  	helper.GenerateBoilerplate(testdir)
    48  
    49  	app := newFn()
    50  	err = app.Command("init").Run(cli.NewContext(app, &flag.FlagSet{}, nil))
    51  	if err != nil {
    52  		t.Fatalf("ERROR: Failed run `init` command: err: %v", err)
    53  	}
    54  
    55  	ffname := "func.yaml"
    56  	b, err := ioutil.ReadFile(ffname)
    57  	if err != nil {
    58  		t.Fatalf("Could not open %s for parsing. Error: %v", ffname, err)
    59  	}
    60  	ff := &common.FuncFile{}
    61  	err = yaml.Unmarshal(b, ff)
    62  	if err != nil {
    63  		t.Fatalf("Could not parse %s. Error: %v", ffname, err)
    64  	}
    65  	// should have version, runtime and entrypoint
    66  	if ff.Version == "" {
    67  		t.Errorf("No version found in generated %s", ffname)
    68  	}
    69  	if ff.Runtime == "" {
    70  		t.Errorf("No runtime found in generated %s", ffname)
    71  	}
    72  	if ff.Entrypoint == "" {
    73  		t.Errorf("No entrypoint found in generated %s", ffname)
    74  	}
    75  }
    76  
    77  func funcNameValidation(name string, t *testing.T) {
    78  	err := commands.ValidateFuncName("fooFunc")
    79  	if err == nil {
    80  		t.Error("Expected validation error for function name")
    81  	}
    82  }
    83  
    84  func TestFuncNameWithUpperCase(t *testing.T) {
    85  	funcNameValidation("fooMyFunc", t)
    86  }
    87  
    88  func TestFuncNameWithColon(t *testing.T) {
    89  	funcNameValidation("foo:myfunc", t)
    90  }