github.com/actions-on-google/gactions@v3.2.0+incompatible/cmd/gactions/cli/ginit/ginit_test.go (about)

     1  // Copyright 2020 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ginit
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"testing"
    21  
    22  	"github.com/actions-on-google/gactions/project"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  type MockStudio struct {
    27  }
    28  
    29  func (MockStudio) ProjectID() string {
    30  	return ""
    31  }
    32  
    33  func (p MockStudio) Download(sample project.SampleProject, dest string) error {
    34  	return nil
    35  }
    36  
    37  // studio project.
    38  func (p MockStudio) AlreadySetup(pathToWorkDir string) bool {
    39  	return false
    40  }
    41  
    42  func (p MockStudio) Files() (map[string][]byte, error) {
    43  	return map[string][]byte{}, nil
    44  }
    45  
    46  func (p MockStudio) ClientSecretJSON() ([]byte, error) {
    47  	return []byte{}, nil
    48  }
    49  
    50  func (p MockStudio) ProjectRoot() string {
    51  	return ""
    52  }
    53  
    54  func TestCmdExecute(cmd *cobra.Command, args []string) (string, error) {
    55  	output := new(bytes.Buffer)
    56  	cmd.SetOutput(output)
    57  	cmd.SetArgs(args)
    58  	err := cmd.Execute()
    59  	return output.String(), err
    60  }
    61  
    62  func execute(args ...string) (string, error) {
    63  	cmd := &cobra.Command{}
    64  	project := MockStudio{}
    65  	AddCommand(context.Background(), cmd, project)
    66  	return TestCmdExecute(cmd, args)
    67  }
    68  
    69  func TestInitWithInvalidArgs(t *testing.T) {
    70  	og := availableProjects
    71  	availableProjects = func(ctx context.Context, p project.Project) ([]project.SampleProject, error) {
    72  		return []project.SampleProject{
    73  			project.SampleProject{"question", "https://google.com"},
    74  		}, nil
    75  	}
    76  	defer func() {
    77  		availableProjects = og
    78  	}()
    79  	tests := []struct {
    80  		invalidArgs []string
    81  	}{
    82  		{
    83  			invalidArgs: []string{"init"},
    84  		},
    85  		{
    86  			invalidArgs: []string{"init", "foo"},
    87  		},
    88  	}
    89  	for _, tc := range tests {
    90  		if _, err := execute(tc.invalidArgs...); err == nil {
    91  			t.Errorf("init didn't fail when %v were passed", tc.invalidArgs)
    92  		}
    93  	}
    94  }
    95  
    96  func TestInitWithValidArgs(t *testing.T) {
    97  	og := availableProjects
    98  	availableProjects = func(ctx context.Context, p project.Project) ([]project.SampleProject, error) {
    99  		return []project.SampleProject{
   100  			project.SampleProject{"question", "https://google.com"},
   101  		}, nil
   102  	}
   103  	defer func() {
   104  		availableProjects = og
   105  	}()
   106  	tests := []struct {
   107  		validArgs []string
   108  	}{
   109  		{
   110  			validArgs: []string{"init", "question"},
   111  		},
   112  		{
   113  			validArgs: []string{"init", "question", "--dest", "~/Code/"},
   114  		},
   115  	}
   116  
   117  	for _, tc := range tests {
   118  		if _, err := execute(tc.validArgs...); err != nil {
   119  			t.Errorf("init failed when %v were passed", tc.validArgs)
   120  		}
   121  	}
   122  }