github.com/ianlewis/go-gitignore@v0.1.1-0.20231110021210-4a0f15cbd56f/gitignore_unix_test.go (about)

     1  // Copyright 2016 Denormal Limited
     2  // Copyright 2023 Google LLC
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //      http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  //go:build !windows
    17  
    18  package gitignore_test
    19  
    20  import (
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/ianlewis/go-gitignore"
    25  )
    26  
    27  // TODO(#18): Re-enable TestNewFromFile on Windows.
    28  func TestNewFromFile(t *testing.T) {
    29  	_test := &gitignoretest{}
    30  	_test.instance = func(file string) (gitignore.GitIgnore, error) {
    31  		return gitignore.NewFromFile(file)
    32  	}
    33  
    34  	// perform the gitignore test
    35  	withfile(t, _test, _GITIGNORE)
    36  } // TestNewFromFile()
    37  
    38  // TODO(#18): Re-enable TestNewFromWhitespaceFile on Windows.
    39  func TestNewFromWhitespaceFile(t *testing.T) {
    40  	_test := &gitignoretest{}
    41  	_test.instance = func(file string) (gitignore.GitIgnore, error) {
    42  		return gitignore.NewFromFile(file)
    43  	}
    44  
    45  	// perform the gitignore test
    46  	withfile(t, _test, _GITIGNORE_WHITESPACE)
    47  } // TestNewFromWhitespaceFile()
    48  
    49  // TODO(#18): Re-enable TestNewFromEmptyFile on Windows.
    50  func TestNewFromEmptyFile(t *testing.T) {
    51  	_test := &gitignoretest{}
    52  	_test.instance = func(file string) (gitignore.GitIgnore, error) {
    53  		return gitignore.NewFromFile(file)
    54  	}
    55  
    56  	// perform the gitignore test
    57  	withfile(t, _test, "")
    58  } // TestNewFromEmptyFile()
    59  
    60  // TODO(#18): Re-enable TestNewWithErrors on Windows.
    61  func TestNewWithErrors(t *testing.T) {
    62  	_test := &gitignoretest{}
    63  	_test.error = func(e gitignore.Error) bool {
    64  		_test.errors = append(_test.errors, e)
    65  		return true
    66  	}
    67  	_test.instance = func(file string) (gitignore.GitIgnore, error) {
    68  		// reset the error slice
    69  		_test.errors = make([]gitignore.Error, 0)
    70  
    71  		// attempt to create the GitIgnore instance
    72  		_ignore := gitignore.NewWithErrors(file, _test.error)
    73  
    74  		// if we encountered errors, and the first error has a zero position
    75  		// then it represents a file access error
    76  		//		- extract the error and return it
    77  		//		- remove it from the list of errors
    78  		var _err error
    79  		if len(_test.errors) > 0 {
    80  			if _test.errors[0].Position().Zero() {
    81  				_err = _test.errors[0].Underlying()
    82  				_test.errors = _test.errors[1:]
    83  			}
    84  		}
    85  
    86  		// return the GitIgnore instance
    87  		return _ignore, _err
    88  	}
    89  
    90  	// perform the gitignore test
    91  	withfile(t, _test, _GITIGNORE)
    92  
    93  	_test.error = nil
    94  	withfile(t, _test, _GITIGNORE)
    95  } // TestNewWithErrors()
    96  
    97  // TODO(#18): Re-enable TestNewWithCache on Windows.
    98  func TestNewWithCache(t *testing.T) {
    99  	// perform the gitignore test with a custom cache
   100  	_test := &gitignoretest{}
   101  	_test.cached = true
   102  	_test.cache = gitignore.NewCache()
   103  	_test.instance = func(file string) (gitignore.GitIgnore, error) {
   104  		// reset the error slice
   105  		_test.errors = make([]gitignore.Error, 0)
   106  
   107  		// attempt to create the GitIgnore instance
   108  		_ignore := gitignore.NewWithCache(file, _test.cache, _test.error)
   109  
   110  		// if we encountered errors, and the first error has a zero position
   111  		// then it represents a file access error
   112  		//		- extract the error and return it
   113  		//		- remove it from the list of errors
   114  		var _err error
   115  		if len(_test.errors) > 0 {
   116  			if _test.errors[0].Position().Zero() {
   117  				_err = _test.errors[0].Underlying()
   118  				_test.errors = _test.errors[1:]
   119  			}
   120  		}
   121  
   122  		// return the GitIgnore instance
   123  		return _ignore, _err
   124  	}
   125  
   126  	// perform the gitignore test
   127  	withfile(t, _test, _GITIGNORE)
   128  
   129  	// repeat the tests while accumulating errors
   130  	_test.error = func(e gitignore.Error) bool {
   131  		_test.errors = append(_test.errors, e)
   132  		return true
   133  	}
   134  	withfile(t, _test, _GITIGNORE)
   135  
   136  	// create a temporary .gitignore
   137  	_file, _err := file(_GITIGNORE)
   138  	if _err != nil {
   139  		t.Fatalf("unable to create temporary .gitignore: %s", _err.Error())
   140  	}
   141  	defer os.Remove(_file.Name())
   142  
   143  	// attempt to load the .gitignore file
   144  	_ignore, _err := _test.instance(_file.Name())
   145  	if _err != nil {
   146  		t.Fatalf("unable to open temporary .gitignore: %s", _err.Error())
   147  	}
   148  
   149  	// remove the .gitignore and try again
   150  	os.Remove(_file.Name())
   151  
   152  	// ensure the retrieved GitIgnore matches the stored instance
   153  	_new, _err := _test.instance(_file.Name())
   154  	if _err != nil {
   155  		t.Fatalf(
   156  			"unexpected error retrieving cached .gitignore: %s", _err.Error(),
   157  		)
   158  	} else if _new != _ignore {
   159  		t.Fatalf(
   160  			"gitignore.NewWithCache() mismatch; expected %v, got %v",
   161  			_ignore, _new,
   162  		)
   163  	}
   164  } // TestNewWithCache()