github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/pkg/git/gittest/gittest.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gittest
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os/exec"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func InitGitDir(t *testing.T, gitDir string) {
    26  	RunGitCommand(t, gitDir, "init")
    27  	RunGitCommand(t, gitDir, "config", "user.email", "test@author.com")
    28  	RunGitCommand(t, gitDir, "config", "user.name", "testAuthor")
    29  	CommitRandomFile(t, gitDir, "Initial commit")
    30  }
    31  
    32  func CreateGitTag(t *testing.T, gitDir, tagValue string) {
    33  	RunGitCommand(t, gitDir, "tag", "-a", tagValue, "-m", "")
    34  }
    35  
    36  func CreateBranch(t *testing.T, gitDir, branch string) {
    37  	RunGitCommand(t, gitDir, "checkout", "-b", branch)
    38  }
    39  
    40  func CommitAllFiles(t *testing.T, gitDir, commitMessage string) {
    41  	RunGitCommand(t, gitDir, "add", ".")
    42  	RunGitCommand(t, gitDir, "commit", "--author=testAuthor <test@author.com>", "-m", commitMessage)
    43  }
    44  
    45  func CommitRandomFile(t *testing.T, gitDir, commitMessage string) {
    46  	file, err := ioutil.TempFile(gitDir, "random-file-")
    47  	require.NoError(t, err)
    48  	require.NoError(t, file.Close())
    49  	CommitAllFiles(t, gitDir, commitMessage)
    50  }
    51  
    52  func Merge(t *testing.T, gitDir, branch string) {
    53  	RunGitCommand(t, gitDir, "merge", "--no-ff", branch)
    54  }
    55  
    56  func RunGitCommand(t *testing.T, gitDir string, args ...string) {
    57  	cmd := exec.Command("git", args...)
    58  	cmd.Dir = gitDir
    59  	output, err := cmd.CombinedOutput()
    60  	require.NoError(t, err, string(output))
    61  }