trpc.group/trpc-go/trpc-cmdline@v1.0.9/plugin/format_test.go (about)

     1  // Tencent is pleased to support the open source community by making tRPC available.
     2  //
     3  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     4  // All rights reserved.
     5  //
     6  // If you have downloaded a copy of the tRPC source code from Tencent,
     7  // please note that tRPC source code is licensed under the  Apache 2.0 License,
     8  // A copy of the Apache 2.0 License is included in this file.
     9  
    10  package plugin
    11  
    12  import (
    13  	"errors"
    14  	"os"
    15  	"testing"
    16  
    17  	"trpc.group/trpc-go/trpc-cmdline/params"
    18  	"trpc.group/trpc-go/trpc-cmdline/util/style"
    19  
    20  	"github.com/agiledragon/gomonkey"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestPlugin_Format(t *testing.T) {
    25  	u := &Formatter{}
    26  	opt := params.Option{
    27  		Language: "go",
    28  	}
    29  	require.Equal(t, "gofmt", u.Name())
    30  
    31  	t.Run("go", func(t *testing.T) {
    32  		p := gomonkey.ApplyFunc(style.GoFmtDir, func(string2 string) error {
    33  			return nil
    34  		})
    35  		p.ApplyFunc(os.Getwd, func() (string, error) {
    36  			return os.TempDir(), nil
    37  		})
    38  		defer p.Reset()
    39  		require.True(t, u.Check(nil, &opt))
    40  		require.Nil(t, u.Run(nil, &opt))
    41  	})
    42  
    43  	t.Run("Getwd error", func(t *testing.T) {
    44  		p := gomonkey.ApplyFunc(os.Getwd, func() (string, error) {
    45  			return "", errors.New("getwd error")
    46  		})
    47  		defer p.Reset()
    48  		require.True(t, u.Check(nil, &opt))
    49  		require.NotNil(t, u.Run(nil, &opt))
    50  	})
    51  }