golang.org/x/tools/gopls@v0.15.3/internal/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  // This file defines tests to ensure the cmd/usage/*.hlp files match
     8  // the output of the tool. The .hlp files are not actually needed by
     9  // the executable (they are not //go:embed-ded, say), but they make it
    10  // easier to review changes to the gopls command's help logic since
    11  // any effects are manifest as changes to these files.
    12  
    13  //go:generate go test -run Help -update-help-files
    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/internal/testenv"
    26  	"golang.org/x/tools/internal/tool"
    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) // This is a lie. We actually need the source code.
    35  	app := cmd.New(nil)
    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) // This is a lie. We actually need the source code.
    68  	app := cmd.New(nil)
    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  }