github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/commands/completion_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/sclevine/spec"
    10  	"github.com/sclevine/spec/report"
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/buildpacks/pack/internal/commands"
    14  	"github.com/buildpacks/pack/pkg/logging"
    15  	h "github.com/buildpacks/pack/testhelpers"
    16  )
    17  
    18  func TestCompletionCommand(t *testing.T) {
    19  	spec.Run(t, "Commands", testCompletionCommand, spec.Random(), spec.Report(report.Terminal{}))
    20  }
    21  
    22  func testCompletionCommand(t *testing.T, when spec.G, it spec.S) {
    23  	var (
    24  		command  *cobra.Command
    25  		logger   logging.Logger
    26  		outBuf   bytes.Buffer
    27  		packHome string
    28  		assert   = h.NewAssertionManager(t)
    29  	)
    30  
    31  	it.Before(func() {
    32  		logger = logging.NewLogWithWriters(&outBuf, &outBuf)
    33  		var err error
    34  		packHome, err = os.MkdirTemp("", "")
    35  		assert.Nil(err)
    36  
    37  		// the CompletionCommand calls a method on its Parent(), so it needs to have
    38  		// one.
    39  		command = &cobra.Command{}
    40  		command.AddCommand(commands.CompletionCommand(logger, packHome))
    41  		command.SetArgs([]string{"completion"})
    42  	})
    43  
    44  	it.After(func() {
    45  		os.RemoveAll(packHome)
    46  	})
    47  
    48  	when("#CompletionCommand", func() {
    49  		when("Shell flag is empty(default value)", func() {
    50  			it("errors should not be occurred", func() {
    51  				assert.Nil(command.Execute())
    52  			})
    53  		})
    54  
    55  		when("PackHome directory does not exist", func() {
    56  			it("should create completion file", func() {
    57  				missingDir := filepath.Join(packHome, "not-found")
    58  
    59  				command = &cobra.Command{}
    60  				command.AddCommand(commands.CompletionCommand(logger, missingDir))
    61  				command.SetArgs([]string{"completion"})
    62  
    63  				assert.Nil(command.Execute())
    64  				assert.Contains(outBuf.String(), filepath.Join(missingDir, "completion.sh"))
    65  			})
    66  		})
    67  
    68  		for _, test := range []struct {
    69  			shell     string
    70  			extension string
    71  		}{
    72  			{shell: "bash", extension: ".sh"},
    73  			{shell: "fish", extension: ".fish"},
    74  			{shell: "powershell", extension: ".ps1"},
    75  			{shell: "zsh", extension: ".zsh"},
    76  		} {
    77  			shell := test.shell
    78  			extension := test.extension
    79  
    80  			when("shell is "+shell, func() {
    81  				it("should create completion file ending in "+extension, func() {
    82  					command.SetArgs([]string{"completion", "--shell", shell})
    83  					assert.Nil(command.Execute())
    84  
    85  					expectedFile := filepath.Join(packHome, "completion"+extension)
    86  					assert.Contains(outBuf.String(), expectedFile)
    87  					assert.FileExists(expectedFile)
    88  					assert.FileIsNotEmpty(expectedFile)
    89  				})
    90  			})
    91  		}
    92  	})
    93  }