| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cmd_test |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import ( |
| 16 |
"bytes" |
| 17 |
"context" |
| 18 |
"flag" |
| 19 |
"os" |
| 20 |
"path/filepath" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/google/go-cmp/cmp" |
| 24 |
"golang.org/x/tools/gopls/internal/cmd" |
| 25 |
"golang.org/x/tools/gopls/internal/tool" |
| 26 |
"golang.org/x/tools/internal/testenv" |
| 27 |
) |
| 28 |
|
| 29 |
var updateHelpFiles = flag.Bool("update-help-files", false, "Write out the help files instead of checking them") |
| 30 |
|
| 31 |
const appName = "gopls" |
| 32 |
|
| 33 |
func TestHelpFiles(t *testing.T) { |
| 34 |
testenv.NeedsGoBuild(t) |
| 35 |
app := cmd.New() |
| 36 |
ctx := context.Background() |
| 37 |
for _, page := range append(app.Commands(), app) { |
| 38 |
t.Run(page.Name(), func(t *testing.T) { |
| 39 |
var buf bytes.Buffer |
| 40 |
s := flag.NewFlagSet(page.Name(), flag.ContinueOnError) |
| 41 |
s.SetOutput(&buf) |
| 42 |
tool.Run(ctx, s, page, []string{"-h"}) |
| 43 |
name := page.Name() |
| 44 |
if name == appName { |
| 45 |
name = "usage" |
| 46 |
} |
| 47 |
helpFile := filepath.Join("usage", name+".hlp") |
| 48 |
got := buf.Bytes() |
| 49 |
if *updateHelpFiles { |
| 50 |
if err := os.WriteFile(helpFile, got, 0666); err != nil { |
| 51 |
t.Errorf("Failed writing %v: %v", helpFile, err) |
| 52 |
} |
| 53 |
return |
| 54 |
} |
| 55 |
want, err := os.ReadFile(helpFile) |
| 56 |
if err != nil { |
| 57 |
t.Fatalf("Missing help file %q", helpFile) |
| 58 |
} |
| 59 |
if diff := cmp.Diff(string(want), string(got)); diff != "" { |
| 60 |
t.Errorf("Help file %q did not match, run with -update-help-files to fix (-want +got)\n%s", helpFile, diff) |
| 61 |
} |
| 62 |
}) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestVerboseHelp(t *testing.T) { |
| 67 |
testenv.NeedsGoBuild(t) |
| 68 |
app := cmd.New() |
| 69 |
ctx := context.Background() |
| 70 |
var buf bytes.Buffer |
| 71 |
s := flag.NewFlagSet(appName, flag.ContinueOnError) |
| 72 |
s.SetOutput(&buf) |
| 73 |
tool.Run(ctx, s, app, []string{"-v", "-h"}) |
| 74 |
got := buf.Bytes() |
| 75 |
|
| 76 |
helpFile := filepath.Join("usage", "usage-v.hlp") |
| 77 |
if *updateHelpFiles { |
| 78 |
if err := os.WriteFile(helpFile, got, 0666); err != nil { |
| 79 |
t.Errorf("Failed writing %v: %v", helpFile, err) |
| 80 |
} |
| 81 |
return |
| 82 |
} |
| 83 |
want, err := os.ReadFile(helpFile) |
| 84 |
if err != nil { |
| 85 |
t.Fatalf("Missing help file %q", helpFile) |
| 86 |
} |
| 87 |
if diff := cmp.Diff(string(want), string(got)); diff != "" { |
| 88 |
t.Errorf("Help file %q did not match, run with -update-help-files to fix (-want +got)\n%s", helpFile, diff) |
| 89 |
} |
| 90 |
} |