github.com/boyter/gocodewalker@v1.3.2/go-gitignore/util_test.go (about)

     1  // SPDX-License-Identifier: MIT
     2  
     3  package gitignore_test
     4  
     5  import (
     6  	"bytes"
     7  	"fmt"
     8  	"github.com/boyter/gocodewalker/go-gitignore"
     9  	"io"
    10  	"os"
    11  	"path/filepath"
    12  	"strings"
    13  )
    14  
    15  func file(content string) (*os.File, error) {
    16  	// create a temporary file
    17  	_file, _err := os.CreateTemp("", "gitignore")
    18  	if _err != nil {
    19  		return nil, _err
    20  	}
    21  
    22  	// populate this file with the example .gitignore
    23  	_, _err = _file.WriteString(content)
    24  	if _err != nil {
    25  		defer os.Remove(_file.Name())
    26  		return nil, _err
    27  	}
    28  	_, _err = _file.Seek(0, io.SeekStart)
    29  	if _err != nil {
    30  		defer os.Remove(_file.Name())
    31  		return nil, _err
    32  	}
    33  
    34  	// we have a temporary file containing the .gitignore
    35  	return _file, nil
    36  } // file()
    37  
    38  func dir(content map[string]string) (string, error) {
    39  	// create a temporary directory
    40  	_dir, _err := os.MkdirTemp("", "")
    41  	if _err != nil {
    42  		return "", _err
    43  	}
    44  
    45  	// resolve the path of this directory
    46  	//		- we do this to handle systems with a temporary directory
    47  	//		  that is a symbolic link
    48  	_dir, _err = filepath.EvalSymlinks(_dir)
    49  	if _err != nil {
    50  		defer os.RemoveAll(_dir)
    51  		return "", _err
    52  	}
    53  
    54  	// populate the temporary directory with the content map
    55  	//		- each key of the map is a file name
    56  	//		- each value of the map is the file content
    57  	//		- file names are relative to the temporary directory
    58  
    59  	for _key, _content := range content {
    60  		// ensure we have content to store
    61  		if _content == "" {
    62  			continue
    63  		}
    64  
    65  		// should we create a directory or a file?
    66  		_isdir := false
    67  		_path := _key
    68  		if strings.HasSuffix(_path, "/") {
    69  			_path = strings.TrimSuffix(_path, "/")
    70  			_isdir = true
    71  		}
    72  
    73  		// construct the absolute path (according to the local file system)
    74  		_abs := _dir
    75  		_parts := strings.Split(_path, "/")
    76  		_last := len(_parts) - 1
    77  		if _isdir {
    78  			_abs = filepath.Join(_abs, filepath.Join(_parts...))
    79  		} else if _last > 0 {
    80  			_abs = filepath.Join(_abs, filepath.Join(_parts[:_last]...))
    81  		}
    82  
    83  		// ensure this directory exists
    84  		_err = os.MkdirAll(_abs, _GITMASK)
    85  		if _err != nil {
    86  			defer os.RemoveAll(_dir)
    87  			return "", _err
    88  		} else if _isdir {
    89  			continue
    90  		}
    91  
    92  		// create the absolute path for the target file
    93  		_abs = filepath.Join(_abs, _parts[_last])
    94  
    95  		// write the contents to this file
    96  		_file, _err := os.Create(_abs)
    97  		if _err != nil {
    98  			defer os.RemoveAll(_dir)
    99  			return "", _err
   100  		}
   101  		_, _err = _file.WriteString(_content)
   102  		if _err != nil {
   103  			defer os.RemoveAll(_dir)
   104  			return "", _err
   105  		}
   106  		_err = _file.Close()
   107  		if _err != nil {
   108  			defer os.RemoveAll(_dir)
   109  			return "", _err
   110  		}
   111  	}
   112  
   113  	// return the temporary directory name
   114  	return _dir, nil
   115  } // dir()
   116  
   117  func exclude(content string) (string, error) {
   118  	// create a temporary folder with the info/ subfolder
   119  	_dir, _err := dir(nil)
   120  	if _err != nil {
   121  		return "", _err
   122  	}
   123  	_info := filepath.Join(_dir, "info")
   124  	_err = os.MkdirAll(_info, _GITMASK)
   125  	if _err != nil {
   126  		defer os.RemoveAll(_dir)
   127  		return "", _err
   128  	}
   129  
   130  	// create the exclude file
   131  	_exclude := filepath.Join(_info, "exclude")
   132  	_err = os.WriteFile(_exclude, []byte(content), _GITMASK)
   133  	if _err != nil {
   134  		defer os.RemoveAll(_dir)
   135  		return "", _err
   136  	}
   137  
   138  	// return the temporary directory name
   139  	return _dir, nil
   140  } // exclude()
   141  
   142  func coincident(a, b gitignore.Position) bool {
   143  	return a.File == b.File &&
   144  		a.Line == b.Line &&
   145  		a.Column == b.Column &&
   146  		a.Offset == b.Offset
   147  } // coincident()
   148  
   149  func pos(p gitignore.Position) string {
   150  	_prefix := p.File
   151  	if _prefix != "" {
   152  		_prefix = _prefix + ": "
   153  	}
   154  
   155  	return fmt.Sprintf("%s%d:%d [%d]", _prefix, p.Line, p.Column, p.Offset)
   156  } // pos()
   157  
   158  func buffer(content string) (*bytes.Buffer, error) {
   159  	// return a buffered .gitignore
   160  	return bytes.NewBufferString(content), nil
   161  } // buffer()
   162  
   163  func null() gitignore.GitIgnore {
   164  	// return an empty GitIgnore instance
   165  	return gitignore.New(bytes.NewBuffer(nil), "", nil)
   166  } // null()