github.com/jhump/golang-x-tools@v0.0.0-20220218190644-4958d6d39439/internal/lsp/cmd/help_test.go (about) 1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cmd_test 6 7 import ( 8 "bytes" 9 "context" 10 "flag" 11 "io/ioutil" 12 "path/filepath" 13 "testing" 14 15 "github.com/jhump/golang-x-tools/internal/lsp/cmd" 16 "github.com/jhump/golang-x-tools/internal/testenv" 17 "github.com/jhump/golang-x-tools/internal/tool" 18 ) 19 20 //go:generate go test -run Help -update-help-files 21 22 var updateHelpFiles = flag.Bool("update-help-files", false, "Write out the help files instead of checking them") 23 24 const appName = "gopls" 25 26 func TestHelpFiles(t *testing.T) { 27 testenv.NeedsGoBuild(t) // This is a lie. We actually need the source code. 28 app := cmd.New(appName, "", nil, nil) 29 ctx := context.Background() 30 for _, page := range append(app.Commands(), app) { 31 t.Run(page.Name(), func(t *testing.T) { 32 var buf bytes.Buffer 33 s := flag.NewFlagSet(page.Name(), flag.ContinueOnError) 34 s.SetOutput(&buf) 35 tool.Run(ctx, s, page, []string{"-h"}) 36 name := page.Name() 37 if name == appName { 38 name = "usage" 39 } 40 helpFile := filepath.Join("usage", name+".hlp") 41 got := buf.Bytes() 42 if *updateHelpFiles { 43 if err := ioutil.WriteFile(helpFile, got, 0666); err != nil { 44 t.Errorf("Failed writing %v: %v", helpFile, err) 45 } 46 return 47 } 48 expect, err := ioutil.ReadFile(helpFile) 49 switch { 50 case err != nil: 51 t.Errorf("Missing help file %q", helpFile) 52 case !bytes.Equal(expect, got): 53 t.Errorf("Help file %q did not match, got:\n%q\nwant:\n%q", helpFile, string(got), string(expect)) 54 } 55 }) 56 } 57 }