github.com/xaverkapeller/go-gitignore@v0.0.0-20201129201858-74ef740b8b77/util_test.go (about) 1 package gitignore_test 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "strings" 11 12 "github.com/denormal/go-gitignore" 13 ) 14 15 func file(content string) (*os.File, error) { 16 // create a temporary file 17 _file, _err := ioutil.TempFile("", "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 := ioutil.TempDir("", "") 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 if content != nil { 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 114 // return the temporary directory name 115 return _dir, nil 116 } // dir() 117 118 func exclude(content string) (string, error) { 119 // create a temporary folder with the info/ subfolder 120 _dir, _err := dir(nil) 121 if _err != nil { 122 return "", _err 123 } 124 _info := filepath.Join(_dir, "info") 125 _err = os.MkdirAll(_info, _GITMASK) 126 if _err != nil { 127 defer os.RemoveAll(_dir) 128 return "", _err 129 } 130 131 // create the exclude file 132 _exclude := filepath.Join(_info, "exclude") 133 _err = ioutil.WriteFile(_exclude, []byte(content), _GITMASK) 134 if _err != nil { 135 defer os.RemoveAll(_dir) 136 return "", _err 137 } 138 139 // return the temporary directory name 140 return _dir, nil 141 } // exclude() 142 143 func coincident(a, b gitignore.Position) bool { 144 return a.File == b.File && 145 a.Line == b.Line && 146 a.Column == b.Column && 147 a.Offset == b.Offset 148 } // coincident() 149 150 func pos(p gitignore.Position) string { 151 _prefix := p.File 152 if _prefix != "" { 153 _prefix = _prefix + ": " 154 } 155 156 return fmt.Sprintf("%s%d:%d [%d]", _prefix, p.Line, p.Column, p.Offset) 157 } // pos() 158 159 func buffer(content string) (*bytes.Buffer, error) { 160 // return a buffered .gitignore 161 return bytes.NewBufferString(content), nil 162 } // buffer() 163 164 func null() gitignore.GitIgnore { 165 // return an empty GitIgnore instance 166 return gitignore.New(bytes.NewBuffer(nil), "", nil) 167 } // null()