github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap/cmd_help_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package main_test 21 22 import ( 23 "bytes" 24 "fmt" 25 "os" 26 "regexp" 27 "strings" 28 29 "github.com/jessevdk/go-flags" 30 "gopkg.in/check.v1" 31 32 snap "github.com/snapcore/snapd/cmd/snap" 33 ) 34 35 func (s *SnapSuite) TestHelpPrintsHelp(c *check.C) { 36 origArgs := os.Args 37 defer func() { os.Args = origArgs }() 38 39 for _, cmdLine := range [][]string{ 40 {"snap"}, 41 {"snap", "help"}, 42 {"snap", "--help"}, 43 {"snap", "-h"}, 44 {"snap", "--help", "install"}, 45 } { 46 s.ResetStdStreams() 47 48 os.Args = cmdLine 49 comment := check.Commentf("%q", cmdLine) 50 51 err := snap.RunMain() 52 c.Assert(err, check.IsNil, comment) 53 c.Check(s.Stdout(), check.Matches, "(?s)"+strings.Join([]string{ 54 snap.LongSnapDescription, 55 "", 56 regexp.QuoteMeta(snap.SnapUsage), 57 "", 58 snap.SnapHelpCategoriesIntro, 59 ".*", "", 60 snap.SnapHelpAllFooter, 61 snap.SnapHelpFooter, 62 }, "\n")+`\s*`, comment) 63 c.Check(s.Stderr(), check.Equals, "", comment) 64 } 65 } 66 67 func (s *SnapSuite) TestHelpAllPrintsLongHelp(c *check.C) { 68 origArgs := os.Args 69 defer func() { os.Args = origArgs }() 70 71 os.Args = []string{"snap", "help", "--all"} 72 73 err := snap.RunMain() 74 c.Assert(err, check.IsNil) 75 c.Check(s.Stdout(), check.Matches, "(?sm)"+strings.Join([]string{ 76 snap.LongSnapDescription, 77 "", 78 regexp.QuoteMeta(snap.SnapUsage), 79 "", 80 snap.SnapHelpAllIntro, 81 "", ".*", "", 82 snap.SnapHelpAllFooter, 83 }, "\n")+`\s*`) 84 c.Check(s.Stderr(), check.Equals, "") 85 } 86 87 func nonHiddenCommands() map[string]bool { 88 parser := snap.Parser(snap.Client()) 89 commands := parser.Commands() 90 names := make(map[string]bool, len(commands)) 91 for _, cmd := range commands { 92 if cmd.Hidden { 93 continue 94 } 95 names[cmd.Name] = true 96 } 97 return names 98 } 99 100 func (s *SnapSuite) testSubCommandHelp(c *check.C, sub, expected string) { 101 parser := snap.Parser(snap.Client()) 102 rest, err := parser.ParseArgs([]string{sub, "--help"}) 103 c.Assert(err, check.DeepEquals, &flags.Error{Type: flags.ErrHelp}) 104 c.Assert(rest, check.HasLen, 0) 105 var buf bytes.Buffer 106 parser.WriteHelp(&buf) 107 c.Check(buf.String(), check.Equals, expected) 108 } 109 110 func (s *SnapSuite) TestSubCommandHelpPrintsHelp(c *check.C) { 111 origArgs := os.Args 112 defer func() { os.Args = origArgs }() 113 114 for cmd := range nonHiddenCommands() { 115 s.ResetStdStreams() 116 os.Args = []string{"snap", cmd, "--help"} 117 118 err := snap.RunMain() 119 comment := check.Commentf("%q", cmd) 120 c.Assert(err, check.IsNil, comment) 121 // regexp matches "Usage: snap <the command>" plus an arbitrary 122 // number of [<something>] plus an arbitrary number of 123 // <<something>> optionally ending in ellipsis 124 c.Check(s.Stdout(), check.Matches, fmt.Sprintf(`(?sm)Usage:\s+snap %s(?: \[[^][]+\])*(?:(?: <[^<>]+>)+(?:\.\.\.)?)?$.*`, cmd), comment) 125 c.Check(s.Stderr(), check.Equals, "", comment) 126 } 127 } 128 129 func (s *SnapSuite) TestHelpCategories(c *check.C) { 130 // non-hidden commands that are not expected to appear in the help summary 131 excluded := []string{ 132 "help", 133 } 134 all := nonHiddenCommands() 135 categorised := make(map[string]bool, len(all)+len(excluded)) 136 for _, cmd := range excluded { 137 categorised[cmd] = true 138 } 139 seen := make(map[string]string, len(all)) 140 seenCmds := func(cmds []string, label string) { 141 for _, cmd := range cmds { 142 categorised[cmd] = true 143 if seen[cmd] != "" { 144 c.Errorf("duplicated: %q in %q and %q", cmd, seen[cmd], label) 145 } 146 seen[cmd] = label 147 } 148 } 149 for _, categ := range snap.HelpCategories { 150 seenCmds(categ.Commands, categ.Label) 151 seenCmds(categ.AllOnlyCommands, categ.Label) 152 } 153 for cmd := range all { 154 if !categorised[cmd] { 155 c.Errorf("uncategorised: %q", cmd) 156 } 157 } 158 for cmd := range categorised { 159 if !all[cmd] { 160 c.Errorf("unknown (hidden?): %q", cmd) 161 } 162 } 163 } 164 165 func (s *SnapSuite) TestHelpCommandAllFails(c *check.C) { 166 origArgs := os.Args 167 defer func() { os.Args = origArgs }() 168 os.Args = []string{"snap", "help", "interfaces", "--all"} 169 170 err := snap.RunMain() 171 c.Assert(err, check.ErrorMatches, "help accepts a command, or '--all', but not both.") 172 } 173 174 func (s *SnapSuite) TestManpageInSection8(c *check.C) { 175 origArgs := os.Args 176 defer func() { os.Args = origArgs }() 177 os.Args = []string{"snap", "help", "--man"} 178 179 err := snap.RunMain() 180 c.Assert(err, check.IsNil) 181 182 c.Check(s.Stdout(), check.Matches, `\.TH snap 8 (?s).*`) 183 } 184 185 func (s *SnapSuite) TestManpageNoDoubleTP(c *check.C) { 186 origArgs := os.Args 187 defer func() { os.Args = origArgs }() 188 os.Args = []string{"snap", "help", "--man"} 189 190 err := snap.RunMain() 191 c.Assert(err, check.IsNil) 192 193 c.Check(s.Stdout(), check.Not(check.Matches), `(?s).*(?m-s)^\.TP\n\.TP$(?s-m).*`) 194 195 } 196 197 func (s *SnapSuite) TestBadSub(c *check.C) { 198 origArgs := os.Args 199 defer func() { os.Args = origArgs }() 200 os.Args = []string{"snap", "debug", "brotato"} 201 202 err := snap.RunMain() 203 c.Assert(err, check.ErrorMatches, `unknown command "brotato", see 'snap help debug'.`) 204 }