github.com/grahambrereton-form3/tilt@v0.10.18/internal/git/gitignore_test.go (about)

     1  package git_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/windmilleng/tilt/internal/git"
    10  	"github.com/windmilleng/tilt/internal/testutils/tempdir"
    11  	"github.com/windmilleng/tilt/pkg/model"
    12  )
    13  
    14  func TestGitIgnoreTester_GitDirMatches(t *testing.T) {
    15  	tf := newTestFixture(t)
    16  	defer tf.TearDown()
    17  
    18  	tests := []struct {
    19  		description string
    20  		path        []string
    21  		expectMatch bool
    22  		expectError bool
    23  	}{
    24  		{
    25  			description: "a file in the .git directory",
    26  			path:        []string{".git", "foo", "bar"},
    27  			expectMatch: true,
    28  			expectError: false,
    29  		},
    30  		{
    31  			description: "a .gitlab-ci.yml file",
    32  			path:        []string{".gitlab-ci.yml"},
    33  			expectMatch: false,
    34  			expectError: false,
    35  		},
    36  		{
    37  			description: "a foo.git file",
    38  			path:        []string{"foo.git"},
    39  			expectMatch: false,
    40  			expectError: false,
    41  		},
    42  	}
    43  
    44  	for _, tt := range tests {
    45  		tf.AssertResult(tt.description, tf.JoinPath(0, tt.path...), tt.expectMatch, tt.expectError)
    46  	}
    47  }
    48  
    49  type testFixture struct {
    50  	repoRoots []*tempdir.TempDirFixture
    51  	tester    model.PathMatcher
    52  	ctx       context.Context
    53  	t         *testing.T
    54  }
    55  
    56  // initializes `tf.repoRoots` to be an array with one dir per gitignore
    57  func newTestFixture(t *testing.T) *testFixture {
    58  	tf := testFixture{}
    59  	tf.repoRoots = append(tf.repoRoots, tempdir.NewTempDirFixture(t))
    60  	tf.ctx = context.Background()
    61  	tf.t = t
    62  	tf.UseSingleRepoTester()
    63  	return &tf
    64  }
    65  
    66  func (tf *testFixture) UseSingleRepoTester() {
    67  	tf.UseSingleRepoTesterWithPath(tf.repoRoots[0].Path())
    68  }
    69  
    70  func (tf *testFixture) UseSingleRepoTesterWithPath(path string) {
    71  	tf.tester = git.NewRepoIgnoreTester(tf.ctx, path)
    72  }
    73  
    74  func (tf *testFixture) JoinPath(repoNum int, path ...string) string {
    75  	return tf.repoRoots[repoNum].JoinPath(path...)
    76  }
    77  
    78  func (tf *testFixture) AssertResult(description, path string, expectedMatches bool, expectError bool) {
    79  	tf.t.Run(description, func(t *testing.T) {
    80  		isIgnored, err := tf.tester.Matches(path)
    81  		if expectError {
    82  			assert.Error(t, err)
    83  		} else {
    84  			if assert.NoError(t, err) {
    85  				assert.Equal(t, expectedMatches, isIgnored)
    86  			}
    87  		}
    88  	})
    89  }
    90  
    91  func (tf *testFixture) TearDown() {
    92  	for _, tempDir := range tf.repoRoots {
    93  		tempDir.TearDown()
    94  	}
    95  }