github.com/jdhenke/godel@v0.0.0-20161213181855-abeb3861bf0d/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  	cmd := exec.Command("git", "init")
    27  	cmd.Dir = gitDir
    28  	output, err := cmd.CombinedOutput()
    29  	require.NoError(t, err, string(output))
    30  
    31  	cmd = exec.Command("git", "config", "user.email", "test@author.com")
    32  	cmd.Dir = gitDir
    33  	output, err = cmd.CombinedOutput()
    34  	require.NoError(t, err, string(output))
    35  
    36  	cmd = exec.Command("git", "config", "user.name", "testAuthor")
    37  	cmd.Dir = gitDir
    38  	output, err = cmd.CombinedOutput()
    39  	require.NoError(t, err, string(output))
    40  
    41  	CommitRandomFile(t, gitDir, "Initial commit")
    42  }
    43  
    44  func CreateGitTag(t *testing.T, gitDir, tagValue string) {
    45  	cmd := exec.Command("git", "tag", "-a", tagValue, "-m", "")
    46  	cmd.Dir = gitDir
    47  	output, err := cmd.CombinedOutput()
    48  	require.NoError(t, err, string(output))
    49  }
    50  
    51  func CommitAllFiles(t *testing.T, gitDir, commitMessage string) {
    52  	cmd := exec.Command("git", "add", ".")
    53  	cmd.Dir = gitDir
    54  	output, err := cmd.CombinedOutput()
    55  	require.NoError(t, err, string(output))
    56  
    57  	cmd = exec.Command("git", "commit", "--author=testAuthor <test@author.com>", "-m", commitMessage)
    58  	cmd.Dir = gitDir
    59  	output, err = cmd.CombinedOutput()
    60  	require.NoError(t, err, string(output))
    61  }
    62  
    63  func CommitRandomFile(t *testing.T, gitDir, commitMessage string) {
    64  	file, err := ioutil.TempFile(gitDir, "random-file-")
    65  	require.NoError(t, err)
    66  	require.NoError(t, file.Close())
    67  	CommitAllFiles(t, gitDir, commitMessage)
    68  }