github.com/gobuffalo/buffalo-cli/v2@v2.0.0-alpha.15.0.20200919213536-a7350c8e6799/built/app_test.go (about)

     1  package built
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/gobuffalo/plugins"
    11  	"github.com/gobuffalo/plugins/plugio"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  type IniterFn func(ctx context.Context, root string, args []string) error
    16  
    17  func (i IniterFn) BuiltInit(ctx context.Context, root string, args []string) error {
    18  	return i(ctx, root, args)
    19  }
    20  
    21  func WithIniter(p plugins.Plugin, fn IniterFn) plugins.Plugin {
    22  	type wi struct {
    23  		IniterFn
    24  		plugins.Plugin
    25  	}
    26  	return wi{
    27  		Plugin:   p,
    28  		IniterFn: fn,
    29  	}
    30  }
    31  
    32  func Test_App_No_Args(t *testing.T) {
    33  	r := require.New(t)
    34  
    35  	var res bool
    36  	app := &App{
    37  		OriginalMain: func() {
    38  			res = true
    39  		},
    40  	}
    41  
    42  	var args []string
    43  	ctx := context.Background()
    44  	err := app.Main(ctx, "", args)
    45  	r.NoError(err)
    46  	r.True(res)
    47  }
    48  
    49  func Test_App_No_Args_Fallthrough(t *testing.T) {
    50  	r := require.New(t)
    51  
    52  	var res bool
    53  	app := &App{
    54  		Fallthrough: func(ctx context.Context, root string, args []string) error {
    55  			res = true
    56  			return nil
    57  		},
    58  	}
    59  
    60  	var args []string
    61  	ctx := context.Background()
    62  	err := app.Main(ctx, "", args)
    63  	r.NoError(err)
    64  	r.True(res)
    65  }
    66  
    67  func Test_App_With_Args_Fallthrough(t *testing.T) {
    68  	r := require.New(t)
    69  
    70  	var res bool
    71  	app := &App{
    72  		Fallthrough: func(ctx context.Context, root string, args []string) error {
    73  			res = true
    74  			return nil
    75  		},
    76  	}
    77  
    78  	ctx := context.Background()
    79  	err := app.Main(ctx, "", []string{"lee", "majors"})
    80  	r.NoError(err)
    81  	r.True(res)
    82  }
    83  
    84  func Test_App_Init_Plugins(t *testing.T) {
    85  	r := require.New(t)
    86  
    87  	var res bool
    88  	var pres bool
    89  
    90  	fn := func(ctx context.Context, root string, args []string) error {
    91  		pres = true
    92  		return nil
    93  	}
    94  
    95  	plugs := plugins.Plugins{
    96  		WithIniter(background(""), fn),
    97  	}
    98  
    99  	app := &App{
   100  		OriginalMain: func() {
   101  			res = true
   102  		},
   103  		Plugger: plugs,
   104  	}
   105  
   106  	var args []string
   107  	ctx := context.Background()
   108  	err := app.Main(ctx, "", args)
   109  	r.NoError(err)
   110  	r.True(res)
   111  	r.True(pres)
   112  }
   113  
   114  func Test_App_Init_Plugins_Error(t *testing.T) {
   115  	r := require.New(t)
   116  
   117  	var res bool
   118  	var pres bool
   119  	exp := fmt.Errorf("boom")
   120  	fn := func(ctx context.Context, root string, args []string) error {
   121  		return exp
   122  	}
   123  
   124  	plugs := plugins.Plugins{
   125  		WithIniter(background(""), fn),
   126  	}
   127  
   128  	app := &App{
   129  		OriginalMain: func() {
   130  			res = true
   131  		},
   132  		Plugger: plugs,
   133  	}
   134  
   135  	var args []string
   136  	ctx := context.Background()
   137  	err := app.Main(ctx, "", args)
   138  	r.Error(err)
   139  	r.Equal(exp, err)
   140  	r.False(res)
   141  	r.False(pres)
   142  }
   143  
   144  func Test_App_Version(t *testing.T) {
   145  	r := require.New(t)
   146  
   147  	stdout := &bytes.Buffer{}
   148  	plugs := plugins.Plugins{
   149  		plugio.NewOuter(stdout),
   150  	}
   151  	app := &App{
   152  		BuildVersion: "v1",
   153  		Plugger:      plugs,
   154  	}
   155  	ctx := context.Background()
   156  
   157  	err := app.Main(ctx, "", []string{"version"})
   158  	r.NoError(err)
   159  
   160  	s := strings.TrimSpace(stdout.String())
   161  	r.Equal("v1", s)
   162  }