github.com/hattya/nazuna@v0.7.1-0.20240331055452-55e14c275c1c/cmd/nzn/help_test.go (about)

     1  //
     2  // nazuna/cmd/nzn :: help_test.go
     3  //
     4  //   Copyright (c) 2013-2022 Akinori Hattori <hattya@gmail.com>
     5  //
     6  //   SPDX-License-Identifier: MIT
     7  //
     8  
     9  package main
    10  
    11  import (
    12  	"fmt"
    13  	"testing"
    14  
    15  	"github.com/hattya/go.cli"
    16  )
    17  
    18  var (
    19  	helpUsage = cli.Dedent(`
    20  		usage: nzn help [<command>]
    21  
    22  		show help for a specified command
    23  	`)
    24  	helpOut = cli.Dedent(`
    25  		Nazuna - A layered dotfiles management
    26  
    27  		commands:
    28  
    29  		  alias      create an alias for the specified path
    30  		  clone      create a copy of an existing repository
    31  		  help       show help for a specified command
    32  		  init       create a new repository in the specified directory
    33  		  layer      manage repository layers
    34  		  link       create a link for the specified path
    35  		  subrepo    manage subrepositories
    36  		  update     update working copy
    37  		  vcs        run the vcs command inside the repository
    38  		  version    show version information
    39  
    40  		options:
    41  
    42  		  -h, --help    show help
    43  		  --version     show version information
    44  	`)
    45  )
    46  
    47  func TestHelp(t *testing.T) {
    48  	s := script{
    49  		{
    50  			cmd: []string{"nzn", "--help"},
    51  			out: helpOut + "\n",
    52  		},
    53  		{
    54  			cmd: []string{"nzn", "help"},
    55  			out: helpOut + "\n",
    56  		},
    57  		{
    58  			cmd: []string{"nzn"},
    59  			out: fmt.Sprintf(cli.Dedent(`
    60  				%v
    61  				[1]
    62  			`), helpOut),
    63  		},
    64  		{
    65  			cmd: []string{"nzn", "--nazuna"},
    66  			out: fmt.Sprintf(cli.Dedent(`
    67  				nzn: flag provided but not defined: -nazuna
    68  				%v
    69  				[2]
    70  			`), helpOut),
    71  		},
    72  		{
    73  			cmd: []string{"nzn", "nazuna"},
    74  			out: fmt.Sprintf(cli.Dedent(`
    75  				nzn: unknown command 'nazuna'
    76  				%v
    77  				[1]
    78  			`), helpOut),
    79  		},
    80  		{
    81  			cmd: []string{"nzn", "help", "help"},
    82  			out: helpUsage + "\n",
    83  		},
    84  		{
    85  			cmd: []string{"nzn", "help", "--help"},
    86  			out: helpUsage + "\n",
    87  		},
    88  		{
    89  			cmd: []string{"nzn", "help", "--nazuna"},
    90  			out: fmt.Sprintf(cli.Dedent(`
    91  				nzn help: flag provided but not defined: -nazuna
    92  				%v
    93  				[2]
    94  			`), helpUsage),
    95  		},
    96  	}
    97  	if err := s.exec(t); err != nil {
    98  		t.Error(err)
    99  	}
   100  }
   101  
   102  func TestHelpError(t *testing.T) {
   103  	s := script{
   104  		{
   105  			cmd: []string{"nzn", "help", "nazuna"},
   106  			out: cli.Dedent(`
   107  				nzn: unknown command 'nazuna'
   108  				type 'nzn help' for usage
   109  				[1]
   110  			`),
   111  		},
   112  	}
   113  	if err := s.exec(t); err != nil {
   114  		t.Error(err)
   115  	}
   116  }