github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/internal/util_test/git.go (about)

     1  /**
     2   * Copyright 2023 CloudWeGo Authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package util_test
    18  
    19  import (
    20  	"encoding/json"
    21  	"os/exec"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  // GetGitRoot gets the git root of this project.
    29  func GetGitRoot() (string, error) {
    30  	cmd := exec.Command("git", "rev-parse", "--show-toplevel")
    31  	sb := strings.Builder{}
    32  	cmd.Stdout = &sb
    33  	err := cmd.Run()
    34  	if err != nil {
    35  		return "", err
    36  	}
    37  	str := sb.String()
    38  	return str[:len(str)-1], nil
    39  }
    40  
    41  // MustGitPath is util for getting relative path to git root
    42  func MustGitPath(relative string) string {
    43  	ret, err := GetGitRoot()
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  	return ret + "/" + relative
    48  }
    49  
    50  // JsonEqual assert two json are equal
    51  func JsonEqual(t *testing.T, expected, actual []byte) {
    52  	var exp, act map[string]interface{}
    53  	err := json.Unmarshal(expected, &exp)
    54  	assert.Nil(t, err)
    55  	err = json.Unmarshal(actual, &act)
    56  	assert.Nil(t, err)
    57  	assert.Equal(t, exp, act)
    58  }