...

Source file src/edge-infra.dev/pkg/lib/build/git/git_test.go

Documentation: edge-infra.dev/pkg/lib/build/git

     1  package git
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"edge-infra.dev/pkg/lib/build/bazel"
    14  	"edge-infra.dev/pkg/lib/cli/sh"
    15  )
    16  
    17  var (
    18  	dir    string
    19  	shell  *sh.Shell
    20  	git    *Git
    21  	branch = "a-super-real-branch"
    22  )
    23  
    24  func TestMain(m *testing.M) {
    25  	var err error
    26  	// initialize repository
    27  	dir, err = newTempGitRepoDir()
    28  	if err != nil {
    29  		log.Fatal("failed to initialize test repository", err)
    30  	}
    31  	defer os.RemoveAll(dir)
    32  	shell = sh.NewInDir(dir)
    33  	shell.RunOrDie("git init -q")
    34  	shell.RunOrDie("git config user.name \"testbot\"")
    35  	shell.RunOrDie("git config user.email \"testmail@testworld.com\"")
    36  	shell.RunOrDie(fmt.Sprintf("git checkout -b %s", branch))
    37  
    38  	err = os.WriteFile(filepath.Join(dir, ".version"), []byte("0.0.1"), 0600)
    39  	if err != nil {
    40  		log.Fatal("failed to resolve test tmp dir", err)
    41  	}
    42  
    43  	shell.RunOrDie("git add .version")
    44  	shell.RunOrDie("git commit -m \"\"")
    45  	fmt.Println(shell.RunOrDie("git status"))
    46  
    47  	git = NewInDir(dir)
    48  
    49  	os.Exit(m.Run())
    50  }
    51  
    52  func TestBranch(t *testing.T) {
    53  	actual, err := git.Branch()
    54  	assert.NoError(t, err)
    55  	assert.Equal(t, branch, actual)
    56  }
    57  
    58  func TestCommit(t *testing.T) {
    59  	commit, err := git.Commit()
    60  	assert.NoError(t, err)
    61  	assert.NotEmpty(t, commit)
    62  }
    63  
    64  func TestCommitForFile(t *testing.T) {
    65  	current, err := git.Commit()
    66  	assert.NoError(t, err)
    67  	assert.NotEmpty(t, current)
    68  
    69  	commitFile(t, "test-file", []byte("this is a very special file"))
    70  	// get the commit the file we care about was changed in
    71  	desired, err := git.Commit()
    72  	assert.NoError(t, err)
    73  	assert.NotEmpty(t, current)
    74  
    75  	// produce another, unrelated commit, to verify that we are fetching a non-HEAD
    76  	// commit
    77  	commitFile(t, "test-file2", []byte("my very special, unrelated, file to commit"))
    78  	// now get the commit for test-file and check that it matches
    79  	testFileCommit, err := git.CommitForFile("test-file")
    80  	assert.NoError(t, err)
    81  	assert.NotEmpty(t, testFileCommit)
    82  	assert.Equal(t, desired, testFileCommit)
    83  }
    84  
    85  func TestTimestamp(t *testing.T) {
    86  	ts, err := git.Timestamp()
    87  	assert.NoError(t, err)
    88  	assert.NotEmpty(t, ts)
    89  
    90  	commitFile(t, "timestamped-test-file", []byte("im STAMPED, baby"))
    91  	desired, err := git.Timestamp()
    92  	assert.NoError(t, err)
    93  	assert.NotEmpty(t, desired)
    94  	previousCommit, err := git.Commit() // try to fetch timestamp of this commit later
    95  	assert.NoError(t, err)
    96  	assert.NotEmpty(t, previousCommit)
    97  
    98  	commitFile(t, "timestamped-test-file-2", []byte("im STAMPED, again, baby"))
    99  	testFileTimestamp, err := git.Timestamp("timestamped-test-file")
   100  	assert.NoError(t, err)
   101  	assert.NotEmpty(t, testFileTimestamp)
   102  	assert.Equal(t, desired, testFileTimestamp)
   103  
   104  	commitTimestamp, err := git.Timestamp(previousCommit)
   105  	assert.NoError(t, err)
   106  	assert.NotEmpty(t, commitTimestamp)
   107  	assert.Equal(t, desired, commitTimestamp)
   108  }
   109  
   110  func TestPath(t *testing.T) {
   111  	p, err := git.Path()
   112  	assert.NoError(t, err)
   113  	assert.Equal(t, dir, p)
   114  }
   115  
   116  func TestRemote(t *testing.T) {
   117  	assert.NoError(t, shell.RunE("git remote add origin https://foo.git"))
   118  	r, err := git.Remote()
   119  	assert.NoError(t, err)
   120  	assert.Equal(t, "https://foo", r)
   121  }
   122  
   123  func TestRemoteWithUser(t *testing.T) {
   124  	repo, err := newTempGitRepoDir()
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  
   129  	shell := sh.NewInDir(repo)
   130  	shell.RunOrDie("git init -q")
   131  	shell.RunOrDie("git config user.name \"testbot\"")
   132  	shell.RunOrDie("git config user.email \"testmail@testworld.com\"")
   133  	assert.NoError(t, shell.RunE("git remote add origin https://user:password@thegithub.com"))
   134  
   135  	git := NewInDir(repo)
   136  	r, err := git.Remote()
   137  	assert.NoError(t, err)
   138  	assert.Equal(t, "https://thegithub.com", r)
   139  }
   140  
   141  func TestRemoteWithGithubSshUrl(t *testing.T) {
   142  	repo, err := newTempGitRepoDir()
   143  	if err != nil {
   144  		t.Fatal(err)
   145  	}
   146  
   147  	shell := sh.NewInDir(repo)
   148  	shell.RunOrDie("git init -q")
   149  	shell.RunOrDie("git config user.name \"testbot\"")
   150  	shell.RunOrDie("git config user.email \"testmail@testworld.com\"")
   151  	assert.NoError(t, shell.RunE("git remote add origin git@github.com:ncrvoyix-swt-retail/edge-infra.git"))
   152  
   153  	git := NewInDir(repo)
   154  	r, err := git.Remote()
   155  	assert.NoError(t, err)
   156  	assert.Equal(t, "git@github.com:ncrvoyix-swt-retail/edge-infra", r)
   157  }
   158  
   159  func TestDirty(t *testing.T) {
   160  	// Should begin in a clean state
   161  	dirty, err := git.IsDirty()
   162  	assert.NoError(t, err)
   163  	assert.False(t, dirty)
   164  
   165  	// Create untracked file, shouldnt impact dirty status
   166  	require.NoError(t, os.WriteFile(filepath.Join(dir, "new-file"), []byte("untracked file"), 0600))
   167  	dirty, err = git.IsDirty()
   168  	assert.NoError(t, err)
   169  	assert.False(t, dirty)
   170  
   171  	// Create newly tracked file, then modify it
   172  	t.Log("committing new-tracked-file")
   173  	commitFile(t, "new-tracked-file", []byte("hi mom"))
   174  	dirty, err = git.IsDirty()
   175  	assert.NoError(t, err)
   176  	assert.False(t, dirty)
   177  
   178  	// Should be dirty after modifying file
   179  	t.Log("modifying new-tracked-file")
   180  	require.NoError(t, os.WriteFile(filepath.Join(dir, "new-tracked-file"), []byte("hi dad"), 0600))
   181  	dirty, err = git.IsDirty()
   182  	assert.NoError(t, err)
   183  	assert.True(t, dirty)
   184  }
   185  
   186  func TestIsRebaseInProgress(t *testing.T) {
   187  	// Should begin in a clean state
   188  	rebase, err := git.IsRebaseInProgress()
   189  	assert.NoError(t, err)
   190  	assert.False(t, rebase)
   191  
   192  	rmergeDir := filepath.Join(dir, ".git", "rebase-merge")
   193  	t.Log(rmergeDir)
   194  	rapplyDir := filepath.Join(dir, ".git", "rebase-apply")
   195  	t.Log(rapplyDir)
   196  
   197  	assert.Equal(t, dir, git.sh.Getwd())
   198  
   199  	// Create rebase-merge directory
   200  	require.NoError(t, os.Mkdir(rmergeDir, 0600))
   201  	rebase, err = git.IsRebaseInProgress()
   202  	assert.NoError(t, err)
   203  	assert.True(t, rebase)
   204  
   205  	// Create rebase-apply directory
   206  	require.NoError(t, os.Mkdir(rapplyDir, 0600))
   207  	rebase, err = git.IsRebaseInProgress()
   208  	assert.NoError(t, err)
   209  	assert.True(t, rebase)
   210  
   211  	// Remove rebase-merge
   212  	require.NoError(t, os.RemoveAll(rmergeDir))
   213  	rebase, err = git.IsRebaseInProgress()
   214  	assert.NoError(t, err)
   215  	assert.True(t, rebase)
   216  
   217  	// Remove rebase-apply
   218  	require.NoError(t, os.RemoveAll(rapplyDir))
   219  	rebase, err = git.IsRebaseInProgress()
   220  	assert.NoError(t, err)
   221  	assert.False(t, rebase)
   222  }
   223  
   224  func commitFile(t *testing.T, name string, contents []byte) {
   225  	t.Helper()
   226  	assert.NoError(t, os.WriteFile(
   227  		filepath.Join(dir, name),
   228  		contents,
   229  		0600,
   230  	))
   231  	assert.NoError(t, shell.RunE(fmt.Sprintf("git add %s", name)))
   232  	assert.NoError(t, shell.RunE("git commit -m \"\""))
   233  }
   234  
   235  func newTempGitRepoDir() (string, error) {
   236  	dir, err := bazel.NewTestTmpDir("git_test-*")
   237  	if err != nil {
   238  		return "", fmt.Errorf("failed to resolve test tmp dir: %w", err)
   239  	}
   240  
   241  	return dir, nil
   242  }
   243  

View as plain text