github.com/q2/git-lfs@v0.5.1-0.20150410234700-03a0d4cec40e/commands/init_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"github.com/bmizerany/assert"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestInit(t *testing.T) {
    13  	repo := NewRepository(t, "empty")
    14  	defer repo.Test()
    15  
    16  	repo.AddPath(repo.Path, ".git")
    17  	repo.AddPath(repo.Path, "subdir")
    18  
    19  	cmd := repo.Command("init")
    20  	cmd.Output = "git lfs initialized"
    21  
    22  	prePushHookFile := filepath.Join(repo.Path, ".git", "hooks", "pre-push")
    23  
    24  	cmd.Before(func() {
    25  		err := os.RemoveAll(filepath.Join(repo.Path, ".git", "hooks"))
    26  		assert.Equal(t, nil, err)
    27  	})
    28  
    29  	cmd.After(func() {
    30  		// assert filter config
    31  		configs := GlobalGitConfig(t)
    32  		AssertIncludeString(t, "filter.lfs.clean=git lfs clean %f", configs)
    33  		AssertIncludeString(t, "filter.lfs.smudge=git lfs smudge %f", configs)
    34  		AssertIncludeString(t, "filter.lfs.required=true", configs)
    35  		found := 0
    36  		for _, line := range configs {
    37  			if strings.HasPrefix(line, "filter.lfs") {
    38  				found += 1
    39  			}
    40  		}
    41  		assert.Equal(t, 3, found)
    42  
    43  		// assert hooks
    44  		stat, err := os.Stat(prePushHookFile)
    45  		assert.Equal(t, nil, err)
    46  		assert.Equal(t, false, stat.IsDir())
    47  	})
    48  
    49  	cmd = repo.Command("init")
    50  	cmd.Output = "Hook already exists: pre-push\ngit lfs initialized"
    51  
    52  	customHook := []byte("echo 'yo'")
    53  	cmd.Before(func() {
    54  		err := ioutil.WriteFile(prePushHookFile, customHook, 0755)
    55  		assert.Equal(t, nil, err)
    56  	})
    57  
    58  	cmd.After(func() {
    59  		by, err := ioutil.ReadFile(prePushHookFile)
    60  		assert.Equal(t, nil, err)
    61  		assert.Equal(t, string(customHook), string(by))
    62  	})
    63  }