github.com/SpiffyEight77/magefiles@v0.6.8/git/dco_test.go (about)

     1  package git
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestSetupDCO(t *testing.T) {
    13  	t.Run("git exists", func(t *testing.T) {
    14  		tmp := t.TempDir()
    15  		testDir := filepath.Join(tmp, "a/b")
    16  		require.NoError(t, os.MkdirAll(testDir, 0755))
    17  		gitDir := filepath.Join(tmp, ".git")
    18  		require.NoError(t, os.Mkdir(gitDir, 0755))
    19  
    20  		require.NoError(t, os.Chdir(testDir))
    21  		require.NoError(t, SetupDCO())
    22  
    23  		// test that the hook was created
    24  		hookPath := filepath.Join(gitDir, "hooks/prepare-commit-msg")
    25  		require.FileExists(t, hookPath)
    26  		hookContents, err := os.ReadFile(hookPath)
    27  		require.NoErrorf(t, err, "error reading %s", hookPath)
    28  		assert.Equal(t, string(hookContents), prepareCommitMsg, "unexpected hook file contents found")
    29  	})
    30  
    31  	t.Run("git exists", func(t *testing.T) {
    32  		tmp := t.TempDir()
    33  		testPath := filepath.Join(tmp, "a/b")
    34  		require.NoError(t, os.MkdirAll(testPath, 0755))
    35  		// there is not .git directory
    36  
    37  		require.NoError(t, os.Chdir(testPath))
    38  		err := SetupDCO()
    39  		require.ErrorContains(t, err, "could not find the repository root")
    40  	})
    41  }