...

Source file src/github.com/shibumi/go-pathspec/gitignore_test.go

Documentation: github.com/shibumi/go-pathspec

     1  //
     2  // Copyright 2014, Sander van Harmelen
     3  // Copyright 2020, Christian Rebischke
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  //
    17  
    18  package pathspec
    19  
    20  import (
    21  	"bytes"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  func TestGitIgnore(t *testing.T) {
    27  	toInclude := []string{"!.#test", "~foo", "foo/foo.txt", "bar/foobar.txt", "foo/bar.txt", "/bar/foo"}
    28  	toIgnore := []string{".#test", "foo/#test#", "foo/bar/.foo.txt.swp", "foo/foobar/foobar.txt", "foo.txt", "test/foo.test", "test/foo/bar.test", "foo/bar", "foo/1/2/bar", "foo/foobar/abcd.txt", "foo/shibumi/test.txt"}
    29  	content := []string{".#*", "\\#*#", ".*.sw[a-z]", "**/foobar/foobar.txt", "/foo.txt", "test/", "foo/**/bar", "/b[^a]r/foo", "abcd.txt", "shibumi/"}
    30  
    31  	for _, f := range toInclude {
    32  		match, err := GitIgnore(content, f)
    33  		if err != nil {
    34  			t.Fatalf("Received an unexpected error: %s", err)
    35  		}
    36  		if match {
    37  			t.Errorf("GitIgnore('%s', %s) returned '%v', want 'false'", content, f, match)
    38  		}
    39  	}
    40  
    41  	for _, f := range toIgnore {
    42  		match, err := GitIgnore(content, f)
    43  		if err != nil {
    44  			t.Fatalf("Received an unexpected error: %s", err)
    45  		}
    46  		if !match {
    47  			t.Errorf("GitIgnore('%s', %s) returned '%v', want 'true'", content, f, match)
    48  		}
    49  	}
    50  }
    51  
    52  func TestReadGitIgnore(t *testing.T) {
    53  	toInclude := []string{"!.#test", "~foo", "foo/foo.txt", "bar/foobar.txt", "foo/bar.txt", "/bar/foo"}
    54  	toIgnore := []string{".#test", "foo/#test#", "foo/bar/.foo.txt.swp", "foo/foobar/foobar.txt", "foo.txt", "test/foo.test", "test/foo/bar.test", "foo/bar", "foo/1/2/bar"}
    55  	content := []byte(".#*\n\\#*#\n.*.sw[a-z]\n**/foobar/foobar.txt\n/foo.txt\ntest/\nfoo/**/bar\n/b[^a]r/foo")
    56  
    57  	for _, f := range toInclude {
    58  		match, err := ReadGitIgnore(bytes.NewReader(content), f)
    59  		if err != nil {
    60  			t.Fatalf("Received an unexpected error: %s", err)
    61  		}
    62  		if match {
    63  			t.Errorf("GitIgnore('%s', %s) returned '%v', want 'false'", strings.Replace(string(content), "\n", ", ", -1), f, match)
    64  		}
    65  	}
    66  
    67  	for _, f := range toIgnore {
    68  		match, err := ReadGitIgnore(bytes.NewReader(content), f)
    69  		if err != nil {
    70  			t.Fatalf("Received an unexpected error: %s", err)
    71  		}
    72  		if !match {
    73  			t.Errorf("GitIgnore('%s', %s) returned '%v', want 'true'", strings.Replace(string(content), "\n", ", ", -1), f, match)
    74  		}
    75  	}
    76  }
    77  

View as plain text