github.com/arsham/gitrelease@v0.3.2-0.20221207124258-6867180b2c2d/commit/helper_test.go (about)

     1  package commit_test
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/exec"
     8  	"path"
     9  	"sort"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/google/go-cmp/cmp"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func createGitRepo(t *testing.T) string {
    18  	t.Helper()
    19  	dir, err := ioutil.TempDir("", "git-lfs-test")
    20  	require.NoError(t, err)
    21  	t.Cleanup(func() {
    22  		require.NoError(t, os.RemoveAll(dir))
    23  	})
    24  
    25  	newDir := path.Join(dir, "project")
    26  	os.Mkdir(newDir, 0o755)
    27  
    28  	commands := [][]string{
    29  		{"init"},
    30  		{"config", "user.email", "arsham@github.com"},
    31  		{"config", "user.name", "arsham"},
    32  	}
    33  
    34  	for _, args := range commands {
    35  		cmd := exec.CommandContext(context.Background(), "git", args...)
    36  		cmd.Dir = newDir
    37  		_, err = cmd.CombinedOutput()
    38  		require.NoError(t, err)
    39  	}
    40  
    41  	return newDir
    42  }
    43  
    44  func createGitTag(t *testing.T, dir, tag string) {
    45  	t.Helper()
    46  	args := []string{"tag", tag}
    47  	cmd := exec.CommandContext(context.Background(), "git", args...)
    48  	cmd.Dir = dir
    49  	out, err := cmd.CombinedOutput()
    50  	require.NoError(t, err, string(out))
    51  }
    52  
    53  func createFile(t *testing.T, dir, filename, content string) {
    54  	t.Helper()
    55  	require.NoError(t, os.Chdir(dir))
    56  	require.NoError(t, os.WriteFile(filename, []byte(content), 0o644))
    57  }
    58  
    59  func commitChanges(t *testing.T, dir, msg string) {
    60  	t.Helper()
    61  
    62  	args := []string{"add", "-A"}
    63  	cmd := exec.CommandContext(context.Background(), "git", args...)
    64  	cmd.Dir = dir
    65  	out, err := cmd.CombinedOutput()
    66  	require.NoError(t, err, string(out))
    67  
    68  	args = []string{"commit", "-am", msg, "--no-gpg-sign"}
    69  	cmd = exec.CommandContext(context.Background(), "git", args...)
    70  	cmd.Dir = dir
    71  	out, err = cmd.CombinedOutput()
    72  	require.NoError(t, err, string(out))
    73  }
    74  
    75  func appendToFile(t *testing.T, dir, filename, msg string) {
    76  	t.Helper()
    77  	require.NoError(t, os.Chdir(dir))
    78  	f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o600)
    79  	require.NoError(t, err)
    80  	defer f.Close()
    81  
    82  	_, err = f.WriteString(msg)
    83  	require.NoError(t, err)
    84  }
    85  
    86  var cmpIgnoreNewlines = cmp.Transformer("IgnoreNewlines", func(in string) string {
    87  	return strings.ReplaceAll(in, "\n", "")
    88  })
    89  
    90  var stringSliceCleaner = cmp.Transformer("CleanStringSlice", func(in []string) []string {
    91  	out := make([]string, 0, len(in))
    92  	for _, s := range in {
    93  		if s != "" {
    94  			out = append(out, s)
    95  		}
    96  	}
    97  	sort.Strings(out)
    98  	return out
    99  })
   100  
   101  var commitComparer = cmp.Options{
   102  	cmpIgnoreNewlines,
   103  	stringSliceCleaner,
   104  }